PubSub.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.pubsub.packet;

  18. import org.jivesoftware.smack.packet.IQ;
  19. import org.jivesoftware.smack.packet.ExtensionElement;
  20. import org.jivesoftware.smackx.pubsub.PubSubElementType;
  21. import org.jxmpp.jid.Jid;

  22. /**
  23.  * The standard PubSub extension of an {@link IQ} packet.  This is the topmost
  24.  * element of all pubsub requests and replies as defined in the <a href="http://xmpp.org/extensions/xep-0060">Publish-Subscribe</a>
  25.  * specification.
  26.  *
  27.  * @author Robin Collier
  28.  */
  29. public class PubSub extends IQ
  30. {
  31.     public static final String ELEMENT = "pubsub";
  32.     public static final String NAMESPACE = "http://jabber.org/protocol/pubsub";

  33.     public PubSub() {
  34.         super(ELEMENT, NAMESPACE);
  35.     }

  36.     public PubSub(PubSubNamespace ns) {
  37.         super(ELEMENT, ns.getXmlns());
  38.     }

  39.     public PubSub(Jid to, Type type, PubSubNamespace ns) {
  40.         super(ELEMENT, (ns == null ? PubSubNamespace.BASIC : ns).getXmlns());
  41.         setTo(to);
  42.         setType(type);
  43.     }

  44.     /**
  45.     * Returns the XML element name of the extension sub-packet root element.
  46.     *
  47.     * @return the XML element name of the packet extension.
  48.     */
  49.     public String getElementName() {
  50.         return ELEMENT;
  51.     }

  52.     @SuppressWarnings("unchecked")
  53.     public <PE extends ExtensionElement> PE getExtension(PubSubElementType elem)
  54.     {
  55.         return (PE) getExtension(elem.getElementName(), elem.getNamespace().getXmlns());
  56.     }

  57.     /**
  58.      * Returns the XML representation of a pubsub element according the specification.
  59.      *
  60.      * The XML representation will be inside of an iq packet like
  61.      * in the following example:
  62.      * <pre>
  63.      * &lt;iq type='set' id="MlIpV-4" to="pubsub.gato.home" from="gato3@gato.home/Smack"&gt;
  64.      *     &lt;pubsub xmlns="http://jabber.org/protocol/pubsub"&gt;
  65.      *                      :
  66.      *         Specific request extension
  67.      *                      :
  68.      *     &lt;/pubsub&gt;
  69.      * &lt;/iq&gt;
  70.      * </pre>
  71.      *
  72.      */
  73.     @Override
  74.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  75.         // N.B. We could use SimpleIQ here, but PubSub IQs will nearly *always* have packet extensions, which means that
  76.         // SimpleIQs xml.setEmptyElement() is counter-productive in this case and we use xml.rightAngleBracket()
  77.         // instead, as there are likely sub-elements to follow.
  78.         xml.rightAngleBracket();
  79.         return xml;
  80.     }

  81.     public static PubSub createPubsubPacket(Jid to, Type type, ExtensionElement extension, PubSubNamespace ns) {
  82.         PubSub pubSub = new PubSub(to, type, ns);
  83.         pubSub.addExtension(extension);
  84.         return pubSub;
  85.     }
  86. }