1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-10-31 17:25:58 +01:00

Make casts in PubSub unnecessary

This commit is contained in:
Florian Schmaus 2015-01-01 15:11:21 +01:00
parent 5c086eeefa
commit a87323cc33
3 changed files with 26 additions and 21 deletions

View file

@ -221,7 +221,7 @@ abstract public class Node
pubSub.addExtension(pe); pubSub.addExtension(pe);
} }
} }
PubSub reply = (PubSub) sendPubsubPacket(pubSub); PubSub reply = sendPubsubPacket(pubSub);
if (returnedExtensions != null) { if (returnedExtensions != null) {
returnedExtensions.addAll(reply.getExtensions()); returnedExtensions.addAll(reply.getExtensions());
} }
@ -265,7 +265,7 @@ abstract public class Node
pubSub.addExtension(pe); pubSub.addExtension(pe);
} }
} }
PubSub reply = (PubSub) sendPubsubPacket(pubSub); PubSub reply = sendPubsubPacket(pubSub);
if (returnedExtensions != null) { if (returnedExtensions != null) {
returnedExtensions.addAll(reply.getExtensions()); returnedExtensions.addAll(reply.getExtensions());
} }
@ -293,8 +293,8 @@ abstract public class Node
public Subscription subscribe(String jid) throws NoResponseException, XMPPErrorException, NotConnectedException public Subscription subscribe(String jid) throws NoResponseException, XMPPErrorException, NotConnectedException
{ {
PubSub pubSub = createPubsubPacket(Type.set, new SubscribeExtension(jid, getId())); PubSub pubSub = createPubsubPacket(Type.set, new SubscribeExtension(jid, getId()));
PubSub reply = (PubSub)sendPubsubPacket(pubSub); PubSub reply = sendPubsubPacket(pubSub);
return (Subscription)reply.getExtension(PubSubElementType.SUBSCRIPTION); return reply.getExtension(PubSubElementType.SUBSCRIPTION);
} }
/** /**
@ -319,8 +319,8 @@ abstract public class Node
{ {
PubSub request = createPubsubPacket(Type.set, new SubscribeExtension(jid, getId())); PubSub request = createPubsubPacket(Type.set, new SubscribeExtension(jid, getId()));
request.addExtension(new FormNode(FormNodeType.OPTIONS, subForm)); request.addExtension(new FormNode(FormNodeType.OPTIONS, subForm));
PubSub reply = (PubSub)PubSubManager.sendPubsubPacket(con, request); PubSub reply = PubSubManager.sendPubsubPacket(con, request);
return (Subscription)reply.getExtension(PubSubElementType.SUBSCRIPTION); return reply.getExtension(PubSubElementType.SUBSCRIPTION);
} }
/** /**
@ -382,8 +382,8 @@ abstract public class Node
*/ */
public SubscribeForm getSubscriptionOptions(String jid, String subscriptionId) throws NoResponseException, XMPPErrorException, NotConnectedException public SubscribeForm getSubscriptionOptions(String jid, String subscriptionId) throws NoResponseException, XMPPErrorException, NotConnectedException
{ {
PubSub packet = (PubSub)sendPubsubPacket(createPubsubPacket(Type.get, new OptionsExtension(jid, getId(), subscriptionId))); PubSub packet = sendPubsubPacket(createPubsubPacket(Type.get, new OptionsExtension(jid, getId(), subscriptionId)));
FormNode ext = (FormNode)packet.getExtension(PubSubElementType.OPTIONS); FormNode ext = packet.getExtension(PubSubElementType.OPTIONS);
return new SubscribeForm(ext.getForm()); return new SubscribeForm(ext.getForm());
} }
@ -486,7 +486,7 @@ abstract public class Node
return PubSub.createPubsubPacket(to, type, ext, ns); 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); return PubSubManager.sendPubsubPacket(con, packet);
} }

View file

