use of org.jivesoftware.smack.SmackException.ConnectionException in project Smack by igniterealtime.
the class SmackExceptionTest method testConnectionException.
@Test
public void testConnectionException() throws UnknownHostException {
List<HostAddress> failedAddresses = new LinkedList<HostAddress>();
String host = "foo.bar.example";
InetAddress inetAddress = InetAddress.getByAddress(host, new byte[] { 0, 0, 0, 0 });
List<InetAddress> inetAddresses = Collections.singletonList(inetAddress);
HostAddress hostAddress = new HostAddress(host, 1234, inetAddresses);
hostAddress.setException(new Exception("Failed for some reason"));
failedAddresses.add(hostAddress);
host = "barz.example";
inetAddress = InetAddress.getByAddress(host, new byte[] { 0, 0, 0, 0 });
inetAddresses = Collections.singletonList(inetAddress);
hostAddress = new HostAddress(host, 5678, inetAddresses);
hostAddress.setException(new Exception("Failed for some other reason"));
failedAddresses.add(hostAddress);
ConnectionException connectionException = ConnectionException.from(failedAddresses);
String message = connectionException.getMessage();
assertEquals("The following addresses failed: 'foo.bar.example:1234' failed because: java.lang.Exception: Failed for some reason, 'barz.example:5678' failed because: java.lang.Exception: Failed for some other reason", message);
}
use of org.jivesoftware.smack.SmackException.ConnectionException in project Smack by igniterealtime.
the class XMPPBOSHConnection method connectInternal.
@Override
protected void connectInternal() throws SmackException, InterruptedException {
done = false;
notified = false;
try {
// Ensure a clean starting state
if (client != null) {
client.close();
client = null;
}
sessionID = null;
// Initialize BOSH client
BOSHClientConfig.Builder cfgBuilder = BOSHClientConfig.Builder.create(config.getURI(), config.getXMPPServiceDomain().toString());
if (config.isProxyEnabled()) {
cfgBuilder.setProxy(config.getProxyAddress(), config.getProxyPort());
}
client = BOSHClient.create(cfgBuilder.build());
client.addBOSHClientConnListener(new BOSHConnectionListener());
client.addBOSHClientResponseListener(new BOSHPacketReader());
// Initialize the debugger
if (config.isDebuggerEnabled()) {
initDebugger();
if (isFirstInitialization) {
if (debugger.getReaderListener() != null) {
addAsyncStanzaListener(debugger.getReaderListener(), null);
}
if (debugger.getWriterListener() != null) {
addPacketSendingListener(debugger.getWriterListener(), null);
}
}
}
// Send the session creation request
client.send(ComposableBody.builder().setNamespaceDefinition("xmpp", XMPP_BOSH_NS).setAttribute(BodyQName.createWithPrefix(XMPP_BOSH_NS, "version", "xmpp"), "1.0").build());
} catch (Exception e) {
throw new ConnectionException(e);
}
// Wait for the response from the server
synchronized (this) {
if (!connected) {
final long deadline = System.currentTimeMillis() + getReplyTimeout();
while (!notified) {
final long now = System.currentTimeMillis();
if (now >= deadline)
break;
wait(deadline - now);
}
}
}
// If there is no feedback, throw an remote server timeout error
if (!connected && !done) {
done = true;
String errorMessage = "Timeout reached for the connection to " + getHost() + ":" + getPort() + ".";
throw new SmackException(errorMessage);
}
tlsHandled.reportSuccess();
saslFeatureReceived.reportSuccess();
}
use of org.jivesoftware.smack.SmackException.ConnectionException in project Smack by igniterealtime.
the class XMPPTCPConnection method connectUsingConfiguration.
private void connectUsingConfiguration() throws ConnectionException, IOException {
List<HostAddress> failedAddresses = populateHostAddresses();
SocketFactory socketFactory = config.getSocketFactory();
ProxyInfo proxyInfo = config.getProxyInfo();
int timeout = config.getConnectTimeout();
if (socketFactory == null) {
socketFactory = SocketFactory.getDefault();
}
for (HostAddress hostAddress : hostAddresses) {
Iterator<InetAddress> inetAddresses = null;
String host = hostAddress.getFQDN();
int port = hostAddress.getPort();
if (proxyInfo == null) {
inetAddresses = hostAddress.getInetAddresses().iterator();
assert (inetAddresses.hasNext());
innerloop: while (inetAddresses.hasNext()) {
// Create a *new* Socket before every connection attempt, i.e. connect() call, since Sockets are not
// re-usable after a failed connection attempt. See also SMACK-724.
socket = socketFactory.createSocket();
final InetAddress inetAddress = inetAddresses.next();
final String inetAddressAndPort = inetAddress + " at port " + port;
LOGGER.finer("Trying to establish TCP connection to " + inetAddressAndPort);
try {
socket.connect(new InetSocketAddress(inetAddress, port), timeout);
} catch (Exception e) {
hostAddress.setException(inetAddress, e);
if (inetAddresses.hasNext()) {
continue innerloop;
} else {
break innerloop;
}
}
LOGGER.finer("Established TCP connection to " + inetAddressAndPort);
// We found a host to connect to, return here
this.host = host;
this.port = port;
return;
}
failedAddresses.add(hostAddress);
} else {
socket = socketFactory.createSocket();
StringUtils.requireNotNullOrEmpty(host, "Host of HostAddress " + hostAddress + " must not be null when using a Proxy");
final String hostAndPort = host + " at port " + port;
LOGGER.finer("Trying to establish TCP connection via Proxy to " + hostAndPort);
try {
proxyInfo.getProxySocketConnection().connect(socket, host, port, timeout);
} catch (IOException e) {
hostAddress.setException(e);
continue;
}
LOGGER.finer("Established TCP connection to " + hostAndPort);
// We found a host to connect to, return here
this.host = host;
this.port = port;
return;
}
}
// HostAddresses in the exception
throw ConnectionException.from(failedAddresses);
}
Aggregations