Remove duplicate code in pubusb LeafNode

Make fields of GetItemsRequest and NodeExtension final and use
XmlStringBuilder.
This commit is contained in:
Florian Schmaus 2014-09-21 20:49:11 +02:00
parent bd0fb973c8
commit d8c77de785
4 changed files with 44 additions and 69 deletions

View File

@ -16,6 +16,8 @@
*/ */
package org.jivesoftware.smackx.pubsub; package org.jivesoftware.smackx.pubsub;
import org.jivesoftware.smack.util.XmlStringBuilder;
/** /**
* Represents a request to subscribe to a node. * Represents a request to subscribe to a node.
* *
@ -23,29 +25,28 @@ package org.jivesoftware.smackx.pubsub;
*/ */
public class GetItemsRequest extends NodeExtension public class GetItemsRequest extends NodeExtension
{ {
protected String subId; protected final String subId;
protected int maxItems; protected final int maxItems;
public GetItemsRequest(String nodeId) public GetItemsRequest(String nodeId)
{ {
super(PubSubElementType.ITEMS, nodeId); this(nodeId, null, -1);
} }
public GetItemsRequest(String nodeId, String subscriptionId) public GetItemsRequest(String nodeId, String subscriptionId)
{ {
super(PubSubElementType.ITEMS, nodeId); this(nodeId, subscriptionId, -1);
subId = subscriptionId;
} }
public GetItemsRequest(String nodeId, int maxItemsToReturn) public GetItemsRequest(String nodeId, int maxItemsToReturn)
{ {
super(PubSubElementType.ITEMS, nodeId); this(nodeId, null, maxItemsToReturn);
maxItems = maxItemsToReturn;
} }
public GetItemsRequest(String nodeId, String subscriptionId, int maxItemsToReturn) public GetItemsRequest(String nodeId, String subscriptionId, int maxItemsToReturn)
{ {
this(nodeId, maxItemsToReturn); super(PubSubElementType.ITEMS, nodeId);
maxItems = maxItemsToReturn;
subId = subscriptionId; subId = subscriptionId;
} }
@ -60,29 +61,13 @@ public class GetItemsRequest extends NodeExtension
} }
@Override @Override
public String toXML() public XmlStringBuilder toXML() {
{ XmlStringBuilder xml = new XmlStringBuilder();
StringBuilder builder = new StringBuilder("<"); xml.halfOpenElement(getElementName());
builder.append(getElementName()); xml.attribute("node", getNode());
xml.optAttribute("subid", getSubscriptionId());
builder.append(" node='"); xml.optIntAttribute("max_items", getMaxItems());
builder.append(getNode()); xml.closeEmptyElement();
builder.append("'"); return xml;
}
if (getSubscriptionId() != null)
{
builder.append(" subid='");
builder.append(getSubscriptionId());
builder.append("'");
}
if (getMaxItems() > 0)
{
builder.append(" max_items='");
builder.append(getMaxItems());
builder.append("'");
}
builder.append("/>");
return builder.toString();
}
} }

View File

