001/**
002 *
003 * Copyright 2003-2007 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.jivesoftware.smack;
019
020import org.jivesoftware.smack.packet.StreamError;
021import org.jivesoftware.smack.packet.XMPPError;
022
023/**
024 * A generic exception that is thrown when an error occurs performing an
025 * XMPP operation. XMPP servers can respond to error conditions with an error code
026 * and textual description of the problem, which are encapsulated in the XMPPError
027 * class. When appropriate, an XMPPError instance is attached instances of this exception.<p>
028 *
029 * When a stream error occurred, the server will send a stream error to the client before
030 * closing the connection. Stream errors are unrecoverable errors. When a stream error
031 * is sent to the client an XMPPException will be thrown containing the StreamError sent
032 * by the server.
033 *
034 * @see XMPPError
035 * @author Matt Tucker
036 */
037public abstract class XMPPException extends Exception {
038    private static final long serialVersionUID = 6881651633890968625L;
039    
040
041    /**
042     * Creates a new XMPPException.
043     */
044    protected XMPPException() {
045        super();
046    }
047
048    /**
049     * Creates a new XMPPException with a description of the exception.
050     *
051     * @param message description of the exception.
052     */
053    protected XMPPException(String message) {
054        super(message);
055    }
056
057
058    /**
059     * Creates a new XMPPException with a description of the exception and the
060     * Throwable that was the root cause of the exception.
061     *
062     * @param message a description of the exception.
063     * @param wrappedThrowable the root cause of the exception.
064     */
065    protected XMPPException(String message, Throwable wrappedThrowable) {
066        super(message, wrappedThrowable);
067    }
068
069    public static class XMPPErrorException extends XMPPException {
070        /**
071         * 
072         */
073        private static final long serialVersionUID = 212790389529249604L;
074        private final XMPPError error;
075
076        /**
077         * Creates a new XMPPException with the XMPPError that was the root case of the exception.
078         * 
079         * @param error the root cause of the exception.
080         */
081        public XMPPErrorException(XMPPError error) {
082            super();
083            this.error = error;
084        }
085
086        /**
087         * Creates a new XMPPException with a description of the exception, an XMPPError, and the
088         * Throwable that was the root cause of the exception.
089         * 
090         * @param message a description of the exception.
091         * @param error the root cause of the exception.
092         * @param wrappedThrowable the root cause of the exception.
093         */
094        public XMPPErrorException(String message, XMPPError error, Throwable wrappedThrowable) {
095            super(message, wrappedThrowable);
096            this.error = error;
097        }
098
099        /**
100         * Creates a new XMPPException with a description of the exception and the XMPPException
101         * that was the root cause of the exception.
102         * 
103         * @param message a description of the exception.
104         * @param error the root cause of the exception.
105         */
106        public XMPPErrorException(String message, XMPPError error) {
107            super(message);
108            this.error = error;
109        }
110
111        /**
112         * Returns the XMPPError associated with this exception, or <tt>null</tt> if there isn't
113         * one.
114         * 
115         * @return the XMPPError associated with this exception.
116         */
117        public XMPPError getXMPPError() {
118            return error;
119        }
120
121        @Override
122        public String getMessage() {
123            String superMessage = super.getMessage();
124            if (superMessage != null) {
125                return superMessage;
126            }
127            else {
128                return error.toString();
129            }
130        }
131
132        @Override
133        public String toString() {
134            return getMessage();
135        }
136    }
137
138    public static class StreamErrorException extends XMPPException {
139        /**
140         * 
141         */
142        private static final long serialVersionUID = 3400556867134848886L;
143        private final StreamError streamError;
144
145        /**
146         * Creates a new XMPPException with the stream error that was the root case of the
147         * exception. When a stream error is received from the server then the underlying connection
148         * will be closed by the server.
149         * 
150         * @param streamError the root cause of the exception.
151         */
152        public StreamErrorException(StreamError streamError) {
153            super();
154            this.streamError = streamError;
155        }
156
157        /**
158         * Returns the StreamError associated with this exception. The underlying TCP connection is
159         * closed by the server after sending the stream error to the client.
160         * 
161         * @return the StreamError associated with this exception.
162         */
163        public StreamError getStreamError() {
164            return streamError;
165        }
166
167        @Override
168        public String getMessage() {
169            return streamError.toString();
170        }
171
172        @Override
173        public String toString() {
174            return getMessage();
175        }
176    }
177}