use of org.jivesoftware.smack.XMPPException 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;
}
use of org.jivesoftware.smack.XMPPException in project Payara by payara.
the class TestXmppNotifier method execute.
@Override
public void execute(AdminCommandContext context) {
ActionReport actionReport = context.getActionReport();
Config config = targetUtil.getConfig(target);
if (config == null) {
context.getActionReport().setMessage("No such config named: " + target);
context.getActionReport().setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}
XmppNotifierConfiguration xmppConfig = config.getExtensionByType(XmppNotifierConfiguration.class);
if (hostName == null) {
hostName = xmppConfig.getHost();
}
if (port == null) {
port = Integer.parseInt(xmppConfig.getPort());
}
if (serviceName == null) {
serviceName = xmppConfig.getServiceName();
}
if (username == null) {
username = xmppConfig.getUsername();
}
if (password == null) {
password = xmppConfig.getPassword();
}
if (securityDisabled == null) {
securityDisabled = Boolean.valueOf(xmppConfig.getSecurityDisabled());
}
if (roomId == null) {
roomId = xmppConfig.getRoomId();
}
// prepare xmpp message
XmppNotificationEvent event = factory.buildNotificationEvent(SUBJECT, MESSAGE);
event.setEventType(SUBJECT);
XmppMessageQueue queue = new XmppMessageQueue();
queue.addMessage(new XmppMessage(event, event.getSubject(), event.getMessage()));
XmppNotifierConfigurationExecutionOptions options = new XmppNotifierConfigurationExecutionOptions();
options.setHost(hostName);
options.setPort(port);
options.setServiceName(serviceName);
if (!Strings.isNullOrEmpty(username)) {
options.setUsername(username);
}
if (!Strings.isNullOrEmpty(password)) {
options.setPassword(password);
}
options.setSecurityDisabled(securityDisabled);
options.setRoomId(roomId);
XMPPTCPConnection connection = null;
try {
// Create connection
XMPPTCPConnectionConfiguration configuration = XMPPTCPConnectionConfiguration.builder().setSecurityMode(options.getSecurityDisabled() ? ConnectionConfiguration.SecurityMode.disabled : ConnectionConfiguration.SecurityMode.required).setServiceName(options.getServiceName()).setHost(options.getHost()).setPort(options.getPort()).build();
connection = new XMPPTCPConnection(configuration);
connection.connect();
if (options.getUsername() != null && options.getPassword() != null) {
connection.login(options.getUsername(), options.getPassword());
} else {
connection.login();
}
} catch (SmackException | IOException | XMPPException ex) {
actionReport.setMessage(ex.getMessage());
actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
queue.resetQueue();
return;
}
XmppNotificationRunnable notifierRun = new XmppNotificationRunnable(queue, options, connection);
// set up logger to store result
Logger logger = Logger.getLogger(XmppNotificationRunnable.class.getCanonicalName());
BlockingQueueHandler bqh = new BlockingQueueHandler();
bqh.setLevel(Level.FINE);
Level oldLevel = logger.getLevel();
logger.setLevel(Level.FINE);
logger.addHandler(bqh);
// send message, this occurs in its own thread
Thread notifierThread = new Thread(notifierRun, "test-xmpp-notifier-thread");
notifierThread.start();
try {
notifierThread.join();
} catch (InterruptedException ex) {
Logger.getLogger(TestXmppNotifier.class.getName()).log(Level.SEVERE, null, ex);
} finally {
connection.disconnect();
logger.setLevel(oldLevel);
}
LogRecord message = bqh.poll();
bqh.clear();
logger.removeHandler(bqh);
if (message == null) {
// something's gone wrong
Logger.getGlobal().log(Level.SEVERE, "Failed to send XMPP message");
actionReport.setMessage("Failed to send XMPP message");
actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
} else {
actionReport.setMessage(message.getMessage());
if (message.getLevel() == Level.FINE) {
actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
} else {
actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
}
}
}
use of org.jivesoftware.smack.XMPPException in project Payara by payara.
the class XmppNotifierService method bootstrap.
@Override
public void bootstrap() {
register(NotifierType.XMPP, XmppNotifier.class, XmppNotifierConfiguration.class, this);
try {
executionOptions = (XmppNotifierConfigurationExecutionOptions) getNotifierConfigurationExecutionOptions();
if (executionOptions != null && executionOptions.isEnabled()) {
initializeExecutor();
XMPPTCPConnectionConfiguration configuration = XMPPTCPConnectionConfiguration.builder().setSecurityMode(executionOptions.getSecurityDisabled() ? ConnectionConfiguration.SecurityMode.disabled : ConnectionConfiguration.SecurityMode.required).setServiceName(executionOptions.getServiceName()).setHost(executionOptions.getHost()).setPort(executionOptions.getPort()).build();
connection = new XMPPTCPConnection(configuration);
connection.connect();
if (executionOptions.getUsername() != null && executionOptions.getPassword() != null) {
connection.login(executionOptions.getUsername(), executionOptions.getPassword());
} else {
connection.login();
}
scheduleExecutor(new XmppNotificationRunnable(queue, executionOptions, connection));
}
} catch (XMPPException e) {
logger.log(Level.SEVERE, "Error occurred on XMPP protocol level while connecting host", e);
} catch (SmackException e) {
logger.log(Level.SEVERE, "Error occurred on Smack protocol level while connecting host", e);
} catch (IOException e) {
logger.log(Level.SEVERE, "IO Error occurred while connecting host", e);
}
}
use of org.jivesoftware.smack.XMPPException in project Smack by igniterealtime.
the class PrivacyTest method testDenyActiveList.
/**
* Check when a client denies the use of the active list.
*/
public void testDenyActiveList() {
try {
PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(getConnection(0));
PrivacyClient client = new PrivacyClient(privacyManager);
privacyManager.addListener(client);
privacyManager.declineActiveList();
Thread.sleep(500);
try {
// The list should not exist and an error will be raised
privacyManager.getActiveList();
} catch (XMPPException xmppException) {
assertEquals(404, xmppException.getXMPPError().getCode());
}
assertEquals(null, null);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.jivesoftware.smack.XMPPException in project Smack by igniterealtime.
the class PrivacyTest method testRemoveList.
/**
* Check when a client add a new list and then remove it.
*/
public void testRemoveList() {
try {
String listName = "testRemoveList";
PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(getConnection(0));
PrivacyClient client = new PrivacyClient(privacyManager);
privacyManager.addListener(client);
// Add the list that will be set as the Default
ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.name(), true, 1);
item.setValue(getConnection(0).getUser());
items.add(item);
privacyManager.createPrivacyList(listName, items);
Thread.sleep(500);
// Set the Default list
privacyManager.setDefaultListName(listName);
Thread.sleep(500);
privacyManager.deletePrivacyList(listName);
Thread.sleep(500);
try {
// The list should not exist and an error will be raised
privacyManager.getPrivacyList(listName);
} catch (XMPPException xmppException) {
assertEquals(404, xmppException.getXMPPError().getCode());
}
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
Aggregations