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 DN response
043 * protocol op, which is used to provide information about the result of
044 * processing a modify DN request.
045 */
046public class ModifyDNResponseProtocolOp
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 DN response protocol op with the provided result code.
064   *
065   * @param  resultCode  The result code for this response.
066   */
067  public ModifyDNResponseProtocolOp(int resultCode)
068  {
069    this.resultCode = resultCode;
070  }
071
072
073
074  /**
075   * Creates a new modify DN response protocol op with the provided result code
076   * and error message.
077   *
078   * @param  resultCode    The result code for this response.
079   * @param  errorMessage  The error message for this response.
080   */
081  public ModifyDNResponseProtocolOp(int resultCode, LocalizableMessage errorMessage)
082  {
083    this.resultCode   = resultCode;
084    this.errorMessage = errorMessage;
085  }
086
087
088
089  /**
090   * Creates a new modify DN 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 ModifyDNResponseProtocolOp(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  /**
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_MODIFY_DN_RESPONSE;
160  }
161
162  @Override
163  public String getProtocolOpName()
164  {
165    return "Modify DN Response";
166  }
167
168  @Override
169  public void write(ASN1Writer stream) throws IOException
170  {
171    stream.writeStartSequence(OP_TYPE_MODIFY_DN_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("ModifyDNResponse(resultCode=");
209    buffer.append(resultCode);
210
211    if (errorMessage != null && errorMessage.length() > 0)
212    {
213      buffer.append(", errorMessage=");
214      buffer.append(errorMessage);
215    }
216    if (matchedDN != null)
217    {
218      buffer.append(", matchedDN=");
219      buffer.append(matchedDN);
220    }
221    if (referralURLs != null && ! referralURLs.isEmpty())
222    {
223      buffer.append(", referralURLs={");
224      Utils.joinAsString(buffer, ", ", referralURLs);
225      buffer.append("}");
226    }
227
228    buffer.append(")");
229  }
230
231  @Override
232  public void toString(StringBuilder buffer, int indent)
233  {
234    StringBuilder indentBuf = new StringBuilder(indent);
235    for (int i=0 ; i < indent; i++)
236    {
237      indentBuf.append(' ');
238    }
239
240    buffer.append(indentBuf);
241    buffer.append("Modify DN Response");
242    buffer.append(EOL);
243
244    buffer.append(indentBuf);
245    buffer.append("  Result Code:  ");
246    buffer.append(resultCode);
247    buffer.append(EOL);
248
249    if (errorMessage != null)
250    {
251      buffer.append(indentBuf);
252      buffer.append("  Error LocalizableMessage:  ");
253      buffer.append(errorMessage);
254      buffer.append(EOL);
255    }
256
257    if (matchedDN != null)
258    {
259      buffer.append(indentBuf);
260      buffer.append("  Matched DN:  ");
261      matchedDN.toString(buffer);
262      buffer.append(EOL);
263    }
264
265    if (referralURLs != null && ! referralURLs.isEmpty())
266    {
267      buffer.append(indentBuf);
268      buffer.append("  Referral URLs:  ");
269      buffer.append(EOL);
270
271      for (String s : referralURLs)
272      {
273        buffer.append(indentBuf);
274        buffer.append("  ");
275        buffer.append(s);
276        buffer.append(EOL);
277      }
278    }
279  }
280}