001    /*
002     * Copyright 2007-2015 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2007-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.experimental;
022    
023    
024    
025    import com.unboundid.ldap.sdk.Control;
026    import com.unboundid.ldap.sdk.LDAPException;
027    import com.unboundid.ldap.sdk.ResultCode;
028    import com.unboundid.util.NotMutable;
029    import com.unboundid.util.ThreadSafety;
030    import com.unboundid.util.ThreadSafetyLevel;
031    
032    import static com.unboundid.ldap.sdk.experimental.ExperimentalMessages.*;
033    
034    
035    
036    /**
037     * This class provides an implementation of the password policy request control
038     * as described in draft-behera-ldap-password-policy-10.  It may be used to
039     * request information related to a user's password policy.  In the UnboundID
040     * Directory Server, this control may be included with add, bind, compare,
041     * modify, and password modify requests.
042     * <BR><BR>
043     * The corresponding {@link DraftBeheraLDAPPasswordPolicy10ResponseControl} may
044     * include at most one warning from the set of
045     * {@link DraftBeheraLDAPPasswordPolicy10WarningType} values and at most one
046     * error from the set of {@link DraftBeheraLDAPPasswordPolicy10ErrorType}
047     * values.  See the documentation for those classes for more information on the
048     * information that may be included.
049     * <BR><BR>
050     * <H2>Example</H2>
051     * The following example demonstrates the use of the password policy request
052     * control in conjunction with a bind operation:
053     * <PRE>
054     * SimpleBindRequest bindRequest = new SimpleBindRequest(
055     *      "uid=john.doe,ou=People,dc=example,dc=com", "password",
056     *      new DraftBeheraLDAPPasswordPolicy10RequestControl());
057     *
058     * BindResult bindResult;
059     * try
060     * {
061     *   bindResult = connection.bind(bindRequest);
062     * }
063     * catch (LDAPException le)
064     * {
065     *   // The bind failed.  There may be a password policy response control to
066     *   // help tell us why.
067     *   bindResult = new BindResult(le.toLDAPResult());
068     * }
069     *
070     * DraftBeheraLDAPPasswordPolicy10ResponseControl pwpResponse =
071     *      DraftBeheraLDAPPasswordPolicy10ResponseControl.get(bindResult);
072     * if (pwpResponse != null)
073     * {
074     *   DraftBeheraLDAPPasswordPolicy10ErrorType errorType =
075     *        pwpResponse.getErrorType();
076     *   if (errorType != null)
077     *   {
078     *     // There was a password policy error.
079     *   }
080     *
081     *   DraftBeheraLDAPPasswordPolicy10WarningType warningType =
082     *        pwpResponse.getWarningType();
083     *   if (warningType != null)
084     *   {
085     *     // There was a password policy warning.
086     *     int value = pwpResponse.getWarningValue();
087     *     switch (warningType)
088     *     {
089     *       case TIME_BEFORE_EXPIRATION:
090     *         // The warning value is the number of seconds until expiration.
091     *         break;
092     *       case GRACE_LOGINS_REMAINING:
093     *         // The warning value is the number of grace logins remaining.
094     *     }
095     *   }
096     * }
097     * </PRE>
098     */
099    @NotMutable()
100    @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
101    public final class DraftBeheraLDAPPasswordPolicy10RequestControl
102           extends Control
103    {
104      /**
105       * The OID (1.3.6.1.4.1.42.2.27.8.5.1) for the password policy request
106       * control.
107       */
108      public static final String PASSWORD_POLICY_REQUEST_OID =
109           "1.3.6.1.4.1.42.2.27.8.5.1";
110    
111    
112    
113      /**
114       * The serial version UID for this serializable class.
115       */
116      private static final long serialVersionUID = 6495056761590890150L;
117    
118    
119    
120      /**
121       * Creates a new password policy request control.  The control will not be
122       * marked critical.
123       */
124      public DraftBeheraLDAPPasswordPolicy10RequestControl()
125      {
126        super(PASSWORD_POLICY_REQUEST_OID, false, null);
127      }
128    
129    
130    
131      /**
132       * Creates a new password policy request control.
133       *
134       * @param  isCritical  Indicates whether the control should be marked
135       * critical.
136       */
137      public DraftBeheraLDAPPasswordPolicy10RequestControl(final boolean isCritical)
138      {
139        super(PASSWORD_POLICY_REQUEST_OID, isCritical, null);
140      }
141    
142    
143    
144      /**
145       * Creates a new password policy request control which is decoded from the
146       * provided generic control.
147       *
148       * @param  control  The generic control to be decoded as a password policy
149       *                  request control.
150       *
151       * @throws  LDAPException  If the provided control cannot be decoded as a
152       *                         password policy request control.
153       */
154      public DraftBeheraLDAPPasswordPolicy10RequestControl(final Control control)
155             throws LDAPException
156      {
157        super(control);
158    
159        if (control.hasValue())
160        {
161          throw new LDAPException(ResultCode.DECODING_ERROR,
162                                  ERR_PWP_REQUEST_HAS_VALUE.get());
163        }
164      }
165    
166    
167    
168      /**
169       * {@inheritDoc}
170       */
171      @Override()
172      public String getControlName()
173      {
174        return INFO_CONTROL_NAME_PW_POLICY_REQUEST.get();
175      }
176    
177    
178    
179      /**
180       * {@inheritDoc}
181       */
182      @Override()
183      public void toString(final StringBuilder buffer)
184      {
185        buffer.append("PasswordPolicyRequestControl(isCritical=");
186        buffer.append(isCritical());
187        buffer.append(')');
188      }
189    }