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.Stanza; 021import org.jivesoftware.smack.packet.StreamError; 022import org.jivesoftware.smack.packet.XMPPError; 023 024/** 025 * A generic exception that is thrown when an error occurs performing an 026 * XMPP operation. XMPP servers can respond to error conditions with an error code 027 * and textual description of the problem, which are encapsulated in the XMPPError 028 * class. When appropriate, an XMPPError instance is attached instances of this exception.<p> 029 * 030 * When a stream error occurred, the server will send a stream error to the client before 031 * closing the connection. Stream errors are unrecoverable errors. When a stream error 032 * is sent to the client an XMPPException will be thrown containing the StreamError sent 033 * by the server. 034 * 035 * @see XMPPError 036 * @author Matt Tucker 037 */ 038public abstract class XMPPException extends Exception { 039 private static final long serialVersionUID = 6881651633890968625L; 040 041 042 /** 043 * Creates a new XMPPException. 044 */ 045 protected XMPPException() { 046 super(); 047 } 048 049 /** 050 * Creates a new XMPPException with a description of the exception. 051 * 052 * @param message description of the exception. 053 */ 054 protected XMPPException(String message) { 055 super(message); 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 public static void ifHasErrorThenThrow(Stanza packet) throws XMPPErrorException { 133 XMPPError xmppError = packet.getError(); 134 if (xmppError != null) { 135 throw new XMPPErrorException(xmppError); 136 } 137 } 138 } 139 140 public static class StreamErrorException extends XMPPException { 141 /** 142 * 143 */ 144 private static final long serialVersionUID = 3400556867134848886L; 145 private final StreamError streamError; 146 147 /** 148 * Creates a new XMPPException with the stream error that was the root case of the 149 * exception. When a stream error is received from the server then the underlying connection 150 * will be closed by the server. 151 * 152 * @param streamError the root cause of the exception. 153 */ 154 public StreamErrorException(StreamError streamError) { 155 super(streamError.getCondition().toString() 156 + " You can read more about the meaning of this stream error at http://xmpp.org/rfcs/rfc6120.html#streams-error-conditions\n" 157 + streamError.toString()); 158 this.streamError = streamError; 159 } 160 161 /** 162 * Returns the StreamError associated with this exception. The underlying TCP connection is 163 * closed by the server after sending the stream error to the client. 164 * 165 * @return the StreamError associated with this exception. 166 */ 167 public StreamError getStreamError() { 168 return streamError; 169 } 170 171 } 172}