001/*
002 * Copyright 2008-2017 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2015-2017 Ping Identity Corporation
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021package com.unboundid.ldap.sdk.unboundidds.extensions;
022
023
024
025import com.unboundid.asn1.ASN1Element;
026import com.unboundid.asn1.ASN1Long;
027import com.unboundid.asn1.ASN1OctetString;
028import com.unboundid.ldap.sdk.Control;
029import com.unboundid.ldap.sdk.ExtendedResult;
030import com.unboundid.ldap.sdk.LDAPException;
031import com.unboundid.ldap.sdk.ResultCode;
032import com.unboundid.util.NotMutable;
033import com.unboundid.util.ThreadSafety;
034import com.unboundid.util.ThreadSafetyLevel;
035
036import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*;
037import static com.unboundid.util.Debug.*;
038
039
040
041/**
042 * This class implements a data structure for storing the information from an
043 * extended result for the get connection ID extended request.  It is able to
044 * decode a generic extended result to obtain the associated connection ID.
045 * <BR>
046 * <BLOCKQUOTE>
047 *   <B>NOTE:</B>  This class, and other classes within the
048 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
049 *   supported for use against Ping Identity, UnboundID, and Alcatel-Lucent 8661
050 *   server products.  These classes provide support for proprietary
051 *   functionality or for external specifications that are not considered stable
052 *   or mature enough to be guaranteed to work in an interoperable way with
053 *   other types of LDAP servers.
054 * </BLOCKQUOTE>
055 */
056@NotMutable()
057@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
058public final class GetConnectionIDExtendedResult
059       extends ExtendedResult
060{
061  /**
062   * The serial version UID for this serializable class.
063   */
064  private static final long serialVersionUID = -3161975076326146250L;
065
066
067
068  // The connection ID for the associated client connection.
069  private final long connectionID;
070
071
072
073  /**
074   * Creates a new get connection ID extended result from the provided generic
075   * extended result.
076   *
077   * @param  extendedResult  The generic extended result to be decoded.
078   *
079   * @throws  LDAPException  If a problem occurs while attempting to decode the
080   *                         provided extended result as a get connection ID
081   *                         result.
082   */
083  public GetConnectionIDExtendedResult(final ExtendedResult extendedResult)
084         throws LDAPException
085  {
086    super(extendedResult);
087
088    final ASN1OctetString value = extendedResult.getValue();
089    if (value == null)
090    {
091      connectionID = -1;
092      return;
093    }
094
095    try
096    {
097      final ASN1Element e = ASN1Element.decode(value.getValue());
098      connectionID = ASN1Long.decodeAsLong(e).longValue();
099    }
100    catch (final Exception e)
101    {
102      debugException(e);
103      throw new LDAPException(ResultCode.DECODING_ERROR,
104                              ERR_GET_CONN_ID_RESPONSE_VALUE_NOT_INT.get(), e);
105    }
106  }
107
108
109
110  /**
111   * Creates a get connection ID extended result with the provided information.
112   *
113   * @param  messageID          The message ID for the LDAP message that is
114   *                            associated with this LDAP result.
115   * @param  resultCode         The result code from the response.
116   * @param  diagnosticMessage  The diagnostic message from the response, if
117   *                            available.
118   * @param  matchedDN          The matched DN from the response, if available.
119   * @param  referralURLs       The set of referral URLs from the response, if
120   *                            available.
121   * @param  connectionID       The connection ID for the response.
122   * @param  responseControls   The set of controls from the response, if
123   *                            available.
124   */
125  public GetConnectionIDExtendedResult(final int messageID,
126                                       final ResultCode resultCode,
127                                       final String diagnosticMessage,
128                                       final String matchedDN,
129                                       final String[] referralURLs,
130                                       final Long connectionID,
131                                       final Control[] responseControls)
132  {
133    super(messageID, resultCode, diagnosticMessage, matchedDN, referralURLs,
134          null, encodeValue(connectionID), responseControls);
135
136    if (connectionID == null)
137    {
138      this.connectionID = -1;
139    }
140    else
141    {
142      this.connectionID = connectionID;
143    }
144  }
145
146
147
148  /**
149   * Encodes the value for this extended result using the provided information.
150   *
151   * @param  connectionID  The connection ID for the response.
152   *
153   * @return  An ASN.1 octet string containing the properly-encoded value, or
154   *          {@code null} if there should be no value.
155   */
156  private static ASN1OctetString encodeValue(final Long connectionID)
157  {
158    if ((connectionID == null) || (connectionID < 0))
159    {
160      return null;
161    }
162    else
163    {
164      return new ASN1OctetString(new ASN1Long(connectionID).encode());
165    }
166  }
167
168
169
170  /**
171   * Retrieves the connection ID from this response.
172   *
173   * @return  The connection ID from this response, or -1 if the connection ID
174   *          is not available for some reason (e.g., because this is an error
175   *          response).
176   */
177  public long getConnectionID()
178  {
179    return connectionID;
180  }
181
182
183
184  /**
185   * {@inheritDoc}
186   */
187  @Override()
188  public String getExtendedResultName()
189  {
190    return INFO_EXTENDED_RESULT_NAME_GET_CONNECTION_ID.get();
191  }
192
193
194
195  /**
196   * {@inheritDoc}
197   */
198  @Override()
199  public void toString(final StringBuilder buffer)
200  {
201    buffer.append("GetConnectionIDExtendedResult(connectionID=");
202    buffer.append(connectionID);
203
204    buffer.append(", resultCode=");
205    buffer.append(getResultCode());
206
207    final int messageID = getMessageID();
208    if (messageID >= 0)
209    {
210      buffer.append(", messageID=");
211      buffer.append(messageID);
212    }
213
214    final String diagnosticMessage = getDiagnosticMessage();
215    if (diagnosticMessage != null)
216    {
217      buffer.append(", diagnosticMessage='");
218      buffer.append(diagnosticMessage);
219      buffer.append('\'');
220    }
221
222    final String matchedDN = getMatchedDN();
223    if (matchedDN != null)
224    {
225      buffer.append(", matchedDN='");
226      buffer.append(matchedDN);
227      buffer.append('\'');
228    }
229
230    final String[] referralURLs = getReferralURLs();
231    if ((referralURLs != null) && (referralURLs.length > 0))
232    {
233      buffer.append(", referralURLs={ '");
234      for (int i=0; i < referralURLs.length; i++)
235      {
236        if (i > 0)
237        {
238          buffer.append("', '");
239        }
240        buffer.append(referralURLs[i]);
241      }
242
243      buffer.append("' }");
244    }
245
246    final Control[] controls = getResponseControls();
247    if (controls.length > 0)
248    {
249      buffer.append(", controls={");
250      for (int i=0; i < controls.length; i++)
251      {
252        if (i > 0)
253        {
254          buffer.append(", ");
255        }
256
257        buffer.append(controls[i]);
258      }
259      buffer.append('}');
260    }
261
262    buffer.append(')');
263  }
264}