001/*
002 * Copyright 2015-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.ASN1OctetString;
027import com.unboundid.asn1.ASN1Sequence;
028import com.unboundid.ldap.sdk.Control;
029import com.unboundid.ldap.sdk.ExtendedRequest;
030import com.unboundid.ldap.sdk.ExtendedResult;
031import com.unboundid.ldap.sdk.LDAPConnection;
032import com.unboundid.ldap.sdk.LDAPException;
033import com.unboundid.ldap.sdk.ResultCode;
034import com.unboundid.util.Debug;
035import com.unboundid.util.NotMutable;
036import com.unboundid.util.StaticUtils;
037import com.unboundid.util.ThreadSafety;
038import com.unboundid.util.ThreadSafetyLevel;
039
040import static com.unboundid.ldap.sdk.unboundidds.extensions.ExtOpMessages.*;
041
042
043
044/**
045 * This class provides an implementation of an extended request that can be used
046 * to retrieve information about which one-time password delivery mechanisms are
047 * supported for a user.
048 * <BR>
049 * <BLOCKQUOTE>
050 *   <B>NOTE:</B>  This class, and other classes within the
051 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
052 *   supported for use against Ping Identity, UnboundID, and Alcatel-Lucent 8661
053 *   server products.  These classes provide support for proprietary
054 *   functionality or for external specifications that are not considered stable
055 *   or mature enough to be guaranteed to work in an interoperable way with
056 *   other types of LDAP servers.
057 * </BLOCKQUOTE>
058 * <BR>
059 * The OID for this extended request is "1.3.6.1.4.1.30221.2.6.47".  It must
060 * have a value with the following encoding:
061 * <BR><BR>
062 * <PRE>
063 *   GetSupportedOTPDeliveryMechanismsRequest ::= SEQUENCE {
064 *        userDN     [0] LDAPDN,
065 *        ... }
066 * </PRE>
067 *
068 * @see  GetSupportedOTPDeliveryMechanismsExtendedResult
069 */
070@NotMutable()
071@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
072public final class GetSupportedOTPDeliveryMechanismsExtendedRequest
073       extends ExtendedRequest
074{
075  /**
076   * The OID (1.3.6.1.4.1.30221.2.6.47) for the get supported one-time password
077   * delivery mechanisms extended request.
078   */
079  public static final String GET_SUPPORTED_OTP_DELIVERY_MECHANISMS_REQUEST_OID =
080       "1.3.6.1.4.1.30221.2.6.47";
081
082
083
084  /**
085   * The BER type for the userDN element.
086   */
087  private static final byte TYPE_USER_DN = (byte) 0x80;
088
089
090
091  /**
092   * The serial version UID for this serializable class.
093   */
094  private static final long serialVersionUID = -1670631089524097883L;
095
096
097
098  // THe DN of the user for whom to retrieve the supported delivery mechanisms.
099  private final String userDN;
100
101
102
103  /**
104   * Creates a new instance of this get supported OTP delivery mechanisms
105   * extended request with the provided information.
106   *
107   * @param  userDN    The DN of the user for whom to retrieve the list of
108   *                   supported OTP delivery mechanisms.  It must not be
109   *                   {@code null}.
110   * @param  controls  The set of controls to include in the request.  It may be
111   *                   {@code null} or empty if no controls should be included.
112   */
113  public GetSupportedOTPDeliveryMechanismsExtendedRequest(final String userDN,
114              final Control... controls)
115  {
116    super(GET_SUPPORTED_OTP_DELIVERY_MECHANISMS_REQUEST_OID,
117         encodeValue(userDN), controls);
118
119    this.userDN = userDN;
120  }
121
122
123
124  /**
125   * Decodes the provided extended request as a get supported OTP delivery
126   * mechanisms request.
127   *
128   * @param  request  The extended request to be decoded as a get supported OTP
129   *                  delivery mechanisms request.
130   *
131   * @throws  LDAPException  If the provided request cannot be decoded as a get
132   *                         supported OTP delivery mechanisms request.
133   */
134  public GetSupportedOTPDeliveryMechanismsExtendedRequest(
135              final ExtendedRequest request)
136         throws LDAPException
137  {
138    super(request);
139
140    final ASN1OctetString value = request.getValue();
141    if (value == null)
142    {
143      throw new LDAPException(ResultCode.DECODING_ERROR,
144           ERR_GET_SUPPORTED_OTP_MECH_REQUEST_NO_VALUE.get());
145    }
146
147    try
148    {
149      final ASN1Element[] elements =
150           ASN1Sequence.decodeAsSequence(value.getValue()).elements();
151      userDN = ASN1OctetString.decodeAsOctetString(elements[0]).stringValue();
152    }
153    catch (final Exception e)
154    {
155      Debug.debugException(e);
156      throw new LDAPException(ResultCode.DECODING_ERROR,
157           ERR_GET_SUPPORTED_OTP_MECH_REQUEST_CANNOT_DECODE.get(
158                StaticUtils.getExceptionMessage(e)),
159           e);
160    }
161  }
162
163
164
165  /**
166   * Encodes the provided information into an ASN.1 octet string suitable for
167   * use as the value for this extended operation.
168   *
169   * @param  userDN  The DN of the user for whom to retrieve the list of
170   *                 supported OTP delivery mechanisms.  It must not be
171   *                 {@code null}.
172   *
173   * @return  The ASN.1 octet string containing the encoded control value.
174   */
175  private static ASN1OctetString encodeValue(final String userDN)
176  {
177    return new ASN1OctetString(new ASN1Sequence(
178         new ASN1OctetString(TYPE_USER_DN, userDN)).encode());
179  }
180
181
182
183  /**
184   * Retrieves the DN of the user for whom to retrieve the list of supported OTP
185   * delivery mechanisms.
186   *
187   * @return  The DN of the user for whom to retrieve the list of supported OTP
188   *          delivery mechanisms.
189   */
190  public String getUserDN()
191  {
192    return userDN;
193  }
194
195
196
197  /**
198   * {@inheritDoc}
199   */
200  @Override()
201  public GetSupportedOTPDeliveryMechanismsExtendedResult process(
202              final LDAPConnection connection, final int depth)
203         throws LDAPException
204  {
205    final ExtendedResult extendedResponse = super.process(connection, depth);
206    return new GetSupportedOTPDeliveryMechanismsExtendedResult(
207         extendedResponse);
208  }
209
210
211
212  /**
213   * {@inheritDoc}.
214   */
215  @Override()
216  public GetSupportedOTPDeliveryMechanismsExtendedRequest duplicate()
217  {
218    return duplicate(getControls());
219  }
220
221
222
223  /**
224   * {@inheritDoc}.
225   */
226  @Override()
227  public GetSupportedOTPDeliveryMechanismsExtendedRequest duplicate(
228              final Control[] controls)
229  {
230    final GetSupportedOTPDeliveryMechanismsExtendedRequest r =
231         new GetSupportedOTPDeliveryMechanismsExtendedRequest(userDN,
232              controls);
233    r.setResponseTimeoutMillis(getResponseTimeoutMillis(null));
234    return r;
235  }
236
237
238
239  /**
240   * {@inheritDoc}
241   */
242  @Override()
243  public String getExtendedRequestName()
244  {
245    return INFO_GET_SUPPORTED_OTP_MECH_REQ_NAME.get();
246  }
247
248
249
250  /**
251   * {@inheritDoc}
252   */
253  @Override()
254  public void toString(final StringBuilder buffer)
255  {
256    buffer.append("GetSupportedOTPDeliveryMechanismsExtendedRequest(userDN='");
257    buffer.append(userDN);
258    buffer.append('\'');
259
260    final Control[] controls = getControls();
261    if (controls.length > 0)
262    {
263      buffer.append(", controls={");
264      for (int i=0; i < controls.length; i++)
265      {
266        if (i > 0)
267        {
268          buffer.append(", ");
269        }
270
271        buffer.append(controls[i]);
272      }
273      buffer.append('}');
274    }
275
276    buffer.append(')');
277  }
278}