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.shared.kerberos.exceptions;
021
022
023import org.apache.directory.shared.kerberos.messages.KrbError;
024
025
026/**
027 * The root of the Kerberos exception hierarchy.
028 *
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 */
031public class KerberosException extends Exception
032{
033    private static final long serialVersionUID = 2968072183596955597L;
034
035    /** the kerberos error */
036    private KrbError error;
037
038    /**
039     * The Kerberos error code associated with this exception.
040     */
041    private final int errorCode;
042
043    /**
044     * Additional data about the error for use by the application
045     * to help it recover from or handle the error.
046     */
047    private byte[] explanatoryData;
048
049
050    /**
051     * Creates a KerberosException with an {@link ErrorType}.
052     *
053     * @param errorType The error type associated with this KerberosException.
054     */
055    public KerberosException( ErrorType errorType )
056    {
057        super( errorType.getMessage() );
058
059        this.errorCode = errorType.getValue();
060    }
061
062
063    /**
064     * 
065     * Creates a new instance of KerberosException.
066     *
067     * @param error the KrbError message
068     */
069    public KerberosException( KrbError error )
070    {
071        super( error.getMessageType().getMessage() );
072        this.errorCode = error.getErrorCode().getValue();
073        this.error = error;
074    }
075
076
077    /**
078     * Creates a KerberosException with an {@link ErrorType} and an
079     * underlying {@link Throwable} that caused this fault.
080     *
081     * @param errorType The error type associated with this KerberosException.
082     * @param cause The underlying failure, if any.
083     */
084    public KerberosException( ErrorType errorType, Throwable cause )
085    {
086        super( errorType.getMessage(), cause );
087
088        this.errorCode = errorType.getValue();
089    }
090
091
092    /**
093     * Creates a KerberosException with an {@link ErrorType} and a custom error message.
094     *
095     * @param errorType The {@link ErrorType} associated with this KerberosException.
096     * @param msg A custom error message for this KerberosException.
097     */
098    public KerberosException( ErrorType errorType, String msg )
099    {
100        super( msg );
101
102        this.errorCode = errorType.getValue();
103    }
104
105
106    /**
107     * Creates a KerberosException with an {@link ErrorType}, a custom error message, and an
108     * underlying {@link Throwable} that caused this fault.
109     *
110     * @param errorType The error type associated with this KerberosException.
111     * @param msg A custom error message for this KerberosException.
112     * @param cause The underlying failure, if any.
113     */
114    public KerberosException( ErrorType errorType, String msg, Throwable cause )
115    {
116        super( msg, cause );
117
118        this.errorCode = errorType.getValue();
119    }
120
121
122    /**
123     * Creates a KerberosException with an {@link ErrorType} and data helping to
124     * explain what caused this fault.
125     *
126     * @param errorType The error type associated with this KerberosException.
127     * @param explanatoryData Data helping to explain this fault, if any.
128     */
129    public KerberosException( ErrorType errorType, byte[] explanatoryData )
130    {
131        super( errorType.getMessage() );
132
133        this.errorCode = errorType.getValue();
134        this.explanatoryData = explanatoryData;
135    }
136
137
138    /**
139     * Creates a KerberosException with an {@link ErrorType}, data helping to
140     * explain what caused this fault, and an underlying {@link Throwable} that caused this fault.
141     *
142     * @param errorType The error type associated with this KerberosException.
143     * @param explanatoryData Data helping to explain this fault, if any.
144     * @param cause The underlying failure, if any.
145     */
146    public KerberosException( ErrorType errorType, byte[] explanatoryData, Throwable cause )
147    {
148        super( errorType.getMessage(), cause );
149
150        this.errorCode = errorType.getValue();
151        this.explanatoryData = explanatoryData;
152    }
153
154
155    /**
156     * Gets the protocol error code associated with this KerberosException.
157     *
158     * @return The error code associated with this KerberosException.
159     */
160    public int getErrorCode()
161    {
162        return this.errorCode;
163    }
164
165
166    /**
167     * Gets the explanatory data associated with this KerberosException.
168     *
169     * @return The explanatory data associated with this KerberosException.
170     */
171    public byte[] getExplanatoryData()
172    {
173        return explanatoryData;
174    }
175
176
177    /**
178     * Creates a KerberosException with an error code and a message.
179     *
180     * @param errorCode The error code associated with this KerberosException.
181     * @param msg The standard Kerberos error message for this KerberosException.
182     */
183    protected KerberosException( int errorCode, String msg )
184    {
185        super( msg );
186
187        this.errorCode = errorCode;
188    }
189
190
191    /**
192     * Creates a KerberosException with an error code, a message and an
193     * underlying {@link Throwable} that caused this fault.
194     *
195     * @param errorCode The error code associated with this KerberosException.
196     * @param msg The standard Kerberos error message for this KerberosException.
197     * @param cause The underlying failure, if any.
198     */
199    protected KerberosException( int errorCode, String msg, Throwable cause )
200    {
201        super( msg, cause );
202
203        this.errorCode = errorCode;
204    }
205
206
207    /**
208     * Creates a KerberosException with an error code, a message, and data
209     * helping to explain what caused this fault.
210     *
211     * @param errorCode The error code associated with this KerberosException.
212     * @param msg The standard Kerberos error message for this KerberosException.
213     * @param explanatoryData Data helping to explain this fault, if any.
214     */
215    protected KerberosException( int errorCode, String msg, byte[] explanatoryData )
216    {
217        super( msg );
218
219        this.errorCode = errorCode;
220        this.explanatoryData = explanatoryData;
221    }
222
223
224    /**
225     * Creates a KerberosException with an error code, a message, and data
226     * helping to explain what caused this fault.
227     *
228     * @param errorCode The error code associated with this KerberosException.
229     * @param msg The standard Kerberos error message for this KerberosException.
230     * @param explanatoryData Data helping to explain this fault, if any.
231     * @param cause The underlying failure, if any.
232     */
233    protected KerberosException( int errorCode, String msg, byte[] explanatoryData, Throwable cause )
234    {
235        super( msg, cause );
236
237        this.errorCode = errorCode;
238        this.explanatoryData = explanatoryData;
239    }
240
241
242    /**
243     * returns the KrbError message associated with this exception
244     * 
245     * @return the error, can be null
246     */
247    public KrbError getError()
248    {
249        return error;
250    }
251}