Skip to content

Commit 699e413

Browse files
committed
[integration-xmpp] Bump Smack to 4.4.2
Fixes spring-projects#3462.
1 parent 27e1f3a commit 699e413

14 files changed

+140
-96
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ ext {
9595
rsocketVersion = '1.1.0'
9696
saajVersion = '1.5.2'
9797
servletApiVersion = '4.0.1'
98-
smackVersion = '4.3.5'
98+
smackVersion = '4.4.2'
9999
soapVersion = '1.4.0'
100100
springAmqpVersion = project.hasProperty('springAmqpVersion') ? project.springAmqpVersion : '2.3.6'
101101
springDataVersion = project.hasProperty('springDataVersion') ? project.springDataVersion : '2021.0.0-SNAPSHOT'
@@ -868,7 +868,7 @@ project('spring-integration-xmpp') {
868868
dependencies {
869869
api project(':spring-integration-core')
870870
api "org.igniterealtime.smack:smack-tcp:$smackVersion"
871-
api "org.igniterealtime.smack:smack-java7:$smackVersion"
871+
api "org.igniterealtime.smack:smack-java8:$smackVersion"
872872
api "org.igniterealtime.smack:smack-extensions:$smackVersion"
873873

874874
testImplementation project(':spring-integration-stream')

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

Lines changed: 3 additions & 2 deletions
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.
@@ -124,8 +124,9 @@ private class ChatMessagePublishingStanzaListener implements StanzaListener {
124124
public void processStanza(Stanza packet) {
125125
if (packet instanceof org.jivesoftware.smack.packet.Message) {
126126
org.jivesoftware.smack.packet.Message xmppMessage = (org.jivesoftware.smack.packet.Message) packet;
127+
// TODO: Remove asBuilder if we redesign RequestReplyHeaderMapper.
127128
Map<String, ?> mappedHeaders =
128-
ChatMessageListeningEndpoint.this.headerMapper.toHeadersFromRequest(xmppMessage);
129+
ChatMessageListeningEndpoint.this.headerMapper.toHeadersFromRequest(xmppMessage.asBuilder());
129130

130131
Object messageBody = xmppMessage.getBody();
131132

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

Lines changed: 22 additions & 15 deletions
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.
@@ -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;
@@ -85,27 +87,30 @@ public String getComponentType() {
8587

8688
@Override
8789
protected void handleMessageInternal(Message<?> message) {
90+
org.jivesoftware.smack.packet.Message xmppMessage;
8891
Assert.isTrue(isInitialized(),
8992
() -> getComponentName() + "#" + getComponentType() + " must be initialized");
9093
try {
9194
Object payload = message.getPayload();
92-
org.jivesoftware.smack.packet.Message xmppMessage;
95+
MessageBuilder xmppMessageBuilder;
9396
if (payload instanceof org.jivesoftware.smack.packet.Message) {
9497
xmppMessage = (org.jivesoftware.smack.packet.Message) payload;
98+
xmppMessageBuilder = xmppMessage.asBuilder();
9599
}
96100
else {
97101
String to = message.getHeaders().get(XmppHeaders.TO, String.class);
98102
Assert.state(StringUtils.hasText(to), () -> "The '" + XmppHeaders.TO + "' header must not be null");
99-
xmppMessage = buildXmppMessage(payload, to);
103+
xmppMessageBuilder = buildXmppMessage(payload, to);
100104
}
101105

102106
if (this.headerMapper != null) {
103-
this.headerMapper.fromHeadersToRequest(message.getHeaders(), xmppMessage);
107+
this.headerMapper.fromHeadersToRequest(message.getHeaders(), xmppMessageBuilder);
104108
}
105109
XMPPConnection xmppConnection = getXmppConnection();
106110
if (!xmppConnection.isConnected() && xmppConnection instanceof AbstractXMPPConnection) {
107111
((AbstractXMPPConnection) xmppConnection).connect();
108112
}
113+
xmppMessage = xmppMessageBuilder.build();
109114
xmppConnection.sendStanza(xmppMessage);
110115
}
111116
catch (InterruptedException e) {
@@ -117,14 +122,16 @@ protected void handleMessageInternal(Message<?> message) {
117122
}
118123
}
119124

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

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

126132
if (payload instanceof ExtensionElement) {
127-
xmppMessage.addExtension((ExtensionElement) payload);
133+
ExtensionElement extensionElement = (ExtensionElement) payload;
134+
xmppMessageBuilder.addExtension(extensionElement);
128135
}
129136
else if (payload instanceof String) {
130137
if (this.extensionProvider != null) {
@@ -135,13 +142,13 @@ else if (payload instanceof String) {
135142
// if the target content isn't XML.
136143
data = "<root>" + data + "</root>";
137144
}
138-
XmlPullParser xmlPullParser = PacketParserUtils.newXmppParser(new StringReader(data));
139-
xmlPullParser.next();
145+
XmlPullParser xmlPullParser = PacketParserUtils.getParserFor(data);
140146
ExtensionElement extension = this.extensionProvider.parse(xmlPullParser);
141-
xmppMessage.addExtension(extension);
147+
xmppMessageBuilder.addExtension(extension);
142148
}
143149
else {
144-
xmppMessage.setBody((String) payload);
150+
String body = (String) payload;
151+
xmppMessageBuilder.setBody(body);
145152
}
146153
}
147154
else {
@@ -151,7 +158,7 @@ else if (payload instanceof String) {
151158
"are supported. Received [" + payload.getClass().getName() +
152159
"]. Consider adding a Transformer prior to this adapter.");
153160
}
154-
return xmppMessage;
161+
return xmppMessageBuilder;
155162
}
156163

157164
}

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

Lines changed: 14 additions & 13 deletions
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-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.
@@ -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);
@@ -126,27 +126,28 @@ protected void populateStandardHeaders(Map<String, Object> headers, Message targ
126126
}
127127
}
128128
if (typeHeader instanceof Message.Type) {
129-
target.setType((Message.Type) typeHeader);
129+
Message.Type messageType = (Message.Type) typeHeader;
130+
target.ofType(messageType);
130131
}
131132
}
132133

133-
private void populateToHeader(Map<String, Object> headers, Message target) {
134+
private void populateToHeader(Map<String, Object> headers, MessageBuilder target) {
134135
String to = getHeaderIfAvailable(headers, XmppHeaders.TO, String.class);
135136
if (StringUtils.hasText(to)) {
136137
try {
137-
target.setTo(JidCreate.from(to));
138+
target.to(to);
138139
}
139140
catch (XmppStringprepException e) {
140141
throw new IllegalStateException("Cannot parse 'xmpp_to' header value", e);
141142
}
142143
}
143144
}
144145

145-
private void populateFromHeader(Map<String, Object> headers, Message target) {
146+
private void populateFromHeader(Map<String, Object> headers, MessageBuilder target) {
146147
String from = getHeaderIfAvailable(headers, XmppHeaders.FROM, String.class);
147148
if (StringUtils.hasText(from)) {
148149
try {
149-
target.setFrom(JidCreate.from(from));
150+
target.from(from);
150151
}
151152
catch (XmppStringprepException e) {
152153
throw new IllegalStateException("Cannot parse 'xmpp_from' header value", e);
@@ -155,7 +156,7 @@ private void populateFromHeader(Map<String, Object> headers, Message target) {
155156
}
156157

157158
@Override
158-
protected void populateUserDefinedHeader(String headerName, Object headerValue, Message target) {
159+
protected void populateUserDefinedHeader(String headerName, Object headerValue, MessageBuilder target) {
159160
JivePropertiesManager.addProperty(target, headerName, headerValue);
160161
}
161162

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

Lines changed: 4 additions & 4 deletions
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.
@@ -16,17 +16,17 @@
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
2929
* @since 2.1
3030
*/
31-
public interface XmppHeaderMapper extends RequestReplyHeaderMapper<Message> {
31+
public interface XmppHeaderMapper extends RequestReplyHeaderMapper<MessageBuilder> {
3232
}

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 5 deletions
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

Lines changed: 3 additions & 2 deletions
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.
@@ -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;
@@ -46,7 +47,7 @@ public class OutboundPresenceTests {
4647
@Test
4748
@Ignore
4849
public void testOutbound() throws Throwable {
49-
Presence presence = new Presence(Presence.Type.available);
50+
Presence presence = StanzaBuilder.buildPresence().build();
5051
input.send(new GenericMessage<Presence>(presence));
5152
Thread.sleep(60 * 1000);
5253
}

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

Lines changed: 6 additions & 5 deletions
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.
@@ -16,9 +16,9 @@
1616

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

19+
import org.jivesoftware.smack.packet.StanzaBuilder;
1920
import org.junit.Ignore;
2021
import org.junit.Test;
21-
import org.jxmpp.jid.impl.JidCreate;
2222
import org.jxmpp.stringprep.XmppStringprepException;
2323

2424
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -41,9 +41,10 @@ public void validateSmackMessageSent() throws XmppStringprepException {
4141

4242
MessageChannel xmppInput = ac.getBean("xmppInput", MessageChannel.class);
4343

44-
org.jivesoftware.smack.packet.Message smackMessage =
45-
new org.jivesoftware.smack.packet.Message(JidCreate.from("[email protected]"));
46-
smackMessage.setBody("Message sent as Smack Message");
44+
org.jivesoftware.smack.packet.Message smackMessage = StanzaBuilder.buildMessage()
45+
46+
.setBody("Message sent as Smack Message")
47+
.build();
4748

4849
Message<org.jivesoftware.smack.packet.Message> message = new GenericMessage<>(smackMessage);
4950

0 commit comments

Comments
 (0)