Search in sources :

Example 1 with XMPPServerCredentials

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

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

the class XMPPConnectionFactory method connect.

/**
 * Connects to a XMPP server based on the details available in serverCredentials object
 * @param serverCredentials
 * @throws XMPPException
 */
public XMPPConnection connect(final XMPPServerCredentials serverCredentials) throws AxisFault {
    // XMPPConnection.DEBUG_ENABLED = true;
    if (XMPPConstants.XMPP_SERVER_TYPE_JABBER.equals(serverCredentials.getServerType())) {
        xmppConnection = new XMPPConnection(serverCredentials.getServerUrl());
        try {
            xmppConnection.connect();
        } catch (XMPPException e) {
            log.error("Failed to connect to server :" + serverCredentials.getServerUrl(), e);
            throw new AxisFault("Failed to connect to server :" + serverCredentials.getServerUrl());
        }
        // This prevents random ssl exception from Smack API
        try {
            Thread.sleep(100);
        } catch (InterruptedException e5) {
            log.debug("Sleep interrupted ", e5);
        }
        if (xmppConnection.isConnected()) {
            String resource = serverCredentials.getResource() + new Object().hashCode();
            if (!xmppConnection.isAuthenticated()) {
                try {
                    xmppConnection.login(serverCredentials.getAccountName(), serverCredentials.getPassword(), resource, true);
                } catch (XMPPException e) {
                    try {
                        log.error("Login failed for " + serverCredentials.getAccountName() + "@" + serverCredentials.getServerUrl() + ".Retrying in 2 secs", e);
                        Thread.sleep(2000);
                        xmppConnection.login(serverCredentials.getAccountName(), serverCredentials.getPassword(), resource, true);
                    } catch (InterruptedException e1) {
                        log.error("Sleep interrupted.", e1);
                    } catch (XMPPException e2) {
                        log.error("Login failed for : " + serverCredentials.getAccountName() + "@" + serverCredentials.getServerUrl(), e2);
                        throw new AxisFault("Login failed for : " + serverCredentials.getAccountName() + "@" + serverCredentials.getServerUrl());
                    }
                }
                // Listen for Message type packets from specified server url
                packetFilter = new FromContainsFilter(serverCredentials.getServerUrl());
            }
        }
    } else if (XMPPConstants.XMPP_SERVER_TYPE_GOOGLETALK.equals(serverCredentials.getServerType())) {
        ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(XMPPConstants.GOOGLETALK_URL, XMPPConstants.GOOGLETALK_PORT, XMPPConstants.GOOGLETALK_SERVICE_NAME);
        xmppConnection = new XMPPConnection(connectionConfiguration);
        try {
            xmppConnection.connect();
            xmppConnection.login(serverCredentials.getAccountName(), serverCredentials.getPassword(), serverCredentials.getResource(), true);
            packetFilter = new ToContainsFilter("@gmail.com");
        } catch (XMPPException e1) {
            log.error("Error occured while connecting to Googletalk server.", e1);
            throw new AxisFault("Error occured while connecting to Googletalk server.");
        }
    }
    ConnectionListener connectionListener = null;
    connectionListener = new ConnectionListener() {

        public void connectionClosed() {
            log.debug("Connection closed normally");
        }

        public void connectionClosedOnError(Exception e1) {
            log.error("Connection to " + serverCredentials.getServerUrl() + " closed with error.", e1);
        }

        public void reconnectingIn(int seconds) {
            log.error("Connection to " + serverCredentials.getServerUrl() + " failed. Reconnecting in " + seconds + "s");
        }

        public void reconnectionFailed(Exception e) {
            log.error("Reconnection to " + serverCredentials.getServerUrl() + " failed.", e);
        }

        public void reconnectionSuccessful() {
            log.debug("Reconnection to " + serverCredentials.getServerUrl() + " successful.");
        }
    };
    if (xmppConnection != null && xmppConnection.isConnected()) {
        xmppConnection.addConnectionListener(connectionListener);
        log.info("Connected to " + serverCredentials.getAccountName() + "@" + serverCredentials.getServerUrl() + "/" + serverCredentials.getResource());
    } else {
        log.warn(" Not Connected to " + serverCredentials.getAccountName() + "@" + serverCredentials.getServerUrl() + "/" + serverCredentials.getResource());
    }
    return xmppConnection;
}
Also used : AxisFault(org.apache.axis2.AxisFault) ToContainsFilter(org.jivesoftware.smack.filter.ToContainsFilter) ConnectionConfiguration(org.jivesoftware.smack.ConnectionConfiguration) FromContainsFilter(org.jivesoftware.smack.filter.FromContainsFilter) XMPPConnection(org.jivesoftware.smack.XMPPConnection) ConnectionListener(org.jivesoftware.smack.ConnectionListener) XMPPException(org.jivesoftware.smack.XMPPException) XMPPException(org.jivesoftware.smack.XMPPException)

Example 3 with XMPPServerCredentials

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

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

the class XMPPClientConnectionFactory method connect.

/**
 * Connects to a XMPP server based on the details available in serverCredentials object
 * @param serverCredentials
 * @throws XMPPException
 */
