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