@ -68,14 +68,10 @@ public class LeafNode extends Node
* @throws NoResponseException if there was no response from the server. * @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException * @throws NotConnectedException
*/ */
@SuppressWarnings("unchecked")
public <T extends Item> List<T> getItems() throws NoResponseException, XMPPErrorException, NotConnectedException public <T extends Item> List<T> getItems() throws NoResponseException, XMPPErrorException, NotConnectedException
{ {
PubSub request = createPubsubPacket(Type.get, new GetItemsRequest(getId())); PubSub request = createPubsubPacket(Type.get, new GetItemsRequest(getId()));
return getItems(request);
PubSub result = (PubSub) con.createPacketCollectorAndSend(request).nextResultOrThrow();
ItemsExtension itemsElem = (ItemsExtension)result.getExtension(PubSubElementType.ITEMS);
return (List<T>)itemsElem.getItems();
} }
/** /**
@ -90,14 +86,10 @@ public class LeafNode extends Node
* @throws NoResponseException if there was no response from the server. * @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException * @throws NotConnectedException
*/ */
@SuppressWarnings("unchecked")
public <T extends Item> List<T> getItems(String subscriptionId) throws NoResponseException, XMPPErrorException, NotConnectedException public <T extends Item> List<T> getItems(String subscriptionId) throws NoResponseException, XMPPErrorException, NotConnectedException
{ {
PubSub request = createPubsubPacket(Type.get, new GetItemsRequest(getId(), subscriptionId)); PubSub request = createPubsubPacket(Type.get, new GetItemsRequest(getId(), subscriptionId));
return getItems(request);
PubSub result = (PubSub) con.createPacketCollectorAndSend(request).nextResultOrThrow();
ItemsExtension itemsElem = (ItemsExtension)result.getExtension(PubSubElementType.ITEMS);
return (List<T>)itemsElem.getItems();
} }
/** /**
@ -114,7 +106,6 @@ public class LeafNode extends Node
* @throws NoResponseException if there was no response from the server. * @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException * @throws NotConnectedException
*/ */
@SuppressWarnings("unchecked")
public <T extends Item> List<T> getItems(Collection<String> ids) throws NoResponseException, XMPPErrorException, NotConnectedException public <T extends Item> List<T> getItems(Collection<String> ids) throws NoResponseException, XMPPErrorException, NotConnectedException
{ {
List<Item> itemList = new ArrayList<Item>(ids.size()); List<Item> itemList = new ArrayList<Item>(ids.size());
@ -124,10 +115,7 @@ public class LeafNode extends Node
itemList.add(new Item(id)); itemList.add(new Item(id));
} }
PubSub request = createPubsubPacket(Type.get, new ItemsExtension(ItemsExtension.ItemsElementType.items, getId(), itemList)); PubSub request = createPubsubPacket(Type.get, new ItemsExtension(ItemsExtension.ItemsElementType.items, getId(), itemList));
return getItems(request);
PubSub result = (PubSub) con.createPacketCollectorAndSend(request).nextResultOrThrow();
ItemsExtension itemsElem = (ItemsExtension)result.getExtension(PubSubElementType.ITEMS);
return (List<T>)itemsElem.getItems();
} }
/** /**
@ -140,14 +128,10 @@ public class LeafNode extends Node
* @throws NoResponseException if there was no response from the server. * @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException * @throws NotConnectedException
*/ */
@SuppressWarnings("unchecked")
public <T extends Item> List<T> getItems(int maxItems) throws NoResponseException, XMPPErrorException, NotConnectedException public <T extends Item> List<T> getItems(int maxItems) throws NoResponseException, XMPPErrorException, NotConnectedException
{ {
PubSub request = createPubsubPacket(Type.get, new GetItemsRequest(getId(), maxItems)); PubSub request = createPubsubPacket(Type.get, new GetItemsRequest(getId(), maxItems));
return getItems(request);
PubSub result = (PubSub) con.createPacketCollectorAndSend(request).nextResultOrThrow();
ItemsExtension itemsElem = (ItemsExtension)result.getExtension(PubSubElementType.ITEMS);
return (List<T>)itemsElem.getItems();
} }
/** /**
@ -163,16 +147,19 @@ public class LeafNode extends Node
* @throws NoResponseException if there was no response from the server. * @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException * @throws NotConnectedException
*/ */
@SuppressWarnings("unchecked")
public <T extends Item> List<T> getItems(int maxItems, String subscriptionId) throws NoResponseException, XMPPErrorException, NotConnectedException public <T extends Item> List<T> getItems(int maxItems, String subscriptionId) throws NoResponseException, XMPPErrorException, NotConnectedException
{ {
PubSub request = createPubsubPacket(Type.get, new GetItemsRequest(getId(), subscriptionId, maxItems)); PubSub request = createPubsubPacket(Type.get, new GetItemsRequest(getId(), subscriptionId, maxItems));
return getItems(request);
PubSub result = (PubSub) con.createPacketCollectorAndSend(request).nextResultOrThrow();
ItemsExtension itemsElem = (ItemsExtension)result.getExtension(PubSubElementType.ITEMS);
return (List<T>)itemsElem.getItems();
} }
@SuppressWarnings("unchecked")
private <T extends Item> List<T> getItems(PubSub request) throws NoResponseException, XMPPErrorException, NotConnectedException {
PubSub result = con.createPacketCollectorAndSend(request).nextResultOrThrow();
ItemsExtension itemsElem = result.getExtension(PubSubElementType.ITEMS);
return (List<T>) itemsElem.getItems();
}
/** /**
* Publishes an event to the node. This is an empty event * Publishes an event to the node. This is an empty event
* with no item. * with no item.

View File

@ -28,8 +28,8 @@ import org.jivesoftware.smack.packet.PacketExtension;
*/ */
public class NodeExtension implements PacketExtension public class NodeExtension implements PacketExtension
{ {
private PubSubElementType element; private final PubSubElementType element;
private String node; private final String node;
/** /**
* Constructs a <tt>NodeExtension</tt> with an element name specified * Constructs a <tt>NodeExtension</tt> with an element name specified

View File

@ -18,6 +18,7 @@ package org.jivesoftware.smackx.pubsub.packet;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.PacketExtension; import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.pubsub.PubSubElementType; import org.jivesoftware.smackx.pubsub.PubSubElementType;
/** /**
@ -85,9 +86,10 @@ public class PubSub extends IQ
this.ns = ns; this.ns = ns;
} }
public PacketExtension getExtension(PubSubElementType elem) @SuppressWarnings("unchecked")
public <PE extends PacketExtension> PE getExtension(PubSubElementType elem)
{ {
return getExtension(elem.getElementName(), elem.getNamespace().getXmlns()); return (PE) getExtension(elem.getElementName(), elem.getNamespace().getXmlns());
} }
/** /**
@ -116,12 +118,13 @@ public class PubSub extends IQ
* </pre> * </pre>
* *
*/ */
public String getChildElementXML() { @Override
StringBuilder buf = new StringBuilder(); public XmlStringBuilder getChildElementXML() {
buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append("\">"); XmlStringBuilder xml = new XmlStringBuilder();
buf.append(getExtensionsXML()); xml.halfOpenElement(getElementName()).xmlnsAttribute(getNamespace()).rightAngleBracket();
buf.append("</").append(getElementName()).append(">"); xml.append(getExtensionsXML());
return buf.toString(); xml.closeElement(getElementName());
return xml;
} }
public static PubSub createPubsubPacket(String to, Type type, PacketExtension extension, PubSubNamespace ns) { public static PubSub createPubsubPacket(String to, Type type, PacketExtension extension, PubSubNamespace ns) {