From 7fce6b5a98d07971e173e47ef1e2d5e4d609671e Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 10 Feb 2019 21:39:48 +0100 Subject: [PATCH] Introduce SmackMessageException --- .../smack/bosh/XMPPBOSHConnection.java | 2 +- .../jivesoftware/smack/SmackException.java | 22 ++++++++++++--- .../httpfileupload/HttpFileUploadManager.java | 2 +- .../address/MultipleRecipientManager.java | 13 ++++----- .../socks5/Socks5BytestreamManager.java | 12 ++++++--- .../bytestreams/socks5/Socks5Client.java | 27 +++++++++++-------- .../socks5/Socks5ClientForInitiator.java | 6 ++--- .../bytestreams/socks5/Socks5Proxy.java | 6 ++--- .../bytestreams/socks5/Socks5Utils.java | 8 +++--- .../bytestreams/socks5/Socks5TestProxy.java | 4 +-- .../iot/IoTDiscoveryIntegrationTest.java | 6 +++-- .../smackx/jingleold/nat/BridgedResolver.java | 6 +++-- .../smack/tcp/XMPPTCPConnection.java | 8 +++--- 13 files changed, 76 insertions(+), 46 deletions(-) diff --git a/smack-bosh/src/main/java/org/jivesoftware/smack/bosh/XMPPBOSHConnection.java b/smack-bosh/src/main/java/org/jivesoftware/smack/bosh/XMPPBOSHConnection.java index 3446c957d..1dbaf91cb 100644 --- a/smack-bosh/src/main/java/org/jivesoftware/smack/bosh/XMPPBOSHConnection.java +++ b/smack-bosh/src/main/java/org/jivesoftware/smack/bosh/XMPPBOSHConnection.java @@ -195,7 +195,7 @@ public class XMPPBOSHConnection extends AbstractXMPPConnection { done = true; String errorMessage = "Timeout reached for the connection to " + getHost() + ":" + getPort() + "."; - throw new SmackException(errorMessage); + throw new SmackException.SmackMessageException(errorMessage); } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/SmackException.java b/smack-core/src/main/java/org/jivesoftware/smack/SmackException.java index 64ff19869..512445e30 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/SmackException.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/SmackException.java @@ -1,6 +1,6 @@ /** * - * Copyright 2014-2015 Florian Schmaus + * Copyright 2014-2019 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,11 +41,11 @@ public class SmackException extends Exception { * * @param wrappedThrowable the root cause of the exception. */ - public SmackException(Throwable wrappedThrowable) { + protected SmackException(Throwable wrappedThrowable) { super(wrappedThrowable); } - public SmackException(String message) { + protected SmackException(String message) { super(message); } @@ -367,6 +367,22 @@ public class SmackException extends Exception { public SmackWrappedException(Exception exception) { super(exception); } + + public SmackWrappedException(String message, Exception exception) { + super(message, exception); + } + } + + public static class SmackMessageException extends SmackException { + + /** + * + */ + private static final long serialVersionUID = 1L; + + public SmackMessageException(String message) { + super(message); + } } public static class SmackSaslException extends SmackException { diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/httpfileupload/HttpFileUploadManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/httpfileupload/HttpFileUploadManager.java index b86577466..29f78c48c 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/httpfileupload/HttpFileUploadManager.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/httpfileupload/HttpFileUploadManager.java @@ -350,7 +350,7 @@ public final class HttpFileUploadManager extends Manager { } if (uploadService == null) { - throw new SmackException("No upload service specified and also none discovered."); + throw new SmackException.SmackMessageException("No upload service specified and also none discovered."); } if (!uploadService.acceptsFileOfSize(fileSize)) { diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/address/MultipleRecipientManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/address/MultipleRecipientManager.java index 1efb36b48..be9c5c70e 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/address/MultipleRecipientManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/address/MultipleRecipientManager.java @@ -21,7 +21,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; -import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException.FeatureNotSupportedException; import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.SmackException.NotConnectedException; @@ -140,21 +139,23 @@ public class MultipleRecipientManager { * @param connection the connection to use to send the reply. * @param original the previously received stanza that was sent to multiple recipients. * @param reply the new message to send as a reply. - * @throws SmackException * @throws XMPPErrorException * @throws InterruptedException + * @throws NotConnectedException + * @throws FeatureNotSupportedException + * @throws NoResponseException */ public static void reply(XMPPConnection connection, Message original, Message reply) - throws SmackException, XMPPErrorException, InterruptedException { + throws XMPPErrorException, InterruptedException, NotConnectedException, NoResponseException, FeatureNotSupportedException { MultipleRecipientInfo info = getMultipleRecipientInfo(original); if (info == null) { - throw new SmackException("Original message does not contain multiple recipient info"); + throw new IllegalArgumentException("Original message does not contain multiple recipient info"); } if (info.shouldNotReply()) { - throw new SmackException("Original message should not be replied"); + throw new IllegalArgumentException("Original message should not be replied"); } if (info.getReplyRoom() != null) { - throw new SmackException("Reply should be sent through a room"); + throw new IllegalArgumentException("Reply should be sent through a room"); } // Any element from the initial message MUST be copied into the reply. if (original.getThread() != null) { diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5BytestreamManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5BytestreamManager.java index fdce8b3f4..7daf62c85 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5BytestreamManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5BytestreamManager.java @@ -36,6 +36,7 @@ import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException.FeatureNotSupportedException; import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.SmackException.NotConnectedException; +import org.jivesoftware.smack.SmackException.SmackMessageException; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnectionRegistry; import org.jivesoftware.smack.XMPPException; @@ -409,12 +410,15 @@ public final class Socks5BytestreamManager extends Manager implements Bytestream * @return the Socket to send/receive data to/from the user * @throws IOException if the bytestream could not be established * @throws InterruptedException if the current thread was interrupted while waiting - * @throws SmackException if the target does not support SOCKS5. * @throws XMPPException + * @throws NotConnectedException + * @throws NoResponseException + * @throws SmackMessageException + * @throws FeatureNotSupportedException */ @Override public Socks5BytestreamSession establishSession(Jid targetJID, String sessionID) - throws IOException, InterruptedException, SmackException, XMPPException { + throws IOException, InterruptedException, XMPPException, NoResponseException, NotConnectedException, SmackMessageException, FeatureNotSupportedException { XMPPConnection connection = connection(); XMPPErrorException discoveryException = null; // check if target supports SOCKS5 Bytestream @@ -439,7 +443,7 @@ public final class Socks5BytestreamManager extends Manager implements Bytestream if (discoveryException != null) { throw discoveryException; } else { - throw new SmackException("no SOCKS5 proxies available"); + throw new SmackException.SmackMessageException("no SOCKS5 proxies available"); } } @@ -480,7 +484,7 @@ public final class Socks5BytestreamManager extends Manager implements Bytestream StreamHost usedStreamHost = initiation.getStreamHost(streamHostUsed.getJID()); if (usedStreamHost == null) { - throw new SmackException("Remote user responded with unknown host"); + throw new SmackException.SmackMessageException("Remote user responded with unknown host"); } // build SOCKS5 client diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5Client.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5Client.java index 556957e69..5ffecf1c3 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5Client.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5Client.java @@ -32,6 +32,9 @@ import java.util.concurrent.TimeoutException; import java.util.logging.Logger; import org.jivesoftware.smack.SmackException; +import org.jivesoftware.smack.SmackException.NoResponseException; +import org.jivesoftware.smack.SmackException.NotConnectedException; +import org.jivesoftware.smack.SmackException.SmackMessageException; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.util.CloseableUtil; import org.jivesoftware.smack.util.StringUtils; @@ -74,16 +77,18 @@ public class Socks5Client { * @throws IOException if initializing the socket failed due to a network error * @throws TimeoutException if connecting to SOCKS5 proxy timed out * @throws InterruptedException if the current thread was interrupted while waiting - * @throws SmackException if the connection to the SOCKS5 proxy failed * @throws XMPPException + * @throws SmackMessageException + * @throws NotConnectedException + * @throws NoResponseException */ public Socket getSocket(int timeout) throws IOException, InterruptedException, - TimeoutException, SmackException, XMPPException { + TimeoutException, XMPPException, SmackMessageException, NotConnectedException, NoResponseException { // wrap connecting in future for timeout FutureTask futureTask = new FutureTask<>(new Callable() { @Override - public Socket call() throws IOException, SmackException { + public Socket call() throws IOException, SmackMessageException { // initialize socket Socket socket = new Socket(); @@ -95,7 +100,7 @@ public class Socks5Client { try { establish(socket); } - catch (SmackException e) { + catch (SmackMessageException e) { if (!socket.isClosed()) { CloseableUtil.maybeClose(socket, LOGGER); } @@ -120,13 +125,13 @@ public class Socks5Client { if (cause instanceof IOException) { throw (IOException) cause; } - if (cause instanceof SmackException) { - throw (SmackException) cause; + if (cause instanceof SmackMessageException) { + throw (SmackMessageException) cause; } } // throw generic Smack exception if unexpected exception was thrown - throw new SmackException("Error while connecting to SOCKS5 proxy", e); + throw new IllegalStateException("Error while connecting to SOCKS5 proxy", e); } } @@ -137,10 +142,10 @@ public class Socks5Client { * supported by the Socks5Client. * * @param socket connected to a SOCKS5 proxy - * @throws SmackException * @throws IOException + * @throws SmackMessageException */ - protected void establish(Socket socket) throws SmackException, IOException { + protected void establish(Socket socket) throws IOException, SmackMessageException { byte[] connectionRequest; byte[] connectionResponse; @@ -166,7 +171,7 @@ public class Socks5Client { // check if server responded with correct version and no-authentication method if (response[0] != (byte) 0x05 || response[1] != (byte) 0x00) { - throw new SmackException("Remote SOCKS5 server responded with unexpected version: " + response[0] + ' ' + response[1] + ". Should be 0x05 0x00."); + throw new SmackException.SmackMessageException("Remote SOCKS5 server responded with unexpected version: " + response[0] + ' ' + response[1] + ". Should be 0x05 0x00."); } // request SOCKS5 connection with given address/digest @@ -180,7 +185,7 @@ public class Socks5Client { // verify response connectionRequest[1] = (byte) 0x00; // set expected return status to 0 if (!Arrays.equals(connectionRequest, connectionResponse)) { - throw new SmackException( + throw new SmackException.SmackMessageException( "Connection request does not equal connection response. Response: " + Arrays.toString(connectionResponse) + ". Request: " + Arrays.toString(connectionRequest)); diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ClientForInitiator.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ClientForInitiator.java index 536aeaa23..714e849af 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ClientForInitiator.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ClientForInitiator.java @@ -24,6 +24,7 @@ import java.util.concurrent.TimeoutException; import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.SmackException.NotConnectedException; +import org.jivesoftware.smack.SmackException.SmackMessageException; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException.XMPPErrorException; @@ -73,7 +74,7 @@ public class Socks5ClientForInitiator extends Socks5Client { @Override public Socket getSocket(int timeout) throws IOException, InterruptedException, - TimeoutException, XMPPException, SmackException { + TimeoutException, XMPPException, SmackMessageException, NotConnectedException, NoResponseException { Socket socket; // check if stream host is the local SOCKS5 proxy @@ -81,7 +82,7 @@ public class Socks5ClientForInitiator extends Socks5Client { Socks5Proxy socks5Server = Socks5Proxy.getSocks5Proxy(); socket = socks5Server.getSocket(this.digest); if (socket == null) { - throw new SmackException("target is not connected to SOCKS5 proxy"); + throw new SmackException.SmackMessageException("target is not connected to SOCKS5 proxy"); } } else { @@ -111,7 +112,6 @@ public class Socks5ClientForInitiator extends Socks5Client { * @throws NoResponseException * @throws NotConnectedException * @throws InterruptedException - * @throws SmackException if there was no response from the server. */ private void activate() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { Bytestream activate = createStreamHostActivation(); diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5Proxy.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5Proxy.java index 81e3057bf..2acf2e8f0 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5Proxy.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5Proxy.java @@ -431,7 +431,7 @@ public final class Socks5Proxy { // first byte is version should be 5 int b = in.read(); if (b != 5) { - throw new SmackException("Only SOCKS5 supported"); + throw new SmackException.SmackMessageException("Only SOCKS5 supported"); } // second byte number of authentication methods supported @@ -457,7 +457,7 @@ public final class Socks5Proxy { authMethodSelectionResponse[1] = (byte) 0xFF; // no acceptable methods out.write(authMethodSelectionResponse); out.flush(); - throw new SmackException("Authentication method not supported"); + throw new SmackException.SmackMessageException("Authentication method not supported"); } authMethodSelectionResponse[1] = (byte) 0x00; // no-authentication method @@ -476,7 +476,7 @@ public final class Socks5Proxy { out.write(connectionRequest); out.flush(); - throw new SmackException("Connection is not allowed"); + throw new SmackException.SmackMessageException("Connection is not allowed"); } connectionRequest[1] = (byte) 0x00; // set return status to 0 (success) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5Utils.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5Utils.java index 209c3d1d4..50c9efef1 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5Utils.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5Utils.java @@ -19,7 +19,7 @@ package org.jivesoftware.smackx.bytestreams.socks5; import java.io.DataInputStream; import java.io.IOException; -import org.jivesoftware.smack.SmackException; +import org.jivesoftware.smack.SmackException.SmackMessageException; import org.jivesoftware.smack.util.SHA1; import org.jxmpp.jid.Jid; @@ -55,14 +55,14 @@ public class Socks5Utils { * @param in the DataInputStream to read the message from * @return the SOCKS5 message * @throws IOException if a network error occurred - * @throws SmackException if the SOCKS5 message contains an unsupported address type + * @throws SmackMessageException if the SOCKS5 message contains an unsupported address type */ - public static byte[] receiveSocks5Message(DataInputStream in) throws IOException, SmackException { + public static byte[] receiveSocks5Message(DataInputStream in) throws IOException, SmackMessageException { byte[] header = new byte[5]; in.readFully(header, 0, 5); if (header[3] != (byte) 0x03) { - throw new SmackException("Unsupported SOCKS5 address type: " + header[3] + " (expected: 0x03)"); + throw new SmackMessageException("Unsupported SOCKS5 address type: " + header[3] + " (expected: 0x03)"); } int addressLength = header[4]; diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5TestProxy.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5TestProxy.java index 180838e29..6684c6f89 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5TestProxy.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5TestProxy.java @@ -260,7 +260,7 @@ public final class Socks5TestProxy { // first byte is version should be 5 int b = in.read(); if (b != 5) { - throw new SmackException("Only SOCKS5 supported"); + throw new SmackException.SmackMessageException("Only SOCKS5 supported"); } // second byte number of authentication methods supported @@ -286,7 +286,7 @@ public final class Socks5TestProxy { authMethodSelectionResponse[1] = (byte) 0xFF; // no acceptable methods out.write(authMethodSelectionResponse); out.flush(); - throw new SmackException("Authentication method not supported"); + throw new SmackException.SmackMessageException("Authentication method not supported"); } authMethodSelectionResponse[1] = (byte) 0x00; // no-authentication method diff --git a/smack-integration-test/src/main/java/org/jivesoftware/smackx/iot/IoTDiscoveryIntegrationTest.java b/smack-integration-test/src/main/java/org/jivesoftware/smackx/iot/IoTDiscoveryIntegrationTest.java index bf8457e6c..1c1c8071d 100644 --- a/smack-integration-test/src/main/java/org/jivesoftware/smackx/iot/IoTDiscoveryIntegrationTest.java +++ b/smack-integration-test/src/main/java/org/jivesoftware/smackx/iot/IoTDiscoveryIntegrationTest.java @@ -76,7 +76,9 @@ public class IoTDiscoveryIntegrationTest extends AbstractSmackIntegrationTest { } } - public static ThingState registerThing(IoTDiscoveryManager iotDiscoveryManager, Thing thing) throws XMPPErrorException, InterruptedException, SmackException { + public static ThingState registerThing(IoTDiscoveryManager iotDiscoveryManager, Thing thing) + throws XMPPErrorException, InterruptedException, SmackException.SmackMessageException, + NotConnectedException, NoResponseException { int attempts = 0; while (true) { try { @@ -86,7 +88,7 @@ public class IoTDiscoveryIntegrationTest extends AbstractSmackIntegrationTest { iotDiscoveryManager.unregister(); } if (attempts++ > 3) { - throw new SmackException("Could no register thing"); + throw new SmackException.SmackMessageException("Could no register thing"); } } } diff --git a/smack-jingle-old/src/main/java/org/jivesoftware/smackx/jingleold/nat/BridgedResolver.java b/smack-jingle-old/src/main/java/org/jivesoftware/smackx/jingleold/nat/BridgedResolver.java index 4181e0a2e..5efb2a610 100644 --- a/smack-jingle-old/src/main/java/org/jivesoftware/smackx/jingleold/nat/BridgedResolver.java +++ b/smack-jingle-old/src/main/java/org/jivesoftware/smackx/jingleold/nat/BridgedResolver.java @@ -26,6 +26,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.jivesoftware.smack.SmackException; +import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.SmackException.NotConnectedException; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; @@ -105,13 +106,14 @@ public class BridgedResolver extends TransportResolver { } @Override - public void initialize() throws SmackException, XMPPErrorException, InterruptedException { + public void initialize() throws SmackException.SmackMessageException, XMPPErrorException, InterruptedException, + NoResponseException, NotConnectedException { clearCandidates(); if (!RTPBridge.serviceAvailable(connection)) { setInitialized(); - throw new SmackException("No RTP Bridge service available"); + throw new SmackException.SmackMessageException("No RTP Bridge service available"); } setInitialized(); diff --git a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java index c28d60730..39e658601 100644 --- a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java +++ b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java @@ -911,13 +911,13 @@ public class XMPPTCPConnection extends AbstractXMPPConnection { case "urn:ietf:params:xml:ns:xmpp-tls": // TLS negotiation has failed. The server will close the connection // TODO Parse failure stanza - throw new SmackException("TLS negotiation has failed"); + throw new SmackException.SmackMessageException("TLS negotiation has failed"); case "http://jabber.org/protocol/compress": // Stream compression has been denied. This is a recoverable // situation. It is still possible to authenticate and // use the connection but using an uncompressed connection // TODO Parse failure stanza - compressSyncPoint.reportFailure(new SmackException( + compressSyncPoint.reportFailure(new SmackException.SmackMessageException( "Could not establish compression")); break; case SaslStreamElements.NAMESPACE: @@ -957,7 +957,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection { if (enabled.isResumeSet()) { smSessionId = enabled.getId(); if (StringUtils.isNullOrEmpty(smSessionId)) { - SmackException xmppException = new SmackException("Stream Management 'enabled' element with resume attribute but without session id received"); + SmackException xmppException = new SmackException.SmackMessageException("Stream Management 'enabled' element with resume attribute but without session id received"); smEnabledSyncPoint.reportFailure(xmppException); throw xmppException; } @@ -1069,7 +1069,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection { case XmlPullParser.END_DOCUMENT: // END_DOCUMENT only happens in an error case, as otherwise we would see a // closing stream element before. - throw new SmackException( + throw new SmackException.SmackMessageException( "Parser got END_DOCUMENT event. This could happen e.g. if the server closed the connection without sending a closing stream element"); } eventType = parser.next();