From 9c0da3ed0756ef89ccac42a2d16c7bf5f6f909b3 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Fri, 5 Jul 2019 11:24:02 +0200 Subject: [PATCH] Change SubscribeExtension's jid field type from String to Jid and make it final. --- .../org/jivesoftware/smackx/pubsub/Node.java | 77 ++++++++++++++++++- .../smackx/pubsub/SubscribeExtension.java | 10 ++- 2 files changed, 81 insertions(+), 6 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/Node.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/Node.java index 879abb640..05def395b 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/Node.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/Node.java @@ -46,6 +46,10 @@ import org.jivesoftware.smackx.shim.packet.Header; import org.jivesoftware.smackx.shim.packet.HeadersExtension; import org.jivesoftware.smackx.xdata.Form; +import org.jxmpp.jid.Jid; +import org.jxmpp.jid.impl.JidCreate; +import org.jxmpp.stringprep.XmppStringprepException; + public abstract class Node { protected final PubSubManager pubSubManager; protected final String id; @@ -386,12 +390,45 @@ public abstract class Node { * @throws NotConnectedException * @throws InterruptedException */ - public Subscription subscribe(String jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + public Subscription subscribe(Jid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { PubSub pubSub = createPubsubPacket(Type.set, new SubscribeExtension(jid, getId())); PubSub reply = sendPubsubPacket(pubSub); return reply.getExtension(PubSubElementType.SUBSCRIPTION); } + /** + * The user subscribes to the node using the supplied jid. The + * bare jid portion of this one must match the jid for the connection. + * + * Please note that the {@link Subscription.State} should be checked + * on return since more actions may be required by the caller. + * {@link Subscription.State#pending} - The owner must approve the subscription + * request before messages will be received. + * {@link Subscription.State#unconfigured} - If the {@link Subscription#isConfigRequired()} is true, + * the caller must configure the subscription before messages will be received. If it is false + * the caller can configure it but is not required to do so. + * + * @param jidString The jid to subscribe as. + * @return The subscription + * @throws XMPPErrorException + * @throws NoResponseException + * @throws NotConnectedException + * @throws InterruptedException + * @throws IllegalArgumentException if the provided string is not a valid JID. + * @deprecated use {@link #subscribe(Jid)} instead. + */ + @Deprecated + // TODO: Remove in Smack 4.5. + public Subscription subscribe(String jidString) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + Jid jid; + try { + jid = JidCreate.from(jidString); + } catch (XmppStringprepException e) { + throw new IllegalArgumentException(e); + } + return subscribe(jid); + } + /** * The user subscribes to the node using the supplied jid and subscription * options. The bare jid portion of this one must match the jid for the @@ -414,13 +451,49 @@ public abstract class Node { * @throws NotConnectedException * @throws InterruptedException */ - public Subscription subscribe(String jid, SubscribeForm subForm) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + public Subscription subscribe(Jid jid, SubscribeForm subForm) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { PubSub request = createPubsubPacket(Type.set, new SubscribeExtension(jid, getId())); request.addExtension(new FormNode(FormNodeType.OPTIONS, subForm)); PubSub reply = sendPubsubPacket(request); return reply.getExtension(PubSubElementType.SUBSCRIPTION); } + /** + * The user subscribes to the node using the supplied jid and subscription + * options. The bare jid portion of this one must match the jid for the + * connection. + * + * Please note that the {@link Subscription.State} should be checked + * on return since more actions may be required by the caller. + * {@link Subscription.State#pending} - The owner must approve the subscription + * request before messages will be received. + * {@link Subscription.State#unconfigured} - If the {@link Subscription#isConfigRequired()} is true, + * the caller must configure the subscription before messages will be received. If it is false + * the caller can configure it but is not required to do so. + * + * @param jidString The jid to subscribe as. + * @param subForm + * + * @return The subscription + * @throws XMPPErrorException + * @throws NoResponseException + * @throws NotConnectedException + * @throws InterruptedException + * @throws IllegalArgumentException if the provided string is not a valid JID. + * @deprecated use {@link #subscribe(Jid, SubscribeForm)} instead. + */ + @Deprecated + // TODO: Remove in Smack 4.5. + public Subscription subscribe(String jidString, SubscribeForm subForm) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + Jid jid; + try { + jid = JidCreate.from(jidString); + } catch (XmppStringprepException e) { + throw new IllegalArgumentException(e); + } + return subscribe(jid, subForm); + } + /** * Remove the subscription related to the specified JID. This will only * work if there is only 1 subscription. If there are multiple subscriptions, diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/SubscribeExtension.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/SubscribeExtension.java index 92e6c0b37..b17db0af0 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/SubscribeExtension.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/SubscribeExtension.java @@ -16,25 +16,27 @@ */ package org.jivesoftware.smackx.pubsub; +import org.jxmpp.jid.Jid; + /** * Represents a request to subscribe to a node. * * @author Robin Collier */ public class SubscribeExtension extends NodeExtension { - protected String jid; + protected final Jid jid; - public SubscribeExtension(String subscribeJid) { + public SubscribeExtension(Jid subscribeJid) { super(PubSubElementType.SUBSCRIBE); jid = subscribeJid; } - public SubscribeExtension(String subscribeJid, String nodeId) { + public SubscribeExtension(Jid subscribeJid, String nodeId) { super(PubSubElementType.SUBSCRIBE, nodeId); jid = subscribeJid; } - public String getJid() { + public Jid getJid() { return jid; }