001/*
002 * Copyright 2008-2018 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2015-2018 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 * <BR>
056 * This extended result does not have an OID.  If the request was processed
057 * successfully by the server, then the response should have a value that is the
058 * BER-encoded integer representation of the connection ID.
059 */
060@NotMutable()
061@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
062public final class GetConnectionIDExtendedResult
063       extends ExtendedResult
064{
065  /**
066   * The serial version UID for this serializable class.
067   */
068  private static final long serialVersionUID = -3161975076326146250L;
069
070
071
072  // The connection ID for the associated client connection.
073  private final long connectionID;
074
075
076
077  /**
078   * Creates a new get connection ID extended result from the provided generic
079   * extended result.
080   *
081   * @param  extendedResult  The generic extended result to be decoded.
082   *
083   * @throws  LDAPException  If a problem occurs while attempting to decode the
084   *                         provided extended result as a get connection ID
085   *                         result.
086   */
087  public GetConnectionIDExtendedResult(final ExtendedResult extendedResult)
088         throws LDAPException
089  {
090    super(extendedResult);
091
092    final ASN1OctetString value = extendedResult.getValue();
093    if (value == null)
094    {
095      connectionID = -1;
096      return;
097    }
098
099    try
100    {
101      final ASN1Element e = ASN1Element.decode(value.getValue());
102      connectionID = ASN1Long.decodeAsLong(e).longValue();
103    }
104    catch (final Exception e)
105    {
106      debugException(e);
107      throw new LDAPException(ResultCode.DECODING_ERROR,
108                              ERR_GET_CONN_ID_RESPONSE_VALUE_NOT_INT.get(), e);
109    }
110  }
111
112
113
114  /**
115   * Creates a get connection ID extended result with the provided information.
116   *
117   * @param  messageID          The message ID for the LDAP message that is
118   *                            associated with this LDAP result.
119   * @param  resultCode         The result code from the response.
120   * @param  diagnosticMessage  The diagnostic message from the response, if
121   *                            available.
122   * @param  matchedDN          The matched DN from the response, if available.
123   * @param  referralURLs       The set of referral URLs from the response, if
124   *                            available.
125   * @param  connectionID       The connection ID for the response.
126   * @param  responseControls   The set of controls from the response, if
127   *                            available.
128   */
129  public GetConnectionIDExtendedResult(final int messageID,
130                                       final ResultCode resultCode,
131                                       final String diagnosticMessage,
132                                       final String matchedDN,
133                                       final String[] referralURLs,
134                                       final Long connectionID,
135                                       final Control[] responseControls)
136  {
137    super(messageID, resultCode, diagnosticMessage, matchedDN, referralURLs,
138          null, encodeValue(connectionID), responseControls);
139
140    if (connectionID == null)
141    {
142      this.connectionID = -1;
143    }
144    else
145    {
146      this.connectionID = connectionID;
147    }
148  }
149
150
151
152  /**
153   * Encodes the value for this extended result using the provided information.
154   *
155   * @param  connectionID  The connection ID for the response.
156   *
157   * @return  An ASN.1 octet string containing the properly-encoded value, or
158   *          {@code null} if there should be no value.
159   */
160  private static ASN1OctetString encodeValue(final Long connectionID)
161  {
162    if ((connectionID == null) || (connectionID < 0))
163    {
164      return null;
165    }
166    else
167    {
168      return new ASN1OctetString(new ASN1Long(connectionID).encode());
169    }
170  }
171
172
173
174  /**
175   * Retrieves the connection ID from this response.
176   *
177   * @return  The connection ID from this response, or -1 if the connection ID
178   *          is not available for some reason (e.g., because this is an error
179   *          response).
180   */
181  public long getConnectionID()
182  {
183    return connectionID;
184  }
185
186
187
188  /**
189   * {@inheritDoc}
190   */
191  @Override()
192  public String getExtendedResultName()
193  {
194    return INFO_EXTENDED_RESULT_NAME_GET_CONNECTION_ID.get();
195  }
196
197
198
199  /**
200   * {@inheritDoc}
201   */
202  @Override()
203  public void toString(final StringBuilder buffer)
204  {
205    buffer.append("GetConnectionIDExtendedResult(connectionID=");
206    buffer.append(connectionID);
207
208    buffer.append(", resultCode=");
209    buffer.append(getResultCode());
210
211    final int messageID = getMessageID();
212    if (messageID >= 0)
213    {
214      buffer.append(", messageID=");
215      buffer.append(messageID);
216    }
217
218    final String diagnosticMessage = getDiagnosticMessage();
219    if (diagnosticMessage != null)
220    {
221      buffer.append(", diagnosticMessage='");
222      buffer.append(diagnosticMessage);
223      buffer.append('\'');
224    }
225
226    final String matchedDN = getMatchedDN();
227    if (matchedDN != null)
228    {
229      buffer.append(", matchedDN='");
230      buffer.append(matchedDN);
231      buffer.append('\'');
232    }
233
234    final String[] referralURLs = getReferralURLs();
235    if ((referralURLs != null) && (referralURLs.length > 0))
236    {
237      buffer.append(", referralURLs={ '");
238      for (int i=0; i < referralURLs.length; i++)
239      {
240        if (i > 0)
241        {
242          buffer.append("', '");
243        }
244        buffer.append(referralURLs[i]);
245      }
246
247      buffer.append("' }");
248    }
249
250    final Control[] controls = getResponseControls();
251    if (controls.length > 0)
252    {
253      buffer.append(", controls={");
254      for (int i=0; i < controls.length; i++)
255      {
256        if (i > 0)
257        {
258          buffer.append(", ");
259        }
260
261        buffer.append(controls[i]);
262      }
263      buffer.append('}');
264    }
265
266    buffer.append(')');
267  }
268}