001/**
002 *
003 * Copyright the original author or authors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.pubsub.packet;
018
019import org.jivesoftware.smack.packet.ExtensionElement;
020import org.jivesoftware.smack.packet.IQ;
021
022import org.jivesoftware.smackx.pubsub.NodeExtension;
023import org.jivesoftware.smackx.pubsub.PubSubElementType;
024
025import org.jxmpp.jid.Jid;
026
027/**
028 * The standard PubSub extension of an {@link IQ} packet.  This is the topmost
029 * element of all pubsub requests and replies as defined in the <a href="http://xmpp.org/extensions/xep-0060">Publish-Subscribe</a>
030 * specification.
031 *
032 * @author Robin Collier
033 */
034public class PubSub extends IQ {
035    public static final String ELEMENT = "pubsub";
036    public static final String NAMESPACE = "http://jabber.org/protocol/pubsub";
037
038    public PubSub() {
039        super(ELEMENT, NAMESPACE);
040    }
041
042    public PubSub(PubSubNamespace ns) {
043        super(ELEMENT, ns.getXmlns());
044    }
045
046    public PubSub(Jid to, Type type, PubSubNamespace ns) {
047        super(ELEMENT, (ns == null ? PubSubNamespace.basic : ns).getXmlns());
048        setTo(to);
049        setType(type);
050    }
051
052    @SuppressWarnings("unchecked")
053    public <PE extends ExtensionElement> PE getExtension(PubSubElementType elem) {
054        return (PE) getExtension(elem.getElementName(), elem.getNamespace().getXmlns());
055    }
056
057    /**
058     * Returns the XML representation of a pubsub element according the specification.
059     *
060     * The XML representation will be inside of an iq stanza like
061     * in the following example:
062     * <pre>
063     * &lt;iq type='set' id="MlIpV-4" to="pubsub.gato.home" from="gato3@gato.home/Smack"&gt;
064     *     &lt;pubsub xmlns="http://jabber.org/protocol/pubsub"&gt;
065     *                      :
066     *         Specific request extension
067     *                      :
068     *     &lt;/pubsub&gt;
069     * &lt;/iq&gt;
070     * </pre>
071     *
072     */
073    @Override
074    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
075        // N.B. We could use SimpleIQ here, but PubSub IQs will nearly *always* have packet extensions, which means that
076        // SimpleIQs xml.setEmptyElement() is counter-productive in this case and we use xml.rightAngleBracket()
077        // instead, as there are likely sub-elements to follow.
078        xml.rightAngleBracket();
079        return xml;
080    }
081
082    public static PubSub createPubsubPacket(Jid to, Type type, NodeExtension extension) {
083        PubSub pubSub = new PubSub(to, type, extension.getPubSubNamespace());
084        pubSub.addExtension(extension);
085        return pubSub;
086    }
087}