Search in sources :

Example 41 with StanzaListener

use of org.jivesoftware.smack.StanzaListener in project Smack by igniterealtime.

the class InBandBytestreamSessionMessageTest method shouldReadAllReceivedData2.

/**
 * Test the input stream read() method.
 *
 * @throws Exception should not happen
 */
@Test
public void shouldReadAllReceivedData2() throws Exception {
    // create random data
    Random rand = new Random();
    byte[] controlData = new byte[3 * blockSize];
    rand.nextBytes(controlData);
    // get IBB sessions data packet listener
    InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream, initiatorJID);
    InputStream inputStream = session.getInputStream();
    StanzaListener listener = Whitebox.getInternalState(inputStream, "dataPacketListener", StanzaListener.class);
    // verify data packet and notify listener
    for (int i = 0; i < controlData.length / blockSize; i++) {
        String base64Data = Base64.encodeToString(controlData, i * blockSize, blockSize);
        DataPacketExtension dpe = new DataPacketExtension(sessionID, i, base64Data);
        Message dataMessage = StanzaBuilder.buildMessage().addExtension(dpe).build();
        listener.processStanza(dataMessage);
    }
    // read data
    byte[] bytes = new byte[3 * blockSize];
    for (int i = 0; i < bytes.length; i++) {
        bytes[i] = (byte) inputStream.read();
    }
    // verify data
    for (int i = 0; i < bytes.length; i++) {
        assertEquals(controlData[i], bytes[i]);
    }
    protocol.verifyAll();
}
Also used : DataPacketExtension(org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension) Random(java.util.Random) Message(org.jivesoftware.smack.packet.Message) InputStream(java.io.InputStream) StanzaListener(org.jivesoftware.smack.StanzaListener) Test(org.junit.jupiter.api.Test)

Example 42 with StanzaListener

use of org.jivesoftware.smack.StanzaListener in project Smack by igniterealtime.

the class InBandBytestreamSessionTest method shouldReplyWithErrorIfAlreadyUsedSequenceIsReceived.

/**
 * If the data stanza has a sequence that is already used an 'unexpected-request' error should
 * be returned. See XEP-0047 Section 2.2.
 *
 * @throws Exception should not happen
 */
@Test
public void shouldReplyWithErrorIfAlreadyUsedSequenceIsReceived() throws Exception {
    // verify reply to first valid data packet is of type RESULT
    protocol.addResponse(null, Verification.requestTypeRESULT);
    // verify reply to invalid data packet is an error
    protocol.addResponse(null, Verification.requestTypeERROR, new Verification<IQ, IQ>() {

        @Override
        public void verify(IQ request, IQ response) {
            assertEquals(StanzaError.Condition.unexpected_request, request.getError().getCondition());
        }
    });
    // get IBB sessions data packet listener
    InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream, initiatorJID);
    InputStream inputStream = session.getInputStream();
    StanzaListener listener = Whitebox.getInternalState(inputStream, "dataPacketListener", StanzaListener.class);
    // build data packets
    String base64Data = Base64.encode("Data");
    DataPacketExtension dpe = new DataPacketExtension(sessionID, 0, base64Data);
    Data data1 = new Data(dpe);
    Data data2 = new Data(dpe);
    // notify listener
    listener.processStanza(data1);
    listener.processStanza(data2);
    protocol.verifyAll();
}
Also used : DataPacketExtension(org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension) InputStream(java.io.InputStream) IQ(org.jivesoftware.smack.packet.IQ) StanzaListener(org.jivesoftware.smack.StanzaListener) Data(org.jivesoftware.smackx.bytestreams.ibb.packet.Data) Test(org.junit.jupiter.api.Test)

Example 43 with StanzaListener

use of org.jivesoftware.smack.StanzaListener in project Smack by igniterealtime.

the class Node method addConfigurationListener.

/**
 * Register a listener for configuration events.  This listener
 * will get called whenever the node's configuration changes.
 *
 * @param listener The handler for the event
 */
public void addConfigurationListener(NodeConfigListener listener) {
    StanzaListener conListener = new NodeConfigTranslator(listener);
    configEventToListenerMap.put(listener, conListener);
    pubSubManager.getConnection().addSyncStanzaListener(conListener, new EventContentFilter(EventElementType.configuration.toString()));
}
Also used : StanzaListener(org.jivesoftware.smack.StanzaListener)

Example 44 with StanzaListener

use of org.jivesoftware.smack.StanzaListener in project Smack by igniterealtime.

the class JingleSession method updatePacketListener.

/**
 * Install the stanza listener. The listener is responsible for responding
 * to any stanza that we receive...
 */
