Search in sources :

Example 1 with Packet

use of org.jivesoftware.smack.packet.Packet in project camel by apache.

the class XmppEndpoint method createConnection.

public synchronized XMPPConnection createConnection() throws XMPPException, SmackException, IOException {
    if (connection != null && connection.isConnected()) {
        // use existing working connection
        return connection;
    }
    // prepare for creating new connection
    connection = null;
    LOG.trace("Creating new connection ...");
    XMPPConnection newConnection = createConnectionInternal();
    newConnection.connect();
    newConnection.addPacketListener(new XmppLogger("INBOUND"), new PacketFilter() {

        public boolean accept(Packet packet) {
            return true;
        }
    });
    newConnection.addPacketSendingListener(new XmppLogger("OUTBOUND"), new PacketFilter() {

        public boolean accept(Packet packet) {
            return true;
        }
    });
    if (!newConnection.isAuthenticated()) {
        if (user != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Logging in to XMPP as user: {} on connection: {}", user, getConnectionMessage(newConnection));
            }
            if (password == null) {
                LOG.warn("No password configured for user: {} on connection: {}", user, getConnectionMessage(newConnection));
            }
            if (createAccount) {
                AccountManager accountManager = AccountManager.getInstance(newConnection);
                accountManager.createAccount(user, password);
            }
            if (login) {
                if (resource != null) {
                    newConnection.login(user, password, resource);
                } else {
                    newConnection.login(user, password);
                }
            }
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Logging in anonymously to XMPP on connection: {}", getConnectionMessage(newConnection));
            }
            newConnection.loginAnonymously();
        }
    // presence is not needed to be sent after login
    }
    // okay new connection was created successfully so assign it as the connection
    LOG.debug("Created new connection successfully: {}", newConnection);
    connection = newConnection;
    return connection;
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) AccountManager(org.jivesoftware.smack.AccountManager) XMPPConnection(org.jivesoftware.smack.XMPPConnection)

Example 2 with Packet

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

the class JingleManagerTest method testInitJingleSessionRequestListeners.

/**
     * Test for the session request detection. Here, we use the same filter we
     * use in the JingleManager...
     */
public void testInitJingleSessionRequestListeners() {
    resetCounter();
    ProviderManager.getInstance().addIQProvider("jingle", "http://jabber.org/protocol/jingle", new JingleProvider());
    PacketFilter initRequestFilter = new PacketFilter() {

        // Return true if we accept this packet
        public boolean accept(Packet pin) {
            if (pin instanceof IQ) {
                System.out.println("packet: " + pin.toXML());
                IQ iq = (IQ) pin;
                if (iq.getType().equals(IQ.Type.set)) {
                    System.out.println("packet");
                    if (iq instanceof Jingle) {
                        Jingle jin = (Jingle) pin;
                        if (jin.getAction().equals(JingleActionEnum.SESSION_INITIATE)) {
                            System.out.println("Session initiation packet accepted... ");
                            return true;
                        }
                    }
                }
            }
            return false;
        }
    };
    // Start a packet listener for session initiation requests
    getConnection(0).addAsyncPacketListener(new PacketListener() {

        public void processStanza(final Packet packet) {
            System.out.println("Packet detected... ");
            incCounter();
        }
    }, initRequestFilter);
    // Create a dummy packet for testing...
    IQfake iqSent = new IQfake(" <jingle xmlns='http://jabber.org/protocol/jingle'" + " initiator=\"user1@thiago\"" + " responder=\"user0@thiago\"" + " action=\"session-initiate\" sid=\"08666555\">" + "</jingle>");
    iqSent.setTo(getFullJID(0));
    iqSent.setFrom(getFullJID(0));
    iqSent.setType(IQ.Type.set);
    System.out.println("Sending packet and waiting... ");
    getConnection(1).sendStanza(iqSent);
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
    }
    System.out.println("Awake... " + valCounter());
    assertTrue(valCounter() > 0);
}
Also used : Jingle(org.jivesoftware.smackx.jingle.packet.Jingle) Packet(org.jivesoftware.smack.packet.Packet) DatagramPacket(java.net.DatagramPacket) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) IQ(org.jivesoftware.smack.packet.IQ) PacketListener(org.jivesoftware.smack.PacketListener) JingleProvider(org.jivesoftware.smackx.jingle.provider.JingleProvider)

