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.Nonza; 021import org.jivesoftware.smack.packet.Stanza; 022import org.jivesoftware.smack.packet.StanzaError; 023import org.jivesoftware.smack.packet.StreamError; 024 025import org.jxmpp.jid.Jid; 026 027/** 028 * A generic exception that is thrown when an error occurs performing an 029 * XMPP operation. XMPP servers can respond to error conditions with an error code 030 * and textual description of the problem, which are encapsulated in the XMPPError 031 * class. When appropriate, an XMPPError instance is attached instances of this exception.<p> 032 * 033 * When a stream error occurred, the server will send a stream error to the client before 034 * closing the connection. Stream errors are unrecoverable errors. When a stream error 035 * is sent to the client an XMPPException will be thrown containing the StreamError sent 036 * by the server. 037 * 038 * @see StanzaError 039 * @author Matt Tucker 040 */ 041public abstract class XMPPException extends Exception { 042 private static final long serialVersionUID = 6881651633890968625L; 043 044 045 /** 046 * Creates a new XMPPException. 047 */ 048 protected XMPPException() { 049 super(); 050 } 051 052 /** 053 * Creates a new XMPPException with a description of the exception. 054 * 055 * @param message description of the exception. 056 */ 057 protected XMPPException(String message) { 058 super(message); 059 } 060 061 /** 062 * Creates a new XMPPException with a description of the exception and the 063 * Throwable that was the root cause of the exception. 064 * 065 * @param message a description of the exception. 066 * @param wrappedThrowable the root cause of the exception. 067 */ 068 protected XMPPException(String message, Throwable wrappedThrowable) { 069 super(message, wrappedThrowable); 070 } 071 072 /** 073 * An exception caused by an XMPP error stanza response on the protocol level. You can examine the underlying 074 * {@link StanzaError} by calling {@link #getStanzaError()}. 075 */ 076 public static class XMPPErrorException extends XMPPException { 077 /** 078 * 079 */ 080 private static final long serialVersionUID = 212790389529249604L; 081 private final StanzaError error; 082 private final Stanza stanza; 083 084 /** 085 * The request which resulted in the XMPP protocol error response. May be {@code null}. 086 */ 087 private final Stanza request; 088 089 /** 090 * Creates a new XMPPErrorException with the XMPPError that was the root case of the exception. 091 * 092 * @param stanza stanza that contained the exception. 093 * @param error the root cause of the exception. 094 */ 095 public XMPPErrorException(Stanza stanza, StanzaError error) { 096 this(stanza, error, null); 097 } 098 099 /** 100 * Creates a new XMPPErrorException with the XMPPError that was the root case of the exception. 101 * 102 * @param request the request which triggered the error. 103 * @param stanza stanza that contained the exception. 104 * @param error the root cause of the exception. 105 * @since 4.3.0 106 */ 107 public XMPPErrorException(Stanza stanza, StanzaError error, Stanza request) { 108 super(); 109 this.error = error; 110 this.stanza = stanza; 111 this.request = request; 112 } 113 114 /** 115 * Returns the stanza error extension element associated with this exception. 116 * 117 * @return the stanza error extension element associated with this exception. 118 */ 119 public StanzaError getStanzaError() { 120 return error; 121 } 122 123 /** 124 * Get the request which triggered the error response causing this exception. 125 * 126 * @return the request or {@code null}. 127 * @since 4.3.0 128 */ 129 public Stanza getRequest() { 130 return request; 131 } 132 133 @Override 134 public String getMessage() { 135 StringBuilder sb = new StringBuilder(); 136 137 if (stanza != null) { 138 Jid from = stanza.getFrom(); 139 if (from != null) { 140 sb.append("XMPP error reply received from " + from + ": "); 141 } 142 } 143 144 sb.append(error); 145 146 if (request != null) { 147 sb.append(" as result of the following request: "); 148 sb.append(request); 149 } 150 151 return sb.toString(); 152 } 153 154 public static void ifHasErrorThenThrow(Stanza packet) throws XMPPErrorException { 155 ifHasErrorThenThrow(packet, null); 156 } 157 158 public static void ifHasErrorThenThrow(Stanza packet, Stanza request) throws XMPPErrorException { 159 StanzaError xmppError = packet.getError(); 160 if (xmppError != null) { 161 throw new XMPPErrorException(packet, xmppError, request); 162 } 163 } 164 } 165 166 public static class FailedNonzaException extends XMPPException { 167 168 /** 169 * 170 */ 171 private static final long serialVersionUID = 1L; 172 173 private final StanzaError.Condition condition; 174 175 private final Nonza nonza; 176 177 public FailedNonzaException(Nonza failedNonza) { 178 this(failedNonza, null); 179 } 180 181 public FailedNonzaException(Nonza nonza, StanzaError.Condition condition) { 182 this.condition = condition; 183 this.nonza = nonza; 184 } 185 186 public StanzaError.Condition getCondition() { 187 return condition; 188 } 189 190 public Nonza getNonza() { 191 return nonza; 192 } 193 } 194 195 public static class StreamErrorException extends XMPPException { 196 /** 197 * 198 */ 199 private static final long serialVersionUID = 3400556867134848886L; 200 private final StreamError streamError; 201 202 /** 203 * Creates a new XMPPException with the stream error that was the root case of the 204 * exception. When a stream error is received from the server then the underlying connection 205 * will be closed by the server. 206 * 207 * @param streamError the root cause of the exception. 208 */ 209 public StreamErrorException(StreamError streamError) { 210 super(streamError.getCondition().toString() 211 + " You can read more about the meaning of this stream error at http://xmpp.org/rfcs/rfc6120.html#streams-error-conditions\n" 212 + streamError.toString()); 213 this.streamError = streamError; 214 } 215 216 /** 217 * Returns the StreamError associated with this exception. The underlying TCP connection is 218 * closed by the server after sending the stream error to the client. 219 * 220 * @return the StreamError associated with this exception. 221 */ 222 public StreamError getStreamError() { 223 return streamError; 224 } 225 226 } 227}