1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 01:29:38 +02:00

Improve Exceptions (SMACK-426)

This commit is contained in:
Florian Schmaus 2014-03-17 21:06:45 +01:00
parent 9c8d7af3bf
commit c592b4f046
9 changed files with 103 additions and 57 deletions

View file

@ -113,7 +113,7 @@ public class MultipleRecipientManager {
(replyRoom != null && replyRoom.trim().length() > 0)) {
// Some specified JEP-33 features were requested so throw an exception alerting
// the user that this features are not available
throw new FeatureNotSupportedException("Extended Stanza Addressing not supported by server");
throw new FeatureNotSupportedException("Extended Stanza Addressing");
}
// Send the packet to each individual recipient
sendToIndividualRecipients(connection, packet, to, cc, bcc);

View file

@ -211,9 +211,11 @@ public class InBandBytestreamSession implements BytestreamSession {
connection.createPacketCollectorAndSend(close).nextResultOrThrow();
}
catch (Exception e) {
// Sadly we are unable to wrap the exception within the IOException, because the
// IOException(String,Throwable) constructor is only support from Android API 9 on.
throw new IOException();
// Sadly we are unable to use the IOException(Throwable) constructor because this
// constructor is only supported from Android API 9 on.
IOException ioException = new IOException();
ioException.initCause(e);
throw ioException;
}
this.inputStream.cleanup();
@ -769,9 +771,11 @@ public class InBandBytestreamSession implements BytestreamSession {
// close session unless it is already closed
if (!this.isClosed) {
InBandBytestreamSession.this.close();
// Sadly we are unable to wrap the exception within the IOException, because the
// IOException(String,Throwable) constructor is only support from Android API 9 on.
throw new IOException();
// Sadly we are unable to use the IOException(Throwable) constructor because this
// constructor is only supported from Android API 9 on.
IOException ioException = new IOException();
ioException.initCause(e);
throw ioException;
}
}

View file

@ -32,6 +32,7 @@ import java.util.concurrent.TimeoutException;
import org.jivesoftware.smack.AbstractConnectionListener;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.FeatureNotSupportedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.XMPPException;
@ -436,7 +437,7 @@ public final class Socks5BytestreamManager implements BytestreamManager {
XMPPErrorException discoveryException = null;
// check if target supports SOCKS5 Bytestream
if (!supportsSocks5(targetJID)) {
throw new SmackException(targetJID + " doesn't support SOCKS5 Bytestream");
throw new FeatureNotSupportedException("SOCKS5 Bytestream", targetJID);
}
List<String> proxies = new ArrayList<String>();

View file

@ -144,8 +144,9 @@ class Socks5Client {
* @return <code>true</code> if if a stream could be established, otherwise <code>false</code>.
* If <code>false</code> is returned the given Socket should be closed.
* @throws SmackException
* @throws IOException
*/
protected boolean establish(Socket socket) throws SmackException {
protected boolean establish(Socket socket) throws SmackException, IOException {
byte[] connectionRequest;
byte[] connectionResponse;
@ -153,39 +154,35 @@ class Socks5Client {
* use DataInputStream/DataOutpuStream to assure read and write is completed in a single
* statement
*/
try {
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
// authentication negotiation
byte[] cmd = new byte[3];
// authentication negotiation
byte[] cmd = new byte[3];
cmd[0] = (byte) 0x05; // protocol version 5
cmd[1] = (byte) 0x01; // number of authentication methods supported
cmd[2] = (byte) 0x00; // authentication method: no-authentication required
cmd[0] = (byte) 0x05; // protocol version 5
cmd[1] = (byte) 0x01; // number of authentication methods supported
cmd[2] = (byte) 0x00; // authentication method: no-authentication required
out.write(cmd);
out.flush();
out.write(cmd);
out.flush();
byte[] response = new byte[2];
in.readFully(response);
byte[] response = new byte[2];
in.readFully(response);
// check if server responded with correct version and no-authentication method
if (response[0] != (byte) 0x05 || response[1] != (byte) 0x00) {
return false;
}
// request SOCKS5 connection with given address/digest
connectionRequest = createSocks5ConnectRequest();
out.write(connectionRequest);
out.flush();
// receive response
connectionResponse = Socks5Utils.receiveSocks5Message(in);
}
catch (IOException e) {
throw new SmackException(e);
// check if server responded with correct version and no-authentication method
if (response[0] != (byte) 0x05 || response[1] != (byte) 0x00) {
return false;
}
// request SOCKS5 connection with given address/digest
connectionRequest = createSocks5ConnectRequest();
out.write(connectionRequest);
out.flush();
// receive response
connectionResponse = Socks5Utils.receiveSocks5Message(in);
// verify response
connectionRequest[1] = (byte) 0x00; // set expected return status to 0
return Arrays.equals(connectionRequest, connectionResponse);

View file

@ -25,6 +25,7 @@ import java.io.OutputStream;
import java.net.ConnectException;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.FeatureNotSupportedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
@ -152,13 +153,10 @@ public class Socks5ByteStreamManagerTest {
fail("exception should be thrown");
}
catch (SmackException e) {
assertTrue(e.getMessage().contains("doesn't support SOCKS5 Bytestream"));
}
catch (IOException e) {
fail(e.getMessage());
}
catch (InterruptedException e) {
catch (FeatureNotSupportedException e) {
assertTrue(e.getFeature().equals("SOCKS5 Bytestream"));
assertTrue(e.getJid().equals(targetJID));
} catch(Exception e) {
fail(e.getMessage());
}