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.server.core.api.authn.ppolicy;
021
022
023import org.apache.directory.api.ldap.model.exception.LdapException;
024
025
026/**
027 * A exception class defined for PasswordPolicy related errors.
028 *
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 */
031public class PasswordPolicyException extends LdapException
032{
033    private static final long serialVersionUID = -9158126177779964262L;
034
035    /** password policy error code */
036    private int errorCode;
037
038    /** the array of valid error codes representing password policy errors */
039    private static final int[] VALID_CODES =
040        { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
041
042
043    public PasswordPolicyException( Throwable cause )
044    {
045        super( cause );
046    }
047
048
049    public PasswordPolicyException( String message )
050    {
051        super( message );
052    }
053
054
055    public PasswordPolicyException( String message, int errorCode )
056    {
057        super( message );
058        validateErrorCode( errorCode );
059        this.errorCode = errorCode;
060    }
061
062
063    public PasswordPolicyException( int errorCode )
064    {
065        validateErrorCode( errorCode );
066        this.errorCode = errorCode;
067    }
068
069
070    public int getErrorCode()
071    {
072        return errorCode;
073    }
074
075
076    /**
077     * this method checks if the given error code is valid or not.
078     * This method was created cause using PasswordPolicyErrorEnum class creates some 
079     * unwanted dependency issues on core-api
080     * 
081     * @param errorCode the error code of password policy
082     */
083    private void validateErrorCode( int errorCode )
084    {
085        for ( int i : VALID_CODES )
086        {
087            if ( i == errorCode )
088            {
089                return;
090            }
091        }
092
093        throw new IllegalArgumentException( "Unknown password policy response error code " + errorCode );
094    }
095}