Search in sources :

Example 1 with Session

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

the class AbstractXMPPConnection method bindResourceAndEstablishSession.

protected Resourcepart bindResourceAndEstablishSession(Resourcepart resource) throws SmackException, InterruptedException, XMPPException {
    // Wait until either:
    // - the servers last features stanza has been parsed
    // - the timeout occurs
    LOGGER.finer("Waiting for last features to be received before continuing with resource binding");
    waitForConditionOrThrowConnectionException(() -> lastFeaturesReceived, "last stream features received from server");
    if (!hasFeature(Bind.ELEMENT, Bind.NAMESPACE)) {
        // server implementations as per RFC6120 7.2
        throw new ResourceBindingNotOfferedException();
    }
    // Resource binding, see RFC6120 7.
    // Note that we can not use IQReplyFilter here, since the users full JID is not yet
    // available. It will become available right after the resource has been successfully bound.
    Bind bindResource = Bind.newSet(resource);
    StanzaCollector packetCollector = createStanzaCollectorAndSend(new StanzaIdFilter(bindResource), bindResource);
    Bind response = packetCollector.nextResultOrThrow();
    // Set the connections user to the result of resource binding. It is important that we don't infer the user
    // from the login() arguments and the configurations service name, as, for example, when SASL External is used,
    // the username is not given to login but taken from the 'external' certificate.
    user = response.getJid();
    xmppServiceDomain = user.asDomainBareJid();
    Session.Feature sessionFeature = getFeature(Session.Feature.class);
    // For more information see http://tools.ietf.org/html/draft-cridland-xmpp-session-01
    if (sessionFeature != null && !sessionFeature.isOptional()) {
        Session session = new Session();
        packetCollector = createStanzaCollectorAndSend(new StanzaIdFilter(session), session);
        packetCollector.nextResultOrThrow();
    }
    return response.getJid().getResourcepart();
}
Also used : Bind(org.jivesoftware.smack.packet.Bind) ResourceBindingNotOfferedException(org.jivesoftware.smack.SmackException.ResourceBindingNotOfferedException) StanzaIdFilter(org.jivesoftware.smack.filter.StanzaIdFilter) Session(org.jivesoftware.smack.packet.Session) SSLSession(javax.net.ssl.SSLSession)

Example 2 with Session

use of org.jivesoftware.smack.packet.Session in project ecf by eclipse.

the class SASLAuthentication method bindResourceAndEstablishSession.

private String bindResourceAndEstablishSession(String resource) throws XMPPException {
    // Wait until server sends response containing the <bind> element
    synchronized (this) {
        if (!resourceBinded) {
            try {
                wait(30000);
            } catch (InterruptedException e) {
            // Ignore
            }
        }
    }
    if (!resourceBinded) {
        // Server never offered resource binding
        throw new XMPPException("Resource binding not offered by server");
    }
    Bind bindResource = new Bind();
    bindResource.setResource(resource);
    PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(bindResource.getPacketID()));
    // Send the packet
    connection.sendPacket(bindResource);
    // Wait up to a certain number of seconds for a response from the server.
    Bind response = (Bind) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    collector.cancel();
    if (response == null) {
        throw new XMPPException("No response from the server.");
    } else // If the server replied with an error, throw an exception.
    if (response.getType() == IQ.Type.ERROR) {
        throw new XMPPException(response.getError());
    }
    String userJID = response.getJid();
    if (sessionSupported) {
        Session session = new Session();
        collector = connection.createPacketCollector(new PacketIDFilter(session.getPacketID()));
        // Send the packet
        connection.sendPacket(session);
        // Wait up to a certain number of seconds for a response from the server.
        IQ ack = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
        collector.cancel();
        if (ack == null) {
            throw new XMPPException("No response from the server.");
        } else // If the server replied with an error, throw an exception.
        if (ack.getType() == IQ.Type.ERROR) {
            throw new XMPPException(ack.getError());
        }
    }
    return userJID;
}
Also used : Bind(org.jivesoftware.smack.packet.Bind) IQ(org.jivesoftware.smack.packet.IQ) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter) Session(org.jivesoftware.smack.packet.Session)

Aggregations

Bind (org.jivesoftware.smack.packet.Bind)2 Session (org.jivesoftware.smack.packet.Session)2 SSLSession (javax.net.ssl.SSLSession)1 ResourceBindingNotOfferedException (org.jivesoftware.smack.SmackException.ResourceBindingNotOfferedException)1 PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)1 StanzaIdFilter (org.jivesoftware.smack.filter.StanzaIdFilter)1 IQ (org.jivesoftware.smack.packet.IQ)1