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