public XMPPConnection connect(final XMPPServerCredentials serverCredentials) throws AxisFault {
    // XMPPConnection.DEBUG_ENABLED = true;
    if (XMPPConstants.XMPP_SERVER_TYPE_JABBER.equals(serverCredentials.getServerType())) {
        xmppConnection = new XMPPConnection(serverCredentials.getServerUrl());
        try {
            xmppConnection.connect();
        } catch (XMPPException e) {
            log.error("Failed to connect to server :" + serverCredentials.getServerUrl(), e);
            throw new AxisFault("Failed to connect to server :" + serverCredentials.getServerUrl());
        }
        // This prevents random ssl exception from Smack API
        try {
            Thread.sleep(100);
        } catch (InterruptedException e5) {
            log.debug("Sleep interrupted ", e5);
        }
        if (xmppConnection.isConnected()) {
            String resource = serverCredentials.getResource() + new Object().hashCode();
            if (!xmppConnection.isAuthenticated()) {
                try {
                    // xmppConnection.login(serverCredentials.getAccountName()+"@"+
                    // serverCredentials.getServerUrl(),
                    xmppConnection.login(serverCredentials.getAccountName(), serverCredentials.getPassword(), resource, true);
                } catch (XMPPException e) {
                    try {
                        log.error("Login failed for " + serverCredentials.getAccountName() + "@" + serverCredentials.getServerUrl() + ".Retrying in 2 secs", e);
                        Thread.sleep(2000);
                        // xmppConnection.login(serverCredentials.getAccountName()+"@"+
                        // serverCredentials.getServerUrl(),
                        xmppConnection.login(serverCredentials.getAccountName(), serverCredentials.getPassword(), resource, true);
                    } catch (InterruptedException e1) {
                        log.error("Sleep interrupted.", e1);
                    } catch (XMPPException e2) {
                        log.error("Login failed for : " + serverCredentials.getAccountName() + "@" + serverCredentials.getServerUrl(), e2);
                        throw new AxisFault("Login failed for : " + serverCredentials.getAccountName() + "@" + serverCredentials.getServerUrl());
                    }
                }
                // Listen for Message type packets from specified server url
                // packetFilter = new AndFilter(new PacketTypeFilter(Message.class),
                // new FromContainsFilter(serverCredentials.getServerUrl()));
                packetFilter = new FromContainsFilter(serverCredentials.getServerUrl());
            }
        }
    } else if (XMPPConstants.XMPP_SERVER_TYPE_GOOGLETALK.equals(serverCredentials.getServerType())) {
        ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(XMPPConstants.GOOGLETALK_URL, XMPPConstants.GOOGLETALK_PORT, XMPPConstants.GOOGLETALK_SERVICE_NAME);
        xmppConnection = new XMPPConnection(connectionConfiguration);
        try {
            xmppConnection.connect();
            xmppConnection.login(serverCredentials.getAccountName(), serverCredentials.getPassword(), serverCredentials.getResource(), true);
            // packetFilter = new AndFilter(new PacketTypeFilter(Message.class),
            // new FromContainsFilter(XMPPConstants.GOOGLETALK_FROM));
            // packetFilter = new FromContainsFilter(XMPPConstants.GOOGLETALK_FROM);
            packetFilter = new ToContainsFilter("@gmail.com");
        } catch (XMPPException e1) {
            log.error("Error occured while connecting to Googletalk server.", e1);
            throw new AxisFault("Error occured while connecting to Googletalk server.");
        }
    }
    ConnectionListener connectionListener = null;
    connectionListener = new ConnectionListener() {

        public void connectionClosed() {
            log.debug("Connection closed normally");
        }

        public void connectionClosedOnError(Exception e1) {
            log.error("Connection to " + serverCredentials.getServerUrl() + " closed with error.", e1);
        }

        public void reconnectingIn(int seconds) {
            log.error("Connection to " + serverCredentials.getServerUrl() + " failed. Reconnecting in " + seconds + "s");
        }

        public void reconnectionFailed(Exception e) {
            log.error("Reconnection to " + serverCredentials.getServerUrl() + " failed.", e);
        }

        public void reconnectionSuccessful() {
            log.debug("Reconnection to " + serverCredentials.getServerUrl() + " successful.");
        }
    };
    if (xmppConnection != null && xmppConnection.isConnected()) {
        xmppConnection.addConnectionListener(connectionListener);
        log.info("Connected to " + serverCredentials.getAccountName() + "@" + serverCredentials.getServerUrl() + "/" + serverCredentials.getResource());
    } else {
        log.warn(" Not Connected to " + serverCredentials.getAccountName() + "@" + serverCredentials.getServerUrl() + "/" + serverCredentials.getResource());
    }
    return xmppConnection;
}
Also used : AxisFault(org.apache.axis2.AxisFault) ToContainsFilter(org.jivesoftware.smack.filter.ToContainsFilter) ConnectionConfiguration(org.jivesoftware.smack.ConnectionConfiguration) FromContainsFilter(org.jivesoftware.smack.filter.FromContainsFilter) XMPPConnection(org.jivesoftware.smack.XMPPConnection) ConnectionListener(org.jivesoftware.smack.ConnectionListener) XMPPException(org.jivesoftware.smack.XMPPException) XMPPException(org.jivesoftware.smack.XMPPException)

Aggregations

AxisFault (org.apache.axis2.AxisFault)3 XMPPConnectionFactory (org.apache.axis2.transport.xmpp.util.XMPPConnectionFactory)2 XMPPServerCredentials (org.apache.axis2.transport.xmpp.util.XMPPServerCredentials)2 ConnectionConfiguration (org.jivesoftware.smack.ConnectionConfiguration)2 ConnectionListener (org.jivesoftware.smack.ConnectionListener)2 XMPPConnection (org.jivesoftware.smack.XMPPConnection)2 XMPPException (org.jivesoftware.smack.XMPPException)2 FromContainsFilter (org.jivesoftware.smack.filter.FromContainsFilter)2 ToContainsFilter (org.jivesoftware.smack.filter.ToContainsFilter)2 Iterator (java.util.Iterator)1 Parameter (org.apache.axis2.description.Parameter)1 ParameterIncludeImpl (org.apache.axis2.description.ParameterIncludeImpl)1