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