Search in sources :

Example 36 with PacketFilter

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

the class SyncPacketSend method getReply.

public static Packet getReply(Connection connection, Packet packet, long timeout) throws XMPPException {
    PacketFilter responseFilter = new PacketIDFilter(packet.getPacketID());
    PacketCollector response = connection.createPacketCollector(responseFilter);
    connection.sendPacket(packet);
    // Wait up to a certain number of seconds for a reply.
    Packet result = response.nextResult(timeout);
    // Stop queuing results
    response.cancel();
    if (result == null) {
        throw new XMPPException(SmackError.NO_RESPONSE_FROM_SERVER);
    } else if (result.getError() != null) {
        throw new XMPPException(result.getError());
    }
    return result;
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) PacketCollector(org.jivesoftware.smack.PacketCollector) XMPPException(org.jivesoftware.smack.XMPPException) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 37 with PacketFilter

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

the class SyncPacketSend method getReply.

public static Packet getReply(Connection connection, Packet packet, long timeout) throws XMPPException {
    PacketFilter responseFilter = new PacketIDFilter(packet.getPacketID());
    PacketCollector response = connection.createPacketCollector(responseFilter);
    connection.sendPacket(packet);
    // Wait up to a certain number of seconds for a reply.
    Packet result = response.nextResult(timeout);
    // Stop queuing results
    response.cancel();
    if (result == null) {
        throw new XMPPException("No response from server.");
    } else if (result.getError() != null) {
        throw new XMPPException(result.getError());
    }
    return result;
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) PacketCollector(org.jivesoftware.smack.PacketCollector) XMPPException(org.jivesoftware.smack.XMPPException) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 38 with PacketFilter

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

the class IQTest method testInvalidNamespace.

/**
 * Check that the server responds a 503 error code when the client sends an IQ stanza with an
 * invalid namespace.
 */
public void testInvalidNamespace() {
    IQ iq = new IQ() {

        public String getChildElementXML() {
            StringBuilder buf = new StringBuilder();
            buf.append("<query xmlns=\"jabber:iq:anything\">");
            buf.append("</query>");
            return buf.toString();
        }
    };
    PacketFilter filter = new AndFilter(new PacketIDFilter(iq.getStanzaId()), new StanzaTypeFilter(IQ.class));
    StanzaCollector collector = getConnection(0).createStanzaCollector(filter);
    // Send the iq packet with an invalid namespace
    getConnection(0).sendStanza(iq);
    IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // Stop queuing results
    collector.cancel();
    if (result == null) {
        fail("No response from server");
    } else if (result.getType() != IQ.Type.error) {
        fail("The server didn't reply with an error packet");
    } else {
        assertEquals("Server answered an incorrect error code", 503, result.getError().getCode());
    }
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) IQ(org.jivesoftware.smack.packet.IQ) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 39 with PacketFilter

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

the class PacketReaderTest method testFiltersRemotion.

/**
 * Tests that PacketReader adds new listeners and also removes them correctly.
 */
