Skip to content

Commit 330dfd0

Browse files
Flowdalicartembilan
authored andcommitted
GH-3462: Upgrade to Smack 4.4.5
Fixes #3462
1 parent 4484c4d commit 330dfd0

14 files changed

+165
-110
lines changed

build.gradle

+7-7
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ ext {
9696
rsocketVersion = '1.1.1'
9797
saajVersion = '2.0.1'
9898
servletApiVersion = '5.0.0'
99-
smackVersion = '4.3.5'
99+
smackVersion = '4.4.5'
100100
springAmqpVersion = project.hasProperty('springAmqpVersion') ? project.springAmqpVersion : '3.0.0-SNAPSHOT'
101101
springDataVersion = project.hasProperty('springDataVersion') ? project.springDataVersion : '2022.0.0-SNAPSHOT'
102102
springGraphqlVersion = '1.0.0-SNAPSHOT'
@@ -1005,12 +1005,12 @@ project('spring-integration-xml') {
10051005
}
10061006

10071007
project('spring-integration-xmpp') {
1008-
description = 'Spring Integration XMPP Support'
1009-
dependencies {
1010-
api project(':spring-integration-core')
1011-
api "org.igniterealtime.smack:smack-tcp:$smackVersion"
1012-
api "org.igniterealtime.smack:smack-java7:$smackVersion"
1013-
api "org.igniterealtime.smack:smack-extensions:$smackVersion"
1008+
description = 'Spring Integration XMPP Support'
1009+
dependencies {
1010+
api project(':spring-integration-core')
1011+
api "org.igniterealtime.smack:smack-tcp:$smackVersion"
1012+
api "org.igniterealtime.smack:smack-java8:$smackVersion"
1013+
api "org.igniterealtime.smack:smack-extensions:$smackVersion"
10141014

10151015
testImplementation project(':spring-integration-stream')
10161016
testImplementation "org.igniterealtime.smack:smack-experimental:$smackVersion"

spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/inbound/ChatMessageListeningEndpoint.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,6 +42,7 @@
4242
* @author Oleg Zhurakousky
4343
* @author Artem Bilan
4444
* @author Gary Russell
45+
* @author Florian Schmaus
4546
*
4647
* @since 2.0
4748
*/
@@ -122,10 +123,9 @@ private class ChatMessagePublishingStanzaListener implements StanzaListener {
122123

123124
@Override
124125
public void processStanza(Stanza packet) {
125-
if (packet instanceof org.jivesoftware.smack.packet.Message) {
126-
org.jivesoftware.smack.packet.Message xmppMessage = (org.jivesoftware.smack.packet.Message) packet;
126+
if (packet instanceof org.jivesoftware.smack.packet.Message xmppMessage) {
127127
Map<String, ?> mappedHeaders =
128-
ChatMessageListeningEndpoint.this.headerMapper.toHeadersFromRequest(xmppMessage);
128+
ChatMessageListeningEndpoint.this.headerMapper.toHeadersFromRequest(xmppMessage.asBuilder());
129129

130130
Object messageBody = xmppMessage.getBody();
131131

spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/outbound/ChatMessageSendingMessageHandler.java

+24-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,16 +16,18 @@
1616

1717
package org.springframework.integration.xmpp.outbound;
1818

19-
import java.io.StringReader;
2019
import java.util.regex.Pattern;
2120

2221
import org.jivesoftware.smack.AbstractXMPPConnection;
2322
import org.jivesoftware.smack.XMPPConnection;
2423
import org.jivesoftware.smack.packet.ExtensionElement;
24+
import org.jivesoftware.smack.packet.MessageBuilder;
25+
import org.jivesoftware.smack.packet.StanzaBuilder;
2526
import org.jivesoftware.smack.provider.ExtensionElementProvider;
2627
import org.jivesoftware.smack.util.PacketParserUtils;
28+
import org.jivesoftware.smack.xml.XmlPullParser;
29+
import org.jxmpp.jid.Jid;
2730
import org.jxmpp.jid.impl.JidCreate;
28-
import org.xmlpull.v1.XmlPullParser;
2931

3032
import org.springframework.integration.xmpp.XmppHeaders;
3133
import org.springframework.integration.xmpp.core.AbstractXmppConnectionAwareMessageHandler;
@@ -44,6 +46,7 @@
4446
* @author Mario Gray
4547
* @author Oleg Zhurakousky
4648
* @author Artem Bilan
49+
* @author Florian Schmaus
4750
*
4851
* @since 2.0
4952
*/
@@ -85,27 +88,30 @@ public String getComponentType() {
8588

8689
@Override
8790
protected void handleMessageInternal(Message<?> message) {
91+
org.jivesoftware.smack.packet.Message xmppMessage;
8892
Assert.isTrue(isInitialized(),
8993
() -> getComponentName() + "#" + getComponentType() + " must be initialized");
9094
try {
9195
Object payload = message.getPayload();
92-
org.jivesoftware.smack.packet.Message xmppMessage;
96+
MessageBuilder xmppMessageBuilder;
9397
if (payload instanceof org.jivesoftware.smack.packet.Message) {
9498
xmppMessage = (org.jivesoftware.smack.packet.Message) payload;
99+
xmppMessageBuilder = xmppMessage.asBuilder();
95100
}
96101
else {
97102
String to = message.getHeaders().get(XmppHeaders.TO, String.class);
98103
Assert.state(StringUtils.hasText(to), () -> "The '" + XmppHeaders.TO + "' header must not be null");
99-
xmppMessage = buildXmppMessage(payload, to);
104+
xmppMessageBuilder = buildXmppMessage(payload, to);
100105
}
101106

102107
if (this.headerMapper != null) {
103-
this.headerMapper.fromHeadersToRequest(message.getHeaders(), xmppMessage);
108+
this.headerMapper.fromHeadersToRequest(message.getHeaders(), xmppMessageBuilder);
104109
}
105110
XMPPConnection xmppConnection = getXmppConnection();
106111
if (!xmppConnection.isConnected() && xmppConnection instanceof AbstractXMPPConnection) {
107112
((AbstractXMPPConnection) xmppConnection).connect();
108113
}
114+
xmppMessage = xmppMessageBuilder.build();
109115
xmppConnection.sendStanza(xmppMessage);
110116
}
111117
catch (InterruptedException e) {
@@ -117,14 +123,16 @@ protected void handleMessageInternal(Message<?> message) {
117123
}
118124
}
119125

120-
private org.jivesoftware.smack.packet.Message buildXmppMessage(Object payload, String to)
126+
private MessageBuilder buildXmppMessage(Object payload, String to)
121127
throws Exception { // NOSONAR Smack throws it
122128

123-
org.jivesoftware.smack.packet.Message xmppMessage;
124-
xmppMessage = new org.jivesoftware.smack.packet.Message(JidCreate.from(to));
129+
Jid toJid = JidCreate.from(to);
130+
MessageBuilder xmppMessageBuilder =
131+
StanzaBuilder.buildMessage()
132+
.to(toJid);
125133

126-
if (payload instanceof ExtensionElement) {
127-
xmppMessage.addExtension((ExtensionElement) payload);
134+
if (payload instanceof ExtensionElement extensionElement) {
135+
xmppMessageBuilder.addExtension(extensionElement);
128136
}
129137
else if (payload instanceof String) {
130138
if (this.extensionProvider != null) {
@@ -135,13 +143,13 @@ else if (payload instanceof String) {
135143
// if the target content isn't XML.
136144
data = "<root>" + data + "</root>";
137145
}
138-
XmlPullParser xmlPullParser = PacketParserUtils.newXmppParser(new StringReader(data));
139-
xmlPullParser.next();
146+
XmlPullParser xmlPullParser = PacketParserUtils.getParserFor(data);
140147
ExtensionElement extension = this.extensionProvider.parse(xmlPullParser);
141-
xmppMessage.addExtension(extension);
148+
xmppMessageBuilder.addExtension(extension);
142149
}
143150
else {
144-
xmppMessage.setBody((String) payload);
151+
String body = (String) payload;
152+
xmppMessageBuilder.setBody(body);
145153
}
146154
}
147155
else {
@@ -151,7 +159,7 @@ else if (payload instanceof String) {
151159
"are supported. Received [" + payload.getClass().getName() +
152160
"]. Consider adding a Transformer prior to this adapter.");
153161
}
154-
return xmppMessage;
162+
return xmppMessageBuilder;
155163
}
156164

157165
}

spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/support/DefaultXmppHeaderMapper.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,10 +22,10 @@
2222
import java.util.Map;
2323

2424
import org.jivesoftware.smack.packet.Message;
25+
import org.jivesoftware.smack.packet.MessageBuilder;
2526
import org.jivesoftware.smackx.jiveproperties.JivePropertiesManager;
2627
import org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension;
2728
import org.jxmpp.jid.Jid;
28-
import org.jxmpp.jid.impl.JidCreate;
2929
import org.jxmpp.stringprep.XmppStringprepException;
3030

3131
import org.springframework.integration.mapping.AbstractHeaderMapper;
@@ -43,7 +43,7 @@
4343
*
4444
* @since 2.1
4545
*/
46-
public class DefaultXmppHeaderMapper extends AbstractHeaderMapper<Message> implements XmppHeaderMapper {
46+
public class DefaultXmppHeaderMapper extends AbstractHeaderMapper<MessageBuilder> implements XmppHeaderMapper {
4747

4848
private static final List<String> STANDARD_HEADER_NAMES = new ArrayList<>();
4949

@@ -60,7 +60,7 @@ public DefaultXmppHeaderMapper() {
6060
}
6161

6262
@Override
63-
protected Map<String, Object> extractStandardHeaders(Message source) {
63+
protected Map<String, Object> extractStandardHeaders(MessageBuilder source) {
6464
Map<String, Object> headers = new HashMap<>();
6565
Jid from = source.getFrom();
6666
if (from != null) {
@@ -86,9 +86,9 @@ protected Map<String, Object> extractStandardHeaders(Message source) {
8686
}
8787

8888
@Override
89-
protected Map<String, Object> extractUserDefinedHeaders(Message source) {
89+
protected Map<String, Object> extractUserDefinedHeaders(MessageBuilder source) {
9090
Map<String, Object> headers = new HashMap<>();
91-
JivePropertiesExtension jpe = (JivePropertiesExtension) source.getExtension(JivePropertiesExtension.NAMESPACE);
91+
JivePropertiesExtension jpe = JivePropertiesExtension.from(source.build());
9292
if (jpe == null) {
9393
return headers;
9494
}
@@ -99,7 +99,7 @@ protected Map<String, Object> extractUserDefinedHeaders(Message source) {
9999
}
100100

101101
@Override
102-
protected void populateStandardHeaders(Map<String, Object> headers, Message target) {
102+
protected void populateStandardHeaders(Map<String, Object> headers, MessageBuilder target) {
103103
String threadId = getHeaderIfAvailable(headers, XmppHeaders.THREAD, String.class);
104104
if (StringUtils.hasText(threadId)) {
105105
target.setThread(threadId);
@@ -125,28 +125,28 @@ protected void populateStandardHeaders(Map<String, Object> headers, Message targ
125125
}
126126
}
127127
}
128-
if (typeHeader instanceof Message.Type) {
129-
target.setType((Message.Type) typeHeader);
128+
if (typeHeader instanceof Message.Type messageType) {
129+
target.ofType(messageType);
130130
}
131131
}
132132

133-
private void populateToHeader(Map<String, Object> headers, Message target) {
133+
private void populateToHeader(Map<String, Object> headers, MessageBuilder target) {
134134
String to = getHeaderIfAvailable(headers, XmppHeaders.TO, String.class);
135135
if (StringUtils.hasText(to)) {
136136
try {
137-
target.setTo(JidCreate.from(to));
137+
target.to(to);
138138
}
139139
catch (XmppStringprepException e) {
140140
throw new IllegalStateException("Cannot parse 'xmpp_to' header value", e);
141141
}
142142
}
143143
}
144144

145-
private void populateFromHeader(Map<String, Object> headers, Message target) {
145+
private void populateFromHeader(Map<String, Object> headers, MessageBuilder target) {
146146
String from = getHeaderIfAvailable(headers, XmppHeaders.FROM, String.class);
147147
if (StringUtils.hasText(from)) {
148148
try {
149-
target.setFrom(JidCreate.from(from));
149+
target.from(from);
150150
}
151151
catch (XmppStringprepException e) {
152152
throw new IllegalStateException("Cannot parse 'xmpp_from' header value", e);
@@ -155,7 +155,7 @@ private void populateFromHeader(Map<String, Object> headers, Message target) {
155155
}
156156

157157
@Override
158-
protected void populateUserDefinedHeader(String headerName, Object headerValue, Message target) {
158+
protected void populateUserDefinedHeader(String headerName, Object headerValue, MessageBuilder target) {
159159
JivePropertiesManager.addProperty(target, headerName, headerValue);
160160
}
161161

spring-integration-xmpp/src/main/java/org/springframework/integration/xmpp/support/XmppHeaderMapper.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,17 +16,19 @@
1616

1717
package org.springframework.integration.xmpp.support;
1818

19-
import org.jivesoftware.smack.packet.Message;
19+
import org.jivesoftware.smack.packet.MessageBuilder;
2020

2121
import org.springframework.integration.mapping.RequestReplyHeaderMapper;
2222

2323
/**
2424
* A convenience interface that extends {@link RequestReplyHeaderMapper}
25-
* but parameterized with the Smack API {@link Message}.
25+
* but parameterized with the Smack API {@link MessageBuilder}.
2626
*
2727
* @author Mark Fisher
2828
* @author Gary Russell
29+
* @author Florian Schmaus
30+
*
2931
* @since 2.1
3032
*/
31-
public interface XmppHeaderMapper extends RequestReplyHeaderMapper<Message> {
33+
public interface XmppHeaderMapper extends RequestReplyHeaderMapper<MessageBuilder> {
3234
}

spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/ChatMessageInboundChannelAdapterParserTests-context.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<beans:bean id="testConnection" class="org.mockito.Mockito" factory-method="spy">
1313
<beans:constructor-arg>
1414
<beans:bean class="org.jivesoftware.smack.tcp.XMPPTCPConnection">
15-
<beans:constructor-arg value="guest"/>
15+
<beans:constructor-arg value="guest@example.org"/>
1616
<beans:constructor-arg value="guest"/>
1717
</beans:bean>
1818
</beans:constructor-arg>

spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/config/ChatMessageInboundChannelAdapterParserTests.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,7 +23,8 @@
2323

2424
import org.jivesoftware.smack.StanzaListener;
2525
import org.jivesoftware.smack.XMPPConnection;
26-
import org.jivesoftware.smack.packet.Message;
26+
import org.jivesoftware.smack.packet.MessageBuilder;
27+
import org.jivesoftware.smack.packet.StanzaBuilder;
2728
import org.jivesoftware.smackx.jiveproperties.JivePropertiesManager;
2829
import org.junit.Test;
2930
import org.junit.runner.RunWith;
@@ -102,12 +103,12 @@ public void testInboundAdapterUsageWithHeaderMapper() throws Exception {
102103

103104
StanzaListener stanzaListener = TestUtils.getPropertyValue(adapter, "stanzaListener", StanzaListener.class);
104105

105-
Message message = new Message();
106+
MessageBuilder message = StanzaBuilder.buildMessage();
106107
message.setBody("hello");
107-
message.setTo(JidCreate.from("oleg"));
108+
message.to(JidCreate.from("oleg"));
108109
JivePropertiesManager.addProperty(message, "foo", "foo");
109110
JivePropertiesManager.addProperty(message, "bar", "bar");
110-
stanzaListener.processStanza(message);
111+
stanzaListener.processStanza(message.build());
111112
org.springframework.messaging.Message<?> siMessage = xmppInbound.receive(0);
112113
assertThat(siMessage.getHeaders().get("foo")).isEqualTo("foo");
113114
assertThat(siMessage.getHeaders().get("xmpp_to")).isEqualTo("oleg");

spring-integration-xmpp/src/test/java/org/springframework/integration/xmpp/ignore/OutboundPresenceTests.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.integration.xmpp.ignore;
1818

1919
import org.jivesoftware.smack.packet.Presence;
20+
import org.jivesoftware.smack.packet.StanzaBuilder;
2021
import org.junit.Ignore;
2122
import org.junit.Test;
2223
import org.junit.runner.RunWith;
@@ -33,6 +34,8 @@
3334
* Tests {@link PresenceSendingMessageHandler} to ensure that we are able to publish status.
3435
*
3536
* @author Josh Long
37+
* @author Florian Schmaus
38+
*
3639
* @since 2.0
3740
*/
3841
@ContextConfiguration
@@ -46,8 +49,9 @@ public class OutboundPresenceTests {
4649
@Test
4750
@Ignore
4851
public void testOutbound() throws Throwable {
49-
Presence presence = new Presence(Presence.Type.available);
50-
input.send(new GenericMessage<Presence>(presence));
52+
Presence presence = StanzaBuilder.buildPresence().build();
53+
input.send(new GenericMessage<>(presence));
5154
Thread.sleep(60 * 1000);
5255
}
56+
5357
}

0 commit comments

Comments
 (0)