Search in sources :

Example 1 with GenericConnectionException

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);
    }
}
Also used : XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) BOSHClientConfig(org.igniterealtime.jbosh.BOSHClientConfig) IOException(java.io.IOException) SmackException(org.jivesoftware.smack.SmackException) GenericConnectionException(org.jivesoftware.smack.SmackException.GenericConnectionException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) StreamErrorException(org.jivesoftware.smack.XMPPException.StreamErrorException) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException) BOSHException(org.igniterealtime.jbosh.BOSHException) IOException(java.io.IOException) SmackWrappedException(org.jivesoftware.smack.SmackException.SmackWrappedException) XMPPException(org.jivesoftware.smack.XMPPException) GenericConnectionException(org.jivesoftware.smack.SmackException.GenericConnectionException) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException) Map(java.util.Map)

Aggregations

IOException (java.io.IOException)1 Map (java.util.Map)1 BOSHClientConfig (org.igniterealtime.jbosh.BOSHClientConfig)1 BOSHException (org.igniterealtime.jbosh.BOSHException)1 SmackException (org.jivesoftware.smack.SmackException)1 GenericConnectionException (org.jivesoftware.smack.SmackException.GenericConnectionException)1 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)1 SmackWrappedException (org.jivesoftware.smack.SmackException.SmackWrappedException)1 XMPPException (org.jivesoftware.smack.XMPPException)1 StreamErrorException (org.jivesoftware.smack.XMPPException.StreamErrorException)1 XmlPullParser (org.jivesoftware.smack.xml.XmlPullParser)1 XmlPullParserException (org.jivesoftware.smack.xml.XmlPullParserException)1