1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-22 14:22:05 +01:00

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

View file

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

View file

@ -28,8 +28,8 @@ import org.jivesoftware.smack.packet.PacketExtension;
*/
public class NodeExtension implements PacketExtension
{
private PubSubElementType element;
private String node;
private final PubSubElementType element;
private final String node;
/**
* 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.PacketExtension;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.pubsub.PubSubElementType;
/**
@ -85,9 +86,10 @@ public class PubSub extends IQ
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>
*
*/
public String getChildElementXML() {
StringBuilder buf = new StringBuilder();
buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append("\">");
buf.append(getExtensionsXML());
buf.append("</").append(getElementName()).append(">");
return buf.toString();
@Override
public XmlStringBuilder getChildElementXML() {
XmlStringBuilder xml = new XmlStringBuilder();
xml.halfOpenElement(getElementName()).xmlnsAttribute(getNamespace()).rightAngleBracket();
xml.append(getExtensionsXML());
xml.closeElement(getElementName());
return xml;
}
public static PubSub createPubsubPacket(String to, Type type, PacketExtension extension, PubSubNamespace ns) {