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