public void testFiltersRemotion() {
    resetCounter();
    int repeat = 10;
    for (int j = 0; j < repeat; j++) {
        PacketListener listener0 = new PacketListener() {

            public void processStanza(Packet packet) {
                System.out.println("Packet Captured");
                incCounter();
            }
        };
        PacketFilter pf0 = new PacketFilter() {

            public boolean accept(Packet packet) {
                System.out.println("Packet Filtered");
                incCounter();
                return true;
            }
        };
        PacketListener listener1 = new PacketListener() {

            public void processStanza(Packet packet) {
                System.out.println("Packet Captured");
                incCounter();
            }
        };
        PacketFilter pf1 = new PacketFilter() {

            public boolean accept(Packet packet) {
                System.out.println("Packet Filtered");
                incCounter();
                return true;
            }
        };
        getConnection(0).addAsyncPacketListener(listener0, pf0);
        getConnection(1).addAsyncPacketListener(listener1, pf1);
        // Check that the listener was added
        Message msg0 = new Message(getConnection(0).getUser(), Message.Type.normal);
        Message msg1 = new Message(getConnection(1).getUser(), Message.Type.normal);
        for (int i = 0; i < 5; i++) {
            getConnection(1).sendStanza(msg0);
            getConnection(0).sendStanza(msg1);
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // Remove the listener
        getConnection(0).removeAsyncPacketListener(listener0);
        getConnection(1).removeAsyncPacketListener(listener1);
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < 10; i++) {
            getConnection(0).sendStanza(msg1);
            getConnection(1).sendStanza(msg0);
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    System.out.println(valCounter());
    assertEquals(valCounter(), repeat * 2 * 10);
}
Also used : PacketFilter(org.jivesoftware.smack.filter.PacketFilter)

Example 40 with PacketFilter

use of org.jivesoftware.smack.filter.PacketFilter in project wso2-axis2-transports by wso2.

the class XMPPSender method sendMessage.

/**
 * Send the given message over XMPP transport
 *
 * @param msgCtx the axis2 message context
 * @throws AxisFault on error
 */
public void sendMessage(MessageContext msgCtx, String targetAddress, OutTransportInfo outTransportInfo) throws AxisFault {
    XMPPConnection xmppConnection = null;
    XMPPOutTransportInfo xmppOutTransportInfo = null;
    XMPPConnectionFactory connectionFactory;
    // if on client side,create connection to xmpp server
    if (msgCtx.isServerSide()) {
        xmppOutTransportInfo = (XMPPOutTransportInfo) msgCtx.getProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO);
        connectionFactory = xmppOutTransportInfo.getConnectionFactory();
    } else {
        getConnectionDetailsFromClientOptions(msgCtx);
        connectionFactory = defaultConnectionFactory;
    }
    synchronized (this) {
        xmppConnection = connectionFactory.getXmppConnection();
        if (xmppConnection == null) {
            connectionFactory.connect(serverCredentials);
            xmppConnection = connectionFactory.getXmppConnection();
        }
    }
    Message message = new Message();
    Options options = msgCtx.getOptions();
    String serviceName = XMPPUtils.getServiceName(targetAddress);
    SOAPVersion version = msgCtx.getEnvelope().getVersion();
    if (version instanceof SOAP12Version) {
        message.setProperty(XMPPConstants.CONTENT_TYPE, HTTPConstants.MEDIA_TYPE_APPLICATION_SOAP_XML + "; action=" + msgCtx.getSoapAction());
    } else {
        message.setProperty(XMPPConstants.CONTENT_TYPE, HTTPConstants.MEDIA_TYPE_TEXT_XML);
    }
    if (targetAddress != null) {
        xmppOutTransportInfo = new XMPPOutTransportInfo(targetAddress);
        xmppOutTransportInfo.setConnectionFactory(defaultConnectionFactory);
    } else if (msgCtx.getTo() != null && !msgCtx.getTo().hasAnonymousAddress()) {
    // TODO
    } else if (msgCtx.isServerSide()) {
        xmppOutTransportInfo = (XMPPOutTransportInfo) msgCtx.getProperty(Constants.OUT_TRANSPORT_INFO);
    }
    try {
        if (msgCtx.isServerSide()) {
            message.setProperty(XMPPConstants.IS_SERVER_SIDE, new Boolean(false));
            message.setProperty(XMPPConstants.IN_REPLY_TO, xmppOutTransportInfo.getInReplyTo());
            message.setProperty(XMPPConstants.SEQUENCE_ID, xmppOutTransportInfo.getSequenceID());
        } else {
            // message is going to be processed on server side
            message.setProperty(XMPPConstants.IS_SERVER_SIDE, new Boolean(true));
            // we are sending a soap envelope as a message
            message.setProperty(XMPPConstants.CONTAINS_SOAP_ENVELOPE, new Boolean(true));
            message.setProperty(XMPPConstants.SERVICE_NAME, serviceName);
            String action = options.getAction();
            if (action == null) {
                AxisOperation axisOperation = msgCtx.getAxisOperation();
                if (axisOperation != null) {
                    action = axisOperation.getSoapAction();
                }
            }
            if (action != null) {
                message.setProperty(XMPPConstants.ACTION, action);
            }
        }
        if (xmppConnection == null) {
            handleException("Connection to XMPP Server is not established.");
        }
        // initialize the chat manager using connection
        ChatManager chatManager = xmppConnection.getChatManager();
        Chat chat = chatManager.createChat(xmppOutTransportInfo.getDestinationAccount(), null);
        boolean waitForResponse = msgCtx.getOperationContext() != null && WSDL2Constants.MEP_URI_OUT_IN.equals(msgCtx.getOperationContext().getAxisOperation().getMessageExchangePattern());
        OMElement msgElement;
        String messageToBeSent = "";
        if (XMPPConstants.XMPP_CONTENT_TYPE_STRING.equals(xmppOutTransportInfo.getContentType())) {
            // if request is received from a chat client, whole soap envelope
            // should not be sent.
            OMElement soapBodyEle = msgCtx.getEnvelope().getBody();
            OMElement responseEle = soapBodyEle.getFirstElement();
            if (responseEle != null) {
                msgElement = responseEle.getFirstElement();
            } else {
                msgElement = responseEle;
            }
        } else {
            // if request received from a ws client whole soap envelope
            // must be sent.
            msgElement = msgCtx.getEnvelope();
        }
        messageToBeSent = msgElement.toString();
        message.setBody(messageToBeSent);
        String key = null;
        if (waitForResponse && !msgCtx.isServerSide()) {
            PacketFilter filter = new PacketTypeFilter(message.getClass());
            xmppConnection.addPacketListener(xmppClientSidePacketListener, filter);
            key = UUID.randomUUID().toString();
            xmppClientSidePacketListener.listenForResponse(key, msgCtx);
            message.setProperty(XMPPConstants.SEQUENCE_ID, key);
        }
        chat.sendMessage(message);
        log.debug("Sent message :" + message.toXML());
        // Is this the best way to do this?
        if (waitForResponse && !msgCtx.isServerSide()) {
            xmppClientSidePacketListener.waitFor(key);
            // xmppConnection.disconnect();
            log.debug("Received response sucessfully");
        }
    } catch (XMPPException e) {
        log.error("Error occurred while sending the message : " + message.toXML(), e);
        handleException("Error occurred while sending the message : " + message.toXML(), e);
    } catch (InterruptedException e) {
        log.error("Error occurred while sending the message : " + message.toXML(), e);
        handleException("Error occurred while sending the message : " + message.toXML(), e);
    } finally {
    // if(xmppConnection != null && !msgCtx.isServerSide()){
    // xmppConnection.disconnect();
    // }
    }
}
Also used : Options(org.apache.axis2.client.Options) XMPPConnectionFactory(org.apache.axis2.transport.xmpp.util.XMPPConnectionFactory) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) AxisMessage(org.apache.axis2.description.AxisMessage) Message(org.jivesoftware.smack.packet.Message) AxisOperation(org.apache.axis2.description.AxisOperation) OMElement(org.apache.axiom.om.OMElement) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) XMPPConnection(org.jivesoftware.smack.XMPPConnection) XMPPOutTransportInfo(org.apache.axis2.transport.xmpp.util.XMPPOutTransportInfo) SOAPVersion(org.apache.axiom.soap.SOAPVersion) Chat(org.jivesoftware.smack.Chat) XMPPException(org.jivesoftware.smack.XMPPException) ChatManager(org.jivesoftware.smack.ChatManager) SOAP12Version(org.apache.axiom.soap.SOAP12Version)

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