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 modify response
043 * protocol op, which is used to provide information about the result of
044 * processing a modify request.
045 */
046public class ModifyResponseProtocolOp
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 modify response protocol op with the provided result code.
064   *
065   * @param  resultCode  The result code for this response.
066   */
067  public ModifyResponseProtocolOp(int resultCode)
068  {
069    this.resultCode = resultCode;
070  }
071
072
073
074  /**
075   * Creates a new modify 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 ModifyResponseProtocolOp(int resultCode, LocalizableMessage errorMessage)
082  {
083    this.resultCode   = resultCode;
084    this.errorMessage = errorMessage;
085  }
086
087
088
089  /**
090   * Creates a new modify 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 ModifyResponseProtocolOp(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   * Retrieves the error message for this response.
121   *
122   * @return  The error message for this response, or <CODE>null</CODE> if none
123   *          is available.
124   */
125  public LocalizableMessage getErrorMessage()
126  {
127    return errorMessage;
128  }
129
130
131  /**
132   * Retrieves the matched DN for this response.
133   *
134   * @return  The matched DN for this response, or <CODE>null</CODE> if none is
135   *          available.
136   */
137  public DN getMatchedDN()
138  {
139    return matchedDN;
140  }
141
142
143
144  /**
145   * Retrieves the set of referral URLs for this response.
146   *
147   * @return  The set of referral URLs for this response, or <CODE>null</CODE>
148   *          if none are available.
149   */
150  public List<String> getReferralURLs()
151  {
152    return referralURLs;
153  }
154
155  @Override
156  public byte getType()
157  {
158    return OP_TYPE_MODIFY_RESPONSE;
159  }
160
161  @Override
162  public String getProtocolOpName()
163  {
164    return "Modify Response";
165  }
166
167  @Override
168  public void write(ASN1Writer stream) throws IOException
169  {
170    stream.writeStartSequence(OP_TYPE_MODIFY_RESPONSE);
171    stream.writeEnumerated(resultCode);
172
173    if(matchedDN == null)
174    {
175      stream.writeOctetString((String)null);
176    }
177    else
178    {
179      stream.writeOctetString(matchedDN.toString());
180    }
181
182    if(errorMessage == null)
183    {
184      stream.writeOctetString((String)null);
185    }
186    else
187    {
188      stream.writeOctetString(errorMessage.toString());
189    }
190
191    if (referralURLs != null && !referralURLs.isEmpty())
192    {
193      stream.writeStartSequence(TYPE_REFERRAL_SEQUENCE);
194      for (String s : referralURLs)
195      {
196        stream.writeOctetString(s);
197      }
198      stream.writeEndSequence();
199    }
200
201    stream.writeEndSequence();
202  }
203
204  @Override
205  public void toString(StringBuilder buffer)
206  {
207    buffer.append("ModifyResponse(resultCode=");
208    buffer.append(resultCode);
209
210    if (errorMessage != null && errorMessage.length() > 0)
211    {
212      buffer.append(", errorMessage=");
213      buffer.append(errorMessage);
214    }
215    if (matchedDN != null)
216    {
217      buffer.append(", matchedDN=");
218      buffer.append(matchedDN);
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("Modify 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 LocalizableMessage:  ");
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}