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 add response
043 * protocol op, which is used to provide information about the result of
044 * processing an add request.
045 */
046public class AddResponseProtocolOp
047       extends ProtocolOp
048{
049  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
050
051  /** The matched DN for this response. */
052  private DN matchedDN;
053  /** The result code for this response. */
054  private int resultCode;
055  /** The set of referral URLs for this response. */
056  private List<String> referralURLs;
057  /** The error message for this response. */
058  private LocalizableMessage errorMessage;
059
060
061
062  /**
063   * Creates a new add response protocol op with the provided result code.
064   *
065   * @param  resultCode  The result code for this response.
066   */
067  public AddResponseProtocolOp(int resultCode)
068  {
069    this.resultCode = resultCode;
070  }
071
072
073
074  /**
075   * Creates a new add response protocol op with the provided result code and
076   * error message.
077   *
078   * @param  resultCode    The result code for this response.
079   * @param  errorMessage  The error message for this response.
080   */
081  public AddResponseProtocolOp(int resultCode, LocalizableMessage errorMessage)
082  {
083    this.resultCode   = resultCode;
084    this.errorMessage = errorMessage;
085  }
086
087
088
089  /**
090   * Creates a new add response protocol op with the provided information.
091   *
092   * @param  resultCode    The result code for this response.
093   * @param  errorMessage  The error message for this response.
094   * @param  matchedDN     The matched DN for this response.
095   * @param  referralURLs  The referral URLs for this response.
096   */
097  public AddResponseProtocolOp(int resultCode, LocalizableMessage errorMessage,
098                               DN matchedDN, List<String> referralURLs)
099  {
100    this.resultCode   = resultCode;
101    this.errorMessage = errorMessage;
102    this.matchedDN    = matchedDN;
103    this.referralURLs = referralURLs;
104  }
105
106
107
108  /**
109   * Retrieves the result code for this response.
110   *
111   * @return  The result code for this response.
112   */
113  public int getResultCode()
114  {
115    return resultCode;
116  }
117
118
119
120  /**
121   * Retrieves the error message for this response.
122   *
123   * @return  The error message for this response, or <CODE>null</CODE> if none
124   *          is available.
125   */
126  public LocalizableMessage getErrorMessage()
127  {
128    return errorMessage;
129  }
130
131
132  /**
133   * Retrieves the matched DN for this response.
134   *
135   * @return  The matched DN for this response, or <CODE>null</CODE> if none is
136   *          available.
137   */
138  public DN getMatchedDN()
139  {
140    return matchedDN;
141  }
142
143
144
145  /**
146   * Retrieves the set of referral URLs for this response.
147   *
148   * @return  The set of referral URLs for this response, or <CODE>null</CODE>
149   *          if none are available.
150   */
151  public List<String> getReferralURLs()
152  {
153    return referralURLs;
154  }
155
156  @Override
157  public byte getType()
158  {
159    return OP_TYPE_ADD_RESPONSE;
160  }
161
162  @Override
163  public String getProtocolOpName()
164  {
165    return "Add Response";
166  }
167
168  @Override
169  public void write(ASN1Writer stream) throws IOException
170  {
171    stream.writeStartSequence(OP_TYPE_ADD_RESPONSE);
172    stream.writeEnumerated(resultCode);
173
174    if(matchedDN == null)
175    {
176      stream.writeOctetString((String)null);
177    }
178    else
179    {
180      stream.writeOctetString(matchedDN.toString());
181    }
182
183    if(errorMessage == null)
184    {
185      stream.writeOctetString((String)null);
186    }
187    else
188    {
189      stream.writeOctetString(errorMessage.toString());
190    }
191
192    if (referralURLs != null && ! referralURLs.isEmpty())
193    {
194      stream.writeStartSequence(TYPE_REFERRAL_SEQUENCE);
195      for (String s : referralURLs)
196      {
197        stream.writeOctetString(s);
198      }
199      stream.writeEndSequence();
200    }
201
202    stream.writeEndSequence();
203  }
204
205  @Override
206  public void toString(StringBuilder buffer)
207  {
208    buffer.append("AddResponse(resultCode=");
209    buffer.append(resultCode);
210
211    if (errorMessage != null && errorMessage.length() > 0)
212    {
213      buffer.append(", errorMessage=").append(errorMessage);
214    }
215    if (matchedDN != null)
216    {
217      buffer.append(", matchedDN=").append(matchedDN);
218    }
219
220    if (referralURLs != null && !referralURLs.isEmpty())
221    {
222      buffer.append(", referralURLs={");
223      Utils.joinAsString(buffer, ", ", referralURLs);
224      buffer.append("}");
225    }
226
227    buffer.append(")");
228  }
229
230  @Override
231  public void toString(StringBuilder buffer, int indent)
232  {
233    StringBuilder indentBuf = new StringBuilder(indent);
234    for (int i=0 ; i < indent; i++)
235    {
236      indentBuf.append(' ');
237    }
238
239    buffer.append(indentBuf);
240    buffer.append("Add Response");
241    buffer.append(EOL);
242
243    buffer.append(indentBuf);
244    buffer.append("  Result Code:  ");
245    buffer.append(resultCode);
246    buffer.append(EOL);
247
248    if (errorMessage != null)
249    {
250      buffer.append(indentBuf);
251      buffer.append("  Error Message:  ");
252      buffer.append(errorMessage);
253      buffer.append(EOL);
254    }
255
256    if (matchedDN != null)
257    {
258      buffer.append(indentBuf);
259      buffer.append("  Matched DN:  ");
260      matchedDN.toString(buffer);
261      buffer.append(EOL);
262    }
263
264    if (referralURLs != null && ! referralURLs.isEmpty())
265    {
266      buffer.append(indentBuf);
267      buffer.append("  Referral URLs:  ");
268      buffer.append(EOL);
269
270      for (String s : referralURLs)
271      {
272        buffer.append(indentBuf);
273        buffer.append("  ");
274        buffer.append(s);
275        buffer.append(EOL);
276      }
277    }
278  }
279}