SmackException.java

  1. /**
  2.  *
  3.  * Copyright 2014 Florian Schmaus
  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 java.util.ArrayList;
  19. import java.util.List;

  20. import org.jivesoftware.smack.filter.StanzaFilter;
  21. import org.jivesoftware.smack.util.dns.HostAddress;
  22. import org.jxmpp.jid.Jid;

  23. /**
  24.  * Smack uses SmackExceptions for errors that are not defined by any XMPP specification.
  25.  *
  26.  * @author Florian Schmaus
  27.  */
  28. public class SmackException extends Exception {

  29.     /**
  30.      *
  31.      */
  32.     private static final long serialVersionUID = 1844674365368214457L;

  33.     /**
  34.      * Creates a new SmackException with the Throwable that was the root cause of the exception.
  35.      *
  36.      * @param wrappedThrowable the root cause of the exception.
  37.      */
  38.     public SmackException(Throwable wrappedThrowable) {
  39.         super(wrappedThrowable);
  40.     }

  41.     public SmackException(String message) {
  42.         super(message);
  43.     }

  44.     public SmackException(String message, Throwable wrappedThrowable) {
  45.         super(message, wrappedThrowable);
  46.     }

  47.     protected SmackException() {
  48.     }

  49.     /**
  50.      * Exception thrown always when there was no response to an request within the packet reply timeout of the used
  51.      * connection instance. You can modify (e.g. increase) the packet reply timeout with
  52.      * {@link XMPPConnection#setPacketReplyTimeout(long)}.
  53.      */
  54.     public static class NoResponseException extends SmackException {
  55.         /**
  56.          *
  57.          */
  58.         private static final long serialVersionUID = -6523363748984543636L;

  59.         private final StanzaFilter filter;

  60.         private NoResponseException(String message, StanzaFilter filter) {
  61.             super(message);
  62.             this.filter = filter;
  63.         }

  64.         /**
  65.          * Get the filter that was used to collect the response.
  66.          *
  67.          * @return the used filter or <code>null</code>.
  68.          */
  69.         public StanzaFilter getFilter() {
  70.             return filter;
  71.         }

  72.         public static NoResponseException newWith(XMPPConnection connection) {
  73.             return newWith(connection, (StanzaFilter) null);
  74.         }

  75.         public static NoResponseException newWith(XMPPConnection connection,
  76.                         PacketCollector collector) {
  77.             return newWith(connection, collector.getStanzaFilter());
  78.         }

  79.         public static NoResponseException newWith(XMPPConnection connection, StanzaFilter filter) {
  80.             final long replyTimeout = connection.getPacketReplyTimeout();
  81.             final StringBuilder sb = new StringBuilder(256);
  82.             sb.append("No response received within reply timeout. Timeout was "
  83.                             + replyTimeout + "ms (~"
  84.                             + replyTimeout / 1000 + "s). Used filter: ");
  85.             if (filter != null) {
  86.                 sb.append(filter.toString());
  87.             }
  88.             else {
  89.                 sb.append("No filter used or filter was 'null'");
  90.             }
  91.             sb.append('.');
  92.             return new NoResponseException(sb.toString(), filter);
  93.         }

  94.     }

  95.     public static class NotLoggedInException extends SmackException {

  96.         /**
  97.          *
  98.          */
  99.         private static final long serialVersionUID = 3216216839100019278L;

  100.         public NotLoggedInException() {
  101.             super("Client is not logged in");
  102.         }
  103.     }

  104.     public static class AlreadyLoggedInException extends SmackException {

  105.         /**
  106.          *
  107.          */
  108.         private static final long serialVersionUID = 5011416918049935231L;

  109.         public AlreadyLoggedInException() {
  110.             super("Client is already logged in");
  111.         }
  112.     }

  113.     public static class AlreadyConnectedException extends SmackException {

  114.         /**
  115.          *
  116.          */
  117.         private static final long serialVersionUID = 5011416918049135231L;

  118.         public AlreadyConnectedException() {
  119.             super("Client is already connected");
  120.         }
  121.     }

  122.     public static class NotConnectedException extends SmackException {

  123.         /**
  124.          *
  125.          */
  126.         private static final long serialVersionUID = 9197980400776001173L;

  127.         public NotConnectedException() {
  128.             super("Client is not, or no longer, connected");
  129.         }
  130.     }

  131.     public static class IllegalStateChangeException extends SmackException {

  132.         /**
  133.          *
  134.          */
  135.         private static final long serialVersionUID = -1766023961577168927L;

  136.         public IllegalStateChangeException() {
  137.         }
  138.     }

  139.     public static abstract class SecurityRequiredException extends SmackException {

  140.         /**
  141.          *
  142.          */
  143.         private static final long serialVersionUID = 384291845029773545L;

  144.         public SecurityRequiredException(String message) {
  145.             super(message);
  146.         }
  147.     }

  148.     public static class SecurityRequiredByClientException extends SecurityRequiredException {
  149.         /**
  150.          *
  151.          */
  152.         private static final long serialVersionUID = 2395325821201543159L;

  153.         public SecurityRequiredByClientException() {
  154.             super("SSL/TLS required by client but not supported by server");
  155.         }
  156.     }

  157.     public static class SecurityRequiredByServerException extends SecurityRequiredException {
  158.         /**
  159.          *
  160.          */
  161.         private static final long serialVersionUID = 8268148813117631819L;

  162.         public SecurityRequiredByServerException() {
  163.             super("SSL/TLS required by server but disabled in client");
  164.         }
  165.     }

  166.     public static class SecurityNotPossibleException extends SmackException {

  167.         /**
  168.          *
  169.          */
  170.         private static final long serialVersionUID = -6836090872690331336L;

  171.         public SecurityNotPossibleException(String message) {
  172.             super(message);
  173.         }
  174.     }

  175.     /**
  176.      * ConnectionException is thrown if Smack is unable to connect to all hosts of a given XMPP
  177.      * service. The failed hosts can be retrieved with
  178.      * {@link ConnectionException#getFailedAddresses()}, which will have the exception causing the
  179.      * connection failure set and retrievable with {@link HostAddress#getException()}.
  180.      */
  181.     public static class ConnectionException extends SmackException {

  182.         /**
  183.          *
  184.          */
  185.         private static final long serialVersionUID = 1686944201672697996L;

  186.         private final List<HostAddress> failedAddresses;

  187.         public ConnectionException(Throwable wrappedThrowable) {
  188.             super(wrappedThrowable);
  189.             failedAddresses = new ArrayList<HostAddress>(0);
  190.         }

  191.         private ConnectionException(String message, List<HostAddress> failedAddresses) {
  192.             super(message);
  193.             this.failedAddresses = failedAddresses;
  194.         }

  195.         public static ConnectionException from(List<HostAddress> failedAddresses) {
  196.             final String DELIMITER = ", ";
  197.             StringBuilder sb = new StringBuilder("The following addresses failed: ");
  198.             for (HostAddress hostAddress : failedAddresses) {
  199.                 sb.append(hostAddress.getErrorMessage());
  200.                 sb.append(DELIMITER);
  201.             }
  202.             // Remove the last delimiter
  203.             sb.setLength(sb.length() - DELIMITER.length());
  204.             return new ConnectionException(sb.toString(), failedAddresses);
  205.         }

  206.         public List<HostAddress> getFailedAddresses() {
  207.             return failedAddresses;
  208.         }
  209.     }

  210.     public static class FeatureNotSupportedException extends SmackException {

  211.         /**
  212.          *
  213.          */
  214.         private static final long serialVersionUID = 4713404802621452016L;

  215.         private final String feature;
  216.         private final Jid jid;

  217.         public FeatureNotSupportedException(String feature) {
  218.             this(feature, null);
  219.         }

  220.         public FeatureNotSupportedException(String feature, Jid jid) {
  221.             super(feature + " not supported" + (jid == null ? "" : " by '" + jid + "'"));
  222.             this.jid = jid;
  223.             this.feature = feature;
  224.         }

  225.         /**
  226.          * Get the feature which is not supported.
  227.          *
  228.          * @return the feature which is not supported
  229.          */
  230.         public String getFeature() {
  231.             return feature;
  232.         }

  233.         /**
  234.          * Get JID which does not support the feature. The JID can be null in cases when there are
  235.          * multiple JIDs queried for this feature.
  236.          *
  237.          * @return the JID which does not support the feature, or null
  238.          */
  239.         public Jid getJid() {
  240.             return jid;
  241.         }
  242.     }

  243.     public static class ResourceBindingNotOfferedException extends SmackException {

  244.         /**
  245.          *
  246.          */
  247.         private static final long serialVersionUID = 2346934138253437571L;

  248.         public ResourceBindingNotOfferedException() {
  249.             super("Resource binding was not offered by server");
  250.         }
  251.     }
  252. }