use of org.jivesoftware.smack.SmackException.GenericConnectionException in project Smack by igniterealtime.
the class XMPPBOSHConnection method connectInternal.
@SuppressWarnings("deprecation")
@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());
}
cfgBuilder.setCompressionEnabled(config.isCompressionEnabled());
for (Map.Entry<String, String> h : config.getHttpHeaders().entrySet()) {
cfgBuilder.addHttpHeader(h.getKey(), h.getValue());
}
client = BOSHClient.create(cfgBuilder.build());
client.addBOSHClientConnListener(new BOSHConnectionListener());
client.addBOSHClientResponseListener(new BOSHPacketReader());
// Initialize the debugger
if (debugger != null) {
initDebugger();
}
// 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 GenericConnectionException(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.SmackMessageException(errorMessage);
}
try {
XmlPullParser parser = PacketParserUtils.getParserFor("<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'/>");
onStreamOpen(parser);
} catch (XmlPullParserException | IOException e) {
throw new AssertionError("Failed to setup stream environment", e);
}
}
Aggregations