From 85c4832a7f30c1f5af98592eb8adfdf2ae22dfd0 Mon Sep 17 00:00:00 2001 From: Greg Turnquist Date: Wed, 20 Jan 2016 16:14:07 -0600 Subject: [PATCH] SWS-885 Upgrade to Smack 4.1.5 --- build.gradle | 5 ++- gradle/wrapper/gradle-wrapper.properties | 2 +- .../transport/xmpp/XmppMessageReceiver.java | 31 ++++++++------- .../xmpp/XmppReceiverConnection.java | 9 ++++- .../transport/xmpp/XmppSenderConnection.java | 23 ++++++----- .../support/XmppConnectionFactoryBean.java | 38 ++++++++++++------- .../xmpp/support/XmppTransportUtils.java | 13 ++++--- .../XmppConnectionFactoryBeanTest.java | 9 +++-- 8 files changed, 81 insertions(+), 49 deletions(-) diff --git a/build.gradle b/build.gradle index f945c8377..03672ce11 100644 --- a/build.gradle +++ b/build.gradle @@ -15,6 +15,7 @@ configure(allprojects) { ext.springVersion = "4.0.9.RELEASE" ext.springSecurityVersion = "3.2.7.RELEASE" ext.axiomVersion = "1.2.15" + ext.smackVersion = "4.1.5" apply plugin: "java" @@ -218,7 +219,9 @@ project('spring-ws-support') { provided("javax.jms:jms-api:1.1-rev-1") provided("javax.mail:javax.mail-api:1.4.7") provided("com.sun.mail:javax.mail:1.4.7") - optional("org.igniterealtime.smack:smack:3.2.1") + optional("org.igniterealtime.smack:smack-tcp:$smackVersion") + optional("org.igniterealtime.smack:smack-resolver-javax:$smackVersion") + optional("org.igniterealtime.smack:smack-extensions:$smackVersion") testCompile("commons-httpclient:commons-httpclient:3.1") testRuntime("org.apache.activemq:activemq-core:4.1.2") { exclude group:'org.apache.geronimo.specs', module:'geronimo-jms_1.1_spec' diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 126e7baea..ea06ca283 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,4 +1,4 @@ -#Tue Dec 15 10:03:30 CST 2015 +#Wed Jan 20 16:03:00 CST 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME diff --git a/spring-ws-support/src/main/java/org/springframework/ws/transport/xmpp/XmppMessageReceiver.java b/spring-ws-support/src/main/java/org/springframework/ws/transport/xmpp/XmppMessageReceiver.java index 30373d408..5b967dde2 100644 --- a/spring-ws-support/src/main/java/org/springframework/ws/transport/xmpp/XmppMessageReceiver.java +++ b/spring-ws-support/src/main/java/org/springframework/ws/transport/xmpp/XmppMessageReceiver.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2014 the original author or authors. + * Copyright 2005-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,13 +16,17 @@ package org.springframework.ws.transport.xmpp; -import org.jivesoftware.smack.PacketListener; +import java.io.IOException; + +import org.jivesoftware.smack.SmackException; +import org.jivesoftware.smack.StanzaListener; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; -import org.jivesoftware.smack.filter.PacketFilter; -import org.jivesoftware.smack.filter.PacketTypeFilter; +import org.jivesoftware.smack.filter.StanzaFilter; +import org.jivesoftware.smack.filter.StanzaTypeFilter; import org.jivesoftware.smack.packet.Message; -import org.jivesoftware.smack.packet.Packet; +import org.jivesoftware.smack.packet.Stanza; +import org.jivesoftware.smack.tcp.XMPPTCPConnection; import org.springframework.ws.transport.support.AbstractStandaloneMessageReceiver; @@ -34,6 +38,7 @@ * * @author Gildas Cuisinier * @author Arjen Poutsma + * @author Greg Turnquist * @see org.springframework.ws.transport.xmpp.support.XmppConnectionFactoryBean * @since 2.0 */ @@ -42,7 +47,7 @@ public class XmppMessageReceiver extends AbstractStandaloneMessageReceiver { /** Default encoding used to read from and write to {@link org.jivesoftware.smack.packet.Message} messages. */ public static final String DEFAULT_MESSAGE_ENCODING = "UTF-8"; - private XMPPConnection connection; + private XMPPTCPConnection connection; private WebServicePacketListener packetListener; @@ -52,12 +57,12 @@ public XmppMessageReceiver() { } /** Sets the {@code XMPPConnection} to use. Setting this property is required. */ - public void setConnection(XMPPConnection connection) { + public void setConnection(XMPPTCPConnection connection) { this.connection = connection; } @Override - protected void onActivate() throws XMPPException { + protected void onActivate() throws XMPPException, IOException, SmackException { if (!connection.isConnected()) { connection.connect(); } @@ -69,8 +74,8 @@ protected void onStart() { logger.info("Starting XMPP receiver [" + connection.getUser() + "]"); } packetListener = new WebServicePacketListener(); - PacketFilter packetFilter = new PacketTypeFilter(Message.class); - connection.addPacketListener(packetListener, packetFilter); + StanzaFilter packetFilter = new StanzaTypeFilter(Message.class); + connection.addAsyncStanzaListener(packetListener, packetFilter); } @Override @@ -78,7 +83,7 @@ protected void onStop() { if (logger.isInfoEnabled()) { logger.info("Stopping XMPP receiver [" + connection.getUser() + "]"); } - connection.removePacketListener(packetListener); + connection.removeAsyncStanzaListener(packetListener); packetListener = null; } @@ -92,10 +97,10 @@ protected void onShutdown() { } } - private class WebServicePacketListener implements PacketListener { + private class WebServicePacketListener implements StanzaListener { @Override - public void processPacket(Packet packet) { + public void processPacket(Stanza packet) { logger.info("Received " + packet); if (packet instanceof Message) { Message message = (Message) packet; diff --git a/spring-ws-support/src/main/java/org/springframework/ws/transport/xmpp/XmppReceiverConnection.java b/spring-ws-support/src/main/java/org/springframework/ws/transport/xmpp/XmppReceiverConnection.java index 299b18139..578ef85e6 100644 --- a/spring-ws-support/src/main/java/org/springframework/ws/transport/xmpp/XmppReceiverConnection.java +++ b/spring-ws-support/src/main/java/org/springframework/ws/transport/xmpp/XmppReceiverConnection.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2014 the original author or authors. + * Copyright 2005-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ import java.net.URISyntaxException; import java.util.Iterator; +import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.packet.Message; @@ -140,6 +141,10 @@ protected OutputStream getResponseOutputStream() throws IOException { @Override protected void onSendAfterWrite(WebServiceMessage message) throws IOException { - connection.sendPacket(responseMessage); + try { + connection.sendStanza(responseMessage); + } catch (SmackException.NotConnectedException e) { + throw new IOException(e); + } } } diff --git a/spring-ws-support/src/main/java/org/springframework/ws/transport/xmpp/XmppSenderConnection.java b/spring-ws-support/src/main/java/org/springframework/ws/transport/xmpp/XmppSenderConnection.java index 25f33b934..37d2efdb1 100644 --- a/spring-ws-support/src/main/java/org/springframework/ws/transport/xmpp/XmppSenderConnection.java +++ b/spring-ws-support/src/main/java/org/springframework/ws/transport/xmpp/XmppSenderConnection.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2014 the original author or authors. + * Copyright 2005-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,13 +24,14 @@ import java.util.Iterator; import org.jivesoftware.smack.PacketCollector; +import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.filter.AndFilter; -import org.jivesoftware.smack.filter.PacketFilter; -import org.jivesoftware.smack.filter.PacketTypeFilter; +import org.jivesoftware.smack.filter.StanzaFilter; +import org.jivesoftware.smack.filter.StanzaTypeFilter; import org.jivesoftware.smack.filter.ThreadFilter; import org.jivesoftware.smack.packet.Message; -import org.jivesoftware.smack.packet.Packet; +import org.jivesoftware.smack.packet.Stanza; import org.springframework.util.Assert; import org.springframework.ws.WebServiceMessage; @@ -129,7 +130,11 @@ protected OutputStream getRequestOutputStream() throws IOException { @Override protected void onSendAfterWrite(WebServiceMessage message) throws IOException { requestMessage.setFrom(connection.getUser()); - connection.sendPacket(requestMessage); + try { + connection.sendStanza(requestMessage); + } catch (SmackException.NotConnectedException e) { + throw new IOException(e); + } } /* @@ -138,10 +143,10 @@ protected void onSendAfterWrite(WebServiceMessage message) throws IOException { @Override protected void onReceiveBeforeRead() throws IOException { - PacketFilter packetFilter = createPacketFilter(); + StanzaFilter packetFilter = createPacketFilter(); PacketCollector collector = connection.createPacketCollector(packetFilter); - Packet packet = receiveTimeout >= 0 ? collector.nextResult(receiveTimeout) : collector.nextResult(); + Stanza packet = receiveTimeout >= 0 ? collector.nextResult(receiveTimeout) : collector.nextResult(); if (packet instanceof Message) { responseMessage = (Message) packet; } @@ -151,9 +156,9 @@ else if (packet != null) { } } - private PacketFilter createPacketFilter() { + private StanzaFilter createPacketFilter() { AndFilter andFilter = new AndFilter(); - andFilter.addFilter(new PacketTypeFilter(Message.class)); + andFilter.addFilter(new StanzaTypeFilter(Message.class)); andFilter.addFilter(new ThreadFilter(requestMessage.getThread())); return andFilter; } diff --git a/spring-ws-support/src/main/java/org/springframework/ws/transport/xmpp/support/XmppConnectionFactoryBean.java b/spring-ws-support/src/main/java/org/springframework/ws/transport/xmpp/support/XmppConnectionFactoryBean.java index f816b6abd..145ecc259 100644 --- a/spring-ws-support/src/main/java/org/springframework/ws/transport/xmpp/support/XmppConnectionFactoryBean.java +++ b/spring-ws-support/src/main/java/org/springframework/ws/transport/xmpp/support/XmppConnectionFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2014 the original author or authors. + * Copyright 2005-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,9 +16,12 @@ package org.springframework.ws.transport.xmpp.support; -import org.jivesoftware.smack.ConnectionConfiguration; -import org.jivesoftware.smack.XMPPConnection; +import java.io.IOException; + +import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.XMPPException; +import org.jivesoftware.smack.tcp.XMPPTCPConnection; +import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.FactoryBean; @@ -33,11 +36,11 @@ * @author Arjen Poutsma * @since 2.0 */ -public class XmppConnectionFactoryBean implements FactoryBean, InitializingBean, DisposableBean { +public class XmppConnectionFactoryBean implements FactoryBean, InitializingBean, DisposableBean { private static final int DEFAULT_PORT = 5222; - private XMPPConnection connection; + private XMPPTCPConnection connection; private String host; @@ -84,13 +87,13 @@ public void setResource(String resource) { } @Override - public void afterPropertiesSet() throws XMPPException { - ConnectionConfiguration configuration = createConnectionConfiguration(host, port, serviceName); + public void afterPropertiesSet() throws XMPPException, IOException, SmackException { + XMPPTCPConnectionConfiguration configuration = createConnectionConfiguration(host, port, serviceName); Assert.notNull(configuration, "'configuration' must not be null"); Assert.hasText(username, "'username' must not be empty"); Assert.hasText(password, "'password' must not be empty"); - connection = new XMPPConnection(configuration); + connection = new XMPPTCPConnection(configuration); connection.connect(); if (StringUtils.hasText(resource)) { connection.login(username, password, resource); @@ -106,13 +109,13 @@ public void destroy() { } @Override - public XMPPConnection getObject() { + public XMPPTCPConnection getObject() { return connection; } @Override - public Class getObjectType() { - return XMPPConnection.class; + public Class getObjectType() { + return XMPPTCPConnection.class; } @Override @@ -127,13 +130,20 @@ public boolean isSingleton() { * @param port the port to connect to * @param serviceName the name of the service to connect to. May be {@code null} */ - protected ConnectionConfiguration createConnectionConfiguration(String host, int port, String serviceName) { + protected XMPPTCPConnectionConfiguration createConnectionConfiguration(String host, int port, String serviceName) { Assert.hasText(host, "'host' must not be empty"); if (StringUtils.hasText(serviceName)) { - return new ConnectionConfiguration(host, port, serviceName); + return XMPPTCPConnectionConfiguration.builder() + .setHost(host) + .setPort(port) + .setServiceName(serviceName) + .build(); } else { - return new ConnectionConfiguration(host, port); + return XMPPTCPConnectionConfiguration.builder() + .setHost(host) + .setPort(port) + .build(); } } diff --git a/spring-ws-support/src/main/java/org/springframework/ws/transport/xmpp/support/XmppTransportUtils.java b/spring-ws-support/src/main/java/org/springframework/ws/transport/xmpp/support/XmppTransportUtils.java index 8321ca62c..8935ea3d0 100644 --- a/spring-ws-support/src/main/java/org/springframework/ws/transport/xmpp/support/XmppTransportUtils.java +++ b/spring-ws-support/src/main/java/org/springframework/ws/transport/xmpp/support/XmppTransportUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,11 +21,12 @@ import java.util.Collections; import java.util.Iterator; +import org.jivesoftware.smack.packet.Message; +import org.jivesoftware.smackx.jiveproperties.JivePropertiesManager; + import org.springframework.util.Assert; import org.springframework.ws.transport.xmpp.XmppTransportConstants; -import org.jivesoftware.smack.packet.Message; - /** * Collection of utility methods to work with Mail transports. * @@ -62,17 +63,17 @@ public static String getErrorMessage(Message message) { } public static void addHeader(Message message, String name, String value) { - message.setProperty(name, value); + JivePropertiesManager.addProperty(message, name, value); } public static Iterator getHeaderNames(Message message) { Assert.notNull(message, "'message' must not be null"); - return message.getPropertyNames().iterator(); + return JivePropertiesManager.getPropertiesNames(message).iterator(); } public static Iterator getHeaders(Message message, String name) { Assert.notNull(message, "'message' must not be null"); - String value = message.getProperty(name).toString(); + String value = JivePropertiesManager.getProperty(message, name).toString(); if (value != null) { return Collections.singletonList(value).iterator(); } diff --git a/spring-ws-support/src/test/java/org/springframework/ws/transport/xmpp/support/XmppConnectionFactoryBeanTest.java b/spring-ws-support/src/test/java/org/springframework/ws/transport/xmpp/support/XmppConnectionFactoryBeanTest.java index 9df0fd33e..7f35ab7d5 100644 --- a/spring-ws-support/src/test/java/org/springframework/ws/transport/xmpp/support/XmppConnectionFactoryBeanTest.java +++ b/spring-ws-support/src/test/java/org/springframework/ws/transport/xmpp/support/XmppConnectionFactoryBeanTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,9 @@ package org.springframework.ws.transport.xmpp.support; +import java.io.IOException; + +import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.XMPPException; import org.junit.Before; import org.junit.Test; @@ -30,12 +33,12 @@ public void createFactoryBean() { factoryBean = new XmppConnectionFactoryBean(); } @Test(expected = IllegalArgumentException.class) - public void noHost() throws XMPPException { + public void noHost() throws XMPPException, IOException, SmackException { factoryBean.afterPropertiesSet(); } @Test(expected = IllegalArgumentException.class) - public void noUsername() throws XMPPException { + public void noUsername() throws XMPPException, IOException, SmackException { factoryBean.setHost("jabber.org"); factoryBean.afterPropertiesSet(); }