2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
*
|
2007-02-12 01:59:05 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2004-11-03 00:53:30 +01:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smack;
|
|
|
|
|
2016-11-29 16:40:08 +01:00
|
|
|
import org.jivesoftware.smack.packet.Nonza;
|
2015-02-05 11:17:27 +01:00
|
|
|
import org.jivesoftware.smack.packet.Stanza;
|
2018-04-07 21:25:40 +02:00
|
|
|
import org.jivesoftware.smack.packet.StanzaError;
|
2005-11-08 21:10:33 +01:00
|
|
|
import org.jivesoftware.smack.packet.StreamError;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2016-11-29 16:40:08 +01:00
|
|
|
import org.jxmpp.jid.Jid;
|
2003-03-07 20:54:10 +01:00
|
|
|
|
2003-01-28 06:49:25 +01:00
|
|
|
/**
|
|
|
|
* A generic exception that is thrown when an error occurs performing an
|
2003-03-14 17:00:03 +01:00
|
|
|
* XMPP operation. XMPP servers can respond to error conditions with an error code
|
|
|
|
* and textual description of the problem, which are encapsulated in the XMPPError
|
2005-11-08 21:10:33 +01:00
|
|
|
* class. When appropriate, an XMPPError instance is attached instances of this exception.<p>
|
|
|
|
*
|
2014-03-12 11:50:05 +01:00
|
|
|
* When a stream error occurred, the server will send a stream error to the client before
|
2005-11-08 21:10:33 +01:00
|
|
|
* closing the connection. Stream errors are unrecoverable errors. When a stream error
|
|
|
|
* is sent to the client an XMPPException will be thrown containing the StreamError sent
|
|
|
|
* by the server.
|
2003-01-28 06:49:25 +01:00
|
|
|
*
|
2018-04-07 21:25:40 +02:00
|
|
|
* @see StanzaError
|
2003-01-28 06:49:25 +01:00
|
|
|
* @author Matt Tucker
|
|
|
|
*/
|
2014-03-12 11:50:05 +01:00
|
|
|
public abstract class XMPPException extends Exception {
|
2013-03-23 12:59:08 +01:00
|
|
|
private static final long serialVersionUID = 6881651633890968625L;
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2003-01-28 06:49:25 +01:00
|
|
|
|
2003-03-14 17:00:03 +01:00
|
|
|
/**
|
|
|
|
* Creates a new XMPPException.
|
|
|
|
*/
|
2014-03-12 11:50:05 +01:00
|
|
|
protected XMPPException() {
|
2003-01-13 17:58:47 +01:00
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2003-03-14 17:00:03 +01:00
|
|
|
/**
|
|
|
|
* Creates a new XMPPException with a description of the exception.
|
|
|
|
*
|
|
|
|
* @param message description of the exception.
|
|
|
|
*/
|
2014-03-12 11:50:05 +01:00
|
|
|
protected XMPPException(String message) {
|
2003-01-13 17:58:47 +01:00
|
|
|
super(message);
|
|
|
|
}
|
|
|
|
|
2003-03-14 17:00:03 +01:00
|
|
|
/**
|
|
|
|
* Creates a new XMPPException with a description of the exception and the
|
|
|
|
* Throwable that was the root cause of the exception.
|
|
|
|
*
|
|
|
|
* @param message a description of the exception.
|
|
|
|
* @param wrappedThrowable the root cause of the exception.
|
|
|
|
*/
|
2014-03-12 11:50:05 +01:00
|
|
|
protected XMPPException(String message, Throwable wrappedThrowable) {
|
2013-11-11 09:33:31 +01:00
|
|
|
super(message, wrappedThrowable);
|
2003-08-19 18:32:13 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 15:58:54 +02:00
|
|
|
/**
|
|
|
|
* An exception caused by an XMPP error stanza response on the protocol level. You can examine the underlying
|
|
|
|
* {@link StanzaError} by calling {@link #getStanzaError()}.
|
|
|
|
*/
|
2014-03-12 11:50:05 +01:00
|
|
|
public static class XMPPErrorException extends XMPPException {
|
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-03-12 11:50:05 +01:00
|
|
|
*/
|
|
|
|
private static final long serialVersionUID = 212790389529249604L;
|
2018-04-07 21:25:40 +02:00
|
|
|
private final StanzaError error;
|
2016-11-29 16:40:08 +01:00
|
|
|
private final Stanza stanza;
|
2003-03-07 20:47:31 +01:00
|
|
|
|
2018-04-23 20:56:54 +02:00
|
|
|
/**
|
|
|
|
* The request which resulted in the XMPP protocol error response. May be {@code null}.
|
|
|
|
*/
|
|
|
|
private final Stanza request;
|
|
|
|
|
2014-03-12 11:50:05 +01:00
|
|
|
/**
|
2016-11-29 16:40:08 +01:00
|
|
|
* Creates a new XMPPErrorException with the XMPPError that was the root case of the exception.
|
2017-12-23 20:21:19 +01:00
|
|
|
*
|
|
|
|
* @param stanza stanza that contained the exception.
|
2014-03-12 11:50:05 +01:00
|
|
|
* @param error the root cause of the exception.
|
|
|
|
*/
|
2018-04-07 21:25:40 +02:00
|
|
|
public XMPPErrorException(Stanza stanza, StanzaError error) {
|
2018-04-23 20:56:54 +02:00
|
|
|
this(stanza, error, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new XMPPErrorException with the XMPPError that was the root case of the exception.
|
|
|
|
*
|
|
|
|
* @param request the request which triggered the error.
|
|
|
|
* @param stanza stanza that contained the exception.
|
|
|
|
* @param error the root cause of the exception.
|
|
|
|
* @since 4.3.0
|
|
|
|
*/
|
|
|
|
public XMPPErrorException(Stanza stanza, StanzaError error, Stanza request) {
|
2016-11-29 16:40:08 +01:00
|
|
|
super();
|
2014-03-12 11:50:05 +01:00
|
|
|
this.error = error;
|
2016-11-29 16:40:08 +01:00
|
|
|
this.stanza = stanza;
|
2018-04-23 20:56:54 +02:00
|
|
|
this.request = request;
|
2014-03-12 11:50:05 +01:00
|
|
|
}
|
2013-03-23 12:59:08 +01:00
|
|
|
|
2018-06-20 15:55:48 +02:00
|
|
|
/**
|
|
|
|
* Returns the stanza error extension element associated with this exception.
|
|
|
|
*
|
|
|
|
* @return the stanza error extension element associated with this exception.
|
|
|
|
*/
|
|
|
|
public StanzaError getStanzaError() {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2021-10-17 16:06:41 +02:00
|
|
|
/**
|
|
|
|
* Gets the stanza associated with this exception.
|
|
|
|
*
|
|
|
|
* @return the stanza from which this exception was created or {@code null} if the exception is not from a
|
|
|
|
* stanza.
|
|
|
|
*/
|
|
|
|
public Stanza getStanza() {
|
|
|
|
return stanza;
|
|
|
|
}
|
|
|
|
|
2018-04-23 20:56:54 +02:00
|
|
|
/**
|
|
|
|
* Get the request which triggered the error response causing this exception.
|
|
|
|
*
|
|
|
|
* @return the request or {@code null}.
|
|
|
|
* @since 4.3.0
|
|
|
|
*/
|
|
|
|
public Stanza getRequest() {
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
2014-03-12 11:50:05 +01:00
|
|
|
@Override
|
|
|
|
public String getMessage() {
|
2016-11-29 16:40:08 +01:00
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
if (stanza != null) {
|
|
|
|
Jid from = stanza.getFrom();
|
|
|
|
if (from != null) {
|
|
|
|
sb.append("XMPP error reply received from " + from + ": ");
|
|
|
|
}
|
2014-03-12 11:50:05 +01:00
|
|
|
}
|
2016-11-29 16:40:08 +01:00
|
|
|
|
|
|
|
sb.append(error);
|
|
|
|
|
2018-04-23 20:56:54 +02:00
|
|
|
if (request != null) {
|
|
|
|
sb.append(" as result of the following request: ");
|
|
|
|
sb.append(request);
|
|
|
|
}
|
|
|
|
|
2016-11-29 16:40:08 +01:00
|
|
|
return sb.toString();
|
2003-10-13 22:42:22 +02:00
|
|
|
}
|
2014-03-12 11:50:05 +01:00
|
|
|
|
2015-02-05 11:17:27 +01:00
|
|
|
public static void ifHasErrorThenThrow(Stanza packet) throws XMPPErrorException {
|
2018-04-23 20:56:54 +02:00
|
|
|
ifHasErrorThenThrow(packet, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void ifHasErrorThenThrow(Stanza packet, Stanza request) throws XMPPErrorException {
|
2018-04-07 21:25:40 +02:00
|
|
|
StanzaError xmppError = packet.getError();
|
2014-08-16 00:04:24 +02:00
|
|
|
if (xmppError != null) {
|
2018-04-23 20:56:54 +02:00
|
|
|
throw new XMPPErrorException(packet, xmppError, request);
|
2014-08-16 00:04:24 +02:00
|
|
|
}
|
|
|
|
}
|
2003-10-13 22:42:22 +02:00
|
|
|
}
|
|
|
|
|
2016-11-29 16:40:08 +01:00
|
|
|
public static class FailedNonzaException extends XMPPException {
|
|
|
|
|
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2016-11-29 16:40:08 +01:00
|
|
|
*/
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
2018-04-07 21:25:40 +02:00
|
|
|
private final StanzaError.Condition condition;
|
2016-11-29 16:40:08 +01:00
|
|
|
|
|
|
|
private final Nonza nonza;
|
|
|
|
|
2019-02-04 08:59:39 +01:00
|
|
|
public FailedNonzaException(Nonza failedNonza) {
|
|
|
|
this(failedNonza, null);
|
|
|
|
}
|
|
|
|
|
2018-04-07 21:25:40 +02:00
|
|
|
public FailedNonzaException(Nonza nonza, StanzaError.Condition condition) {
|
2016-11-29 16:40:08 +01:00
|
|
|
this.condition = condition;
|
|
|
|
this.nonza = nonza;
|
|
|
|
}
|
|
|
|
|
2018-04-07 21:25:40 +02:00
|
|
|
public StanzaError.Condition getCondition() {
|
2016-11-29 16:40:08 +01:00
|
|
|
return condition;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Nonza getNonza() {
|
|
|
|
return nonza;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 11:50:05 +01:00
|
|
|
public static class StreamErrorException extends XMPPException {
|
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-03-12 11:50:05 +01:00
|
|
|
*/
|
|
|
|
private static final long serialVersionUID = 3400556867134848886L;
|
|
|
|
private final StreamError streamError;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new XMPPException with the stream error that was the root case of the
|
|
|
|
* exception. When a stream error is received from the server then the underlying connection
|
|
|
|
* will be closed by the server.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-03-12 11:50:05 +01:00
|
|
|
* @param streamError the root cause of the exception.
|
|
|
|
*/
|
|
|
|
public StreamErrorException(StreamError streamError) {
|
2015-02-28 10:16:32 +01:00
|
|
|
super(streamError.getCondition().toString()
|
|
|
|
+ " You can read more about the meaning of this stream error at http://xmpp.org/rfcs/rfc6120.html#streams-error-conditions\n"
|
|
|
|
+ streamError.toString());
|
2014-03-12 11:50:05 +01:00
|
|
|
this.streamError = streamError;
|
2003-10-13 22:42:22 +02:00
|
|
|
}
|
2014-03-12 11:50:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the StreamError associated with this exception. The underlying TCP connection is
|
|
|
|
* closed by the server after sending the stream error to the client.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-03-12 11:50:05 +01:00
|
|
|
* @return the StreamError associated with this exception.
|
|
|
|
*/
|
|
|
|
public StreamError getStreamError() {
|
|
|
|
return streamError;
|
2005-11-08 21:10:33 +01:00
|
|
|
}
|
2014-03-12 11:50:05 +01:00
|
|
|
|
2003-09-19 09:54:34 +02:00
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|