public BlogPostComment sendBlogPostComment(String content, BlogPost blogPost) throws SmackException.NotConnectedException, InterruptedException, XMPPException.XMPPErrorException, SmackException.NoResponseException { Jid jid = XMPPSession.getInstance().getUser().asEntityBareJid(); String userName = XMPPUtils.fromJIDToUserName(jid.toString()); Jid pubSubServiceJid = XMPPSession.getInstance().getPubSubService(); // create stanza PublishCommentExtension publishCommentExtension = new PublishCommentExtension(blogPost.getId(), userName, jid, content, new Date()); PubSub publishCommentPubSub = PubSub.createPubsubPacket(pubSubServiceJid, IQ.Type.set, publishCommentExtension, null); // send stanza XMPPSession.getInstance().sendStanza(publishCommentPubSub); return new BlogPostComment(publishCommentExtension.getId(), blogPost.getId(), content, userName, jid.toString(), publishCommentExtension.getPublished()); }
/** * Creates a node with specified configuration. * * Note: This is the only way to create a collection node. * * @param name The name of the node, which must be unique within the * pubsub service * @param config The configuration for the node * @return The node that was created * @throws XMPPErrorException * @throws NoResponseException * @throws NotConnectedException */ public Node createNode(String name, Form config) throws NoResponseException, XMPPErrorException, NotConnectedException { PubSub request = PubSub.createPubsubPacket(to, Type.set, new NodeExtension(PubSubElementType.CREATE, name), null); boolean isLeafNode = true; if (config != null) { request.addExtension(new FormNode(FormNodeType.CONFIGURE, config)); FormField nodeTypeField = config.getField(ConfigureNodeFields.node_type.getFieldName()); if (nodeTypeField != null) isLeafNode = nodeTypeField.getValues().get(0).equals(NodeType.leaf.toString()); } // Errors will cause exceptions in getReply, so it only returns // on success. sendPubsubPacket(con, request); Node newNode = isLeafNode ? new LeafNode(con, name) : new CollectionNode(con, name); newNode.setTo(to); nodeMap.put(newNode.getId(), newNode); return newNode; }
private List<Subscription> getSubscriptions(List<ExtensionElement> additionalExtensions, Collection<ExtensionElement> returnedExtensions, PubSubNamespace pubSubNamespace) throws NoResponseException, XMPPErrorException, NotConnectedException { PubSub pubSub = createPubsubPacket(Type.get, new NodeExtension(PubSubElementType.SUBSCRIPTIONS, getId()), pubSubNamespace); if (additionalExtensions != null) { for (ExtensionElement pe : additionalExtensions) { pubSub.addExtension(pe); } } PubSub reply = sendPubsubPacket(pubSub); if (returnedExtensions != null) { returnedExtensions.addAll(reply.getExtensions()); } SubscriptionsExtension subElem = (SubscriptionsExtension) reply.getExtension(PubSubElementType.SUBSCRIPTIONS); return subElem.getSubscriptions(); }
/** * Get the affiliations of this node. * <p> * {@code additionalExtensions} can be used e.g. to add a "Result Set Management" extension. * {@code returnedExtensions} will be filled with the stanza(/packet) extensions found in the answer. * </p> * * @param additionalExtensions additional {@code PacketExtensions} add to the request * @param returnedExtensions a collection that will be filled with the returned packet * extensions * @return List of {@link Affiliation} * @throws NoResponseException * @throws XMPPErrorException * @throws NotConnectedException */ public List<Affiliation> getAffiliations(List<ExtensionElement> additionalExtensions, Collection<ExtensionElement> returnedExtensions) throws NoResponseException, XMPPErrorException, NotConnectedException { PubSub pubSub = createPubsubPacket(Type.get, new NodeExtension(PubSubElementType.AFFILIATIONS, getId())); if (additionalExtensions != null) { for (ExtensionElement pe : additionalExtensions) { pubSub.addExtension(pe); } } PubSub reply = sendPubsubPacket(pubSub); if (returnedExtensions != null) { returnedExtensions.addAll(reply.getExtensions()); } AffiliationsExtension affilElem = (AffiliationsExtension) reply.getExtension(PubSubElementType.AFFILIATIONS); return affilElem.getAffiliations(); }
@Override public PubSub parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackException { String namespace = parser.getNamespace(); PubSubNamespace pubSubNamespace = PubSubNamespace.valueOfFromXmlns(namespace); PubSub pubsub = new PubSub(pubSubNamespace); outerloop: while (true) { int eventType = parser.next(); switch (eventType) { case XmlPullParser.START_TAG: PacketParserUtils.addExtensionElement(pubsub, parser); break; case XmlPullParser.END_TAG: if (parser.getDepth() == initialDepth) { break outerloop; } break; } } return pubsub; }
@Test public void getConfigFormWithInsufficientPriviliges() throws XMPPException, SmackException, IOException { ThreadedDummyConnection con = ThreadedDummyConnection.newInstance(); PubSubManager mgr = new PubSubManager(con); DiscoverInfo info = new DiscoverInfo(); Identity ident = new Identity("pubsub", null, "leaf"); info.addIdentity(ident); con.addIQReply(info); Node node = mgr.getNode("princely_musings"); PubSub errorIq = new PubSub(); XMPPError error = new XMPPError(Condition.forbidden); errorIq.setError(error); con.addIQReply(errorIq); try { node.getNodeConfiguration(); } catch (XMPPErrorException e) { Assert.assertEquals(XMPPError.Type.AUTH, e.getXMPPError().getType()); } }
public Map<String, Object> extractHeadersFromXmpp(Packet xmppPacket, Exchange exchange) { Map<String, Object> answer = new HashMap<String, Object>(); PacketExtension jpe = xmppPacket.getExtension(JivePropertiesExtension.NAMESPACE); if (jpe != null && jpe instanceof JivePropertiesExtension) { extractHeadersFrom((JivePropertiesExtension)jpe, exchange, answer); } if (jpe != null && jpe instanceof DefaultPacketExtension) { extractHeadersFrom((DefaultPacketExtension)jpe, exchange, answer); } if (xmppPacket instanceof Message) { Message xmppMessage = (Message) xmppPacket; answer.put(XmppConstants.MESSAGE_TYPE, xmppMessage.getType()); answer.put(XmppConstants.SUBJECT, xmppMessage.getSubject()); answer.put(XmppConstants.THREAD_ID, xmppMessage.getThread()); } else if (xmppPacket instanceof PubSub) { PubSub pubsubPacket = (PubSub) xmppPacket; answer.put(XmppConstants.MESSAGE_TYPE, pubsubPacket.getType()); } answer.put(XmppConstants.FROM, xmppPacket.getFrom()); answer.put(XmppConstants.PACKET_ID, xmppPacket.getPacketID()); answer.put(XmppConstants.TO, xmppPacket.getTo()); return answer; }
/** * Creates a node with specified configuration. * * Note: This is the only way to create a collection node. * * @param name The name of the node, which must be unique within the * pubsub service * @param config The configuration for the node * @return The node that was created * @exception XMPPException */ public Node createNode(String name, Form config) throws XMPPException { PubSub request = createPubsubPacket(to, Type.SET, new NodeExtension(PubSubElementType.CREATE, name)); boolean isLeafNode = true; if (config != null) { request.addExtension(new FormNode(FormNodeType.CONFIGURE, config)); FormField nodeTypeField = config.getField(ConfigureNodeFields.node_type.getFieldName()); if (nodeTypeField != null) isLeafNode = nodeTypeField.getValues().next().equals(NodeType.leaf.toString()); } // Errors will cause exceptions in getReply, so it only returns // on success. sendPubsubPacket(con, to, Type.SET, request); Node newNode = isLeafNode ? new LeafNode(con, name) : new CollectionNode(con, name); newNode.setTo(to); nodeMap.put(newNode.getId(), newNode); return newNode; }
/** * Get the items specified from the node. This would typically be * used when the server does not return the payload due to size * constraints. The user would be required to retrieve the payload * after the items have been retrieved via {@link #getItems()} or an * event, that did not include the payload. * * @param ids Item ids of the items to retrieve * * @return The list of {@link Item} with payload * * @throws XMPPException */ public <T extends Item> List<T> getItems(Collection<String> ids) throws XMPPException { List<Item> itemList = new ArrayList<Item>(ids.size()); for (String id : ids) { itemList.add(new Item(id)); } PubSub request = createPubsubPacket(Type.GET, new ItemsExtension(ItemsExtension.ItemsElementType.items, getId(), itemList)); PubSub result = (PubSub)SyncPacketSend.getReply(con, request); ItemsExtension itemsElem = (ItemsExtension)result.getExtension(PubSubElementType.ITEMS); return (List<T>)itemsElem.getItems(); }
/** * Creates a node with specified configuration. * * Note: This is the only way to create a collection node. * * @param name * The name of the node, which must be unique within the pubsub * service * @param config * The configuration for the node * @return The node that was created * @exception XMPPException */ public Node createNode(String name, Form config) throws XMPPException { PubSub request = createPubsubPacket(to, Type.SET, new NodeExtension( PubSubElementType.CREATE, name)); boolean isLeafNode = true; if (config != null) { request.addExtension(new FormNode(FormNodeType.CONFIGURE, config)); FormField nodeTypeField = config .getField(ConfigureNodeFields.node_type.getFieldName()); if (nodeTypeField != null) isLeafNode = nodeTypeField.getValues().next() .equals(NodeType.leaf.toString()); } // Errors will cause exceptions in getReply, so it only returns // on success. sendPubsubPacket(con, to, Type.SET, request); Node newNode = isLeafNode ? new LeafNode(con, name) : new CollectionNode(con, name); newNode.setTo(to); nodeMap.put(newNode.getId(), newNode); return newNode; }
/** * Get the items specified from the node. This would typically be used when * the server does not return the payload due to size constraints. The user * would be required to retrieve the payload after the items have been * retrieved via {@link #getItems()} or an event, that did not include the * payload. * * @param ids * Item ids of the items to retrieve * * @return The list of {@link Item} with payload * * @throws XMPPException */ public <T extends Item> List<T> getItems(Collection<String> ids) throws XMPPException { List<Item> itemList = new ArrayList<Item>(ids.size()); for (String id : ids) { itemList.add(new Item(id)); } PubSub request = createPubsubPacket(Type.GET, new ItemsExtension( ItemsExtension.ItemsElementType.items, getId(), itemList)); PubSub result = (PubSub) SyncPacketSend.getReply(con, request); ItemsExtension itemsElem = (ItemsExtension) result .getExtension(PubSubElementType.ITEMS); return (List<T>) itemsElem.getItems(); }
public IQ parseIQ(XmlPullParser parser) throws Exception { PubSub pubsub = new PubSub(); String namespace = parser.getNamespace(); pubsub.setPubSubNamespace(PubSubNamespace.valueOfFromXmlns(namespace)); boolean done = false; while (!done) { int eventType = parser.next(); if (eventType == XmlPullParser.START_TAG) { PacketExtension ext = PacketParserUtils.parsePacketExtension( parser.getName(), namespace, parser); if (ext != null) { pubsub.addExtension(ext); } } else if (eventType == XmlPullParser.END_TAG) { if (parser.getName().equals("pubsub")) { done = true; } } } return pubsub; }
/** * Creates an instant node, if supported. * * @return The node that was created * @throws XMPPErrorException * @throws NoResponseException * @throws NotConnectedException */ public LeafNode createNode() throws NoResponseException, XMPPErrorException, NotConnectedException { PubSub reply = sendPubsubPacket(Type.set, new NodeExtension(PubSubElementType.CREATE), null); NodeExtension elem = reply.getExtension("create", PubSubNamespace.BASIC.getXmlns()); LeafNode newNode = new LeafNode(con, elem.getNode()); newNode.setTo(to); nodeMap.put(newNode.getId(), newNode); return newNode; }
/** * Retrieves the requested node, if it exists. It will throw an * exception if it does not. * * @param id - The unique id of the node * @return the node * @throws XMPPErrorException The node does not exist * @throws NoResponseException if there was no response from the server. * @throws NotConnectedException */ @SuppressWarnings("unchecked") public <T extends Node> T getNode(String id) throws NoResponseException, XMPPErrorException, NotConnectedException { Node node = nodeMap.get(id); if (node == null) { DiscoverInfo info = new DiscoverInfo(); info.setTo(to); info.setNode(id); DiscoverInfo infoReply = (DiscoverInfo) con.createPacketCollectorAndSend(info).nextResultOrThrow(); if (infoReply.hasIdentity(PubSub.ELEMENT, "leaf")) { node = new LeafNode(con, id); } else if (infoReply.hasIdentity(PubSub.ELEMENT, "collection")) { node = new CollectionNode(con, id); } else { // XEP-60 5.3 states that // "The 'disco#info' result MUST include an identity with a category of 'pubsub' and a type of either 'leaf' or 'collection'." // If this is not the case, then we are dealing with an PubSub implementation that doesn't follow the specification. throw new AssertionError( "PubSub service '" + to + "' returned disco info result for node '" + id + "', but it did not contain an Identity of type 'leaf' or 'collection' (and category 'pubsub'), which is not allowed according to XEP-60 5.3."); } node.setTo(to); nodeMap.put(id, node); } return (T) node; }
static PubSub sendPubsubPacket(XMPPConnection con, String to, Type type, List<ExtensionElement> extList, PubSubNamespace ns) throws NoResponseException, XMPPErrorException, NotConnectedException { PubSub pubSub = new PubSub(to, type, ns); for (ExtensionElement pe : extList) { pubSub.addExtension(pe); } return sendPubsubPacket(con ,pubSub); }
static PubSub sendPubsubPacket(XMPPConnection con, PubSub packet) throws NoResponseException, XMPPErrorException, NotConnectedException { IQ resultIQ = con.createPacketCollectorAndSend(packet).nextResultOrThrow(); if (resultIQ instanceof EmptyResultIQ) { return null; } return (PubSub) resultIQ; }
@SuppressWarnings("unchecked") private <T extends Item> List<T> getItems(PubSub request, List<ExtensionElement> returnedExtensions) throws NoResponseException, XMPPErrorException, NotConnectedException { PubSub result = con.createPacketCollectorAndSend(request).nextResultOrThrow(); ItemsExtension itemsElem = result.getExtension(PubSubElementType.ITEMS); if (returnedExtensions != null) { returnedExtensions.addAll(result.getExtensions()); } return (List<T>) itemsElem.getItems(); }
/** * Delete the items with the specified id's from the node. * * @param itemIds The list of id's of items to delete * @throws XMPPErrorException * @throws NoResponseException if there was no response from the server. * @throws NotConnectedException */ public void deleteItem(Collection<String> itemIds) throws NoResponseException, XMPPErrorException, NotConnectedException { List<Item> items = new ArrayList<Item>(itemIds.size()); for (String id : itemIds) { items.add(new Item(id)); } PubSub request = createPubsubPacket(Type.set, new ItemsExtension(ItemsExtension.ItemsElementType.retract, getId(), items)); con.createPacketCollectorAndSend(request).nextResultOrThrow(); }
/** * Creates an instant node, if supported. * * @return The node that was created * @exception XMPPException */ public LeafNode createNode() throws XMPPException { PubSub reply = (PubSub)sendPubsubPacket(Type.SET, new NodeExtension(PubSubElementType.CREATE)); NodeExtension elem = (NodeExtension)reply.getExtension("create", PubSubNamespace.BASIC.getXmlns()); LeafNode newNode = new LeafNode(con, elem.getNode()); newNode.setTo(to); nodeMap.put(newNode.getId(), newNode); return newNode; }
/** * Gets the affiliations on the root node. * * @return List of affiliations * * @throws XMPPException */ public List<Affiliation> getAffiliations() throws XMPPException { PubSub reply = (PubSub)sendPubsubPacket(Type.GET, new NodeExtension(PubSubElementType.AFFILIATIONS)); AffiliationsExtension listElem = (AffiliationsExtension)reply.getExtension(PubSubElementType.AFFILIATIONS); return listElem.getAffiliations(); }
/** * Returns the default settings for Node configuration. * * @return configuration form containing the default settings. */ public ConfigureForm getDefaultConfiguration() throws XMPPException { // Errors will cause exceptions in getReply, so it only returns // on success. PubSub reply = (PubSub)sendPubsubPacket(Type.GET, new NodeExtension(PubSubElementType.DEFAULT), PubSubElementType.DEFAULT.getNamespace()); return NodeUtils.getFormFromPacket(reply, PubSubElementType.DEFAULT); }
static PubSub createPubsubPacket(String to, Type type, PacketExtension ext, PubSubNamespace ns) { PubSub request = new PubSub(); request.setTo(to); request.setType(type); if (ns != null) { request.setPubSubNamespace(ns); } request.addExtension(ext); return request; }
/** * Get the current items stored in the node. * * @return List of {@link Item} in the node * * @throws XMPPException */ public <T extends Item> List<T> getItems() throws XMPPException { PubSub request = createPubsubPacket(Type.GET, new GetItemsRequest(getId())); PubSub result = (PubSub)SyncPacketSend.getReply(con, request); ItemsExtension itemsElem = (ItemsExtension)result.getExtension(PubSubElementType.ITEMS); return (List<T>)itemsElem.getItems(); }
/** * Get items persisted on the node, limited to the specified number. * * @param maxItems Maximum number of items to return * * @return List of {@link Item} * * @throws XMPPException */ public <T extends Item> List<T> getItems(int maxItems) throws XMPPException { PubSub request = createPubsubPacket(Type.GET, new GetItemsRequest(getId(), maxItems)); PubSub result = (PubSub)SyncPacketSend.getReply(con, request); ItemsExtension itemsElem = (ItemsExtension)result.getExtension(PubSubElementType.ITEMS); return (List<T>)itemsElem.getItems(); }
/** * Delete the items with the specified id's from the node. * * @param itemIds The list of id's of items to delete * * @throws XMPPException */ public void deleteItem(Collection<String> itemIds) throws XMPPException { List<Item> items = new ArrayList<Item>(itemIds.size()); for (String id : itemIds) { items.add(new Item(id)); } PubSub request = createPubsubPacket(Type.SET, new ItemsExtension(ItemsExtension.ItemsElementType.retract, getId(), items)); SyncPacketSend.getReply(con, request); }
/** * Update the configuration with the contents of the new {@link Form} * * @param submitForm */ public void sendConfigurationForm(Form submitForm) throws XMPPException { PubSub packet = createPubsubPacket(Type.SET, new FormNode(FormNodeType.CONFIGURE_OWNER, getId(), submitForm), PubSubNamespace.OWNER); SyncPacketSend.getReply(con, packet); }
/** * Get the subscriptions currently associated with this node. * * @return List of {@link Subscription} * * @throws XMPPException */ public List<Subscription> getSubscriptions() throws XMPPException { PubSub reply = (PubSub)sendPubsubPacket(Type.GET, new NodeExtension(PubSubElementType.SUBSCRIPTIONS, getId())); SubscriptionsExtension subElem = (SubscriptionsExtension)reply.getExtension(PubSubElementType.SUBSCRIPTIONS); return subElem.getSubscriptions(); }
public IQ parseIQ(XmlPullParser parser) throws Exception { PubSub pubsub = new PubSub(); String namespace = parser.getNamespace(); pubsub.setPubSubNamespace(PubSubNamespace.valueOfFromXmlns(namespace)); boolean done = false; while (!done) { int eventType = parser.next(); if (eventType == XmlPullParser.START_TAG) { PacketExtension ext = PacketParserUtils.parsePacketExtension(parser.getName(), namespace, parser); if (ext != null) { pubsub.addExtension(ext); } } else if (eventType == XmlPullParser.END_TAG) { if (parser.getName().equals("pubsub")) { done = true; } } } return pubsub; }