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