mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-13 11:09:39 +02:00
SMACK-272 Add support for XEP-0060 (pubsub)
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@11346 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
92ba2d7f33
commit
f7a1c750ad
73 changed files with 7214 additions and 0 deletions
106
source/org/jivesoftware/smackx/pubsub/packet/PubSub.java
Normal file
106
source/org/jivesoftware/smackx/pubsub/packet/PubSub.java
Normal file
|
@ -0,0 +1,106 @@
|
|||
/**
|
||||
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smackx.pubsub.packet;
|
||||
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.PacketExtension;
|
||||
import org.jivesoftware.smackx.pubsub.PubSubElementType;
|
||||
|
||||
/**
|
||||
* The standard PubSub extension of an {@link IQ} packet. This is the topmost
|
||||
* element of all pubsub requests and replies as defined in the <a href="http://xmpp.org/extensions/xep-0060">Publish-Subscribe</a>
|
||||
* specification.
|
||||
*
|
||||
* @author Robin Collier
|
||||
*/
|
||||
public class PubSub extends IQ
|
||||
{
|
||||
private PubSubNamespace ns = PubSubNamespace.BASIC;
|
||||
|
||||
/**
|
||||
* Returns the XML element name of the extension sub-packet root element.
|
||||
*
|
||||
* @return the XML element name of the packet extension.
|
||||
*/
|
||||
public String getElementName() {
|
||||
return "pubsub";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the XML namespace of the extension sub-packet root element.
|
||||
* According the specification the namespace is
|
||||
* http://jabber.org/protocol/pubsub with a specific fragment depending
|
||||
* on the request. The namespace is defined at <a href="http://xmpp.org/registrar/namespaces.html">XMPP Registrar</a> at
|
||||
*
|
||||
* The default value has no fragment.
|
||||
*
|
||||
* @return the XML namespace of the packet extension.
|
||||
*/
|
||||
public String getNamespace()
|
||||
{
|
||||
return ns.getXmlns();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the namespace for the packet if it something other than the default
|
||||
* case of {@link PubSubNamespace#BASIC}. The {@link #getNamespace()} method will return
|
||||
* the result of calling {@link PubSubNamespace#getXmlns()} on the specified enum.
|
||||
*
|
||||
* @param ns - The new value for the namespace.
|
||||
*/
|
||||
public void setPubSubNamespace(PubSubNamespace ns)
|
||||
{
|
||||
this.ns = ns;
|
||||
}
|
||||
|
||||
public PacketExtension getExtension(PubSubElementType elem)
|
||||
{
|
||||
return getExtension(elem.getElementName(), elem.getNamespace().getXmlns());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current value of the namespace. The {@link #getNamespace()} method will return
|
||||
* the result of calling {@link PubSubNamespace#getXmlns()} this value.
|
||||
*
|
||||
* @return The current value of the namespace.
|
||||
*/
|
||||
public PubSubNamespace getPubSubNamespace()
|
||||
{
|
||||
return ns;
|
||||
}
|
||||
/**
|
||||
* Returns the XML representation of a pubsub element according the specification.
|
||||
*
|
||||
* The XML representation will be inside of an iq packet like
|
||||
* in the following example:
|
||||
* <pre>
|
||||
* <iq type='set' id="MlIpV-4" to="pubsub.gato.home" from="gato3@gato.home/Smack">
|
||||
* <pubsub xmlns="http://jabber.org/protocol/pubsub">
|
||||
* :
|
||||
* Specific request extension
|
||||
* :
|
||||
* </pubsub>
|
||||
* </iq>
|
||||
* </pre>
|
||||
*
|
||||
*/
|
||||
public String getChildElementXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append("\">");
|
||||
buf.append(getExtensionsXML());
|
||||
buf.append("</").append(getElementName()).append(">");
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
/**
|
||||
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smackx.pubsub.packet;
|
||||
|
||||
/**
|
||||
* Defines all the valid namespaces that are used with the {@link PubSub} packet
|
||||
* as defined by the specification.
|
||||
*
|
||||
* @author Robin Collier
|
||||
*/
|
||||
public enum PubSubNamespace
|
||||
{
|
||||
BASIC(null),
|
||||
ERROR("errors"),
|
||||
EVENT("event"),
|
||||
OWNER("owner");
|
||||
|
||||
private String fragment;
|
||||
|
||||
private PubSubNamespace(String fragment)
|
||||
{
|
||||
this.fragment = fragment;
|
||||
}
|
||||
|
||||
public String getXmlns()
|
||||
{
|
||||
String ns = "http://jabber.org/protocol/pubsub";
|
||||
|
||||
if (fragment != null)
|
||||
ns += '#' + fragment;
|
||||
|
||||
return ns;
|
||||
}
|
||||
|
||||
public String getFragment()
|
||||
{
|
||||
return fragment;
|
||||
}
|
||||
|
||||
public static PubSubNamespace valueOfFromXmlns(String ns)
|
||||
{
|
||||
int index = ns.lastIndexOf('#');
|
||||
|
||||
if (index != -1)
|
||||
{
|
||||
String suffix = ns.substring(ns.lastIndexOf('#')+1);
|
||||
return valueOf(suffix.toUpperCase());
|
||||
}
|
||||
else
|
||||
return BASIC;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
/**
|
||||
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smackx.pubsub.packet;
|
||||
|
||||
import org.jivesoftware.smack.PacketCollector;
|
||||
import org.jivesoftware.smack.SmackConfiguration;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.filter.PacketFilter;
|
||||
import org.jivesoftware.smack.filter.PacketIDFilter;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
|
||||
/**
|
||||
* Utility class for doing synchronous calls to the server. Provides several
|
||||
* methods for sending a packet to the server and waiting for the reply.
|
||||
*
|
||||
* @author Robin Collier
|
||||
*/
|
||||
final public class SyncPacketSend
|
||||
{
|
||||
private SyncPacketSend()
|
||||
{ }
|
||||
|
||||
static public Packet getReply(XMPPConnection connection, Packet packet, long timeout)
|
||||
throws XMPPException
|
||||
{
|
||||
PacketFilter responseFilter = new PacketIDFilter(packet.getPacketID());
|
||||
PacketCollector response = connection.createPacketCollector(responseFilter);
|
||||
|
||||
connection.sendPacket(packet);
|
||||
|
||||
// Wait up to a certain number of seconds for a reply.
|
||||
Packet result = response.nextResult(timeout);
|
||||
|
||||
// Stop queuing results
|
||||
response.cancel();
|
||||
|
||||
if (result == null) {
|
||||
throw new XMPPException("No response from server.");
|
||||
}
|
||||
else if (result.getError() != null) {
|
||||
throw new XMPPException(result.getError());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static public Packet getReply(XMPPConnection connection, Packet packet)
|
||||
throws XMPPException
|
||||
{
|
||||
return getReply(connection, packet, SmackConfiguration.getPacketReplyTimeout());
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue