Search in sources :

Example 16 with PacketFilter

use of org.jivesoftware.smack.filter.PacketFilter in project Smack by igniterealtime.

the class GroupChatInvitationTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    // Register listener for groupchat invitations.
    PacketFilter filter = new StanzaExtensionFilter("x", "jabber:x:conference");
    collector = getConnection(1).createStanzaCollector(filter);
}
Also used : PacketFilter(org.jivesoftware.smack.filter.PacketFilter) StanzaExtensionFilter(org.jivesoftware.smack.filter.StanzaExtensionFilter)

Example 17 with PacketFilter

use of org.jivesoftware.smack.filter.PacketFilter 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 occurred 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)

Example 18 with PacketFilter

use of org.jivesoftware.smack.filter.PacketFilter 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 19 with PacketFilter

use of org.jivesoftware.smack.filter.PacketFilter in project Smack by igniterealtime.

the class JingleProviderTest method testParseIQSimple.

/**
 * Test for parsing a Jingle
 */
public void testParseIQSimple() {
    // Create a dummy packet for testing...
    IQfake iqSent = new IQfake(" <jingle xmlns='urn:xmpp:tmp:jingle'" + " initiator=\"gorrino@viejo.com\"" + " responder=\"colico@hepatico.com\"" + " action=\"transport-info\" sid=\"\">" + " <transport xmlns='urn:xmpp:tmp:jingle:transports:ice-udp'>" + " <candidate generation=\"1\"" + " ip=\"192.168.1.1\"" + " password=\"secret\"" + " port=\"8080\"" + " username=\"username\"" + " preference=\"1\"/>" + " </transport>" + "</jingle>");
    iqSent.setTo(getFullJID(0));
    iqSent.setFrom(getFullJID(0));
    iqSent.setType(IQ.Type.get);
    // Create a filter and a collector...
    PacketFilter filter = new StanzaTypeFilter(IQ.class);
    StanzaCollector collector = getConnection(0).createStanzaCollector(filter);
    System.out.println("Testing if a Jingle IQ can be sent and received...");
    // Send the iq packet with an invalid namespace
    getConnection(0).sendStanza(iqSent);
    // Receive the packet
    IQ iqReceived = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // Stop queuing results
    collector.cancel();
    if (iqReceived == null) {
        fail("No response from server");
    } else if (iqReceived.getType() == IQ.Type.error) {
        fail("The server did reply with an error packet: " + iqReceived.getError().getCode());
    } else {
        assertTrue(iqReceived instanceof Jingle);
        Jingle jin = (Jingle) iqReceived;
        System.out.println("Sent:     " + iqSent.toXML());
        System.out.println("Received: " + jin.toXML());
    }
}
Also used : Jingle(org.jivesoftware.smackx.jingle.packet.Jingle) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) IQ(org.jivesoftware.smack.packet.IQ) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 20 with PacketFilter

use of org.jivesoftware.smack.filter.PacketFilter in project ecf by eclipse.

the class AdHocCommandManager method init.

/**
 * <ul>
 * <li>Adds listeners to the connection</li>
 * <li>Registers the ad-hoc command feature to the ServiceDiscoveryManager</li>
 * <li>Registers the items of the feature</li>
 * <li>Adds packet listeners to handle execution requests</li>
 * <li>Creates and start the session sweeper</li>
 * </ul>
 */
private void init() {
    // Register the new instance and associate it with the connection
    instances.put(connection, this);
    // Add a listener to the connection that removes the registered instance
    // when the connection is closed
    connection.addConnectionListener(new ConnectionListener() {

        public void connectionClosed() {
            // Unregister this instance since the connection has been closed
            instances.remove(connection);
        }

        public void connectionClosedOnError(Exception e) {
            // Unregister this instance since the connection has been closed
            instances.remove(connection);
        }

        public void reconnectionSuccessful() {
            // Register this instance since the connection has been
            // reestablished
            instances.put(connection, AdHocCommandManager.this);
        }

        public void reconnectingIn(int seconds) {
        // Nothing to do
        }

        public void reconnectionFailed(Exception e) {
        // Nothing to do
        }
    });
    // Add the feature to the service discovery manage to show that this
    // connection supports the AdHoc-Commands protocol.
    // This information will be used when another client tries to
    // discover whether this client supports AdHoc-Commands or not.
    ServiceDiscoveryManager.getInstanceFor(connection).addFeature(DISCO_NAMESPACE);
    // Set the NodeInformationProvider that will provide information about
    // which AdHoc-Commands are registered, whenever a disco request is
    // received
    ServiceDiscoveryManager.getInstanceFor(connection).setNodeInformationProvider(discoNode, new NodeInformationProvider() {

        public List<DiscoverItems.Item> getNodeItems() {
            List<DiscoverItems.Item> answer = new ArrayList<DiscoverItems.Item>();
            Collection<AdHocCommandInfo> commandsList = getRegisteredCommands();
            for (AdHocCommandInfo info : commandsList) {
                DiscoverItems.Item item = new DiscoverItems.Item(info.getOwnerJID());
                item.setName(info.getName());
                item.setNode(info.getNode());
                answer.add(item);
            }
            return answer;
        }

        public List<String> getNodeFeatures() {
            return null;
        }

        public List<Identity> getNodeIdentities() {
            return null;
        }

        public List<PacketExtension> getNodePacketExtensions() {
            return null;
        }
    });
    // The packet listener and the filter for processing some AdHoc Commands
    // Packets
    PacketListener listener = new PacketListener() {

        public void processPacket(Packet packet) {
            AdHocCommandData requestData = (AdHocCommandData) packet;
            processAdHocCommand(requestData);
        }
    };
    PacketFilter filter = new PacketTypeFilter(AdHocCommandData.class);
    connection.addPacketListener(listener, filter);
    sessionsSweeper = null;
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) NodeInformationProvider(org.jivesoftware.smackx.NodeInformationProvider) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) DiscoverItems(org.jivesoftware.smackx.packet.DiscoverItems) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) AdHocCommandData(org.jivesoftware.smackx.packet.AdHocCommandData) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

PacketFilter (org.jivesoftware.smack.filter.PacketFilter)40 XMPPException (org.jivesoftware.smack.XMPPException)23 PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)22 PacketCollector (org.jivesoftware.smack.PacketCollector)21 IQ (org.jivesoftware.smack.packet.IQ)18 PacketTypeFilter (org.jivesoftware.smack.filter.PacketTypeFilter)15 AndFilter (org.jivesoftware.smack.filter.AndFilter)13 Packet (org.jivesoftware.smack.packet.Packet)13 Registration (org.jivesoftware.smack.packet.Registration)6 MUCAdmin (org.jivesoftware.smackx.packet.MUCAdmin)6 MUCOwner (org.jivesoftware.smackx.packet.MUCOwner)6 ArrayList (java.util.ArrayList)5 PacketListener (org.jivesoftware.smack.PacketListener)5 FromMatchesFilter (org.jivesoftware.smack.filter.FromMatchesFilter)5 Presence (org.jivesoftware.smack.packet.Presence)5 PacketInterceptor (org.jivesoftware.smack.PacketInterceptor)4 XMPPConnection (org.jivesoftware.smack.XMPPConnection)4 Message (org.jivesoftware.smack.packet.Message)4 MUCInitialPresence (org.jivesoftware.smackx.packet.MUCInitialPresence)4 HashMap (java.util.HashMap)2