Example 3 with Packet

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

the class InBandBytestreamTest method testRespondWithErrorOnInBandBytestreamRequest.

/**
     * Target should respond with not-acceptable error if no listeners for incoming In-Band
     * Bytestream requests are registered.
     * 
     * @throws XMPPException should not happen
     */
public void testRespondWithErrorOnInBandBytestreamRequest() throws XMPPException {
    XMPPConnection targetConnection = getConnection(0);
    XMPPConnection initiatorConnection = getConnection(1);
    Open open = new Open("sessionID", 1024);
    open.setFrom(initiatorConnection.getUser());
    open.setTo(targetConnection.getUser());
    StanzaCollector collector = initiatorConnection.createStanzaCollector(new PacketIDFilter(open.getStanzaId()));
    initiatorConnection.sendStanza(open);
    Packet result = collector.nextResult();
    assertNotNull(result.getError());
    assertEquals(XMPPError.Condition.no_acceptable.toString(), result.getError().getCondition());
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) XMPPConnection(org.jivesoftware.smack.XMPPConnection) StanzaCollector(org.jivesoftware.smack.StanzaCollector) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter) Open(org.jivesoftware.smackx.bytestreams.ibb.packet.Open)

Example 4 with Packet

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

the class Socks5ByteStreamTest method testRespondWithErrorOnSocks5BytestreamRequest.

/**
     * Target should respond with not-acceptable error if no listeners for incoming Socks5
     * bytestream requests are registered.
     * 
     * @throws XMPPException should not happen
     */
public void testRespondWithErrorOnSocks5BytestreamRequest() throws XMPPException {
    XMPPConnection targetConnection = getConnection(0);
    XMPPConnection initiatorConnection = getConnection(1);
    Bytestream bytestreamInitiation = Socks5PacketUtils.createBytestreamInitiation(initiatorConnection.getUser(), targetConnection.getUser(), "session_id");
    bytestreamInitiation.addStreamHost("proxy.localhost", "127.0.0.1", 7777);
    StanzaCollector collector = initiatorConnection.createStanzaCollector(new PacketIDFilter(bytestreamInitiation.getStanzaId()));
    initiatorConnection.sendStanza(bytestreamInitiation);
    Packet result = collector.nextResult();
    assertNotNull(result.getError());
    assertEquals(XMPPError.Condition.no_acceptable.toString(), result.getError().getCondition());
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) XMPPConnection(org.jivesoftware.smack.XMPPConnection) StanzaCollector(org.jivesoftware.smack.StanzaCollector) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 5 with Packet

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

the class XHTMLExtensionTest method testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage.

/**
     * Low level API test. Test a message with two XHTML bodies and several XHTML tags.
     * 1. User_1 will send a message with XHTML to user_2
     * 2. User_2 will receive the message and iterate over the XHTML bodies to check if everything
     * is fine
     * 3. User_1 will wait several seconds for an ACK from user_2, if none is received then
     * something is wrong
     */
