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 modify DN response
033 * protocol op, which is used to provide information about the result of
034 * processing a modify DN request.
035 */
036public class ModifyDNResponseProtocolOp
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 modify DN response protocol op with the provided result code.
054   *
055   * @param  resultCode  The result code for this response.
056   */
057  public ModifyDNResponseProtocolOp(int resultCode)
058  {
059    this.resultCode = resultCode;
060  }
061
062
063
064  /**
065   * Creates a new modify DN response protocol op with the provided result code
066   * and error message.
067   *
068   * @param  resultCode    The result code for this response.
069   * @param  errorMessage  The error message for this response.
070   */
071  public ModifyDNResponseProtocolOp(int resultCode, LocalizableMessage errorMessage)
072  {
073    this.resultCode   = resultCode;
074    this.errorMessage = errorMessage;
075  }
076
077
078
079  /**
080   * Creates a new modify DN 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 ModifyDNResponseProtocolOp(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   * Retrieves the error message for this response.
111   *
112   * @return  The error message for this response, or <CODE>null</CODE> if none
113   *          is available.
114   */
115  public LocalizableMessage getErrorMessage()
116  {
117    return errorMessage;
118  }
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_MODIFY_DN_RESPONSE;
150  }
151
152  @Override
153  public String getProtocolOpName()
154  {
155    return "Modify DN Response";
156  }
157
158  @Override
159  public void write(ASN1Writer stream) throws IOException
160  {
161    stream.writeStartSequence(OP_TYPE_MODIFY_DN_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("ModifyDNResponse(resultCode=");
199    buffer.append(resultCode);
200
201    if (errorMessage != null && errorMessage.length() > 0)
202    {
203      buffer.append(", errorMessage=");
204      buffer.append(errorMessage);
205    }
206    if (matchedDN != null)
207    {
208      buffer.append(", matchedDN=");
209      buffer.append(matchedDN);
210    }
211    if (referralURLs != null && ! referralURLs.isEmpty())
212    {
213      buffer.append(", referralURLs={");
214      Utils.joinAsString(buffer, ", ", referralURLs);
215      buffer.append("}");
216    }
217
218    buffer.append(")");
219  }
220
221  @Override
222  public void toString(StringBuilder buffer, int indent)
223  {
224    StringBuilder indentBuf = new StringBuilder(indent);
225    for (int i=0 ; i < indent; i++)
226    {
227      indentBuf.append(' ');
228    }
229
230    buffer.append(indentBuf);
231    buffer.append("Modify DN Response");
232    buffer.append(EOL);
233
234    buffer.append(indentBuf);
235    buffer.append("  Result Code:  ");
236    buffer.append(resultCode);
237    buffer.append(EOL);
238
239    if (errorMessage != null)
240    {
241      buffer.append(indentBuf);
242      buffer.append("  Error LocalizableMessage:  ");
243      buffer.append(errorMessage);
244      buffer.append(EOL);
245    }
246
247    if (matchedDN != null)
248    {
249      buffer.append(indentBuf);
250      buffer.append("  Matched DN:  ");
251      buffer.append(matchedDN);
252      buffer.append(EOL);
253    }
254
255    if (referralURLs != null && ! referralURLs.isEmpty())
256    {
257      buffer.append(indentBuf);
258      buffer.append("  Referral URLs:  ");
259      buffer.append(EOL);
260
261      for (String s : referralURLs)
262      {
263        buffer.append(indentBuf);
264        buffer.append("  ");
265        buffer.append(s);
266        buffer.append(EOL);
267      }
268    }
269  }
270}