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