001/*
002 * Copyright 2008-2022 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2008-2022 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-2022 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.controls;
037
038
039
040import com.unboundid.asn1.ASN1Element;
041import com.unboundid.asn1.ASN1OctetString;
042import com.unboundid.asn1.ASN1Sequence;
043import com.unboundid.ldap.sdk.Control;
044import com.unboundid.ldap.sdk.DecodeableControl;
045import com.unboundid.ldap.sdk.LDAPException;
046import com.unboundid.ldap.sdk.LDAPResult;
047import com.unboundid.ldap.sdk.ResultCode;
048import com.unboundid.util.NotMutable;
049import com.unboundid.util.NotNull;
050import com.unboundid.util.Nullable;
051import com.unboundid.util.StaticUtils;
052import com.unboundid.util.ThreadSafety;
053import com.unboundid.util.ThreadSafetyLevel;
054
055import static com.unboundid.ldap.sdk.unboundidds.controls.ControlMessages.*;
056
057
058
059/**
060 * This class defines an intermediate client response control, which can be used
061 * to provide a server with information about the client and any downstream
062 * clients that it may have.
063 * <BR>
064 * <BLOCKQUOTE>
065 *   <B>NOTE:</B>  This class, and other classes within the
066 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
067 *   supported for use against Ping Identity, UnboundID, and
068 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
069 *   for proprietary functionality or for external specifications that are not
070 *   considered stable or mature enough to be guaranteed to work in an
071 *   interoperable way with other types of LDAP servers.
072 * </BLOCKQUOTE>
073 * <BR>
074 * This control is not based on any public standard.  It was originally
075 * developed for use with the Ping Identity, UnboundID, and Nokia/Alcatel-Lucent
076 * 8661 Directory Server.  The value of this control uses the following
077 * encoding:
078 * <BR><BR>
079 * <PRE>
080 * IntermediateClientResponse ::= SEQUENCE {
081 *      upstreamResponse       [0] IntermediateClientResponse OPTIONAL,
082 *      upstreamServerAddress  [1] OCTET STRING OPTIONAL,
083 *      upstreamServerSecure   [2] BOOLEAN DEFAULT FALSE,
084 *      serverName             [3] OCTET STRING OPTIONAL,
085 *      serverSessionID        [4] OCTET STRING OPTIONAL,
086 *      serverResponseID       [5] OCTET STRING OPTIONAL,
087 *      ... }
088 * </PRE>
089 * See the documentation in the {@link IntermediateClientRequestControl} class
090 * for an example of using the intermediate client request and response
091 * controls.
092 */
093@NotMutable()
094@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
095public final class IntermediateClientResponseControl
096       extends Control
097       implements DecodeableControl
098{
099  /**
100   * The OID (1.3.6.1.4.1.30221.2.5.2) for the intermediate client response
101   * control.
102   */
103  @NotNull public static final String INTERMEDIATE_CLIENT_RESPONSE_OID =
104       "1.3.6.1.4.1.30221.2.5.2";
105
106
107
108  /**
109   * The serial version UID for this serializable class.
110   */
111  private static final long serialVersionUID = 7476073413872875835L;
112
113
114
115  // The value for this intermediate client response control.
116  @NotNull private final IntermediateClientResponseValue value;
117
118
119
120  /**
121   * Creates a new empty control instance that is intended to be used only for
122   * decoding controls via the {@code DecodeableControl} interface.
123   */
124  IntermediateClientResponseControl()
125  {
126    value = null;
127  }
128
129
130
131  /**
132   * Creates a new intermediate client response control with the provided
133   * information.  It will not be marked critical.
134   *
135   * @param  upstreamResponse       A wrapped intermediate client response from
136   *                                an upstream server.  It may be {@code null}
137   *                                if there is no wrapped upstream response.
138   * @param  upstreamServerAddress  The IP address or resolvable name of the
139   *                                upstream server system.  It may be
140   *                                {@code null} if there is no upstream server
141   *                                or its address is not available.
142   * @param  upstreamServerSecure   Indicates whether communication with the
143   *                                upstream server is secure.  It may be
144   *                                {@code null} if there is no upstream server
145   *                                or it is not known whether the communication
146   *                                is secure.
147   * @param  serverName             An identifier string that summarizes the
148   *                                server application that created this
149   *                                intermediate client response.  It may be
150   *                                {@code null} if that information is not
151   *                                available.
152   * @param  serverSessionID        A string that may be used to identify the
153   *                                session in the server application.  It may
154   *                                be {@code null} if there is no available
155   *                                session identifier.
156   * @param  serverResponseID       A string that may be used to identify the
157   *                                response in the server application.  It may
158   *                                be {@code null} if there is no available
159   *                                response identifier.
160   */
161  public IntermediateClientResponseControl(
162              @Nullable final IntermediateClientResponseValue upstreamResponse,
163              @Nullable final String upstreamServerAddress,
164              @Nullable final Boolean upstreamServerSecure,
165              @Nullable final String serverName,
166              @Nullable final String serverSessionID,
167              @Nullable final String serverResponseID)
168  {
169    this(false,
170         new IntermediateClientResponseValue(upstreamResponse,
171                  upstreamServerAddress, upstreamServerSecure, serverName,
172                  serverSessionID, serverResponseID));
173  }
174
175
176
177  /**
178   * Creates a new intermediate client response control with the provided
179   * information.
180   *
181   * @param  oid         The OID for the control.
182   * @param  isCritical  Indicates whether the control should be marked
183   *                     critical.
184   * @param  value       The encoded value for the control.  This may be
185   *                     {@code null} if no value was provided.
186   *
187   * @throws  LDAPException  If the provided control cannot be decoded as an
188   *                         intermediate client response control.
189   */
190  public IntermediateClientResponseControl(@NotNull final String oid,
191              final boolean isCritical,
192              @Nullable final ASN1OctetString value)
193         throws LDAPException
194  {
195    super(oid, isCritical, value);
196
197    if (value == null)
198    {
199      throw new LDAPException(ResultCode.DECODING_ERROR,
200                              ERR_ICRESP_CONTROL_NO_VALUE.get());
201    }
202
203    final ASN1Sequence valueSequence;
204    try
205    {
206      final ASN1Element valueElement = ASN1Element.decode(value.getValue());
207      valueSequence = ASN1Sequence.decodeAsSequence(valueElement);
208    }
209    catch (final Exception e)
210    {
211      throw new LDAPException(ResultCode.DECODING_ERROR,
212           ERR_ICRESP_CONTROL_VALUE_NOT_SEQUENCE.get(
213                StaticUtils.getExceptionMessage(e)),
214           e);
215    }
216
217    this.value = IntermediateClientResponseValue.decode(valueSequence);
218  }
219
220
221
222  /**
223   * Creates a new intermediate client response control with the provided value.
224   * It will be marked critical.
225   *
226   * @param  value  The value to use for this intermediate client response
227   *                control.  It must not be {@code null}.
228   */
229  public IntermediateClientResponseControl(
230              @NotNull final IntermediateClientResponseValue value)
231  {
232    this(false, value);
233  }
234
235
236
237  /**
238   * Creates a new intermediate client response control with the provided value.
239   *
240   * @param  isCritical  Indicates whether the control should be marked
241   *                     critical.  Response controls should generally not be
242   *                     critical.
243   * @param  value       The value to use for this intermediate client response
244   *                     control.  It must not be {@code null}.
245   */
246  public IntermediateClientResponseControl(final boolean isCritical,
247              @NotNull final IntermediateClientResponseValue value)
248  {
249    super(INTERMEDIATE_CLIENT_RESPONSE_OID, isCritical,
250          new ASN1OctetString(value.encode().encode()));
251
252    this.value = value;
253  }
254
255
256
257  /**
258   * {@inheritDoc}
259   */
260  @Override()
261  @NotNull()
262  public IntermediateClientResponseControl decodeControl(
263              @NotNull final String oid,
264              final boolean isCritical,
265              @Nullable final ASN1OctetString value)
266          throws LDAPException
267  {
268    return new IntermediateClientResponseControl(oid, isCritical, value);
269  }
270
271
272
273  /**
274   * Extracts an intermediate client response control from the provided result.
275   *
276   * @param  result  The result from which to retrieve the intermediate client
277   *                 response control.
278   *
279   * @return  The intermediate client response control contained in the provided
280   *          result, or {@code null} if the result did not contain an
281   *          intermediate client response control.
282   *
283   * @throws  LDAPException  If a problem is encountered while attempting to
284   *                         decode the intermediate client response control
285   *                         contained in the provided result.
286   */
287  @Nullable()
288  public static IntermediateClientResponseControl get(
289                     @NotNull final LDAPResult result)
290         throws LDAPException
291  {
292    final Control c =
293         result.getResponseControl(INTERMEDIATE_CLIENT_RESPONSE_OID);
294    if (c == null)
295    {
296      return null;
297    }
298
299    if (c instanceof IntermediateClientResponseControl)
300    {
301      return (IntermediateClientResponseControl) c;
302    }
303    else
304    {
305      return new IntermediateClientResponseControl(c.getOID(), c.isCritical(),
306           c.getValue());
307    }
308  }
309
310
311
312  /**
313   * Retrieves the value for this intermediate client response.
314   *
315   * @return  The value for this intermediate client response.
316   */
317  @NotNull()
318  public IntermediateClientResponseValue getResponseValue()
319  {
320    return value;
321  }
322
323
324
325  /**
326   * Retrieves the wrapped response from an upstream server, if available.
327   *
328   * @return  The wrapped response from an upstream server, or {@code null} if
329   *          there is none.
330   */
331  @Nullable()
332  public IntermediateClientResponseValue getUpstreamResponse()
333  {
334    return value.getUpstreamResponse();
335  }
336
337
338
339  /**
340   * Retrieves the IP address or resolvable name of the upstream server system,
341   * if available.
342   *
343   * @return  The IP address or resolvable name of the upstream server system,
344   *          {@code null} if there is no upstream server or its address is not
345   *          available.
346   */
347  @Nullable()
348  public String getUpstreamServerAddress()
349  {
350    return value.getUpstreamServerAddress();
351  }
352
353
354
355  /**
356   * Indicates whether the communication with the communication with the
357   * upstream server is secure (i.e., whether communication between the
358   * server application and the upstream server is safe from interpretation or
359   * undetectable alteration by a third party observer or interceptor).
360   *
361   *
362   * @return  {@code Boolean.TRUE} if communication with the upstream server is
363   *          secure, {@code Boolean.FALSE} if it is not secure, or
364   *          {@code null} if there is no upstream server or it is not known
365   *          whether the communication is secure.
366   */
367  @Nullable()
368  public Boolean upstreamServerSecure()
369  {
370    return value.upstreamServerSecure();
371  }
372
373
374
375  /**
376   * Retrieves a string that identifies the server application that created this
377   * intermediate client response value.
378   *
379   * @return  A string that may be used to identify the server application that
380   *          created this intermediate client response value.
381   */
382  @Nullable()
383  public String getServerName()
384  {
385    return value.getServerName();
386  }
387
388
389
390  /**
391   * Retrieves a string that may be used to identify the session in the server
392   * application.
393   *
394   * @return  A string that may be used to identify the session in the server
395   *          application, or {@code null} if there is none.
396   */
397  @Nullable()
398  public String getServerSessionID()
399  {
400    return value.getServerSessionID();
401  }
402
403
404
405  /**
406   * Retrieves a string that may be used to identify the response in the server
407   * application.
408   *
409   * @return  A string that may be used to identify the response in the server
410   *          application, or {@code null} if there is none.
411   */
412  @Nullable()
413  public String getServerResponseID()
414  {
415    return value.getServerResponseID();
416  }
417
418
419
420  /**
421   * {@inheritDoc}
422   */
423  @Override()
424  @NotNull()
425  public String getControlName()
426  {
427    return INFO_CONTROL_NAME_INTERMEDIATE_CLIENT_RESPONSE.get();
428  }
429
430
431
432  /**
433   * {@inheritDoc}
434   */
435  @Override()
436  public void toString(@NotNull final StringBuilder buffer)
437  {
438    buffer.append("IntermediateClientResponseControl(isCritical=");
439    buffer.append(isCritical());
440    buffer.append(", value=");
441    value.toString(buffer);
442    buffer.append(')');
443  }
444}