public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() {
    // Create a chat for each connection
    Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
    final StanzaCollector chat2 = getConnection(1).createStanzaCollector(new ThreadFilter(chat1.getThreadID()));
    // Create a Listener that listens for Messages with the extension 
    //"http://jabber.org/protocol/xhtml-im"
    // This listener will listen on the conn2 and answer an ACK if everything is ok
    PacketFilter packetFilter = new StanzaExtensionFilter("html", "http://jabber.org/protocol/xhtml-im");
    PacketListener packetListener = new PacketListener() {

        @Override
        public void processStanza(Packet packet) {
        }
    };
    getConnection(1).addAsyncPacketListener(packetListener, packetFilter);
    // User1 creates a message to send to user2
    Message msg = new Message();
    msg.setSubject("Any subject you want");
    msg.setBody("awesome! As Emerson once said: A foolish consistency is the hobgoblin of little minds.");
    // Create an XHTMLExtension and add it to the message
    XHTMLExtension xhtmlExtension = new XHTMLExtension();
    xhtmlExtension.addBody("<body xml:lang=\"es-ES\"><h1>impresionante!</h1><p>Como Emerson dijo una vez:</p><blockquote><p>Una consistencia ridicula es el espantajo de mentes pequenas.</p></blockquote></body>");
    xhtmlExtension.addBody("<body xml:lang=\"en-US\"><h1>awesome!</h1><p>As Emerson once said:</p><blockquote><p>A foolish consistency is the hobgoblin of little minds.</p></blockquote></body>");
    msg.addExtension(xhtmlExtension);
    // User1 sends the message that contains the XHTML to user2
    try {
        bodiesSent = xhtmlExtension.getBodiesCount();
        bodiesReceived = 0;
        chat1.sendMessage(msg);
    } catch (Exception e) {
        fail("An error occured sending the message with XHTML");
    }
    Packet packet = chat2.nextResult(2000);
    int received = 0;
    Message message = (Message) packet;
    assertNotNull("Body is null", message.getBody());
    try {
        xhtmlExtension = (XHTMLExtension) message.getExtension("html", "http://jabber.org/protocol/xhtml-im");
        assertNotNull("Message without extension \"http://jabber.org/protocol/xhtml-im\"", xhtmlExtension);
        assertTrue("Message without XHTML bodies", xhtmlExtension.getBodiesCount() > 0);
        for (Iterator<String> it = xhtmlExtension.getBodies(); it.hasNext(); ) {
            received++;
            System.out.println(it.next());
        }
        bodiesReceived = received;
    } catch (ClassCastException e) {
        fail("ClassCastException - Most probable cause is that smack providers is " + "misconfigured");
    }
    // Wait half second so that the complete test can run
    assertEquals("Number of sent and received XHTMP bodies does not match", bodiesSent, bodiesReceived);
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) Message(org.jivesoftware.smack.packet.Message) StanzaExtensionFilter(org.jivesoftware.smack.filter.StanzaExtensionFilter) PacketListener(org.jivesoftware.smack.PacketListener) ThreadFilter(org.jivesoftware.smack.filter.ThreadFilter) Chat(org.jivesoftware.smack.Chat) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Aggregations

Packet (org.jivesoftware.smack.packet.Packet)12 StanzaCollector (org.jivesoftware.smack.StanzaCollector)5 Message (org.jivesoftware.smack.packet.Message)5 XMPPConnection (org.jivesoftware.smack.XMPPConnection)4 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)4 PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)4 PacketListener (org.jivesoftware.smack.PacketListener)3 Chat (org.jivesoftware.smack.Chat)2 ThreadFilter (org.jivesoftware.smack.filter.ThreadFilter)2 IQ (org.jivesoftware.smack.packet.IQ)2 Time (org.jivesoftware.smackx.packet.Time)2 DatagramPacket (java.net.DatagramPacket)1 AbstractConnectionListener (org.jivesoftware.smack.AbstractConnectionListener)1 AccountManager (org.jivesoftware.smack.AccountManager)1 ConnectionConfiguration (org.jivesoftware.smack.ConnectionConfiguration)1 PacketCollector (org.jivesoftware.smack.PacketCollector)1 AndFilter (org.jivesoftware.smack.filter.AndFilter)1 FromMatchesFilter (org.jivesoftware.smack.filter.FromMatchesFilter)1 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)1 PacketTypeFilter (org.jivesoftware.smack.filter.PacketTypeFilter)1