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.IQ; 020import org.jivesoftware.smack.packet.XmlElement; 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 @SuppressWarnings("this-escape") 047 public PubSub(Jid to, Type type, PubSubNamespace ns) { 048 super(ELEMENT, (ns == null ? PubSubNamespace.basic : ns).getXmlns()); 049 setTo(to); 050 setType(type); 051 } 052 053 @SuppressWarnings({"unchecked", "TypeParameterUnusedInFormals"}) 054 public <PE extends XmlElement> PE getExtension(PubSubElementType elem) { 055 return (PE) getExtensionElement(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 like 062 * in the following example: 063 * <pre> 064 * <iq type='set' id="MlIpV-4" to="pubsub.gato.home" from="gato3@gato.home/Smack"> 065 * <pubsub xmlns="http://jabber.org/protocol/pubsub"> 066 * : 067 * Specific request extension 068 * : 069 * </pubsub> 070 * </iq> 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, NodeExtension extension) { 084 PubSub pubSub = new PubSub(to, type, extension.getPubSubNamespace()); 085 pubSub.addExtension(extension); 086 return pubSub; 087 } 088}