001    /*
002     * Copyright 2007-2013 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2007-2013 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     *       System.err.println("Password policy error:  " + errorType.getName());
079     *     }
080     *
081     *     DraftBeheraLDAPPasswordPolicy10WarningType warningType =
082     *          pwpResponse.getWarningType();
083     *     if (warningType != null)
084     *     {
085     *       int value = pwpResponse.getWarningValue();
086     *       switch (warningType)
087     *       {
088     *         case TIME_BEFORE_EXPIRATION:
089     *           System.err.println("Your password will expire in " + value +
090     *                              " seconds.");
091     *           break;
092     *         case GRACE_LOGINS_REMAINING:
093     *           System.err.println("You have " + value +
094     *                              " grace logins remaining.");
095     *       }
096     *     }
097     *   }
098     * </PRE>
099     */
100    @NotMutable()
101    @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
102    public final class DraftBeheraLDAPPasswordPolicy10RequestControl
103           extends Control
104    {
105      /**
106       * The OID (1.3.6.1.4.1.42.2.27.8.5.1) for the password policy request
107       * control.
108       */
109      public static final String PASSWORD_POLICY_REQUEST_OID =
110           "1.3.6.1.4.1.42.2.27.8.5.1";
111    
112    
113    
114      /**
115       * The serial version UID for this serializable class.
116       */
117      private static final long serialVersionUID = 6495056761590890150L;
118    
119    
120    
121      /**
122       * Creates a new password policy request control.  The control will not be
123       * marked critical.
124       */
125      public DraftBeheraLDAPPasswordPolicy10RequestControl()
126      {
127        super(PASSWORD_POLICY_REQUEST_OID, false, null);
128      }
129    
130    
131    
132      /**
133       * Creates a new password policy request control.
134       *
135       * @param  isCritical  Indicates whether the control should be marked
136       * critical.
137       */
138      public DraftBeheraLDAPPasswordPolicy10RequestControl(final boolean isCritical)
139      {
140        super(PASSWORD_POLICY_REQUEST_OID, isCritical, null);
141      }
142    
143    
144    
145      /**
146       * Creates a new password policy request control which is decoded from the
147       * provided generic control.
148       *
149       * @param  control  The generic control to be decoded as a password policy
150       *                  request control.
151       *
152       * @throws  LDAPException  If the provided control cannot be decoded as a
153       *                         password policy request control.
154       */
155      public DraftBeheraLDAPPasswordPolicy10RequestControl(final Control control)
156             throws LDAPException
157      {
158        super(control);
159    
160        if (control.hasValue())
161        {
162          throw new LDAPException(ResultCode.DECODING_ERROR,
163                                  ERR_PWP_REQUEST_HAS_VALUE.get());
164        }
165      }
166    
167    
168    
169      /**
170       * {@inheritDoc}
171       */
172      @Override()
173      public String getControlName()
174      {
175        return INFO_CONTROL_NAME_PW_POLICY_REQUEST.get();
176      }
177    
178    
179    
180      /**
181       * {@inheritDoc}
182       */
183      @Override()
184      public void toString(final StringBuilder buffer)
185      {
186        buffer.append("PasswordPolicyRequestControl(isCritical=");
187        buffer.append(isCritical());
188        buffer.append(')');
189      }
190    }