void updatePacketListener() {
    removeAsyncPacketListener();
    LOGGER.fine("UpdatePacketListener");
    packetListener = new StanzaListener() {

        @Override
        public void processStanza(Stanza packet) {
            try {
                receivePacketAndRespond((IQ) packet);
            } catch (Exception e) {
                LOGGER.log(Level.WARNING, "exception", e);
            }
        }
    };
    packetFilter = new StanzaFilter() {

        @Override
        public boolean accept(Stanza packet) {
            if (packet instanceof IQ) {
                IQ iq = (IQ) packet;
                Jid me = getConnection().getUser();
                if (!iq.getTo().equals(me)) {
                    return false;
                }
                Jid other = getResponder().equals(me) ? getInitiator() : getResponder();
                if (iq.getFrom() == null || !iq.getFrom().equals(other == null ? "" : other)) {
                    return false;
                }
                if (iq instanceof Jingle) {
                    Jingle jin = (Jingle) iq;
                    String sid = jin.getSid();
                    if (sid == null || !sid.equals(getSid())) {
                        LOGGER.fine("Ignored Jingle(SID) " + sid + "|" + getSid() + " :" + iq.toXML());
                        return false;
                    }
                    Jid ini = jin.getInitiator();
                    if (!ini.equals(getInitiator())) {
                        LOGGER.fine("Ignored Jingle(INI): " + iq.toXML());
                        return false;
                    }
                } else {
                    // We accept some non-Jingle IQ packets: ERRORs and ACKs
                    if (iq.getType().equals(IQ.Type.set)) {
                        LOGGER.fine("Ignored Jingle(TYPE): " + iq.toXML());
                        return false;
                    } else if (iq.getType().equals(IQ.Type.get)) {
                        LOGGER.fine("Ignored Jingle(TYPE): " + iq.toXML());
                        return false;
                    }
                }
                return true;
            }
            return false;
        }
    };
    getConnection().addAsyncStanzaListener(packetListener, packetFilter);
}
Also used : Jingle(org.jivesoftware.smackx.jingleold.packet.Jingle) StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) Jid(org.jxmpp.jid.Jid) Stanza(org.jivesoftware.smack.packet.Stanza) IQ(org.jivesoftware.smack.packet.IQ) StanzaListener(org.jivesoftware.smack.StanzaListener) SmackException(org.jivesoftware.smack.SmackException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) XMPPException(org.jivesoftware.smack.XMPPException)

Example 45 with StanzaListener

use of org.jivesoftware.smack.StanzaListener in project Smack by igniterealtime.

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;
    streamFeaturesAfterAuthenticationReceived = false;
    authenticate(username, password, config.getAuthzid(), sslSession);
    // Wait for stream features after the authentication.
    // TODO: The name of this synchronization point "maybeCompressFeaturesReceived" is not perfect. It should be
    // renamed to "streamFeaturesAfterAuthenticationReceived".
    waitForConditionOrThrowConnectionException(() -> streamFeaturesAfterAuthenticationReceived, "compress features from server");
    // If compression is enabled then request the server to use stream compression. XEP-170
    // recommends to perform stream compression before resource binding.
    maybeEnableCompression();
    smResumedSyncPoint = SyncPointState.initial;
    smResumptionFailed = null;
    if (isSmResumptionPossible()) {
        smResumedSyncPoint = SyncPointState.request_sent;
        sendNonza(new Resume(clientHandledStanzasCount, smSessionId));
        waitForConditionOrConnectionException(() -> smResumedSyncPoint == SyncPointState.successful || smResumptionFailed != null, "resume previous stream");
        if (smResumedSyncPoint == SyncPointState.successful) {
            // We successfully resumed the stream, be done here
            afterSuccessfulLogin(true);
            return;
        }
        // normal resource binding can be tried.
        assert smResumptionFailed != null;
        LOGGER.fine("Stream resumption failed, continuing with normal stream establishment process: " + smResumptionFailed);
    }
    // We either failed to resume a previous stream management (SM) session, or we did not even try. In any case,
    // mark SM as not enabled. Most importantly, we do this prior calling bindResourceAndEstablishSession(), as the
    // bind IQ may trigger a SM ack request, which would be invalid in the pre resource bound state.
    smEnabledSyncPoint = false;
    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();
    }
    // 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;
        sendNonza(new Enable(useSmResumption, smClientMaxResumptionTime));
        // 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.
        waitForConditionOrThrowConnectionException(() -> smEnabledSyncPoint, "enabling stream mangement");
        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());
            }
        }
    }
    // before it is informed about connection success
    if (!stanzaDroppedListeners.isEmpty()) {
        for (Stanza stanza : previouslyUnackedStanzas) {
            for (StanzaListener listener : stanzaDroppedListeners) {
                try {
                    listener.processStanza(stanza);
                } catch (InterruptedException | NotConnectedException | NotLoggedInException e) {
                    LOGGER.log(Level.FINER, "StanzaDroppedListener received exception", e);
                }
            }
        }
    } else {
        for (Stanza stanza : previouslyUnackedStanzas) {
            sendStanzaInternal(stanza);
        }
    }
    afterSuccessfulLogin(false);
}
Also used : NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) Stanza(org.jivesoftware.smack.packet.Stanza) SSLSession(javax.net.ssl.SSLSession) Enable(org.jivesoftware.smack.sm.packet.StreamManagement.Enable) StanzaListener(org.jivesoftware.smack.StanzaListener) NotLoggedInException(org.jivesoftware.smack.SmackException.NotLoggedInException) LinkedList(java.util.LinkedList) Resume(org.jivesoftware.smack.sm.packet.StreamManagement.Resume)

Aggregations

StanzaListener (org.jivesoftware.smack.StanzaListener)46 Stanza (org.jivesoftware.smack.packet.Stanza)24 InputStream (java.io.InputStream)12 Message (org.jivesoftware.smack.packet.Message)12 DataPacketExtension (org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension)12 Test (org.junit.jupiter.api.Test)12 IQ (org.jivesoftware.smack.packet.IQ)10 SmackException (org.jivesoftware.smack.SmackException)9 Data (org.jivesoftware.smackx.bytestreams.ibb.packet.Data)9 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)8 IOException (java.io.IOException)7 XMPPConnection (org.jivesoftware.smack.XMPPConnection)6 XMPPException (org.jivesoftware.smack.XMPPException)6 ArrayList (java.util.ArrayList)5 Random (java.util.Random)5 ActionEvent (java.awt.event.ActionEvent)4 List (java.util.List)4 SmackIntegrationTest (org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)4 SimpleResultSyncPoint (org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint)4 StanzaFilter (org.jivesoftware.smack.filter.StanzaFilter)4