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     */
020    package org.apache.directory.shared.ldap.exception;
021    
022    
023    import javax.naming.AuthenticationNotSupportedException;
024    
025    import org.apache.directory.shared.i18n.I18n;
026    import org.apache.directory.shared.ldap.message.ResultCodeEnum;
027    
028    
029    /**
030     * A subclass of the {@link AuthenticationNotSupportedException} carrying along
031     * an unequivocal ResultCodeEnum value.
032     * 
033     * @see <a
034     *      href="http://java.sun.com/j2se/1.4.2/docs/guide/jndi/jndi-ldap-gl.html#EXCEPT">
035     *      LDAP ResultCode to JNDI Exception Mappings</a>
036     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037     * @version $Rev: 912436 $
038     */
039    public class LdapAuthenticationNotSupportedException extends AuthenticationNotSupportedException implements
040        LdapException
041    {
042        static final long serialVersionUID = 2532733848470791678L;
043    
044        /** the result code type safe enumeration */
045        private final ResultCodeEnum resultCode;
046    
047    
048        // ------------------------------------------------------------------------
049        // C O N S T R U C T O R S
050        // ------------------------------------------------------------------------
051    
052        /**
053         * Creates an LdapException with the resultCode.
054         * 
055         * @see AuthenticationNotSupportedException#AuthenticationNotSupportedException()
056         * @param resultCode
057         *            the resultCode enumeration
058         * @throws IllegalArgumentException
059         *             if the resultCode is not one of the codes corresponding to an
060         *             AuthenticationNotSupportedException
061         */
062        public LdapAuthenticationNotSupportedException(ResultCodeEnum resultCode)
063        {
064            super();
065            this.resultCode = resultCode;
066            checkResultCode();
067        }
068    
069    
070        /**
071         * Sets the resultCode associated with this LdapException.
072         * 
073         * @see AuthenticationNotSupportedException#AuthenticationNotSupportedException(String)
074         * @param resultCode
075         *            the resultCode enumeration
076         * @throws IllegalArgumentException
077         *             if the resultCode is not one of the codes corresponding to an
078         *             AuthenticationNotSupportedException
079         */
080        public LdapAuthenticationNotSupportedException(String explanation, ResultCodeEnum resultCode)
081        {
082            super( explanation );
083            this.resultCode = resultCode;
084            checkResultCode();
085        }
086    
087    
088        // ------------------------------------------------------------------------
089        // LdapException methods
090        // ------------------------------------------------------------------------
091    
092        /**
093         * @see LdapException#getResultCode()
094         */
095        public ResultCodeEnum getResultCode()
096        {
097            return resultCode;
098        }
099    
100    
101        // ------------------------------------------------------------------------
102        // P R I V A T E M E T H O D S
103        // ------------------------------------------------------------------------
104    
105        /**
106         * Checks to make sure the resultCode value is right for this exception
107         * type.
108         * 
109         * @throws IllegalArgumentException
110         *             if the result code is not one of
111         *             {@link ResultCodeEnum#INAPPROPRIATEAUTHENTICATION},
112         *             {@link ResultCodeEnum#AUTHMETHODNOTSUPPORTED},
113         *             {@link ResultCodeEnum#CONFIDENTIALITYREQUIRED}.
114         */
115        private void checkResultCode()
116        {
117            switch ( resultCode )
118            {
119                case INAPPROPRIATE_AUTHENTICATION :
120                case CONFIDENTIALITY_REQUIRED :
121                case AUTH_METHOD_NOT_SUPPORTED :
122                    break;
123                    
124                default:
125                    throw new IllegalArgumentException( I18n.err( I18n.ERR_04140, resultCode ) );
126            }
127        }
128    }