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.PubSubElementType;
023
024import org.jxmpp.jid.Jid;
025
026/**
027 * The standard PubSub extension of an {@link IQ} packet.  This is the topmost
028 * element of all pubsub requests and replies as defined in the <a href="http://xmpp.org/extensions/xep-0060">Publish-Subscribe</a> 
029 * specification.
030 * 
031 * @author Robin Collier
032 */
033public class PubSub extends IQ
034{
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    {
055        return (PE) getExtension(elem.getElementName(), elem.getNamespace().getXmlns());
056    }
057
058    /**
059     * Returns the XML representation of a pubsub element according the specification.
060     * 
061     * The XML representation will be inside of an iq stanza(/packet) like
062     * in the following example:
063     * <pre>
064     * &lt;iq type='set' id="MlIpV-4" to="pubsub.gato.home" from="gato3@gato.home/Smack"&gt;
065     *     &lt;pubsub xmlns="http://jabber.org/protocol/pubsub"&gt;
066     *                      :
067     *         Specific request extension
068     *                      :
069     *     &lt;/pubsub&gt;
070     * &lt;/iq&gt;
071     * </pre>
072     * 
073     */
074    @Override
075    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
076        // N.B. We could use SimpleIQ here, but PubSub IQs will nearly *always* have packet extensions, which means that
077        // SimpleIQs xml.setEmptyElement() is counter-productive in this case and we use xml.rightAngleBracket()
078        // instead, as there are likely sub-elements to follow.
079        xml.rightAngleBracket();
080        return xml;
081    }
082
083    public static PubSub createPubsubPacket(Jid to, Type type, ExtensionElement extension, PubSubNamespace ns) {
084        PubSub pubSub = new PubSub(to, type, ns);
085        pubSub.addExtension(extension);
086        return pubSub;
087    }
088}