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 response
033 * protocol op, which is used to provide information about the result of
034 * processing a modify request.
035 */
036public class ModifyResponseProtocolOp
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 response protocol op with the provided result code.
054   *
055   * @param  resultCode  The result code for this response.
056   */
057  public ModifyResponseProtocolOp(int resultCode)
058  {
059    this.resultCode = resultCode;
060  }
061
062
063
064  /**
065   * Creates a new modify 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 ModifyResponseProtocolOp(int resultCode, LocalizableMessage errorMessage)
072  {
073    this.resultCode   = resultCode;
074    this.errorMessage = errorMessage;
075  }
076
077
078
079  /**
080   * Creates a new modify 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 ModifyResponseProtocolOp(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   * Retrieves the matched DN for this response.
123   *
124   * @return  The matched DN for this response, or <CODE>null</CODE> if none is
125   *          available.
126   */
127  public DN getMatchedDN()
128  {
129    return matchedDN;
130  }
131
132
133
134  /**
135   * Retrieves the set of referral URLs for this response.
136   *
137   * @return  The set of referral URLs for this response, or <CODE>null</CODE>
138   *          if none are available.
139   */
140  public List<String> getReferralURLs()
141  {
142    return referralURLs;
143  }
144
145  @Override
146  public byte getType()
147  {
148    return OP_TYPE_MODIFY_RESPONSE;
149  }
150
151  @Override
152  public String getProtocolOpName()
153  {
154    return "Modify Response";
155  }
156
157  @Override
158  public void write(ASN1Writer stream) throws IOException
159  {
160    stream.writeStartSequence(OP_TYPE_MODIFY_RESPONSE);
161    stream.writeEnumerated(resultCode);
162
163    if(matchedDN == null)
164    {
165      stream.writeOctetString((String)null);
166    }
167    else
168    {
169      stream.writeOctetString(matchedDN.toString());
170    }
171
172    if(errorMessage == null)
173    {
174      stream.writeOctetString((String)null);
175    }
176    else
177    {
178      stream.writeOctetString(errorMessage.toString());
179    }
180
181    if (referralURLs != null && !referralURLs.isEmpty())
182    {
183      stream.writeStartSequence(TYPE_REFERRAL_SEQUENCE);
184      for (String s : referralURLs)
185      {
186        stream.writeOctetString(s);
187      }
188      stream.writeEndSequence();
189    }
190
191    stream.writeEndSequence();
192  }
193
194  @Override
195  public void toString(StringBuilder buffer)
196  {
197    buffer.append("ModifyResponse(resultCode=");
198    buffer.append(resultCode);
199
200    if (errorMessage != null && errorMessage.length() > 0)
201    {
202      buffer.append(", errorMessage=");
203      buffer.append(errorMessage);
204    }
205    if (matchedDN != null)
206    {
207      buffer.append(", matchedDN=");
208      buffer.append(matchedDN);
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("Modify 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 LocalizableMessage:  ");
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}