Skip to content

Merging with igniterealtime:master #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 25, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove duplicate code in pubusb LeafNode
Make fields of GetItemsRequest and NodeExtension final and use
XmlStringBuilder.
  • Loading branch information
Flowdalic committed Sep 21, 2014
commit d8c77de785eb42ba8f58f8f703573b01b5f10ac2
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,37 @@
*/
package org.jivesoftware.smackx.pubsub;

import org.jivesoftware.smack.util.XmlStringBuilder;

/**
* Represents a request to subscribe to a node.
*
* @author Robin Collier
*/
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;
}

Expand All @@ -60,29 +61,13 @@ public int getMaxItems()
}

@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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,10 @@ public DiscoverItems discoverItems() throws NoResponseException, XMPPErrorExcept
* @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);
}

/**
Expand All @@ -90,14 +86,10 @@ public <T extends Item> List<T> getItems() throws NoResponseException, XMPPError
* @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);
}

/**
Expand All @@ -114,7 +106,6 @@ public <T extends Item> List<T> getItems(String subscriptionId) throws NoRespons
* @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());
Expand All @@ -124,10 +115,7 @@ public <T extends Item> List<T> getItems(Collection<String> ids) throws NoRespon
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);
}

/**
Expand All @@ -140,14 +128,10 @@ public <T extends Item> List<T> getItems(Collection<String> ids) throws NoRespon
* @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);
}

/**
Expand All @@ -163,16 +147,19 @@ public <T extends Item> List<T> getItems(int maxItems) throws NoResponseExceptio
* @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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
*/
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.pubsub.PubSubElementType;

/**
Expand Down Expand Up @@ -85,9 +86,10 @@ public void setPubSubNamespace(PubSubNamespace 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());
}

/**
Expand Down Expand Up @@ -116,12 +118,13 @@ public PubSubNamespace getPubSubNamespace()
* </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) {
Expand Down