Search in sources :

Example 11 with ConnectionConfiguration

use of org.jivesoftware.smack.ConnectionConfiguration 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)

Example 12 with ConnectionConfiguration

use of org.jivesoftware.smack.ConnectionConfiguration in project jbpm-work-items by kiegroup.

the class JabberWorkItemHandler method executeWorkItem.

public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
    this.user = (String) workItem.getParameter("User");
    this.password = (String) workItem.getParameter("Password");
    this.server = (String) workItem.getParameter("Server");
    String portString = (String) workItem.getParameter("Port");
    if (portString != null && !portString.equals("")) {
        this.port = Integer.valueOf((String) workItem.getParameter("Port"));
    }
    this.service = (String) workItem.getParameter("Service");
    this.text = (String) workItem.getParameter("Text");
    String to = (String) workItem.getParameter("To");
    if (to == null || to.trim().length() == 0) {
        throw new RuntimeException("IM must have one or more to adresses");
    }
    for (String s : to.split(";")) {
        if (s != null && !"".equals(s)) {
            this.toUsers.add(s);
        }
    }
    if (conf == null) {
        conf = new ConnectionConfiguration(server, port, service);
    }
    try {
        if (server != null && !server.equals("") && port != 0) {
            if (connection == null) {
                connection = new XMPPConnection(conf);
            }
        } else {
            if (connection == null) {
                connection = new XMPPConnection(service);
            }
        }
        connection.connect();
        logger.info("Connected to {}", connection.getHost());
        connection.login(user, password);
        logger.info("Logged in as {}", connection.getUser());
        Presence presence = new Presence(Presence.Type.available);
        connection.sendPacket(presence);
        for (String toUser : toUsers) {
            ChatManager chatmanager = connection.getChatManager();
            Chat chat = chatmanager.createChat(toUser, null);
            // google bounces back the default message types, you must use chat
            Message msg = new Message(toUser, Message.Type.chat);
            msg.setBody(text);
            chat.sendMessage(msg);
            logger.info("Message Sent {}", msg);
        }
        connection.disconnect();
        manager.completeWorkItem(workItem.getId(), null);
    } catch (Exception e) {
        handleException(e);
    }
}
Also used : ConnectionConfiguration(org.jivesoftware.smack.ConnectionConfiguration) Message(org.jivesoftware.smack.packet.Message) Chat(org.jivesoftware.smack.Chat) Presence(org.jivesoftware.smack.packet.Presence) XMPPConnection(org.jivesoftware.smack.XMPPConnection) ChatManager(org.jivesoftware.smack.ChatManager)

Example 13 with ConnectionConfiguration

use of org.jivesoftware.smack.ConnectionConfiguration in project Smack by igniterealtime.

the class SmackTestCase method createConnection.

/**
 * Creates a new XMPPTCPConnection using the connection preferences. This is useful when
 * not using a connection from the connection pool in a test case.
 *
 * @return a new XMPP connection.
 */
protected XMPPTCPConnection createConnection() {
    // Create the configuration for this new connection
    ConnectionConfiguration config = new ConnectionConfiguration(host, port);
    config.setCompressionEnabled(compressionEnabled);
    config.setSendPresence(sendInitialPresence());
    if (getSocketFactory() == null) {
        config.setSocketFactory(getSocketFactory());
    }
    return new XMPPTCPConnection(config);
}
Also used : ConnectionConfiguration(org.jivesoftware.smack.ConnectionConfiguration)

Example 14 with ConnectionConfiguration

use of org.jivesoftware.smack.ConnectionConfiguration in project Smack by igniterealtime.

the class XmppConnectionDescriptor method construct.

public C construct(Configuration sinttestConfiguration, Collection<ConnectionConfigurationBuilderApplier> customConnectionConfigurationAppliers) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    CCB connectionConfigurationBuilder = getNewBuilder();
    if (extraBuilder != null) {
        extraBuilder.accept(connectionConfigurationBuilder);
    }
    for (ConnectionConfigurationBuilderApplier customConnectionConfigurationApplier : customConnectionConfigurationAppliers) {
        customConnectionConfigurationApplier.applyConfigurationTo(connectionConfigurationBuilder);
    }
    sinttestConfiguration.configurationApplier.applyConfigurationTo(connectionConfigurationBuilder);
    ConnectionConfiguration connectionConfiguration = connectionConfigurationBuilder.build();
    CC concreteConnectionConfiguration = connectionConfigurationClass.cast(connectionConfiguration);
    C connection = connectionConstructor.newInstance(concreteConnectionConfiguration);
    connection.setReplyTimeout(sinttestConfiguration.replyTimeout);
    return connection;
}
Also used : ConnectionConfiguration(org.jivesoftware.smack.ConnectionConfiguration) ModularXmppClientToServerConnectionConfiguration(org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration)

Aggregations

ConnectionConfiguration (org.jivesoftware.smack.ConnectionConfiguration)14 XMPPConnection (org.jivesoftware.smack.XMPPConnection)9 XMPPException (org.jivesoftware.smack.XMPPException)6 Message (org.jivesoftware.smack.packet.Message)3 SSLContext (javax.net.ssl.SSLContext)2 AxisFault (org.apache.axis2.AxisFault)2 ConnectionListener (org.jivesoftware.smack.ConnectionListener)2 FromContainsFilter (org.jivesoftware.smack.filter.FromContainsFilter)2 ToContainsFilter (org.jivesoftware.smack.filter.ToContainsFilter)2 XMPPTCPConnection (org.jivesoftware.smack.tcp.XMPPTCPConnection)2 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)1 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)1 PerunNotifMessageDto (cz.metacentrum.perun.notif.dto.PerunNotifMessageDto)1 PoolMessage (cz.metacentrum.perun.notif.dto.PoolMessage)1 PerunNotifReceiver (cz.metacentrum.perun.notif.entities.PerunNotifReceiver)1 InetAddress (java.net.InetAddress)1 KeyManagementException (java.security.KeyManagementException)1 KeyStore (java.security.KeyStore)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SecureRandom (java.security.SecureRandom)1