Skip to content

Commit 2e09994

Browse files
committed
Use per-connection JxmppContext
Fixes SMACK-924
1 parent 8ac3b0c commit 2e09994

File tree

225 files changed

+933
-449
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

225 files changed

+933
-449
lines changed

smack-bosh/src/main/java/org/jivesoftware/smack/bosh/BOSHConfiguration.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.jivesoftware.smack.ConnectionConfiguration;
2626
import org.jivesoftware.smack.proxy.ProxyInfo;
2727

28+
import org.jxmpp.JxmppContext;
29+
2830
/**
2931
* Configuration to use while establishing the connection to the XMPP server via
3032
* HTTP binding.
@@ -90,15 +92,20 @@ public Map<String, String> getHttpHeaders() {
9092
}
9193

9294
public static Builder builder() {
93-
return new Builder();
95+
return builder(getDefaultJxmppContext());
96+
}
97+
98+
public static Builder builder(JxmppContext jxmppContext) {
99+
return new Builder(jxmppContext);
94100
}
95101

96102
public static final class Builder extends ConnectionConfiguration.Builder<Builder, BOSHConfiguration> {
97103
private boolean https;
98104
private String file;
99105
private Map<String, String> httpHeaders = new HashMap<>();
100106

101-
private Builder() {
107+
private Builder(JxmppContext jxmppContext) {
108+
super(jxmppContext);
102109
}
103110

104111
public Builder setUseHttps(boolean useHttps) {

smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
import org.jivesoftware.smack.xml.XmlPullParser;
121121
import org.jivesoftware.smack.xml.XmlPullParserException;
122122

123+
import org.jxmpp.JxmppContext;
123124
import org.jxmpp.jid.DomainBareJid;
124125
import org.jxmpp.jid.EntityBareJid;
125126
import org.jxmpp.jid.EntityFullJid;
@@ -317,6 +318,8 @@ protected enum SyncPointState {
317318
*/
318319
protected final ConnectionConfiguration config;
319320

