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