Hi,
I'm trying to get some pubsub code working. I have the following code that attempts to create a node and send items. It appears to successfully send the item but then I get an error.
import java.util.LinkedList;
import java.util.List;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.pubsub.AccessModel;
import org.jivesoftware.smackx.pubsub.ConfigureForm;
import org.jivesoftware.smackx.pubsub.FormType;
import org.jivesoftware.smackx.pubsub.Item;
import org.jivesoftware.smackx.pubsub.LeafNode;
import org.jivesoftware.smackx.pubsub.Node;
import org.jivesoftware.smackx.pubsub.PayloadItem;
import org.jivesoftware.smackx.pubsub.PubSubManager;
import org.jivesoftware.smackx.pubsub.PublishModel;
import org.jivesoftware.smackx.pubsub.SimplePayload;
public class XMPPPublisher {
private final static String XMPP_SERVER_NAME = "...";
private final static String XMPP_USER_USERNAME = "...";
private final static String XMPP_USER_PASSWORD = "...";
private final static int LOCAL_SOCKS_PROXY_PORT = 7923;
private final static int XMPP_PORT = 5222;
static PayloadItem<SimplePayload> createItem() {
SimplePayload payload = new SimplePayload( "message", "pubsub:test:book", "xxx");
String itemId = Long.toString(System.currentTimeMillis());
PayloadItem<SimplePayload> item = new PayloadItem(itemId, payload);
System.out.println("Item: " + item.toXML());
return item;
}
static Node getNode(PubSubManager manager, List<Node> nodes, String nodeName ) throws XMPPException {
for (Node node:nodes) {
if (node.getId().contentEquals(nodeName)){
return node;
}
}
// needed first time only to configure node
ConfigureForm form = new ConfigureForm(FormType.submit);
form.setPersistentItems(false);
form.setDeliverPayloads(true);
form.setNotifyRetract(true);
form.setPublishModel(PublishModel.open);
form.setAccessModel(AccessModel.open);
return manager.createNode(nodeName, form);
}
public static void main(String[] args) {
// connect as publisher
SmackConfiguration.setLocalSocks5ProxyPort(LOCAL_SOCKS_PROXY_PORT);
ConnectionConfiguration config = new ConnectionConfiguration(XMPP_SERVER_NAME, XMPP_PORT);
XMPPConnection connection = new XMPPConnection(config);
try {
connection.connect();
connection.login(XMPP_USER_USERNAME, XMPP_USER_PASSWORD, XMPP_SERVER_NAME);
// create node (comment second line and uncomment third to just get already created node
PubSubManager pubSubManager = new PubSubManager(connection, "pubsub." + connection.getServiceName());
LeafNode myNode;
String nodeName = "TestNode13";
try {
LeafNode existingNode = pubSubManager.getNode(nodeName);
//exists, so delete
pubSubManager.deleteNode(nodeName);
} catch (XMPPException e) {
}
ConfigureForm form = new ConfigureForm(FormType.submit);
form.setPersistentItems(false);
form.setDeliverPayloads(false);
form.setNotifyRetract(true);
form.setPublishModel(PublishModel.open);
form.setAccessModel(AccessModel.open);
form.setSubscribe(true);
myNode = (LeafNode) pubSubManager.createNode(nodeName, form);
List<PayloadItem<SimplePayload>> items = new LinkedList();
items.add(createItem());
System.out.println("Items created");
/*try {
Thread.sleep(500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
items.add(createItem());*/
for (PayloadItem<SimplePayload> item: items) {
System.out.println("Sending item");
myNode.send(item);
}
System.out.println("Done");
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
connection.disconnect();
}
}
}
I get the following output:
Item: <item id='1389627527131'>xxx</item>
Items created
Sending item
The error I get is:
bad-request(400)
at org.jivesoftware.smackx.pubsub.packet.SyncPacketSend.getReply(SyncPacketSend.ja va:53)
at org.jivesoftware.smackx.pubsub.packet.SyncPacketSend.getReply(SyncPacketSend.ja va:61)
at org.jivesoftware.smackx.pubsub.LeafNode.send(LeafNode.java:299)
at org.jivesoftware.smackx.pubsub.LeafNode.send(LeafNode.java:276)
at XMPPPublisher.main(XMPPPublisher.java:100)
Any help most appreciated. I'm using the most recent version of the Smack API.
Regards,
Sean