321+
private final JxmppContext jxmppContext;
322+
320323
/**
321324
* Defines how the from attribute of outgoing stanzas should be handled.
322325
*/
@@ -388,6 +391,9 @@ protected AbstractXMPPConnection(ConnectionConfiguration configuration) {
388391
saslAuthentication = new SASLAuthentication(this, configuration);
389392
config = configuration;
390393

394+
jxmppContext = configuration.getJxmppContext();
395+
assert(jxmppContext != null);
396+
391397
// Install the SASL Nonza callbacks.
392398
buildNonzaCallback()
393399
.listenFor(SaslNonza.Challenge.class, c -> {
@@ -1427,7 +1433,7 @@ protected final void parseAndProcessNonza(XmlPullParser parser) throws IOExcepti
14271433
return;
14281434
}
14291435

1430-
Nonza nonza = nonzaProvider.parse(parser, incomingStreamXmlEnvironment);
1436+
Nonza nonza = nonzaProvider.parse(parser, incomingStreamXmlEnvironment, getJxmppContext());
14311437

14321438
maybeNotifyDebuggerAboutIncoming(nonza);
14331439

@@ -1465,7 +1471,7 @@ protected void parseAndProcessStanza(XmlPullParser parser)
14651471
Stanza stanza = null;
14661472
try {
14671473
try {
1468-
stanza = PacketParserUtils.parseStanza(parser, incomingStreamXmlEnvironment);
1474+
stanza = PacketParserUtils.parseStanza(parser, incomingStreamXmlEnvironment, getJxmppContext());
14691475
} catch (NullPointerException e) {
14701476
// Those exceptions should probably be wrapped into a SmackParsingException and therefore likely constitute a missing verification in the throwing parser.
14711477
String message = "Smack parser throw unexpected exception '" + e.getMessage() + "', please report this at " + Smack.BUG_REPORT_URL;
@@ -1899,7 +1905,7 @@ protected final void parseFeatures(XmlPullParser parser) throws XmlPullParserExc
18991905
default:
19001906
ExtensionElementProvider<ExtensionElement> provider = ProviderManager.getStreamFeatureProvider(name, namespace);
19011907
if (provider != null) {
1902-
streamFeature = provider.parse(parser, incomingStreamXmlEnvironment);
1908+
streamFeature = provider.parse(parser, incomingStreamXmlEnvironment, getJxmppContext());
19031909
}
19041910
break;
19051911
}
@@ -2146,6 +2152,11 @@ public long getLastStanzaReceived() {
21462152
return lastStanzaReceived;
21472153
}
21482154

2155+
@Override
2156+
public JxmppContext getJxmppContext() {
2157+
return jxmppContext;
2158+
}
2159+
21492160
/**
21502161
* Get the timestamp when the connection was the first time authenticated, i.e., when the first successful login was
21512162
* performed. Note that this value is not reset on disconnect, so it represents the timestamp from the last
@@ -2293,7 +2304,7 @@ protected String onStreamOpen(XmlPullParser parser) {
22932304
if (reportedServerDomainString != null) {
22942305
DomainBareJid reportedServerDomain;
22952306
try {
2296-
reportedServerDomain = JidCreate.domainBareFrom(reportedServerDomainString);
2307+
reportedServerDomain = JidCreate.domainBareFrom(reportedServerDomainString, getJxmppContext());
22972308
DomainBareJid configuredXmppServiceDomain = config.getXMPPServiceDomain();
22982309
if (!configuredXmppServiceDomain.equals(reportedServerDomain)) {
22992310
LOGGER.warning("Domain reported by server '" + reportedServerDomain

smack-core/src/main/java/org/jivesoftware/smack/ConnectionConfiguration.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
import org.jivesoftware.smack.util.dns.SmackDaneProvider;
7878
import org.jivesoftware.smack.util.dns.SmackDaneVerifier;
7979

80+
import org.jxmpp.JxmppContext;
8081
import org.jxmpp.jid.DomainBareJid;
8182
import org.jxmpp.jid.EntityBareJid;
8283
import org.jxmpp.jid.impl.JidCreate;
@@ -188,6 +189,8 @@ public abstract class ConnectionConfiguration {
188189

189190
private final StanzaIdSourceFactory stanzaIdSourceFactory;
190191

192+
private final JxmppContext jxmppContext;
193+
191194
protected ConnectionConfiguration(Builder<?, ?> builder) {
192195
try {
193196
smackTlsContext = getSmackTlsContext(builder.dnssecMode, builder.sslContextFactory,
@@ -249,6 +252,8 @@ protected ConnectionConfiguration(Builder<?, ?> builder) {
249252

250253
stanzaIdSourceFactory = builder.stanzaIdSourceFactory;
251254

255+
jxmppContext = builder.jxmppContext;
256+
252257
// If the enabledSaslMechanisms are set, then they must not be empty
253258
assert enabledSaslMechanisms == null || !enabledSaslMechanisms.isEmpty();
254259
}
@@ -303,6 +308,10 @@ private static SmackTlsContext getSmackTlsContext(DnssecMode dnssecMode, SslCont
303308
return new SmackTlsContext(context, daneVerifier, trustManager);
304309
}
305310

311+
protected static JxmppContext getDefaultJxmppContext() {
312+
return SmackConfiguration.getDefaultJxmppContext();
313+
}
314+
306315
public String getHostString() {
307316
if (hostAddress != null) {
308317
return hostAddress.toString();
@@ -356,6 +365,10 @@ public DnsName getXmppServiceDomainAsDnsNameIfPossible() {
356365
return xmppServiceDomainDnsName;
357366
}
358367

368+
public JxmppContext getJxmppContext() {
369+
return jxmppContext;
370+
}
371+
359372
/**
360373
* Returns the TLS security mode used when making the connection. By default,
361374
* the mode is {@link SecurityMode#required}.
@@ -645,6 +658,7 @@ StanzaIdSource constructStanzaIdSource() {
645658
* @param <C> the resulting connection configuration type parameter.
646659
*/
647660
public abstract static class Builder<B extends Builder<B, C>, C extends ConnectionConfiguration> {
661+
private final JxmppContext jxmppContext;
648662
private SecurityMode securityMode = SecurityMode.required;
649663
private DnssecMode dnssecMode = DnssecMode.disabled;
650664
private KeyManager[] keyManagers;
@@ -678,7 +692,8 @@ public abstract static class Builder<B extends Builder<B, C>, C extends Connecti
678692
private StanzaIdSourceFactory stanzaIdSourceFactory = new StandardStanzaIdSource.Factory();
679693

680694
@SuppressWarnings("this-escape")
681-
protected Builder() {
695+
protected Builder(JxmppContext jxmppContext) {
696+
this.jxmppContext = jxmppContext;
682697
if (SmackConfiguration.DEBUG) {
683698
enableDefaultDebugger();
684699
}
@@ -695,7 +710,7 @@ protected Builder() {
695710
* @since 4.4.0
696711
*/
697712
public B setXmppAddressAndPassword(CharSequence jid, String password) throws XmppStringprepException {
698-
return setXmppAddressAndPassword(JidCreate.entityBareFrom(jid), password);
713+
return setXmppAddressAndPassword(JidCreate.entityBareFrom(jid.toString(), jxmppContext), password);
699714
}
700715

701716
/**
@@ -765,7 +780,7 @@ public B setXmppDomain(DomainBareJid xmppDomain) {
765780
* @throws XmppStringprepException if the given string is not a domain bare JID.
766781
*/
767782
public B setXmppDomain(String xmppServiceDomain) throws XmppStringprepException {
768-
this.xmppServiceDomain = JidCreate.domainBareFrom(xmppServiceDomain);
783+
this.xmppServiceDomain = JidCreate.domainBareFrom(xmppServiceDomain, jxmppContext);
769784
return getThis();
770785
}
771786

smack-core/src/main/java/org/jivesoftware/smack/SmackConfiguration.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import org.jivesoftware.smack.parsing.ParsingExceptionCallback;
3939
import org.jivesoftware.smack.util.Objects;
4040

41+
import org.jxmpp.JxmppContext;
42+
4143
/**
4244
* Represents the configuration of Smack. The configuration is used for:
4345
* <ul>
@@ -71,6 +73,8 @@ public final class SmackConfiguration {
7173

7274
private static List<String> defaultMechs = new ArrayList<>();
7375

76+
private static JxmppContext defaultJxmppContext = JxmppContext.getDefaultContext();
77+
7478
static Set<String> disabledSmackClasses = new HashSet<>();
7579

7680
static final List<XMPPInputOutputStream> compressionHandlers = new ArrayList<>(2);
@@ -140,6 +144,14 @@ public static void setDefaultReplyTimeout(int timeout) {
140144
defaultPacketReplyTimeout = timeout;
141145
}
142146

147+
public static JxmppContext getDefaultJxmppContext() {
148+
return defaultJxmppContext;
149+
}
150+
151+
public static void setDefaultJxmppContext(JxmppContext jxmppContext) {
152+
defaultJxmppContext = Objects.requireNonNull(jxmppContext);
153+
}
154+
143155
public static void setDefaultSmackDebuggerFactory(SmackDebuggerFactory debuggerFactory) {
144156
DEFAULT_DEBUGGER_FACTORY = Objects.requireNonNull(debuggerFactory, "Debugger factory must not be null");
145157
}

smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.jivesoftware.smack.util.Consumer;
4343
import org.jivesoftware.smack.util.XmppElementUtil;
4444

45+
import org.jxmpp.JxmppContext;
4546
import org.jxmpp.jid.DomainBareJid;
4647
import org.jxmpp.jid.EntityFullJid;
4748

@@ -713,4 +714,9 @@ default boolean hasFeature(String element, String namespace) {
713714
* @return the timestamp in milliseconds
714715
*/
715716
long getLastStanzaReceived();
717+
718+
default JxmppContext getJxmppContext() {
719+
return JxmppContext.getDefaultContext();
720+
};
721+
716722
}

smack-core/src/main/java/org/jivesoftware/smack/c2s/ModularXmppClientToServerConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ private void parseAndProcessElement(String element) {
595595
}
596596
break;
597597
case "error":
598-
StreamError streamError = PacketParserUtils.parseStreamError(parser, null);
598+
StreamError streamError = PacketParserUtils.parseStreamError(parser, null, getJxmppContext());
599599
StreamErrorException streamErrorException = new StreamErrorException(streamError);
600600
setCurrentConnectionExceptionAndNotify(streamErrorException);
601601
throw streamErrorException;

smack-core/src/main/java/org/jivesoftware/smack/c2s/ModularXmppClientToServerConnectionConfiguration.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.jivesoftware.smack.fsm.StateDescriptorGraph.GraphVertex;
3535
import org.jivesoftware.smack.util.CollectionUtil;
3636

37+
import org.jxmpp.JxmppContext;
38+
3739
public final class ModularXmppClientToServerConnectionConfiguration extends ConnectionConfiguration {
3840

3941
final Set<ModularXmppClientToServerConnectionModuleDescriptor> moduleDescriptors;
@@ -83,7 +85,11 @@ public String getStateGraphInDotFormat() {
8385
}
8486

8587
public static Builder builder() {
86-
return new Builder();
88+
return builder(getDefaultJxmppContext());
89+
}
90+
91+
public static Builder builder(JxmppContext jxmppContext) {
92+
return new Builder(jxmppContext);
8793
}
8894

8995
public static final class Builder
@@ -93,7 +99,8 @@ public static final class Builder
9399

94100
private boolean failOnUnknownStates;
95101

96-
private Builder() {
102+
private Builder(JxmppContext jxmppContext) {
103+
super(jxmppContext);
97104
SmackConfiguration.addAllKnownModulesTo(this);
98105
}
99106

smack-core/src/main/java/org/jivesoftware/smack/compress/provider/CompressedProvider.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2018 Florian Schmaus
3+
* Copyright 2018-2025 Florian Schmaus
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -21,6 +21,8 @@
2121
import org.jivesoftware.smack.provider.NonzaProvider;
2222
import org.jivesoftware.smack.xml.XmlPullParser;
2323

24+
import org.jxmpp.JxmppContext;
25+
2426
public final class CompressedProvider extends NonzaProvider<Compressed> {
2527

2628
public static final CompressedProvider INSTANCE = new CompressedProvider();
@@ -29,7 +31,7 @@ private CompressedProvider() {
2931
}
3032

3133
@Override
32-
public Compressed parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) {
34+
public Compressed parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) {
3335
return Compressed.INSTANCE;
3436
}
3537

smack-core/src/main/java/org/jivesoftware/smack/compress/provider/FailureProvider.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2018-2019 Florian Schmaus
3+
* Copyright 2018-2025 Florian Schmaus
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -29,6 +29,8 @@
2929
import org.jivesoftware.smack.xml.XmlPullParser;
3030
import org.jivesoftware.smack.xml.XmlPullParserException;
3131

32+
import org.jxmpp.JxmppContext;
33+
3234
public final class FailureProvider extends NonzaProvider<Failure> {
3335

3436
private static final Logger LOGGER = Logger.getLogger(FailureProvider.class.getName());
@@ -39,7 +41,7 @@ private FailureProvider() {
3941
}
4042

4143
@Override
42-
public Failure parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
44+
public Failure parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException, SmackParsingException {
4345
Failure.CompressFailureError compressFailureError = null;
4446
StanzaError stanzaError = null;
4547
XmlEnvironment failureXmlEnvironment = XmlEnvironment.from(parser, xmlEnvironment);
@@ -61,7 +63,7 @@ public Failure parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlE
6163
case StreamOpen.SERVER_NAMESPACE:
6264
switch (name) {
6365
case StanzaError.ERROR:
64-
stanzaError = PacketParserUtils.parseError(parser, failureXmlEnvironment);
66+
stanzaError = PacketParserUtils.parseError(parser, failureXmlEnvironment, jxmppContext);
6567
break;
6668
default:
6769
LOGGER.warning("Unknown element in " + namespace + ": " + name);

smack-core/src/main/java/org/jivesoftware/smack/parsing/StandardExtensionElementProvider.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.jivesoftware.smack.xml.XmlPullParser;
2929
import org.jivesoftware.smack.xml.XmlPullParserException;
3030

31+
import org.jxmpp.JxmppContext;
32+
3133
/**
3234
* The parser for {@link StandardExtensionElement}s.
3335
*
@@ -39,7 +41,7 @@ public class StandardExtensionElementProvider extends ExtensionElementProvider<S
3941
public static StandardExtensionElementProvider INSTANCE = new StandardExtensionElementProvider();
4042

4143
@Override
42-
public StandardExtensionElement parse(final XmlPullParser parser, final int initialDepth, XmlEnvironment xmlEnvironment)
44+
public StandardExtensionElement parse(final XmlPullParser parser, final int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext)
4345
throws XmlPullParserException, IOException {
4446
// Unlike most (all?) other providers, we don't know the name and namespace of the element
4547
// we are parsing here.
@@ -79,7 +81,8 @@ public StandardExtensionElement parse(final XmlPullParser parser, final int init
7981
XmlPullParser.Event event = parser.next();
8082
switch (event) {
8183
case START_ELEMENT:
82-
builder.addElement(parse(parser, parser.getDepth(), xmlEnvironment));
84+
var element = parse(parser, parser.getDepth(), xmlEnvironment, jxmppContext);
85+
builder.addElement(element);
8386
break;
8487
case TEXT_CHARACTERS:
8588
builder.setText(parser.getText());

0 commit comments

Comments
 (0)