INT-3972: Allow Disabling of Roster Subscriptions

JIRA: https://jira.spring.io/browse/INT-3972
This commit is contained in:
Artem Bilan
2016-03-25 13:27:34 -04:00
committed by Gary Russell
parent f660b6df2f
commit 69ee423ce2
6 changed files with 34 additions and 9 deletions

View File

@@ -124,6 +124,16 @@ public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnectio
this.port = port;
}
/**
* Sets the subscription processing mode, which dictates what action
* Smack will take when subscription requests from other users are made.
* The default subscription mode is {@link Roster.SubscriptionMode#accept_all}.
* <p> To disable Roster subscription (e.g. for sub-protocol without its support such a GCM)
* specify this option as {@code null}.
* @param subscriptionMode the {@link Roster.SubscriptionMode} to use.
* Can be {@code null}.
* @see Roster#setSubscriptionMode(Roster.SubscriptionMode)
*/
public void setSubscriptionMode(Roster.SubscriptionMode subscriptionMode) {
this.subscriptionMode = subscriptionMode;
}
@@ -167,7 +177,8 @@ public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnectio
this.connection.addConnectionListener(new LoggingConnectionListener());
this.connection.login();
if (this.subscriptionMode != null) {
Roster.getInstanceFor(this.connection).setSubscriptionMode(this.subscriptionMode);
Roster.getInstanceFor(this.connection)
.setSubscriptionMode(this.subscriptionMode);
}
this.running = true;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,8 +26,10 @@ import org.springframework.util.StringUtils;
/**
* Parser for 'xmpp:xmpp-connection' element
*
* @author Oleg Zhurakousky
* @author Artem Bilan
*
* @since 2.0
*/
public class XmppConnectionParser extends AbstractSingleBeanDefinitionParser {
@@ -51,12 +53,13 @@ public class XmppConnectionParser extends AbstractSingleBeanDefinitionParser {
parserContext.getReaderContext().error("One of 'service-name' or 'user' attributes is required", element);
}
String[] attributes = {"user", "password", "resource", "subscription-mode", "host", "port", "service-name",
String[] attributes = {"user", "password", "resource", "host", "port", "service-name",
IntegrationNamespaceUtils.AUTO_STARTUP, IntegrationNamespaceUtils.PHASE};
for (String attribute : attributes) {
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, attribute);
}
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "subscription-mode", true);
}
}

View File

@@ -67,10 +67,12 @@
</xsd:attribute>
<xsd:attribute name="subscription-mode" default="accept_all">
<xsd:annotation>
<xsd:documentation><![CDATA[
The subscription mode for the XMPP connection. Dictates the policy for handling inbound messages from entries not already on the roster.
Values can be "accept_all," "manual," or "reject_all."
]]></xsd:documentation>
<xsd:documentation>
The subscription mode for the XMPP connection.
Dictates the policy for handling inbound messages from entries not already on the roster.
Values can be "accept_all", "manual", "reject_all" or empty assuming 'null'
to disable the Roster subscription altogether.
</xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:union memberTypes="subscriptionModeEnumeration xsd:string"/>

View File

@@ -12,5 +12,6 @@
<int-xmpp:xmpp-connection user="happy.user@my.domain" password="blah" host="localhost" auto-startup="false"/>
<int-xmpp:xmpp-connection id="connectionWithResource" resource="Smack" service-name="my.domain"
user="happy.user" password="blah" host="localhost" auto-startup="false"/>
user="happy.user" password="blah" host="localhost" auto-startup="false"
subscription-mode=""/>
</beans>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -52,6 +52,7 @@ public class XmppConnectionParserTests {
xmppFb = ac.getBean("&connectionWithResource", XmppConnectionFactoryBean.class);
assertEquals("Smack", TestUtils.getPropertyValue(xmppFb, "resource"));
assertNull(TestUtils.getPropertyValue(xmppFb, "subscriptionMode"));
ac.close();
}

View File

@@ -52,6 +52,13 @@ The default name _xmppConnection_ will be used for this connection bean.
If the XMPP Connection goes stale, reconnection attempts will be made with an automatic login as long as the previous connection state was logged (authenticated).
We also register a `ConnectionListener` which will log connection events if the DEBUG logging level is enabled.
The `subscription-mode` initiates the Roster listener to deal with incoming subscriptions from other users.
This functionality isn't always available for the target XMPP servers.
For example GCM fully disables it.
To switch off the Roster listener for subscriptions you should configure it with an empty string when using XML
configuration: `subscription-mode=""`, or with `XmppConnectionFactoryBean.setSubscriptionMode(null)`
when using Java Configuration.
[[xmpp-messages]]
=== XMPP Messages