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