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