@ -25,6 +25,8 @@ import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException; import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException.XMPPErrorException; 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.IQ.Type;
import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension; import org.jivesoftware.smack.packet.PacketExtension;
@ -87,8 +89,8 @@ final public class PubSubManager
*/ */
public LeafNode createNode() throws NoResponseException, XMPPErrorException, NotConnectedException public LeafNode createNode() throws NoResponseException, XMPPErrorException, NotConnectedException
{ {
PubSub reply = (PubSub)sendPubsubPacket(Type.set, new NodeExtension(PubSubElementType.CREATE), null); PubSub reply = sendPubsubPacket(Type.set, new NodeExtension(PubSubElementType.CREATE), null);
NodeExtension elem = (NodeExtension)reply.getExtension("create", PubSubNamespace.BASIC.getXmlns()); NodeExtension elem = reply.getExtension("create", PubSubNamespace.BASIC.getXmlns());
LeafNode newNode = new LeafNode(con, elem.getNode()); LeafNode newNode = new LeafNode(con, elem.getNode());
newNode.setTo(to); newNode.setTo(to);
@ -232,7 +234,7 @@ final public class PubSubManager
public List<Subscription> getSubscriptions() throws NoResponseException, XMPPErrorException, NotConnectedException public List<Subscription> getSubscriptions() throws NoResponseException, XMPPErrorException, NotConnectedException
{ {
Packet reply = sendPubsubPacket(Type.get, new NodeExtension(PubSubElementType.SUBSCRIPTIONS), null); 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(); return subElem.getSubscriptions();
} }
@ -247,8 +249,8 @@ final public class PubSubManager
*/ */
public List<Affiliation> getAffiliations() throws NoResponseException, XMPPErrorException, NotConnectedException public List<Affiliation> getAffiliations() throws NoResponseException, XMPPErrorException, NotConnectedException
{ {
PubSub reply = (PubSub)sendPubsubPacket(Type.get, new NodeExtension(PubSubElementType.AFFILIATIONS), null); PubSub reply = sendPubsubPacket(Type.get, new NodeExtension(PubSubElementType.AFFILIATIONS), null);
AffiliationsExtension listElem = (AffiliationsExtension)reply.getExtension(PubSubElementType.AFFILIATIONS); AffiliationsExtension listElem = reply.getExtension(PubSubElementType.AFFILIATIONS);
return listElem.getAffiliations(); return listElem.getAffiliations();
} }
@ -278,7 +280,7 @@ final public class PubSubManager
{ {
// Errors will cause exceptions in getReply, so it only returns // Errors will cause exceptions in getReply, so it only returns
// on success. // 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); return NodeUtils.getFormFromPacket(reply, PubSubElementType.DEFAULT);
} }
@ -297,12 +299,12 @@ final public class PubSubManager
return mgr.discoverInfo(to); 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 { throws NoResponseException, XMPPErrorException, NotConnectedException {
return sendPubsubPacket(con, to, type, Collections.singletonList(ext), ns); return sendPubsubPacket(con, to, type, Collections.singletonList(ext), ns);
} }
static Packet sendPubsubPacket(XMPPConnection con, String to, Type type, List<PacketExtension> extList, PubSubNamespace ns) throws NoResponseException, XMPPErrorException, NotConnectedException static PubSub sendPubsubPacket(XMPPConnection con, String to, Type type, List<PacketExtension> extList, PubSubNamespace ns) throws NoResponseException, XMPPErrorException, NotConnectedException
{ {
PubSub pubSub = new PubSub(to, type, ns); PubSub pubSub = new PubSub(to, type, ns);
for (PacketExtension pe : extList) { for (PacketExtension pe : extList) {
@ -311,9 +313,13 @@ final public class PubSubManager
return sendPubsubPacket(con ,pubSub); 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;
} }
} }

View file

@ -38,9 +38,8 @@ public class NodeUtils
*/ */
public static ConfigureForm getFormFromPacket(Packet packet, PubSubElementType elem) 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(); Form formReply = config.getForm();
return new ConfigureForm(formReply); return new ConfigureForm(formReply);
} }
} }