View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.directory.ldap.client.template;
21  
22  
23  import org.apache.directory.api.ldap.codec.api.LdapApiService;
24  import org.apache.directory.api.ldap.extras.controls.ppolicy.PasswordPolicy;
25  import org.apache.directory.api.ldap.extras.controls.ppolicy_impl.PasswordPolicyDecorator;
26  import org.apache.directory.api.ldap.model.exception.LdapException;
27  import org.apache.directory.api.ldap.model.message.Control;
28  import org.apache.directory.api.ldap.model.message.Response;
29  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
30  import org.apache.directory.api.ldap.model.message.ResultResponse;
31  import org.apache.directory.ldap.client.template.exception.PasswordException;
32  
33  
34  /**
35   * A base, abstract, implementation of <code>PasswordPolicyResponder</code>.  
36   * Extend this class and override success(PasswordPolicy), 
37   * fail(ResultResponse, PasswordPolicy, ResultCodeEnum), or
38   * exception(LdapException).  If that does not offer enough
39   * flexibility, you must implement PasswordPolicyResponder yourself.
40   *
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   */
43  public abstract class AbstractPasswordPolicyResponder implements PasswordPolicyResponder
44  {
45      private final PasswordPolicyDecorator passwordPolicyRequestControl;
46  
47  
48      protected AbstractPasswordPolicyResponder( LdapApiService ldapApiService )
49      {
50          this.passwordPolicyRequestControl = new PasswordPolicyDecorator(
51              ldapApiService );
52      }
53      
54      
55      /**
56       * Translates an <code>LdapException</code> to a 
57       * <code>PasswordException</code> to be thrown when 
58       * {@link #process(PasswordPolicyOperation)} fails.
59       * 
60       * @param e The exception to set
61       * @return The created PasswordException
62       */
63      protected PasswordException exception( LdapException e )
64      {
65          return new PasswordException().setLdapException( e );
66      }
67      
68      
69      /**
70       * Returns an exception to be thrown in the case of a non SUCCESS 
71       * <code>resultCode</code>.
72       * 
73       * @param resultResponse The result response
74       * @param passwordPolicy The password policy in use
75       * @param resultCode The result
76       * @return The created PasswordException
77       */
78      protected PasswordException fail( ResultResponse resultResponse, 
79              PasswordPolicy passwordPolicy, ResultCodeEnum resultCode )
80      {
81          PasswordException exception = new PasswordException();
82          exception.setResultCode( resultCode );
83          if ( passwordPolicy != null
84              && passwordPolicy.getResponse() != null
85              && passwordPolicy.getResponse().getPasswordPolicyError() != null )
86          {
87              exception.setPasswordPolicyError( passwordPolicy.getResponse().getPasswordPolicyError() );
88          }
89          return exception;
90      }
91  
92  
93      private PasswordPolicy getPasswordPolicy( Response response )
94      {
95          Control control = response.getControls().get( passwordPolicyRequestControl.getOid() );
96          return control == null
97              ? null
98              : ( ( PasswordPolicyDecorator ) control ).getDecorated();
99      }
100 
101 
102     @Override
103     public final PasswordWarning process( PasswordPolicyOperation operation )
104         throws PasswordException
105     {
106         try
107         {
108             ResultResponse response = operation.process();
109             PasswordPolicy passwordPolicy = getPasswordPolicy( response );
110             ResultCodeEnum resultCode = response.getLdapResult().getResultCode();
111             if ( resultCode == ResultCodeEnum.SUCCESS )
112             {
113                 return success( passwordPolicy );
114             }
115             else
116             {
117                 throw fail( response, passwordPolicy, resultCode );
118             }
119         }
120         catch ( LdapException e )
121         {
122             throw new PasswordException().setLdapException( e );
123         }
124     }
125     
126     /**
127      * Returns a <code>PasswordWarning</code>, or <code>null</code> if no 
128      * warnings were present in the supplied <code>passwordPolicy</code>.
129      * 
130      * @param passwordPolicy The PasswordPolicy in use
131      * @return The created PasswordWarning
132      */
133     protected PasswordWarning success( PasswordPolicy passwordPolicy ) 
134     {
135         return passwordPolicy == null
136                 ? null
137                 : PasswordWarningImpl.newWarning( passwordPolicy );
138     }
139 }