diff --git a/build.gradle b/build.gradle index 2068c38793..fe38741754 100644 --- a/build.gradle +++ b/build.gradle @@ -96,7 +96,7 @@ ext { rsocketVersion = '1.1.1' saajVersion = '2.0.1' servletApiVersion = '5.0.0' - smackVersion = '4.3.5' + smackVersion = '4.4.5' springAmqpVersion = project.hasProperty('springAmqpVersion') ? project.springAmqpVersion : '3.0.0-SNAPSHOT' springDataVersion = project.hasProperty('springDataVersion') ? project.springDataVersion : '2022.0.0-SNAPSHOT' springGraphqlVersion = '1.0.0-SNAPSHOT' @@ -1005,12 +1005,12 @@ project('spring-integration-xml') { } project('spring-integration-xmpp') { - description = 'Spring Integration XMPP Support' - dependencies { - api project(':spring-integration-core') - api "org.igniterealtime.smack:smack-tcp:$smackVersion" - api "org.igniterealtime.smack:smack-java7:$smackVersion" - api "org.igniterealtime.smack:smack-extensions:$smackVersion" + description = 'Spring Integration XMPP Support' + dependencies { + api project(':spring-integration-core') + api "org.igniterealtime.smack:smack-tcp:$smackVersion" + api "org.igniterealtime.smack:smack-java8:$smackVersion" + api "org.igniterealtime.smack:smack-extensions:$smackVersion" testImplementation project(':spring-integration-stream') testImplementation "org.igniterealtime.smack:smack-experimental:$smackVersion" diff --git a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/inbound/ChatMessageListeningEndpoint.java b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/inbound/ChatMessageListeningEndpoint.java index ed28da7cca..275573cfdb 100644 --- a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/inbound/ChatMessageListeningEndpoint.java +++ b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/inbound/ChatMessageListeningEndpoint.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 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. @@ -42,6 +42,7 @@ import org.springframework.util.Assert; * @author Oleg Zhurakousky * @author Artem Bilan * @author Gary Russell + * @author Florian Schmaus * * @since 2.0 */ @@ -122,10 +123,9 @@ public class ChatMessageListeningEndpoint extends AbstractXmppConnectionAwareEnd @Override public void processStanza(Stanza packet) { - if (packet instanceof org.jivesoftware.smack.packet.Message) { - org.jivesoftware.smack.packet.Message xmppMessage = (org.jivesoftware.smack.packet.Message) packet; + if (packet instanceof org.jivesoftware.smack.packet.Message xmppMessage) { Map mappedHeaders = - ChatMessageListeningEndpoint.this.headerMapper.toHeadersFromRequest(xmppMessage); + ChatMessageListeningEndpoint.this.headerMapper.toHeadersFromRequest(xmppMessage.asBuilder()); Object messageBody = xmppMessage.getBody(); diff --git a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/outbound/ChatMessageSendingMessageHandler.java b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/outbound/ChatMessageSendingMessageHandler.java index 5191d73592..54199b22de 100644 --- a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/outbound/ChatMessageSendingMessageHandler.java +++ b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/outbound/ChatMessageSendingMessageHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 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,16 +16,18 @@ package org.springframework.integration.xmpp.outbound; -import java.io.StringReader; import java.util.regex.Pattern; import org.jivesoftware.smack.AbstractXMPPConnection; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.packet.ExtensionElement; +import org.jivesoftware.smack.packet.MessageBuilder; +import org.jivesoftware.smack.packet.StanzaBuilder; import org.jivesoftware.smack.provider.ExtensionElementProvider; import org.jivesoftware.smack.util.PacketParserUtils; +import org.jivesoftware.smack.xml.XmlPullParser; +import org.jxmpp.jid.Jid; import org.jxmpp.jid.impl.JidCreate; -import org.xmlpull.v1.XmlPullParser; import org.springframework.integration.xmpp.XmppHeaders; import org.springframework.integration.xmpp.core.AbstractXmppConnectionAwareMessageHandler; @@ -44,6 +46,7 @@ import org.springframework.util.StringUtils; * @author Mario Gray * @author Oleg Zhurakousky * @author Artem Bilan + * @author Florian Schmaus * * @since 2.0 */ @@ -85,27 +88,30 @@ public class ChatMessageSendingMessageHandler extends AbstractXmppConnectionAwar @Override protected void handleMessageInternal(Message message) { + org.jivesoftware.smack.packet.Message xmppMessage; Assert.isTrue(isInitialized(), () -> getComponentName() + "#" + getComponentType() + " must be initialized"); try { Object payload = message.getPayload(); - org.jivesoftware.smack.packet.Message xmppMessage; + MessageBuilder xmppMessageBuilder; if (payload instanceof org.jivesoftware.smack.packet.Message) { xmppMessage = (org.jivesoftware.smack.packet.Message) payload; + xmppMessageBuilder = xmppMessage.asBuilder(); } else { String to = message.getHeaders().get(XmppHeaders.TO, String.class); Assert.state(StringUtils.hasText(to), () -> "The '" + XmppHeaders.TO + "' header must not be null"); - xmppMessage = buildXmppMessage(payload, to); + xmppMessageBuilder = buildXmppMessage(payload, to); } if (this.headerMapper != null) { - this.headerMapper.fromHeadersToRequest(message.getHeaders(), xmppMessage); + this.headerMapper.fromHeadersToRequest(message.getHeaders(), xmppMessageBuilder); } XMPPConnection xmppConnection = getXmppConnection(); if (!xmppConnection.isConnected() && xmppConnection instanceof AbstractXMPPConnection) { ((AbstractXMPPConnection) xmppConnection).connect(); } + xmppMessage = xmppMessageBuilder.build(); xmppConnection.sendStanza(xmppMessage); } catch (InterruptedException e) { @@ -117,14 +123,16 @@ public class ChatMessageSendingMessageHandler extends AbstractXmppConnectionAwar } } - private org.jivesoftware.smack.packet.Message buildXmppMessage(Object payload, String to) + private MessageBuilder buildXmppMessage(Object payload, String to) throws Exception { // NOSONAR Smack throws it - org.jivesoftware.smack.packet.Message xmppMessage; - xmppMessage = new org.jivesoftware.smack.packet.Message(JidCreate.from(to)); + Jid toJid = JidCreate.from(to); + MessageBuilder xmppMessageBuilder = + StanzaBuilder.buildMessage() + .to(toJid); - if (payload instanceof ExtensionElement) { - xmppMessage.addExtension((ExtensionElement) payload); + if (payload instanceof ExtensionElement extensionElement) { + xmppMessageBuilder.addExtension(extensionElement); } else if (payload instanceof String) { if (this.extensionProvider != null) { @@ -135,13 +143,13 @@ public class ChatMessageSendingMessageHandler extends AbstractXmppConnectionAwar // if the target content isn't XML. data = "" + data + ""; } - XmlPullParser xmlPullParser = PacketParserUtils.newXmppParser(new StringReader(data)); - xmlPullParser.next(); + XmlPullParser xmlPullParser = PacketParserUtils.getParserFor(data); ExtensionElement extension = this.extensionProvider.parse(xmlPullParser); - xmppMessage.addExtension(extension); + xmppMessageBuilder.addExtension(extension); } else { - xmppMessage.setBody((String) payload); + String body = (String) payload; + xmppMessageBuilder.setBody(body); } } else { @@ -151,7 +159,7 @@ public class ChatMessageSendingMessageHandler extends AbstractXmppConnectionAwar "are supported. Received [" + payload.getClass().getName() + "]. Consider adding a Transformer prior to this adapter."); } - return xmppMessage; + return xmppMessageBuilder; } } diff --git a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/support/DefaultXmppHeaderMapper.java b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/support/DefaultXmppHeaderMapper.java index 94672e77e8..e751c906ec 100644 --- a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/support/DefaultXmppHeaderMapper.java +++ b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/support/DefaultXmppHeaderMapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 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. @@ -22,10 +22,10 @@ import java.util.List; import java.util.Map; import org.jivesoftware.smack.packet.Message; +import org.jivesoftware.smack.packet.MessageBuilder; import org.jivesoftware.smackx.jiveproperties.JivePropertiesManager; import org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension; import org.jxmpp.jid.Jid; -import org.jxmpp.jid.impl.JidCreate; import org.jxmpp.stringprep.XmppStringprepException; import org.springframework.integration.mapping.AbstractHeaderMapper; @@ -43,7 +43,7 @@ import org.springframework.util.StringUtils; * * @since 2.1 */ -public class DefaultXmppHeaderMapper extends AbstractHeaderMapper implements XmppHeaderMapper { +public class DefaultXmppHeaderMapper extends AbstractHeaderMapper implements XmppHeaderMapper { private static final List STANDARD_HEADER_NAMES = new ArrayList<>(); @@ -60,7 +60,7 @@ public class DefaultXmppHeaderMapper extends AbstractHeaderMapper imple } @Override - protected Map extractStandardHeaders(Message source) { + protected Map extractStandardHeaders(MessageBuilder source) { Map headers = new HashMap<>(); Jid from = source.getFrom(); if (from != null) { @@ -86,9 +86,9 @@ public class DefaultXmppHeaderMapper extends AbstractHeaderMapper imple } @Override - protected Map extractUserDefinedHeaders(Message source) { + protected Map extractUserDefinedHeaders(MessageBuilder source) { Map headers = new HashMap<>(); - JivePropertiesExtension jpe = (JivePropertiesExtension) source.getExtension(JivePropertiesExtension.NAMESPACE); + JivePropertiesExtension jpe = JivePropertiesExtension.from(source.build()); if (jpe == null) { return headers; } @@ -99,7 +99,7 @@ public class DefaultXmppHeaderMapper extends AbstractHeaderMapper imple } @Override - protected void populateStandardHeaders(Map headers, Message target) { + protected void populateStandardHeaders(Map headers, MessageBuilder target) { String threadId = getHeaderIfAvailable(headers, XmppHeaders.THREAD, String.class); if (StringUtils.hasText(threadId)) { target.setThread(threadId); @@ -125,16 +125,16 @@ public class DefaultXmppHeaderMapper extends AbstractHeaderMapper imple } } } - if (typeHeader instanceof Message.Type) { - target.setType((Message.Type) typeHeader); + if (typeHeader instanceof Message.Type messageType) { + target.ofType(messageType); } } - private void populateToHeader(Map headers, Message target) { + private void populateToHeader(Map headers, MessageBuilder target) { String to = getHeaderIfAvailable(headers, XmppHeaders.TO, String.class); if (StringUtils.hasText(to)) { try { - target.setTo(JidCreate.from(to)); + target.to(to); } catch (XmppStringprepException e) { throw new IllegalStateException("Cannot parse 'xmpp_to' header value", e); @@ -142,11 +142,11 @@ public class DefaultXmppHeaderMapper extends AbstractHeaderMapper imple } } - private void populateFromHeader(Map headers, Message target) { + private void populateFromHeader(Map headers, MessageBuilder target) { String from = getHeaderIfAvailable(headers, XmppHeaders.FROM, String.class); if (StringUtils.hasText(from)) { try { - target.setFrom(JidCreate.from(from)); + target.from(from); } catch (XmppStringprepException e) { throw new IllegalStateException("Cannot parse 'xmpp_from' header value", e); @@ -155,7 +155,7 @@ public class DefaultXmppHeaderMapper extends AbstractHeaderMapper imple } @Override - protected void populateUserDefinedHeader(String headerName, Object headerValue, Message target) { + protected void populateUserDefinedHeader(String headerName, Object headerValue, MessageBuilder target) { JivePropertiesManager.addProperty(target, headerName, headerValue); } diff --git a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/support/XmppHeaderMapper.java b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/support/XmppHeaderMapper.java index 1e205fb261..e96dd6e37b 100644 --- a/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/support/XmppHeaderMapper.java +++ b/spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/support/XmppHeaderMapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 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,17 +16,19 @@ package org.springframework.integration.xmpp.support; -import org.jivesoftware.smack.packet.Message; +import org.jivesoftware.smack.packet.MessageBuilder; import org.springframework.integration.mapping.RequestReplyHeaderMapper; /** * A convenience interface that extends {@link RequestReplyHeaderMapper} - * but parameterized with the Smack API {@link Message}. + * but parameterized with the Smack API {@link MessageBuilder}. * * @author Mark Fisher * @author Gary Russell + * @author Florian Schmaus + * * @since 2.1 */ -public interface XmppHeaderMapper extends RequestReplyHeaderMapper { +public interface XmppHeaderMapper extends RequestReplyHeaderMapper { } diff --git a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/ChatMessageInboundChannelAdapterParserTests-context.xml b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/ChatMessageInboundChannelAdapterParserTests-context.xml index dd670d4950..0e447a443e 100644 --- a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/ChatMessageInboundChannelAdapterParserTests-context.xml +++ b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/ChatMessageInboundChannelAdapterParserTests-context.xml @@ -12,7 +12,7 @@ - + diff --git a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/ChatMessageInboundChannelAdapterParserTests.java b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/ChatMessageInboundChannelAdapterParserTests.java index 5ae18b4b9f..0c8e58f198 100644 --- a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/ChatMessageInboundChannelAdapterParserTests.java +++ b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/ChatMessageInboundChannelAdapterParserTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 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,7 +23,8 @@ import java.util.Map; import org.jivesoftware.smack.StanzaListener; import org.jivesoftware.smack.XMPPConnection; -import org.jivesoftware.smack.packet.Message; +import org.jivesoftware.smack.packet.MessageBuilder; +import org.jivesoftware.smack.packet.StanzaBuilder; import org.jivesoftware.smackx.jiveproperties.JivePropertiesManager; import org.junit.Test; import org.junit.runner.RunWith; @@ -102,12 +103,12 @@ public class ChatMessageInboundChannelAdapterParserTests { StanzaListener stanzaListener = TestUtils.getPropertyValue(adapter, "stanzaListener", StanzaListener.class); - Message message = new Message(); + MessageBuilder message = StanzaBuilder.buildMessage(); message.setBody("hello"); - message.setTo(JidCreate.from("oleg")); + message.to(JidCreate.from("oleg")); JivePropertiesManager.addProperty(message, "foo", "foo"); JivePropertiesManager.addProperty(message, "bar", "bar"); - stanzaListener.processStanza(message); + stanzaListener.processStanza(message.build()); org.springframework.messaging.Message siMessage = xmppInbound.receive(0); assertThat(siMessage.getHeaders().get("foo")).isEqualTo("foo"); assertThat(siMessage.getHeaders().get("xmpp_to")).isEqualTo("oleg"); diff --git a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/ignore/OutboundPresenceTests.java b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/ignore/OutboundPresenceTests.java index b8cc831cea..cb0ff6aaea 100644 --- a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/ignore/OutboundPresenceTests.java +++ b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/ignore/OutboundPresenceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 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. @@ -17,6 +17,7 @@ package org.springframework.integration.xmpp.ignore; import org.jivesoftware.smack.packet.Presence; +import org.jivesoftware.smack.packet.StanzaBuilder; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; @@ -33,6 +34,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; * Tests {@link PresenceSendingMessageHandler} to ensure that we are able to publish status. * * @author Josh Long + * @author Florian Schmaus + * * @since 2.0 */ @ContextConfiguration @@ -46,8 +49,9 @@ public class OutboundPresenceTests { @Test @Ignore public void testOutbound() throws Throwable { - Presence presence = new Presence(Presence.Type.available); - input.send(new GenericMessage(presence)); + Presence presence = StanzaBuilder.buildPresence().build(); + input.send(new GenericMessage<>(presence)); Thread.sleep(60 * 1000); } + } diff --git a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/ignore/SmackMessageSampleTests.java b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/ignore/SmackMessageSampleTests.java index 0be37cacde..3d84978860 100644 --- a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/ignore/SmackMessageSampleTests.java +++ b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/ignore/SmackMessageSampleTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 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,9 @@ package org.springframework.integration.xmpp.ignore; +import org.jivesoftware.smack.packet.StanzaBuilder; import org.junit.Ignore; import org.junit.Test; -import org.jxmpp.jid.impl.JidCreate; import org.jxmpp.stringprep.XmppStringprepException; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -30,6 +30,7 @@ import org.springframework.messaging.support.GenericMessage; * @author Oleg Zhurakousky * @author Gary Russell * @author Artem Bilan + * @author Florian Schmaus */ public class SmackMessageSampleTests { @@ -41,9 +42,10 @@ public class SmackMessageSampleTests { MessageChannel xmppInput = ac.getBean("xmppInput", MessageChannel.class); - org.jivesoftware.smack.packet.Message smackMessage = - new org.jivesoftware.smack.packet.Message(JidCreate.from("springintegration@gmail.com")); - smackMessage.setBody("Message sent as Smack Message"); + org.jivesoftware.smack.packet.Message smackMessage = StanzaBuilder.buildMessage() + .to("springintegration@gmail.com") + .setBody("Message sent as Smack Message") + .build(); Message message = new GenericMessage<>(smackMessage); diff --git a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/inbound/ChatMessageListeningEndpointTests.java b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/inbound/ChatMessageListeningEndpointTests.java index 1bc1818c64..793471298b 100644 --- a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/inbound/ChatMessageListeningEndpointTests.java +++ b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/inbound/ChatMessageListeningEndpointTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -26,7 +26,7 @@ import static org.mockito.BDDMockito.willAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; -import java.io.StringReader; +import java.io.IOException; import java.util.HashSet; import java.util.Set; import java.util.concurrent.CountDownLatch; @@ -35,16 +35,20 @@ import java.util.concurrent.TimeUnit; import org.jivesoftware.smack.StanzaListener; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.packet.Message; +import org.jivesoftware.smack.packet.MessageBuilder; +import org.jivesoftware.smack.packet.StanzaBuilder; +import org.jivesoftware.smack.packet.StreamOpen; import org.jivesoftware.smack.tcp.XMPPTCPConnection; import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration; import org.jivesoftware.smack.util.PacketParserUtils; +import org.jivesoftware.smack.xml.XmlPullParser; +import org.jivesoftware.smack.xml.XmlPullParserException; import org.jivesoftware.smackx.gcm.packet.GcmPacketExtension; import org.junit.jupiter.api.Test; import org.jxmpp.jid.impl.JidCreate; import org.jxmpp.stringprep.XmppStringprepException; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; -import org.xmlpull.v1.XmlPullParser; import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.factory.BeanFactory; @@ -143,10 +147,11 @@ public class ChatMessageListeningEndpointTests { endpoint.setErrorChannel(errorChannel); endpoint.afterPropertiesSet(); StanzaListener listener = (StanzaListener) TestUtils.getPropertyValue(endpoint, "stanzaListener"); - Message smackMessage = new Message(JidCreate.from("kermit@frog.com")); + MessageBuilder smackMessage = StanzaBuilder.buildMessage(); + smackMessage.to(JidCreate.from("kermit@frog.com")); smackMessage.setBody("hello"); smackMessage.setThread("1234"); - listener.processStanza(smackMessage); + listener.processStanza(smackMessage.build()); ErrorMessage msg = (ErrorMessage) errorChannel.receive(); @@ -167,12 +172,11 @@ public class ChatMessageListeningEndpointTests { endpoint.afterPropertiesSet(); endpoint.start(); - Message smackMessage = new Message(); + MessageBuilder smackMessage = StanzaBuilder.buildMessage(); smackMessage.setBody("foo"); XmlPullParser xmlPullParser = - PacketParserUtils.newXmppParser(new StringReader(smackMessage.toXML(null).toString())); - xmlPullParser.next(); + PacketParserUtils.getParserFor(smackMessage.build().toXML().toString()); testXMPPConnection.parseAndProcessStanza(xmlPullParser); org.springframework.messaging.Message receive = inputChannel.receive(10000); @@ -196,9 +200,8 @@ public class ChatMessageListeningEndpointTests { endpoint.setPayloadExpression(null); - smackMessage = new Message(); - xmlPullParser = PacketParserUtils.newXmppParser(new StringReader(smackMessage.toXML(null).toString())); - xmlPullParser.next(); + Message message = StanzaBuilder.buildMessage().build(); + xmlPullParser = PacketParserUtils.getParserFor(message.toXML().toString()); testXMPPConnection.parseAndProcessStanza(xmlPullParser); ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(String.class); @@ -208,7 +211,7 @@ public class ChatMessageListeningEndpointTests { verify(logger).info(argumentCaptor.capture()); assertThat(argumentCaptor.getValue()) - .isEqualTo("The XMPP Message [" + smackMessage + "] with empty body is ignored."); + .isEqualTo("The XMPP Message [" + message + "] with empty body is ignored."); endpoint.stop(); } @@ -225,7 +228,7 @@ public class ChatMessageListeningEndpointTests { " }\n" + "}"; GcmPacketExtension packetExtension = new GcmPacketExtension(data); - Message smackMessage = new Message(); + MessageBuilder smackMessage = StanzaBuilder.buildMessage(); smackMessage.addExtension(packetExtension); TestXMPPConnection testXMPPConnection = new TestXMPPConnection(); @@ -241,8 +244,7 @@ public class ChatMessageListeningEndpointTests { endpoint.start(); XmlPullParser xmlPullParser = - PacketParserUtils.newXmppParser(new StringReader(smackMessage.toXML(null).toString())); - xmlPullParser.next(); + PacketParserUtils.getParserFor(smackMessage.build().toXML().toString()); testXMPPConnection.parseAndProcessStanza(xmlPullParser); org.springframework.messaging.Message receive = inputChannel.receive(10000); @@ -255,12 +257,24 @@ public class ChatMessageListeningEndpointTests { private static class TestXMPPConnection extends XMPPTCPConnection { + TestXMPPConnection() throws XmppStringprepException { - super(XMPPTCPConnectionConfiguration.builder().setXmppDomain("/foo").build()); + super(XMPPTCPConnectionConfiguration.builder().setXmppDomain("example.org").build()); + + StreamOpen streamOpen = new StreamOpen("example.org"); + XmlPullParser parser; + try { + parser = PacketParserUtils.getParserFor(streamOpen.toXML().toString()); + } + catch (XmlPullParserException | IOException e) { + throw new AssertionError(e); + } + onStreamOpen(parser); } @Override - protected void parseAndProcessStanza(XmlPullParser parser) throws Exception { + protected void parseAndProcessStanza(XmlPullParser parser) + throws XmlPullParserException, IOException, InterruptedException { super.parseAndProcessStanza(parser); } diff --git a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/inbound/PresenceListeningEndpointTests.java b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/inbound/PresenceListeningEndpointTests.java index 44097d99ef..64b732d092 100644 --- a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/inbound/PresenceListeningEndpointTests.java +++ b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/inbound/PresenceListeningEndpointTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 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,8 +23,7 @@ import java.util.Set; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.packet.Presence; -import org.jivesoftware.smack.packet.Presence.Mode; -import org.jivesoftware.smack.packet.Presence.Type; +import org.jivesoftware.smack.packet.StanzaBuilder; import org.jivesoftware.smack.roster.Roster; import org.jivesoftware.smack.roster.RosterListener; import org.junit.Test; @@ -45,6 +44,7 @@ import org.springframework.messaging.support.ErrorMessage; * @author Gunnar Hillert * @author Gary Russell * @author Artem Bilan + * @author Florian Schmaus */ public class PresenceListeningEndpointTests { @@ -74,7 +74,6 @@ public class PresenceListeningEndpointTests { } @Test - @SuppressWarnings("unchecked") public void testRosterPresenceChangeEvent() { XMPPConnection connection = mock(XMPPConnection.class); PresenceListeningEndpoint rosterEndpoint = new PresenceListeningEndpoint(connection); @@ -84,7 +83,11 @@ public class PresenceListeningEndpointTests { rosterEndpoint.afterPropertiesSet(); rosterEndpoint.start(); RosterListener rosterListener = (RosterListener) TestUtils.getPropertyValue(rosterEndpoint, "rosterListener"); - Presence presence = new Presence(Type.available, "Hello", 1, Mode.chat); + Presence presence = StanzaBuilder.buildPresence() + .setStatus("Hello") + .setPriority(1) + .setMode(Presence.Mode.chat) + .build(); rosterListener.presenceChanged(presence); Message message = channel.receive(10); assertThat(message.getPayload()).isEqualTo(presence); @@ -126,7 +129,7 @@ public class PresenceListeningEndpointTests { endpoint.setErrorChannel(errorChannel); endpoint.afterPropertiesSet(); RosterListener listener = (RosterListener) TestUtils.getPropertyValue(endpoint, "rosterListener"); - Presence presence = new Presence(Type.available); + Presence presence = StanzaBuilder.buildPresence().build(); listener.presenceChanged(presence); diff --git a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/outbound/ChatMessageSendingMessageHandlerTests.java b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/outbound/ChatMessageSendingMessageHandlerTests.java index 749023eab1..519da3cff0 100644 --- a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/outbound/ChatMessageSendingMessageHandlerTests.java +++ b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/outbound/ChatMessageSendingMessageHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 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. @@ -25,10 +25,10 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smack.packet.StanzaBuilder; import org.jivesoftware.smackx.gcm.packet.GcmPacketExtension; import org.jivesoftware.smackx.gcm.provider.GcmExtensionProvider; import org.junit.Test; -import org.jxmpp.jid.impl.JidCreate; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; @@ -46,6 +46,7 @@ import org.springframework.messaging.support.GenericMessage; * @author Oleg Zhurakousky * @author Gunnar Hillert * @author Artem Bilan + * @author Florian Schmaus */ public class ChatMessageSendingMessageHandlerTests { @@ -111,28 +112,43 @@ public class ChatMessageSendingMessageHandlerTests { handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); - org.jivesoftware.smack.packet.Message smackMessage = - new org.jivesoftware.smack.packet.Message(JidCreate.from("kermit@frog.com")); - smackMessage.setBody("Test Message"); + org.jivesoftware.smack.packet.Message smackMessage = StanzaBuilder.buildMessage() + .to("kermit@frog.com") + .setBody("Test Message") + .build(); Message message = MessageBuilder.withPayload(smackMessage).build(); // first Message new handler.handleMessage(message); - verify(connection, times(1)).sendStanza(smackMessage); + verify(connection).isConnected(); + verify(connection).sendStanza(Mockito.argThat((org.jivesoftware.smack.packet.Message m) -> { + boolean bodyMatches = "Test Message".equals(m.getBody()); + boolean toMatches = m.getTo().toString().equals("kermit@frog.com"); + return bodyMatches && toMatches; + })); // assuming we know thread ID although currently we do not provide this capability - smackMessage = new org.jivesoftware.smack.packet.Message(JidCreate.from("kermit@frog.com")); - smackMessage.setBody("Hello Kitty"); - smackMessage.setThread("123"); + smackMessage = StanzaBuilder.buildMessage() + .ofType(org.jivesoftware.smack.packet.Message.Type.normal) + .to("kermit@frog.com") + .setBody("Hello Kitty") + .setThread("123") + .build(); message = MessageBuilder.withPayload(smackMessage).build(); reset(connection); handler.handleMessage(message); // in threaded conversation we need to look for existing chat - verify(connection, times(1)).sendStanza(smackMessage); + verify(connection).isConnected(); + verify(connection).sendStanza(Mockito.argThat((org.jivesoftware.smack.packet.Message m) -> { + boolean bodyMatches = "Hello Kitty".equals(m.getBody()); + boolean toMatches = "kermit@frog.com".equals(m.getTo().toString()); + boolean threadMatches = "123".equals(m.getThread()); + return bodyMatches && toMatches && threadMatches; + })); } @Test diff --git a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/outbound/PresenceSendingMessageHandlerTests.java b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/outbound/PresenceSendingMessageHandlerTests.java index 0cacfe202b..e1b136491f 100644 --- a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/outbound/PresenceSendingMessageHandlerTests.java +++ b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/outbound/PresenceSendingMessageHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 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. @@ -20,7 +20,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import org.jivesoftware.smack.XMPPConnection; -import org.jivesoftware.smack.packet.Presence; +import org.jivesoftware.smack.packet.StanzaBuilder; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; @@ -34,19 +34,19 @@ import org.springframework.messaging.support.GenericMessage; * @author Oleg Zhurakousky * @author Gunnar Hillert * @author Artem Bilan + * @author Florian Schmaus */ public class PresenceSendingMessageHandlerTests { - @SuppressWarnings({"unchecked", "rawtypes"}) @Test public void testPresencePayload() { PresenceSendingMessageHandler handler = new PresenceSendingMessageHandler(mock(XMPPConnection.class)); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); - handler.handleMessage(new GenericMessage(new Presence(Presence.Type.subscribe))); + handler.handleMessage(new GenericMessage<>(StanzaBuilder.buildPresence().build())); } - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) @Test(expected = MessageHandlingException.class) public void testWrongPayload() { PresenceSendingMessageHandler handler = new PresenceSendingMessageHandler(mock(XMPPConnection.class)); diff --git a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/support/DefaultXmppHeaderMapperTests.java b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/support/DefaultXmppHeaderMapperTests.java index ab3f14b86a..67f6ebe877 100644 --- a/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/support/DefaultXmppHeaderMapperTests.java +++ b/spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/support/DefaultXmppHeaderMapperTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 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. @@ -22,9 +22,10 @@ import java.util.HashMap; import java.util.Map; import org.jivesoftware.smack.packet.Message; +import org.jivesoftware.smack.packet.MessageBuilder; +import org.jivesoftware.smack.packet.StanzaBuilder; import org.jivesoftware.smackx.jiveproperties.JivePropertiesManager; import org.junit.Test; -import org.jxmpp.jid.impl.JidCreate; import org.jxmpp.stringprep.XmppStringprepException; import org.springframework.integration.xmpp.XmppHeaders; @@ -51,7 +52,7 @@ public class DefaultXmppHeaderMapperTests { headerMap.put(XmppHeaders.SUBJECT, "test.subject"); headerMap.put(XmppHeaders.TYPE, "headline"); MessageHeaders headers = new MessageHeaders(headerMap); - Message target = new Message(); + MessageBuilder target = StanzaBuilder.buildMessage(); mapper.fromHeadersToRequest(headers, target); // "standard" XMPP headers @@ -84,7 +85,7 @@ public class DefaultXmppHeaderMapperTests { headerMap.put(XmppHeaders.SUBJECT, "test.subject"); headerMap.put(XmppHeaders.TYPE, "headline"); MessageHeaders headers = new MessageHeaders(headerMap); - Message target = new Message(); + MessageBuilder target = StanzaBuilder.buildMessage(); mapper.fromHeadersToRequest(headers, target); // "standard" XMPP headers not included @@ -94,7 +95,7 @@ public class DefaultXmppHeaderMapperTests { Object from = target.getFrom(); assertThat(from).isNull(); assertThat(target.getSubject()).isNull(); - assertThat(target.getType()).isEqualTo(Message.Type.normal); + assertThat(target.getType()).isNull(); // user-defined headers are included if in the list assertThat(JivePropertiesManager.getProperty(target, "userDefined1")).isEqualTo("foo"); @@ -111,10 +112,12 @@ public class DefaultXmppHeaderMapperTests { @Test public void toHeadersStandardOnly() throws XmppStringprepException { DefaultXmppHeaderMapper mapper = new DefaultXmppHeaderMapper(); - Message source = new Message(JidCreate.from("test.to"), Message.Type.headline); - source.setFrom(JidCreate.from("test.from")); - source.setSubject("test.subject"); - source.setThread("test.thread"); + MessageBuilder source = StanzaBuilder.buildMessage() + .ofType(Message.Type.headline) + .to("test.to") + .from("test.from") + .setSubject("test.subject") + .setThread("test.thread"); JivePropertiesManager.addProperty(source, "userDefined1", "foo"); JivePropertiesManager.addProperty(source, "userDefined2", "bar"); Map headers = mapper.toHeadersFromRequest(source); @@ -131,10 +134,12 @@ public class DefaultXmppHeaderMapperTests { public void toHeadersUserDefinedOnly() throws XmppStringprepException { DefaultXmppHeaderMapper mapper = new DefaultXmppHeaderMapper(); mapper.setReplyHeaderNames("userDefined*"); - Message source = new Message(JidCreate.from("test.to"), Message.Type.headline); - source.setFrom(JidCreate.from("test.from")); - source.setSubject("test.subject"); - source.setThread("test.thread"); + MessageBuilder source = StanzaBuilder.buildMessage() + .ofType(Message.Type.headline) + .to("test.to") + .from("test.from") + .setSubject("test.subject") + .setThread("test.thread"); JivePropertiesManager.addProperty(source, "userDefined1", "foo"); JivePropertiesManager.addProperty(source, "userDefined2", "bar"); Map headers = mapper.toHeadersFromReply(source);