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   *     https://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.PasswordPolicyResponse;
25  import org.apache.directory.api.ldap.extras.controls.ppolicy.PasswordPolicyResponseImpl;
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 PasswordPolicyResponse passwordPolicyResponseControl;
46  
47  
48      protected AbstractPasswordPolicyResponder( LdapApiService ldapApiService )
49      {
50          this.passwordPolicyResponseControl = new PasswordPolicyResponseImpl();
51      }
52      
53      
54      /**
55       * Translates an <code>LdapException</code> to a 
56       * <code>PasswordException</code> to be thrown when 
57       * {@link #process(PasswordPolicyOperation)} fails.
58       * 
59       * @param e The exception to set
60       * @return The created PasswordException
61       */
62      protected PasswordException exception( LdapException e )
63      {
64          return new PasswordException().setLdapException( e );
65      }
66      
67      
68      /**
69       * Returns an exception to be thrown in the case of a non SUCCESS 
70       * <code>resultCode</code>.
71       * 
72       * @param resultResponse The result response
73       * @param passwordPolicyResponse The password policy in use
74       * @param resultCode The result
75       * @return The created PasswordException
76       */
77      protected PasswordException fail( ResultResponse resultResponse, 
78              PasswordPolicyResponse passwordPolicyResponse, ResultCodeEnum resultCode )
79      {
80          String diagnosticMessage = "";
81          
82          if  ( ( resultResponse != null ) && resultResponse.getLdapResult() != null )
83          {
84              diagnosticMessage = resultResponse.getLdapResult().getDiagnosticMessage();
85          }
86  
87          PasswordException exception = new PasswordException( diagnosticMessage );
88          exception.setResultCode( resultCode );
89          
90          if ( passwordPolicyResponse != null
91              && passwordPolicyResponse.getPasswordPolicyError() != null )
92          {
93              exception.setPasswordPolicyError( passwordPolicyResponse.getPasswordPolicyError() );
94          }
95          return exception;
96      }
97  
98  
99      private PasswordPolicyResponse getPasswordPolicy( Response response )
100     {
101         Control control = response.getControls().get( passwordPolicyResponseControl.getOid() );
102         
103         return control == null
104             ? null
105             : ( PasswordPolicyResponse ) control;
106     }
107 
108 
109     /**
110      * {@inheritDoc}
111      */
112     @Override
113     public final PasswordWarning process( PasswordPolicyOperation operation )
114         throws PasswordException
115     {
116         try
117         {
118             ResultResponse response = operation.process();
119             PasswordPolicyResponse passwordPolicyResponse = getPasswordPolicy( response );
120             ResultCodeEnum resultCode = response.getLdapResult().getResultCode();
121             
122             if ( resultCode == ResultCodeEnum.SUCCESS )
123             {
124                 return success( passwordPolicyResponse );
125             }
126             else
127             {
128                 throw fail( response, passwordPolicyResponse, resultCode );
129             }
130         }
131         catch ( LdapException e )
132         {
133             throw new PasswordException().setLdapException( e );
134         }
135     }
136     
137     /**
138      * Returns a <code>PasswordWarning</code>, or <code>null</code> if no 
139      * warnings were present in the supplied <code>passwordPolicy</code>.
140      * 
141      * @param passwordPolicyResponse The PasswordPolicyReponse in use
142      * @return The created PasswordWarning
143      */
144     protected PasswordWarning success( PasswordPolicyResponse passwordPolicyResponse ) 
145     {
146         return passwordPolicyResponse == null
147                 ? null
148                 : PasswordWarningImpl.newWarning( passwordPolicyResponse );
149     }
150 }