From a87323cc3348be55cc8bcba7defb56f54f1c5b77 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 1 Jan 2015 15:11:21 +0100 Subject: [PATCH] Make casts in PubSub unnecessary --- .../org/jivesoftware/smackx/pubsub/Node.java | 18 ++++++------- .../smackx/pubsub/PubSubManager.java | 26 ++++++++++++------- .../smackx/pubsub/util/NodeUtils.java | 3 +-- 3 files changed, 26 insertions(+), 21 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 efe26f53e..88224e23f 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 @@ -221,7 +221,7 @@ abstract public class Node pubSub.addExtension(pe); } } - PubSub reply = (PubSub) sendPubsubPacket(pubSub); + PubSub reply = sendPubsubPacket(pubSub); if (returnedExtensions != null) { returnedExtensions.addAll(reply.getExtensions()); } @@ -265,7 +265,7 @@ abstract public class Node pubSub.addExtension(pe); } } - PubSub reply = (PubSub) sendPubsubPacket(pubSub); + PubSub reply = sendPubsubPacket(pubSub); if (returnedExtensions != null) { returnedExtensions.addAll(reply.getExtensions()); } @@ -293,8 +293,8 @@ abstract public class Node public Subscription subscribe(String jid) throws NoResponseException, XMPPErrorException, NotConnectedException { PubSub pubSub = createPubsubPacket(Type.set, new SubscribeExtension(jid, getId())); - PubSub reply = (PubSub)sendPubsubPacket(pubSub); - return (Subscription)reply.getExtension(PubSubElementType.SUBSCRIPTION); + PubSub reply = sendPubsubPacket(pubSub); + return reply.getExtension(PubSubElementType.SUBSCRIPTION); } /** @@ -319,8 +319,8 @@ abstract public class Node { PubSub request = createPubsubPacket(Type.set, new SubscribeExtension(jid, getId())); request.addExtension(new FormNode(FormNodeType.OPTIONS, subForm)); - PubSub reply = (PubSub)PubSubManager.sendPubsubPacket(con, request); - return (Subscription)reply.getExtension(PubSubElementType.SUBSCRIPTION); + PubSub reply = PubSubManager.sendPubsubPacket(con, request); + return reply.getExtension(PubSubElementType.SUBSCRIPTION); } /** @@ -382,8 +382,8 @@ abstract public class Node */ public SubscribeForm getSubscriptionOptions(String jid, String subscriptionId) throws NoResponseException, XMPPErrorException, NotConnectedException { - PubSub packet = (PubSub)sendPubsubPacket(createPubsubPacket(Type.get, new OptionsExtension(jid, getId(), subscriptionId))); - FormNode ext = (FormNode)packet.getExtension(PubSubElementType.OPTIONS); + PubSub packet = sendPubsubPacket(createPubsubPacket(Type.get, new OptionsExtension(jid, getId(), subscriptionId))); + FormNode ext = packet.getExtension(PubSubElementType.OPTIONS); return new SubscribeForm(ext.getForm()); } @@ -486,7 +486,7 @@ abstract public class Node return PubSub.createPubsubPacket(to, type, ext, ns); } - protected Packet sendPubsubPacket(PubSub packet) throws NoResponseException, XMPPErrorException, NotConnectedException + protected PubSub sendPubsubPacket(PubSub packet) throws NoResponseException, XMPPErrorException, NotConnectedException { return PubSubManager.sendPubsubPacket(con, packet); } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/PubSubManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/PubSubManager.java index 8a13c1262..e91939220 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/PubSubManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/PubSubManager.java @@ -25,6 +25,8 @@ import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.SmackException.NotConnectedException; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException.XMPPErrorException; +import org.jivesoftware.smack.packet.EmptyResultIQ; +import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ.Type; import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.PacketExtension; @@ -87,8 +89,8 @@ final public class PubSubManager */ public LeafNode createNode() throws NoResponseException, XMPPErrorException, NotConnectedException { - PubSub reply = (PubSub)sendPubsubPacket(Type.set, new NodeExtension(PubSubElementType.CREATE), null); - NodeExtension elem = (NodeExtension)reply.getExtension("create", PubSubNamespace.BASIC.getXmlns()); + PubSub reply = sendPubsubPacket(Type.set, new NodeExtension(PubSubElementType.CREATE), null); + NodeExtension elem = reply.getExtension("create", PubSubNamespace.BASIC.getXmlns()); LeafNode newNode = new LeafNode(con, elem.getNode()); newNode.setTo(to); @@ -232,7 +234,7 @@ final public class PubSubManager public List getSubscriptions() throws NoResponseException, XMPPErrorException, NotConnectedException { Packet reply = sendPubsubPacket(Type.get, new NodeExtension(PubSubElementType.SUBSCRIPTIONS), null); - SubscriptionsExtension subElem = (SubscriptionsExtension)reply.getExtension(PubSubElementType.SUBSCRIPTIONS.getElementName(), PubSubElementType.SUBSCRIPTIONS.getNamespace().getXmlns()); + SubscriptionsExtension subElem = reply.getExtension(PubSubElementType.SUBSCRIPTIONS.getElementName(), PubSubElementType.SUBSCRIPTIONS.getNamespace().getXmlns()); return subElem.getSubscriptions(); } @@ -247,8 +249,8 @@ final public class PubSubManager */ public List getAffiliations() throws NoResponseException, XMPPErrorException, NotConnectedException { - PubSub reply = (PubSub)sendPubsubPacket(Type.get, new NodeExtension(PubSubElementType.AFFILIATIONS), null); - AffiliationsExtension listElem = (AffiliationsExtension)reply.getExtension(PubSubElementType.AFFILIATIONS); + PubSub reply = sendPubsubPacket(Type.get, new NodeExtension(PubSubElementType.AFFILIATIONS), null); + AffiliationsExtension listElem = reply.getExtension(PubSubElementType.AFFILIATIONS); return listElem.getAffiliations(); } @@ -278,7 +280,7 @@ final public class PubSubManager { // Errors will cause exceptions in getReply, so it only returns // on success. - PubSub reply = (PubSub)sendPubsubPacket(Type.get, new NodeExtension(PubSubElementType.DEFAULT), PubSubElementType.DEFAULT.getNamespace()); + PubSub reply = sendPubsubPacket(Type.get, new NodeExtension(PubSubElementType.DEFAULT), PubSubElementType.DEFAULT.getNamespace()); return NodeUtils.getFormFromPacket(reply, PubSubElementType.DEFAULT); } @@ -297,12 +299,12 @@ final public class PubSubManager return mgr.discoverInfo(to); } - private Packet sendPubsubPacket(Type type, PacketExtension ext, PubSubNamespace ns) + private PubSub sendPubsubPacket(Type type, PacketExtension ext, PubSubNamespace ns) throws NoResponseException, XMPPErrorException, NotConnectedException { return sendPubsubPacket(con, to, type, Collections.singletonList(ext), ns); } - static Packet sendPubsubPacket(XMPPConnection con, String to, Type type, List extList, PubSubNamespace ns) throws NoResponseException, XMPPErrorException, NotConnectedException + static PubSub sendPubsubPacket(XMPPConnection con, String to, Type type, List extList, PubSubNamespace ns) throws NoResponseException, XMPPErrorException, NotConnectedException { PubSub pubSub = new PubSub(to, type, ns); for (PacketExtension pe : extList) { @@ -311,9 +313,13 @@ final public class PubSubManager return sendPubsubPacket(con ,pubSub); } - static Packet sendPubsubPacket(XMPPConnection con, PubSub packet) throws NoResponseException, XMPPErrorException, NotConnectedException + static PubSub sendPubsubPacket(XMPPConnection con, PubSub packet) throws NoResponseException, XMPPErrorException, NotConnectedException { - return con.createPacketCollectorAndSend(packet).nextResultOrThrow(); + IQ resultIQ = con.createPacketCollectorAndSend(packet).nextResultOrThrow(); + if (resultIQ instanceof EmptyResultIQ) { + return null; + } + return (PubSub) resultIQ; } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/util/NodeUtils.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/util/NodeUtils.java index 319dc101e..78d83e55a 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/util/NodeUtils.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/util/NodeUtils.java @@ -38,9 +38,8 @@ public class NodeUtils */ public static ConfigureForm getFormFromPacket(Packet packet, PubSubElementType elem) { - FormNode config = (FormNode)packet.getExtension(elem.getElementName(), elem.getNamespace().getXmlns()); + FormNode config = packet.getExtension(elem.getElementName(), elem.getNamespace().getXmlns()); Form formReply = config.getForm(); return new ConfigureForm(formReply); - } }