001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.directory.ldap.client.template; 021 022 023import org.apache.directory.api.ldap.codec.api.LdapApiService; 024import org.apache.directory.api.ldap.extras.controls.ppolicy.PasswordPolicy; 025import org.apache.directory.api.ldap.extras.controls.ppolicy_impl.PasswordPolicyDecorator; 026import org.apache.directory.api.ldap.model.exception.LdapException; 027import org.apache.directory.api.ldap.model.message.Control; 028import org.apache.directory.api.ldap.model.message.Response; 029import org.apache.directory.api.ldap.model.message.ResultCodeEnum; 030import org.apache.directory.api.ldap.model.message.ResultResponse; 031import org.apache.directory.ldap.client.template.exception.PasswordException; 032 033 034/** 035 * The default implementation of {@link PasswordPolicyResponder}. 036 * 037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 038 */ 039public class PasswordPolicyResponderImpl implements PasswordPolicyResponder 040{ 041 private final PasswordPolicyDecorator passwordPolicyRequestControl; 042 043 044 public PasswordPolicyResponderImpl( LdapApiService ldapApiService ) 045 { 046 this.passwordPolicyRequestControl = new PasswordPolicyDecorator( 047 ldapApiService ); 048 } 049 050 051 private PasswordPolicy getPasswordPolicy( Response response ) 052 { 053 Control control = response.getControls().get( passwordPolicyRequestControl.getOid() ); 054 return control == null 055 ? null 056 : ( ( PasswordPolicyDecorator ) control ).getDecorated(); 057 } 058 059 060 @Override 061 public PasswordWarning process( PasswordPolicyOperation operation ) 062 throws PasswordException 063 { 064 try 065 { 066 ResultResponse response = operation.process(); 067 PasswordPolicy passwordPolicy = getPasswordPolicy( response ); 068 069 ResultCodeEnum resultCode = response.getLdapResult().getResultCode(); 070 if ( resultCode == ResultCodeEnum.SUCCESS ) 071 { 072 if ( passwordPolicy != null ) 073 { 074 return PasswordWarningImpl.newWarning( passwordPolicy ); 075 } 076 return null; 077 } 078 else 079 { 080 PasswordException exception = new PasswordException(); 081 exception.setResultCode( resultCode ); 082 if ( passwordPolicy != null 083 && passwordPolicy.getResponse() != null 084 && passwordPolicy.getResponse().getPasswordPolicyError() != null ) 085 { 086 exception.setPasswordPolicyError( passwordPolicy.getResponse().getPasswordPolicyError() ); 087 } 088 throw exception; 089 } 090 } 091 catch ( LdapException e ) 092 { 093 throw new PasswordException().setLdapException( e ); 094 } 095 } 096}