001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2006-2008 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2016 ForgeRock AS.
016 */
017package org.opends.server.protocols.ldap;
018
019import java.io.IOException;
020import java.util.List;
021
022import org.forgerock.i18n.LocalizableMessage;
023import org.forgerock.i18n.slf4j.LocalizedLogger;
024import org.forgerock.opendj.io.ASN1Writer;
025import org.forgerock.util.Utils;
026import org.forgerock.opendj.ldap.DN;
027
028import static org.opends.server.protocols.ldap.LDAPConstants.*;
029import static org.opends.server.util.ServerConstants.*;
030
031/**
032 * This class defines the structures and methods for an LDAP delete response
033 * protocol op, which is used to provide information about the result of
034 * processing a delete request.
035 */
036public class DeleteResponseProtocolOp
037       extends ProtocolOp
038{
039  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
040
041
042  /** The matched DN for this response. */
043  private DN matchedDN;
044  /** The result code for this response. */
045  private int resultCode;
046  /** The set of referral URLs for this response. */
047  private List<String> referralURLs;
048  /** The error message for this response. */
049  private LocalizableMessage errorMessage;
050
051
052
053  /**
054   * Creates a new delete response protocol op with the provided result code.
055   *
056   * @param  resultCode  The result code for this response.
057   */
058  public DeleteResponseProtocolOp(int resultCode)
059  {
060    this.resultCode = resultCode;
061  }
062
063
064
065  /**
066   * Creates a new delete response protocol op with the provided result code and
067   * error message.
068   *
069   * @param  resultCode    The result code for this response.
070   * @param  errorMessage  The error message for this response.
071   */
072  public DeleteResponseProtocolOp(int resultCode, LocalizableMessage errorMessage)
073  {
074    this.resultCode   = resultCode;
075    this.errorMessage = errorMessage;
076  }
077
078
079
080  /**
081   * Creates a new delete response protocol op with the provided information.
082   *
083   * @param  resultCode    The result code for this response.
084   * @param  errorMessage  The error message for this response.
085   * @param  matchedDN     The matched DN for this response.
086   * @param  referralURLs  The referral URLs for this response.
087   */
088  public DeleteResponseProtocolOp(int resultCode, LocalizableMessage errorMessage,
089                                  DN matchedDN, List<String> referralURLs)
090  {
091    this.resultCode   = resultCode;
092    this.errorMessage = errorMessage;
093    this.matchedDN    = matchedDN;
094    this.referralURLs = referralURLs;
095  }
096
097
098
099  /**
100   * Retrieves the result code for this response.
101   *
102   * @return  The result code for this response.
103   */
104  public int getResultCode()
105  {
106    return resultCode;
107  }
108
109
110
111  /**
112   * Retrieves the error message for this response.
113   *
114   * @return  The error message for this response, or <CODE>null</CODE> if none
115   *          is available.
116   */
117  public LocalizableMessage getErrorMessage()
118  {
119    return errorMessage;
120  }
121
122
123
124  /**
125   * Retrieves the matched DN for this response.
126   *
127   * @return  The matched DN for this response, or <CODE>null</CODE> if none is
128   *          available.
129   */
130  public DN getMatchedDN()
131  {
132    return matchedDN;
133  }
134
135
136
137  /**
138   * Retrieves the set of referral URLs for this response.
139   *
140   * @return  The set of referral URLs for this response, or <CODE>null</CODE>
141   *          if none are available.
142   */
143  public List<String> getReferralURLs()
144  {
145    return referralURLs;
146  }
147
148  @Override
149  public byte getType()
150  {
151    return OP_TYPE_DELETE_RESPONSE;
152  }
153
154  @Override
155  public String getProtocolOpName()
156  {
157    return "Delete Response";
158  }
159
160  @Override
161  public void write(ASN1Writer stream) throws IOException
162  {
163    stream.writeStartSequence(OP_TYPE_DELETE_RESPONSE);
164    stream.writeEnumerated(resultCode);
165
166    if(matchedDN == null)
167    {
168      stream.writeOctetString((String)null);
169    }
170    else
171    {
172      stream.writeOctetString(matchedDN.toString());
173    }
174
175    if(errorMessage == null)
176    {
177      stream.writeOctetString((String)null);
178    }
179    else
180    {
181      stream.writeOctetString(errorMessage.toString());
182    }
183
184    if (referralURLs != null && !referralURLs.isEmpty())
185    {
186      stream.writeStartSequence(TYPE_REFERRAL_SEQUENCE);
187      for (String s : referralURLs)
188      {
189        stream.writeOctetString(s);
190      }
191      stream.writeEndSequence();
192    }
193
194    stream.writeEndSequence();
195  }
196
197  @Override
198  public void toString(StringBuilder buffer)
199  {
200    buffer.append("DeleteResponse(resultCode=");
201    buffer.append(resultCode);
202
203    if (errorMessage != null && errorMessage.length() > 0)
204    {
205      buffer.append(", errorMessage=");
206      buffer.append(errorMessage);
207    }
208    if (matchedDN != null)
209    {
210      buffer.append(", matchedDN=");
211      buffer.append(matchedDN);
212    }
213    if (referralURLs != null && !referralURLs.isEmpty())
214    {
215      buffer.append(", referralURLs={");
216      Utils.joinAsString(buffer, ", ", referralURLs);
217      buffer.append("}");
218    }
219
220    buffer.append(")");
221  }
222
223  @Override
224  public void toString(StringBuilder buffer, int indent)
225  {
226    StringBuilder indentBuf = new StringBuilder(indent);
227    for (int i=0 ; i < indent; i++)
228    {
229      indentBuf.append(' ');
230    }
231
232    buffer.append(indentBuf);
233    buffer.append("Delete Response");
234    buffer.append(EOL);
235
236    buffer.append(indentBuf);
237    buffer.append("  Result Code:  ");
238    buffer.append(resultCode);
239    buffer.append(EOL);
240
241    if (errorMessage != null)
242    {
243      buffer.append(indentBuf);
244      buffer.append("  Error LocalizableMessage:  ");
245      buffer.append(errorMessage);
246      buffer.append(EOL);
247    }
248
249    if (matchedDN != null)
250    {
251      buffer.append(indentBuf);
252      buffer.append("  Matched DN:  ");
253      buffer.append(matchedDN);
254      buffer.append(EOL);
255    }
256
257    if (referralURLs != null && !referralURLs.isEmpty())
258    {
259      buffer.append(indentBuf);
260      buffer.append("  Referral URLs:  ");
261      buffer.append(EOL);
262
263      for (String s : referralURLs)
264      {
265        buffer.append(indentBuf);
266        buffer.append("  ");
267        buffer.append(s);
268        buffer.append(EOL);
269      }
270    }
271  }
272}