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