Search in sources :

Example 1 with XMPPConnectionFactory

use of org.apache.axis2.transport.xmpp.util.XMPPConnectionFactory in project wso2-axis2-transports by wso2.

the class XMPPSender method init.

/**
 * Initialize the transport sender by reading pre-defined connection factories for
 * outgoing messages. These will create sessions (one per each destination dealt with)
 * to be used when messages are being sent.
 * @param confContext the configuration context
 * @param transportOut the transport sender definition from axis2.xml
 * @throws AxisFault on error
 */
public void init(ConfigurationContext confContext, TransportOutDescription transportOut) throws AxisFault {
    // if connection details are available from axis configuration
    // use those & connect to jabber server(s)
    serverCredentials = new XMPPServerCredentials();
    getConnectionDetailsFromAxisConfiguration(transportOut);
    defaultConnectionFactory = new XMPPConnectionFactory();
}
Also used : XMPPServerCredentials(org.apache.axis2.transport.xmpp.util.XMPPServerCredentials) XMPPConnectionFactory(org.apache.axis2.transport.xmpp.util.XMPPConnectionFactory)

Example 2 with XMPPConnectionFactory

use of org.apache.axis2.transport.xmpp.util.XMPPConnectionFactory in project wso2-axis2-transports by wso2.

the class XMPPListener method initializeConnectionFactories.

/**
 * Extract connection details & connect to those xmpp servers.
 * @param transportIn TransportInDescription
 */
private void initializeConnectionFactories(TransportInDescription transportIn) throws AxisFault {
    Iterator serversToListenOn = transportIn.getParameters().iterator();
    while (serversToListenOn.hasNext()) {
        Parameter connection = (Parameter) serversToListenOn.next();
        log.info("Trying to establish connection for : " + connection.getName());
        ParameterIncludeImpl pi = new ParameterIncludeImpl();
        try {
            pi.deserializeParameters((OMElement) connection.getValue());
        } catch (AxisFault axisFault) {
            log.error("Error reading parameters");
        }
        Iterator params = pi.getParameters().iterator();
        serverCredentials = new XMPPServerCredentials();
        while (params.hasNext()) {
            Parameter param = (Parameter) params.next();
            if (XMPPConstants.XMPP_SERVER_URL.equals(param.getName())) {
                serverCredentials.setServerUrl((String) param.getValue());
            } else if (XMPPConstants.XMPP_SERVER_USERNAME.equals(param.getName())) {
                serverCredentials.setAccountName((String) param.getValue());
            } else if (XMPPConstants.XMPP_SERVER_PASSWORD.equals(param.getName())) {
                serverCredentials.setPassword((String) param.getValue());
            } else if (XMPPConstants.XMPP_SERVER_TYPE.equals(param.getName())) {
                serverCredentials.setServerType((String) param.getValue());
            } else if (XMPPConstants.XMPP_DOMAIN_NAME.equals(param.getName())) {
                serverCredentials.setDomainName((String) param.getValue());
            }
        }
        XMPPConnectionFactory xmppConnectionFactory = new XMPPConnectionFactory();
        xmppConnectionFactory.connect(serverCredentials);
        connectionFactories.put(serverCredentials.getAccountName() + "@" + serverCredentials.getServerUrl(), xmppConnectionFactory);
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) XMPPServerCredentials(org.apache.axis2.transport.xmpp.util.XMPPServerCredentials) XMPPConnectionFactory(org.apache.axis2.transport.xmpp.util.XMPPConnectionFactory) ParameterIncludeImpl(org.apache.axis2.description.ParameterIncludeImpl) Iterator(java.util.Iterator) Parameter(org.apache.axis2.description.Parameter)

Example 3 with XMPPConnectionFactory

use of org.apache.axis2.transport.xmpp.util.XMPPConnectionFactory in project wso2-axis2-transports by wso2.

the class XMPPListener method start.

/**
 * Start a pool of Workers. For each connection in connectionFactories,
 * assign a packer listener. This packet listener will trigger when a
 * message arrives.
 */
public void start() throws AxisFault {
    // create thread pool of workers
    ExecutorService workerPool = new ThreadPoolExecutor(1, WORKERS_MAX_THREADS, WORKER_KEEP_ALIVE, TIME_UNIT, new LinkedBlockingQueue(), new org.apache.axis2.util.threadpool.DefaultThreadFactory(new ThreadGroup("XMPP Worker thread group"), "XMPPWorker"));
    Iterator iter = connectionFactories.values().iterator();
    while (iter.hasNext()) {
        XMPPConnectionFactory connectionFactory = (XMPPConnectionFactory) iter.next();
        XMPPPacketListener xmppPacketListener = new XMPPPacketListener(connectionFactory, this.configurationContext, workerPool);
        connectionFactory.listen(xmppPacketListener);
    }
}
Also used : XMPPConnectionFactory(org.apache.axis2.transport.xmpp.util.XMPPConnectionFactory) XMPPPacketListener(org.apache.axis2.transport.xmpp.util.XMPPPacketListener) ExecutorService(java.util.concurrent.ExecutorService) Iterator(java.util.Iterator) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Example 4 with XMPPConnectionFactory

use of org.apache.axis2.transport.xmpp.util.XMPPConnectionFactory 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

XMPPConnectionFactory (org.apache.axis2.transport.xmpp.util.XMPPConnectionFactory)4 Iterator (java.util.Iterator)2 XMPPServerCredentials (org.apache.axis2.transport.xmpp.util.XMPPServerCredentials)2 ExecutorService (java.util.concurrent.ExecutorService)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 OMElement (org.apache.axiom.om.OMElement)1 SOAP12Version (org.apache.axiom.soap.SOAP12Version)1 SOAPVersion (org.apache.axiom.soap.SOAPVersion)1 AxisFault (org.apache.axis2.AxisFault)1 Options (org.apache.axis2.client.Options)1 AxisMessage (org.apache.axis2.description.AxisMessage)1 AxisOperation (org.apache.axis2.description.AxisOperation)1 Parameter (org.apache.axis2.description.Parameter)1 ParameterIncludeImpl (org.apache.axis2.description.ParameterIncludeImpl)1 XMPPOutTransportInfo (org.apache.axis2.transport.xmpp.util.XMPPOutTransportInfo)1 XMPPPacketListener (org.apache.axis2.transport.xmpp.util.XMPPPacketListener)1 Chat (org.jivesoftware.smack.Chat)1 ChatManager (org.jivesoftware.smack.ChatManager)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1