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   * The default implementation of {@link PasswordPolicyResponder}.
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public class PasswordPolicyResponderImpl implements PasswordPolicyResponder
40  {
41      private final PasswordPolicyDecorator passwordPolicyRequestControl;
42  
43  
44      public PasswordPolicyResponderImpl( LdapApiService ldapApiService )
45      {
46          this.passwordPolicyRequestControl = new PasswordPolicyDecorator(
47              ldapApiService );
48      }
49  
50  
51      private PasswordPolicy getPasswordPolicy( Response response )
52      {
53          Control control = response.getControls().get( passwordPolicyRequestControl.getOid() );
54          return control == null
55              ? null
56              : ( ( PasswordPolicyDecorator ) control ).getDecorated();
57      }
58  
59  
60      @Override
61      public PasswordWarning process( PasswordPolicyOperation operation )
62          throws PasswordException
63      {
64          try
65          {
66              ResultResponse response = operation.process();
67              PasswordPolicy passwordPolicy = getPasswordPolicy( response );
68  
69              ResultCodeEnum resultCode = response.getLdapResult().getResultCode();
70              if ( resultCode == ResultCodeEnum.SUCCESS )
71              {
72                  if ( passwordPolicy != null )
73                  {
74                      return PasswordWarningImpl.newWarning( passwordPolicy );
75                  }
76                  return null;
77              }
78              else
79              {
80                  PasswordException exception = new PasswordException();
81                  exception.setResultCode( resultCode );
82                  if ( passwordPolicy != null
83                      && passwordPolicy.getResponse() != null
84                      && passwordPolicy.getResponse().getPasswordPolicyError() != null )
85                  {
86                      exception.setPasswordPolicyError( passwordPolicy.getResponse().getPasswordPolicyError() );
87                  }
88                  throw exception;
89              }
90          }
91          catch ( LdapException e )
92          {
93              throw new PasswordException().setLdapException( e );
94          }
95      }
96  }