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.opendj.ldap.ByteString;
026import org.forgerock.util.Utils;
027import org.forgerock.opendj.ldap.DN;
028
029import static org.opends.server.protocols.ldap.LDAPConstants.*;
030import static org.opends.server.util.ServerConstants.*;
031
032/**
033 * This class defines the structures and methods for an LDAP extended response
034 * protocol op, which is used to provide information about the result of
035 * processing a extended request.
036 */
037public class ExtendedResponseProtocolOp
038       extends ProtocolOp
039{
040  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
041
042  /** The value for this extended response. */
043  private ByteString value;
044
045  /** The matched DN for this response. */
046  private DN matchedDN;
047  /** The result code for this response. */
048  private int resultCode;
049  /** The set of referral URLs for this response. */
050  private List<String> referralURLs;
051  /** The error message for this response. */
052  private LocalizableMessage errorMessage;
053
054  /** The OID for this extended response. */
055  private String oid;
056
057
058
059  /**
060   * Creates a new extended response protocol op with the provided result code.
061   *
062   * @param  resultCode  The result code for this response.
063   */
064  public ExtendedResponseProtocolOp(int resultCode)
065  {
066    this.resultCode = resultCode;
067  }
068
069
070
071  /**
072   * Creates a new extended response protocol op with the provided result code
073   * and error message.
074   *
075   * @param  resultCode    The result code for this response.
076   * @param  errorMessage  The error message for this response.
077   */
078  public ExtendedResponseProtocolOp(int resultCode, LocalizableMessage errorMessage)
079  {
080    this.resultCode   = resultCode;
081    this.errorMessage = errorMessage;
082  }
083
084
085
086  /**
087   * Creates a new extended response protocol op with the provided information.
088   *
089   * @param  resultCode    The result code for this response.
090   * @param  errorMessage  The error message for this response.
091   * @param  matchedDN     The matched DN for this response.
092   * @param  referralURLs  The referral URLs for this response.
093   */
094  public ExtendedResponseProtocolOp(int resultCode, LocalizableMessage errorMessage,
095                                    DN matchedDN, List<String> referralURLs)
096  {
097    this.resultCode   = resultCode;
098    this.errorMessage = errorMessage;
099    this.matchedDN    = matchedDN;
100    this.referralURLs = referralURLs;
101  }
102
103
104
105  /**
106   * Creates a new extended response protocol op with the provided information.
107   *
108   * @param  resultCode    The result code for this response.
109   * @param  errorMessage  The error message for this response.
110   * @param  matchedDN     The matched DN for this response.
111   * @param  referralURLs  The referral URLs for this response.
112   * @param  oid           The OID for this extended response.
113   * @param  value         The value for this extended response.
114   */
115  public ExtendedResponseProtocolOp(int resultCode, LocalizableMessage errorMessage,
116                                    DN matchedDN, List<String> referralURLs,
117                                    String oid, ByteString value)
118  {
119    this.resultCode   = resultCode;
120    this.errorMessage = errorMessage;
121    this.matchedDN    = matchedDN;
122    this.referralURLs = referralURLs;
123    this.oid          = oid;
124    this.value        = value;
125  }
126
127
128
129  /**
130   * Retrieves the result code for this response.
131   *
132   * @return  The result code for this response.
133   */
134  public int getResultCode()
135  {
136    return resultCode;
137  }
138
139
140
141  /**
142   * Retrieves the error message for this response.
143   *
144   * @return  The error message for this response, or <CODE>null</CODE> if none
145   *          is available.
146   */
147  public LocalizableMessage getErrorMessage()
148  {
149    return errorMessage;
150  }
151
152
153  /**
154   * Retrieves the matched DN for this response.
155   *
156   * @return  The matched DN for this response, or <CODE>null</CODE> if none is
157   *          available.
158   */
159  public DN getMatchedDN()
160  {
161    return matchedDN;
162  }
163
164
165
166  /**
167   * Retrieves the set of referral URLs for this response.
168   *
169   * @return  The set of referral URLs for this response, or <CODE>null</CODE>
170   *          if none are available.
171   */
172  public List<String> getReferralURLs()
173  {
174    return referralURLs;
175  }
176
177
178
179  /**
180   * Retrieves the OID for this extended response.
181   *
182   * @return  The OID for this extended response, or <CODE>null</CODE> if none
183   *          was provided.
184   */
185  public String getOID()
186  {
187    return oid;
188  }
189
190
191
192  /**
193   * Retrieves the value for this extended response.
194   *
195   * @return  The value for this extended response, or <CODE>null</CODE> if none
196   *          was provided.
197   */
198  public ByteString getValue()
199  {
200    return value;
201  }
202
203  @Override
204  public byte getType()
205  {
206    return OP_TYPE_EXTENDED_RESPONSE;
207  }
208
209  @Override
210  public String getProtocolOpName()
211  {
212    return "Extended Response";
213  }
214
215  @Override
216  public void write(ASN1Writer stream) throws IOException
217  {
218    stream.writeStartSequence(OP_TYPE_EXTENDED_RESPONSE);
219    stream.writeEnumerated(resultCode);
220
221    if(matchedDN == null)
222    {
223      stream.writeOctetString((String)null);
224    }
225    else
226    {
227      stream.writeOctetString(matchedDN.toString());
228    }
229
230    if(errorMessage == null)
231    {
232      stream.writeOctetString((String)null);
233    }
234    else
235    {
236      stream.writeOctetString(errorMessage.toString());
237    }
238
239    if (referralURLs != null && !referralURLs.isEmpty())
240    {
241      stream.writeStartSequence(TYPE_REFERRAL_SEQUENCE);
242      for (String s : referralURLs)
243      {
244        stream.writeOctetString(s);
245      }
246      stream.writeEndSequence();
247    }
248
249    if (oid != null && oid.length() > 0)
250    {
251      stream.writeOctetString(TYPE_EXTENDED_RESPONSE_OID, oid);
252    }
253
254    if (value != null)
255    {
256      stream.writeOctetString(TYPE_EXTENDED_RESPONSE_VALUE, value);
257    }
258
259    stream.writeEndSequence();
260  }
261
262  @Override
263  public void toString(StringBuilder buffer)
264  {
265    buffer.append("ExtendedResponse(resultCode=");
266    buffer.append(resultCode);
267
268    if (errorMessage != null && errorMessage.length() > 0)
269    {
270      buffer.append(", errorMessage=");
271      buffer.append(errorMessage);
272    }
273    if (matchedDN != null)
274    {
275      buffer.append(", matchedDN=");
276      buffer.append(matchedDN);
277    }
278    if (referralURLs != null && !referralURLs.isEmpty())
279    {
280      buffer.append(", referralURLs={");
281      Utils.joinAsString(buffer, ", ", referralURLs);
282      buffer.append("}");
283    }
284    if (oid != null && oid.length() > 0)
285    {
286      buffer.append(", oid=");
287      buffer.append(oid);
288    }
289    if (value != null)
290    {
291      buffer.append(", value=");
292      buffer.append(value);
293    }
294
295    buffer.append(")");
296  }
297
298  @Override
299  public void toString(StringBuilder buffer, int indent)
300  {
301    StringBuilder indentBuf = new StringBuilder(indent);
302    for (int i=0 ; i < indent; i++)
303    {
304      indentBuf.append(' ');
305    }
306
307    buffer.append(indentBuf);
308    buffer.append("Extended Response");
309    buffer.append(EOL);
310
311    buffer.append(indentBuf);
312    buffer.append("  Result Code:  ");
313    buffer.append(resultCode);
314    buffer.append(EOL);
315
316    if (errorMessage != null)
317    {
318      buffer.append(indentBuf);
319      buffer.append("  Error LocalizableMessage:  ");
320      buffer.append(errorMessage);
321      buffer.append(EOL);
322    }
323
324    if (matchedDN != null)
325    {
326      buffer.append(indentBuf);
327      buffer.append("  Matched DN:  ");
328      buffer.append(matchedDN);
329      buffer.append(EOL);
330    }
331
332    if (referralURLs != null && !referralURLs.isEmpty())
333    {
334      buffer.append(indentBuf);
335      buffer.append("  Referral URLs:  ");
336      buffer.append(EOL);
337
338      for (String s : referralURLs)
339      {
340        buffer.append(indentBuf);
341        buffer.append("  ");
342        buffer.append(s);
343        buffer.append(EOL);
344      }
345    }
346
347    if (oid != null && oid.length() > 0)
348    {
349      buffer.append(indentBuf);
350      buffer.append("  Response OID:  ");
351      buffer.append(oid);
352      buffer.append(EOL);
353    }
354
355    if (value != null)
356    {
357      buffer.append(indentBuf);
358      buffer.append("  Response Value:  ");
359      buffer.append(value);
360      buffer.append(EOL);
361    }
362  }
363}