Search in sources :

Example 6 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 7 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 8 with Packet

use of org.jivesoftware.smack.packet.Packet in project asmack by Flowdalic.

the class AndroidDebugger method createDebug.

/**
     * Creates the listeners that will print in the console when new activity is detected.
     */
private void createDebug() {
    // Create a special Reader that wraps the main Reader and logs data to the GUI.
    ObservableReader debugReader = new ObservableReader(reader);
    readerListener = new ReaderListener() {

        public void read(String str) {
            Log.d("SMACK", "RCV (" + connection.getConnectionCounter() + "): " + str);
        }
    };
    debugReader.addReaderListener(readerListener);
    // Create a special Writer that wraps the main Writer and logs data to the GUI.
    ObservableWriter debugWriter = new ObservableWriter(writer);
    writerListener = new WriterListener() {

        public void write(String str) {
            Log.d("SMACK", "SENT (" + connection.getConnectionCounter() + "): " + str);
        }
    };
    debugWriter.addWriterListener(writerListener);
    // Assign the reader/writer objects to use the debug versions. The packet reader
    // and writer will use the debug versions when they are created.
    reader = debugReader;
    writer = debugWriter;
    // Create a thread that will listen for all incoming packets and write them to
    // the GUI. This is what we call "interpreted" packet data, since it's the packet
    // data as Smack sees it and not as it's coming in as raw XML.
    listener = new PacketListener() {

        public void processPacket(Packet packet) {
            if (printInterpreted) {
                Log.d("SMACK", "RCV PKT (" + connection.getConnectionCounter() + "): " + packet.toXML());
            }
        }
    };
    connListener = new AbstractConnectionListener() {

        public void connectionClosed() {
            Log.d("SMACK", "Connection closed (" + connection.getConnectionCounter() + ")");
        }

        public void connectionClosedOnError(Exception e) {
            Log.d("SMACK", "Connection closed due to an exception (" + connection.getConnectionCounter() + ")");
        }

        public void reconnectionFailed(Exception e) {
            Log.d("SMACK", "Reconnection failed due to an exception (" + connection.getConnectionCounter() + ")");
        }

        public void reconnectionSuccessful() {
            Log.d("SMACK", "Connection reconnected (" + connection.getConnectionCounter() + ")");
        }

        public void reconnectingIn(int seconds) {
            Log.d("SMACK", "Connection (" + connection.getConnectionCounter() + ") will reconnect in " + seconds);
        }
    };
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) AbstractConnectionListener(org.jivesoftware.smack.AbstractConnectionListener) PacketListener(org.jivesoftware.smack.PacketListener)

Example 9 with Packet

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

the class MultipleRecipientManagerTest method testSending.

/**
     * Ensures that sending and receiving of packets is ok.
     */
public void testSending() throws XMPPException {
    StanzaCollector collector1 = getConnection(1).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
    StanzaCollector collector2 = getConnection(2).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
    StanzaCollector collector3 = getConnection(3).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
    Message message = new Message();
    message.setBody("Hola");
    List<String> to = Arrays.asList(new String[] { getBareJID(1) });
    List<String> cc = Arrays.asList(new String[] { getBareJID(2) });
    List<String> bcc = Arrays.asList(new String[] { getBareJID(3) });
    MultipleRecipientManager.send(getConnection(0), message, to, cc, bcc);
    Packet message1 = collector1.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection 1 never received the message", message1);
    MultipleRecipientInfo info1 = MultipleRecipientManager.getMultipleRecipientInfo(message1);
    assertNotNull("Message 1 does not contain MultipleRecipientInfo", info1);
    assertFalse("Message 1 should be 'replyable'", info1.shouldNotReply());
    List<?> addresses1 = info1.getTOAddresses();
    assertEquals("Incorrect number of TO addresses", 1, addresses1.size());
    String address1 = ((MultipleAddresses.Address) addresses1.get(0)).getJid();
    assertEquals("Incorrect TO address", getBareJID(1), address1);
    addresses1 = info1.getCCAddresses();
    assertEquals("Incorrect number of CC addresses", 1, addresses1.size());
    address1 = ((MultipleAddresses.Address) addresses1.get(0)).getJid();
    assertEquals("Incorrect CC address", getBareJID(2), address1);
    Packet message2 = collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection 2 never received the message", message2);
    MultipleRecipientInfo info2 = MultipleRecipientManager.getMultipleRecipientInfo(message2);
    assertNotNull("Message 2 does not contain MultipleRecipientInfo", info2);
    assertFalse("Message 2 should be 'replyable'", info2.shouldNotReply());
    List<MultipleAddresses.Address> addresses2 = info2.getTOAddresses();
    assertEquals("Incorrect number of TO addresses", 1, addresses2.size());
    String address2 = ((MultipleAddresses.Address) addresses2.get(0)).getJid();
    assertEquals("Incorrect TO address", getBareJID(1), address2);
    addresses2 = info2.getCCAddresses();
    assertEquals("Incorrect number of CC addresses", 1, addresses2.size());
    address2 = ((MultipleAddresses.Address) addresses2.get(0)).getJid();
    assertEquals("Incorrect CC address", getBareJID(2), address2);
    Packet message3 = collector3.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection 3 never received the message", message3);
    MultipleRecipientInfo info3 = MultipleRecipientManager.getMultipleRecipientInfo(message3);
    assertNotNull("Message 3 does not contain MultipleRecipientInfo", info3);
    assertFalse("Message 3 should be 'replyable'", info3.shouldNotReply());
    List<MultipleAddresses.Address> addresses3 = info3.getTOAddresses();
    assertEquals("Incorrect number of TO addresses", 1, addresses3.size());
    String address3 = ((MultipleAddresses.Address) addresses3.get(0)).getJid();
    assertEquals("Incorrect TO address", getBareJID(1), address3);
    addresses3 = info3.getCCAddresses();
    assertEquals("Incorrect number of CC addresses", 1, addresses3.size());
    address3 = ((MultipleAddresses.Address) addresses3.get(0)).getJid();
    assertEquals("Incorrect CC address", getBareJID(2), address3);
    collector1.cancel();
    collector2.cancel();
    collector3.cancel();
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Packet(org.jivesoftware.smack.packet.Packet) MultipleAddresses(org.jivesoftware.smackx.packet.MultipleAddresses) Message(org.jivesoftware.smack.packet.Message) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 10 with Packet

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

the class XHTMLExtensionTest method testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage.

/**
     * Low level API test.
     * 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 testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() {
    // 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()));
    // User1 creates a message to send to user2
    Message msg = new Message();
    msg.setSubject("Any subject you want");
    msg.setBody("Hey John, this is my new green!!!!");
    // Create a XHTMLExtension Package and add it to the message
    XHTMLExtension xhtmlExtension = new XHTMLExtension();
    xhtmlExtension.addBody("<body><p style='font-size:large'>Hey John, this is my new <span style='color:green'>green</span><em>!!!!</em></p></body>");
    msg.addExtension(xhtmlExtension);
    // User1 sends the message that contains the XHTML to user2
    try {
        chat1.sendMessage(msg);
    } catch (Exception e) {
        fail("An error occured sending the message with XHTML");
    }
    Packet packet = chat2.nextResult(2000);
    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(); ) {
            String body = it.next();
            System.out.println(body);
        }
    } catch (ClassCastException e) {
        fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
    }
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) Message(org.jivesoftware.smack.packet.Message) 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