Search in sources :

Example 1 with Mechanisms

use of org.jivesoftware.smack.packet.Mechanisms in project Smack by igniterealtime.

the class AbstractXMPPConnection method parseFeatures.

protected final void parseFeatures(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException {
    streamFeatures.clear();
    final int initialDepth = parser.getDepth();
    while (true) {
        XmlPullParser.Event eventType = parser.next();
        if (eventType == XmlPullParser.Event.START_ELEMENT && parser.getDepth() == initialDepth + 1) {
            XmlElement streamFeature = null;
            String name = parser.getName();
            String namespace = parser.getNamespace();
            switch(name) {
                case StartTls.ELEMENT:
                    streamFeature = PacketParserUtils.parseStartTlsFeature(parser);
                    break;
                case Mechanisms.ELEMENT:
                    streamFeature = new Mechanisms(PacketParserUtils.parseMechanisms(parser));
                    break;
                case Bind.ELEMENT:
                    streamFeature = Bind.Feature.INSTANCE;
                    break;
                case Session.ELEMENT:
                    streamFeature = PacketParserUtils.parseSessionFeature(parser);
                    break;
                case Compress.Feature.ELEMENT:
                    streamFeature = PacketParserUtils.parseCompressionFeature(parser);
                    break;
                default:
                    ExtensionElementProvider<ExtensionElement> provider = ProviderManager.getStreamFeatureProvider(name, namespace);
                    if (provider != null) {
                        streamFeature = provider.parse(parser, incomingStreamXmlEnvironment);
                    }
                    break;
            }
            if (streamFeature != null) {
                addStreamFeature(streamFeature);
            }
        } else if (eventType == XmlPullParser.Event.END_ELEMENT && parser.getDepth() == initialDepth) {
            break;
        }
    }
}
Also used : Mechanisms(org.jivesoftware.smack.packet.Mechanisms) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) XmlElement(org.jivesoftware.smack.packet.XmlElement)

Example 2 with Mechanisms

use of org.jivesoftware.smack.packet.Mechanisms in project xabber-android by redsolution.

the class XMPPTCPConnection method loginInternal.

@Override
protected synchronized void loginInternal(String username, String password, Resourcepart resource) throws XMPPException, SmackException, IOException, InterruptedException {
    // Authenticate using SASL
    SSLSession sslSession = secureSocket != null ? secureSocket.getSession() : null;
    saslAuthentication.authenticate(username, password, config.getAuthzid(), sslSession);
    // If compression is enabled then request the server to use stream compression. XEP-170
    // recommends to perform stream compression before resource binding.
    maybeEnableCompression();
    if (isSmResumptionPossible()) {
        smResumedSyncPoint.sendAndWaitForResponse(new Resume(clientHandledStanzasCount, smSessionId));
        if (smResumedSyncPoint.wasSuccessful()) {
            // We successfully resumed the stream, be done here
            afterSuccessfulLogin(true);
            return;
        }
        // SM resumption failed, what Smack does here is to report success of
        // lastFeaturesReceived in case of sm resumption was answered with 'failed' so that
        // normal resource binding can be tried.
        LOGGER.fine("Stream resumption failed, continuing with normal stream establishment process");
    }
    List<Stanza> previouslyUnackedStanzas = new LinkedList<Stanza>();
    if (unacknowledgedStanzas != null) {
        // There was a previous connection with SM enabled but that was either not resumable or
        // failed to resume. Make sure that we (re-)send the unacknowledged stanzas.
        unacknowledgedStanzas.drainTo(previouslyUnackedStanzas);
        // Reset unacknowledged stanzas to 'null' to signal that we never send 'enable' in this
        // XMPP session (There maybe was an enabled in a previous XMPP session of this
        // connection instance though). This is used in writePackets to decide if stanzas should
        // be added to the unacknowledged stanzas queue, because they have to be added right
        // after the 'enable' stream element has been sent.
        dropSmState();
    }
    // X-TOKEN
    Mechanisms mechanisms = getFeature(Mechanisms.ELEMENT, Mechanisms.NAMESPACE);
    List<String> mechanismsList = mechanisms.getMechanisms();
    if (mechanismsList.contains(SASLXTOKENMechanism.NAME) && !getUsedSaslMechansism().equals(SASLXTOKENMechanism.NAME)) {
        XTokenManager.getInstance().sendXTokenRequest(this);
    }
    // Now bind the resource. It is important to do this *after* we dropped an eventually
    // existing Stream Management state. As otherwise <bind/> and <session/> may end up in
    // unacknowledgedStanzas and become duplicated on reconnect. See SMACK-706.
    bindResourceAndEstablishSession(resource);
    if (isSmAvailable() && useSm) {
        // Remove what is maybe left from previously stream managed sessions
        serverHandledStanzasCount = 0;
        // XEP-198 3. Enabling Stream Management. If the server response to 'Enable' is 'Failed'
        // then this is a non recoverable error and we therefore throw an exception.
        smEnabledSyncPoint.sendAndWaitForResponseOrThrow(new Enable(useSmResumption, smClientMaxResumptionTime));
        synchronized (requestAckPredicates) {
            if (requestAckPredicates.isEmpty()) {
                // Assure that we have at lest one predicate set up that so that we request acks
                // for the server and eventually flush some stanzas from the unacknowledged
                // stanza queue
                requestAckPredicates.add(Predicate.forMessagesOrAfter5Stanzas());
            }
        }
    }
    // (Re-)send the stanzas *after* we tried to enable SM
    for (Stanza stanza : previouslyUnackedStanzas) {
        sendStanzaInternal(stanza);
    }
    afterSuccessfulLogin(false);
}
Also used : Mechanisms(org.jivesoftware.smack.packet.Mechanisms) Stanza(org.jivesoftware.smack.packet.Stanza) SSLSession(javax.net.ssl.SSLSession) Enable(org.jivesoftware.smack.sm.packet.StreamManagement.Enable) LinkedList(java.util.LinkedList) Resume(org.jivesoftware.smack.sm.packet.StreamManagement.Resume)

Aggregations

Mechanisms (org.jivesoftware.smack.packet.Mechanisms)2 LinkedList (java.util.LinkedList)1 SSLSession (javax.net.ssl.SSLSession)1 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)1 Stanza (org.jivesoftware.smack.packet.Stanza)1 XmlElement (org.jivesoftware.smack.packet.XmlElement)1 Enable (org.jivesoftware.smack.sm.packet.StreamManagement.Enable)1 Resume (org.jivesoftware.smack.sm.packet.StreamManagement.Resume)1 XmlPullParser (org.jivesoftware.smack.xml.XmlPullParser)1