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.api.ldap.model.exception;
021
022
023import org.apache.directory.api.i18n.I18n;
024import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
025
026
027/**
028 * A subclass of {@link LdapOperationException} designed to hold an unequivocal LDAP
029 * result code.
030 * 
031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032 */
033public class LdapInvalidDnException extends LdapOperationException
034{
035    /** The serial version UUID */
036    static final long serialVersionUID = 1L;
037
038
039    /**
040     * to be used by some special exceptions like LdapInvalidDnException
041     */
042    public LdapInvalidDnException( String message )
043    {
044        super( message );
045    }
046
047
048    /**
049     * to be used by some special exceptions like LdapInvalidDnException
050     * 
051     * @param message The exception message
052     * @param cause The root cause for this exception
053     */
054    public LdapInvalidDnException( String message, Throwable cause )
055    {
056        super( message, cause );
057    }
058
059
060    /**
061     * Creates a new instance of LdapInvalidDnException.
062     *
063     * @param resultCode the ResultCodeEnum for this exception
064     * @param message The exception message
065     */
066    public LdapInvalidDnException( ResultCodeEnum resultCode, String message )
067    {
068        super( message );
069        checkResultCode( resultCode );
070        this.resultCode = resultCode;
071    }
072
073
074    /**
075     * Creates a new instance of LdapInvalidDnException.
076     *
077     * @param resultCode the ResultCodeEnum for this exception
078     * @param message The exception message
079     * @param cause The root cause for this exception
080     */
081    public LdapInvalidDnException( ResultCodeEnum resultCode, String message, Throwable cause )
082    {
083        super( message, cause );
084        checkResultCode( resultCode );
085        this.resultCode = resultCode;
086    }
087
088
089    /**
090     * Creates a new instance of LdapInvalidDnException.
091     * 
092     * @param resultCode the ResultCodeEnum for this exception
093     */
094    public LdapInvalidDnException( ResultCodeEnum resultCode )
095    {
096        super( null );
097        checkResultCode( resultCode );
098        this.resultCode = resultCode;
099    }
100
101
102    /**
103     * Checks to make sure the resultCode value is right for this exception
104     * type.
105     * 
106     * @throws IllegalArgumentException
107     *             if the result code is not one of
108     *             {@link ResultCodeEnum#INVALID_DN_SYNTAX},
109     *             {@link ResultCodeEnum#NAMING_VIOLATION}.
110     */
111    private void checkResultCode( ResultCodeEnum resultCode )
112    {
113        switch ( resultCode )
114        {
115            case INVALID_DN_SYNTAX:
116            case NAMING_VIOLATION:
117                return;
118
119            default:
120                throw new IllegalArgumentException( I18n.err( I18n.ERR_04140_UNACCEPTABLE_RESUT_CODE, resultCode ) );
121        }
122    }
123}