XMPPException.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smack;

  18. import org.jivesoftware.smack.packet.Stanza;
  19. import org.jivesoftware.smack.packet.StreamError;
  20. import org.jivesoftware.smack.packet.XMPPError;

  21. /**
  22.  * A generic exception that is thrown when an error occurs performing an
  23.  * XMPP operation. XMPP servers can respond to error conditions with an error code
  24.  * and textual description of the problem, which are encapsulated in the XMPPError
  25.  * class. When appropriate, an XMPPError instance is attached instances of this exception.<p>
  26.  *
  27.  * When a stream error occurred, the server will send a stream error to the client before
  28.  * closing the connection. Stream errors are unrecoverable errors. When a stream error
  29.  * is sent to the client an XMPPException will be thrown containing the StreamError sent
  30.  * by the server.
  31.  *
  32.  * @see XMPPError
  33.  * @author Matt Tucker
  34.  */
  35. public abstract class XMPPException extends Exception {
  36.     private static final long serialVersionUID = 6881651633890968625L;
  37.    

  38.     /**
  39.      * Creates a new XMPPException.
  40.      */
  41.     protected XMPPException() {
  42.         super();
  43.     }

  44.     /**
  45.      * Creates a new XMPPException with a description of the exception.
  46.      *
  47.      * @param message description of the exception.
  48.      */
  49.     protected XMPPException(String message) {
  50.         super(message);
  51.     }

  52.     /**
  53.      * Creates a new XMPPException with a description of the exception and the
  54.      * Throwable that was the root cause of the exception.
  55.      *
  56.      * @param message a description of the exception.
  57.      * @param wrappedThrowable the root cause of the exception.
  58.      */
  59.     protected XMPPException(String message, Throwable wrappedThrowable) {
  60.         super(message, wrappedThrowable);
  61.     }

  62.     public static class XMPPErrorException extends XMPPException {
  63.         /**
  64.          *
  65.          */
  66.         private static final long serialVersionUID = 212790389529249604L;
  67.         private final XMPPError error;

  68.         /**
  69.          * Creates a new XMPPException with the XMPPError that was the root case of the exception.
  70.          *
  71.          * @param error the root cause of the exception.
  72.          */
  73.         public XMPPErrorException(XMPPError error) {
  74.             super();
  75.             this.error = error;
  76.         }

  77.         /**
  78.          * Creates a new XMPPException with a description of the exception, an XMPPError, and the
  79.          * Throwable that was the root cause of the exception.
  80.          *
  81.          * @param message a description of the exception.
  82.          * @param error the root cause of the exception.
  83.          * @param wrappedThrowable the root cause of the exception.
  84.          */
  85.         public XMPPErrorException(String message, XMPPError error, Throwable wrappedThrowable) {
  86.             super(message, wrappedThrowable);
  87.             this.error = error;
  88.         }

  89.         /**
  90.          * Creates a new XMPPException with a description of the exception and the XMPPException
  91.          * that was the root cause of the exception.
  92.          *
  93.          * @param message a description of the exception.
  94.          * @param error the root cause of the exception.
  95.          */
  96.         public XMPPErrorException(String message, XMPPError error) {
  97.             super(message);
  98.             this.error = error;
  99.         }

  100.         /**
  101.          * Returns the XMPPError associated with this exception, or <tt>null</tt> if there isn't
  102.          * one.
  103.          *
  104.          * @return the XMPPError associated with this exception.
  105.          */
  106.         public XMPPError getXMPPError() {
  107.             return error;
  108.         }

  109.         @Override
  110.         public String getMessage() {
  111.             String superMessage = super.getMessage();
  112.             if (superMessage != null) {
  113.                 return superMessage;
  114.             }
  115.             else {
  116.                 return error.toString();
  117.             }
  118.         }

  119.         public static void ifHasErrorThenThrow(Stanza packet) throws XMPPErrorException {
  120.             XMPPError xmppError = packet.getError();
  121.             if (xmppError != null) {
  122.                 throw new XMPPErrorException(xmppError);
  123.             }
  124.         }
  125.     }

  126.     public static class StreamErrorException extends XMPPException {
  127.         /**
  128.          *
  129.          */
  130.         private static final long serialVersionUID = 3400556867134848886L;
  131.         private final StreamError streamError;

  132.         /**
  133.          * Creates a new XMPPException with the stream error that was the root case of the
  134.          * exception. When a stream error is received from the server then the underlying connection
  135.          * will be closed by the server.
  136.          *
  137.          * @param streamError the root cause of the exception.
  138.          */
  139.         public StreamErrorException(StreamError streamError) {
  140.             super(streamError.getCondition().toString()
  141.                   + " You can read more about the meaning of this stream error at http://xmpp.org/rfcs/rfc6120.html#streams-error-conditions\n"
  142.                   + streamError.toString());
  143.             this.streamError = streamError;
  144.         }

  145.         /**
  146.          * Returns the StreamError associated with this exception. The underlying TCP connection is
  147.          * closed by the server after sending the stream error to the client.
  148.          *
  149.          * @return the StreamError associated with this exception.
  150.          */
  151.         public StreamError getStreamError() {
  152.             return streamError;
  153.         }

  154.     }
  155. }