1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-12-14 15:01:07 +01:00

Refactor PEP to use PubSub API.

Fixes SMACK-416.
This commit is contained in:
Florian Schmaus 2015-08-17 08:16:49 +02:00
parent 0d6f00873f
commit 33e5c37af8
17 changed files with 479 additions and 453 deletions

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2003-2007 Jive Software.
* Copyright 2003-2007 Jive Software, 2015 Florian Schmaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,8 +17,9 @@
package org.jivesoftware.smackx.pep;
import org.jivesoftware.smackx.pep.packet.PEPEvent;
import org.jxmpp.jid.Jid;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smackx.pubsub.EventElement;
import org.jxmpp.jid.EntityBareJid;
/**
@ -26,6 +27,7 @@ import org.jxmpp.jid.Jid;
* A listener that is fired anytime a PEP event message is received.
*
* @author Jeff Williams
* @author Florian Schmaus
*/
public interface PEPListener {
@ -34,7 +36,8 @@ public interface PEPListener {
*
* @param from the user that sent the entries.
* @param event the event contained in the message.
* @param message the message stanza containing the PEP event.
*/
public void eventReceived(Jid from, PEPEvent event);
public void eventReceived(EntityBareJid from, EventElement event, Message message);
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2003-2007 Jive Software.
* Copyright 2003-2007 Jive Software, 2015 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,21 +17,32 @@
package org.jivesoftware.smackx.pep;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.StanzaExtensionFilter;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.filter.jidtype.FromJidTypeFilter;
import org.jivesoftware.smack.filter.jidtype.AbstractJidTypeFilter.JidType;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smackx.pep.packet.PEPEvent;
import org.jivesoftware.smackx.pep.packet.PEPItem;
import org.jivesoftware.smackx.pep.packet.PEPPubSub;
import org.jxmpp.jid.Jid;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.pubsub.EventElement;
import org.jivesoftware.smackx.pubsub.Item;
import org.jivesoftware.smackx.pubsub.LeafNode;
import org.jivesoftware.smackx.pubsub.PubSubFeature;
import org.jivesoftware.smackx.pubsub.PubSubManager;
import org.jivesoftware.smackx.pubsub.filter.EventExtensionFilter;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.EntityBareJid;
/**
*
@ -44,38 +55,55 @@ import org.jxmpp.jid.Jid;
* <pre>
* PEPManager pepManager = new PEPManager(smackConnection);
* pepManager.addPEPListener(new PEPListener() {
* public void eventReceived(String inFrom, PEPEvent inEvent) {
* LOGGER.debug("Event received: " + inEvent);
* public void eventReceived(EntityBareJid from, EventElement event, Message message) {
* LOGGER.debug("Event received: " + event);
* }
* });
*
* PEPProvider pepProvider = new PEPProvider();
* pepProvider.registerPEPParserExtension("http://jabber.org/protocol/tune", new TuneProvider());
* ProviderManager.getInstance().addExtensionProvider("event", "http://jabber.org/protocol/pubsub#event", pepProvider);
*
* Tune tune = new Tune("jeff", "1", "CD", "My Title", "My Track");
* pepManager.publish(tune);
* </pre>
*
* @author Jeff Williams
* @author Florian Schmaus
*/
public class PEPManager {
public final class PEPManager extends Manager {
private List<PEPListener> pepListeners = new ArrayList<PEPListener>();
private static final Map<XMPPConnection, PEPManager> INSTANCES = new WeakHashMap<>();
private XMPPConnection connection;
public static synchronized PEPManager getInstanceFor(XMPPConnection connection) {
PEPManager pepManager = INSTANCES.get(connection);
if (pepManager == null) {
pepManager = new PEPManager(connection);
INSTANCES.put(connection, pepManager);
}
return pepManager;
}
private StanzaFilter packetFilter = new StanzaExtensionFilter("event", "http://jabber.org/protocol/pubsub#event");
private StanzaListener packetListener;
private static final StanzaFilter FROM_BARE_JID_WITH_EVENT_EXTENSION_FILTER = new AndFilter(
new FromJidTypeFilter(JidType.BareJid),
EventExtensionFilter.INSTANCE);
private final Set<PEPListener> pepListeners = new CopyOnWriteArraySet<>();
/**
* Creates a new PEP exchange manager.
*
* @param connection an XMPPConnection which is used to send and receive messages.
*/
public PEPManager(XMPPConnection connection) {
this.connection = connection;
init();
private PEPManager(XMPPConnection connection) {
super(connection);
StanzaListener packetListener = new StanzaListener() {
public void processPacket(Stanza stanza) {
Message message = (Message) stanza;
EventElement event = EventElement.from(stanza);
assert(event != null);
EntityBareJid from = message.getFrom().asEntityBareJidIfPossible();
assert(from != null);
for (PEPListener listener : pepListeners) {
listener.eventReceived(from, event, message);
}
}
};
// TODO Add filter to check if from supports PubSub as per xep163 2 2.4
connection.addSyncStanzaListener(packetListener, FROM_BARE_JID_WITH_EVENT_EXTENSION_FILTER);
}
/**
@ -84,12 +112,8 @@ public class PEPManager {
*
* @param pepListener a roster exchange listener.
*/
public void addPEPListener(PEPListener pepListener) {
synchronized (pepListeners) {
if (!pepListeners.contains(pepListener)) {
pepListeners.add(pepListener);
}
}
public boolean addPEPListener(PEPListener pepListener) {
return pepListeners.add(pepListener);
}
/**
@ -97,63 +121,44 @@ public class PEPManager {
*
* @param pepListener a roster exchange listener.
*/
public void removePEPListener(PEPListener pepListener) {
synchronized (pepListeners) {
pepListeners.remove(pepListener);
}
public boolean removePEPListener(PEPListener pepListener) {
return pepListeners.remove(pepListener);
}
/**
* Publish an event.
*
* @param item the item to publish.
* @throws NotConnectedException
* @throws InterruptedException
* @param node the node to publish on.
* @throws NotConnectedException
* @throws InterruptedException
* @throws XMPPErrorException
* @throws NoResponseException
*/
public void publish(PEPItem item) throws NotConnectedException, InterruptedException {
// Create a new message to publish the event.
PEPPubSub pubSub = new PEPPubSub(item);
pubSub.setType(Type.set);
//pubSub.setFrom(connection.getUser());
// Send the message that contains the roster
connection.sendStanza(pubSub);
public void publish(Item item, String node) throws NotConnectedException, InterruptedException,
NoResponseException, XMPPErrorException {
XMPPConnection connection = connection();
PubSubManager pubSubManager = PubSubManager.getInstance(connection, connection.getUser().asEntityBareJid());
LeafNode pubSubNode = pubSubManager.getNode(node);
pubSubNode.publish(item);
}
/**
* Fires roster exchange listeners.
* XEP-163 5.
*/
private void firePEPListeners(Jid from, PEPEvent event) {
PEPListener[] listeners = null;
synchronized (pepListeners) {
listeners = new PEPListener[pepListeners.size()];
pepListeners.toArray(listeners);
}
for (int i = 0; i < listeners.length; i++) {
listeners[i].eventReceived(from, event);
}
}
private static final PubSubFeature[] REQUIRED_FEATURES = new PubSubFeature[] {
// @formatter:off
PubSubFeature.auto_create,
PubSubFeature.auto_subscribe,
PubSubFeature.filtered_notifications,
// @formatter:on
};
private void init() {
// Listens for all roster exchange packets and fire the roster exchange listeners.
packetListener = new StanzaListener() {
public void processPacket(Stanza packet) {
Message message = (Message) packet;
PEPEvent event = (PEPEvent) message.getExtension("event", "http://jabber.org/protocol/pubsub#event");
// Fire event for roster exchange listeners
firePEPListeners(message.getFrom(), event);
}
};
connection.addSyncStanzaListener(packetListener, packetFilter);
}
public void destroy() {
if (connection != null)
connection.removeSyncStanzaListener(packetListener);
}
protected void finalize() throws Throwable {
destroy();
super.finalize();
public boolean isSupported() throws NoResponseException, XMPPErrorException,
NotConnectedException, InterruptedException {
XMPPConnection connection = connection();
ServiceDiscoveryManager serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
BareJid localBareJid = connection.getUser().asBareJid();
return serviceDiscoveryManager.supportsFeatures(localBareJid, REQUIRED_FEATURES);
}
}

View file

@ -1,102 +0,0 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* 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.pep.packet;
import org.jivesoftware.smack.packet.ExtensionElement;
/**
* Represents XMPP Personal Event Protocol packets.<p>
*
* The 'http://jabber.org/protocol/pubsub#event' namespace is used to publish personal events items from one client
* to subscribed clients (See XEP-163).
*
* @author Jeff Williams
*/
public class PEPEvent implements ExtensionElement {
PEPItem item;
/**
* Creates a new empty roster exchange package.
*
*/
public PEPEvent() {
super();
}
/**
* Creates a new empty roster exchange package.
*
*/
public PEPEvent(PEPItem item) {
super();
this.item = item;
}
public void addPEPItem(PEPItem item) {
this.item = item;
}
/**
* Returns the XML element name of the extension sub-packet root element.
* Always returns "x"
*
* @return the XML element name of the stanza(/packet) extension.
*/
public String getElementName() {
return "event";
}
/**
* Returns the XML namespace of the extension sub-packet root element.
* According the specification the namespace is always "jabber:x:roster"
* (which is not to be confused with the 'jabber:iq:roster' namespace
*
* @return the XML namespace of the stanza(/packet) extension.
*/
public String getNamespace() {
return "http://jabber.org/protocol/pubsub";
}
/**
* Returns the XML representation of a Personal Event Publish according the specification.
*
* Usually the XML representation will be inside of a Message XML representation like
* in the following example:
* <pre>
* &lt;message id="MlIpV-4" to="gato1@gato.home" from="gato3@gato.home/Smack"&gt;
* &lt;subject&gt;Any subject you want&lt;/subject&gt;
* &lt;body&gt;This message contains roster items.&lt;/body&gt;
* &lt;x xmlns="jabber:x:roster"&gt;
* &lt;item jid="gato1@gato.home"/&gt;
* &lt;item jid="gato2@gato.home"/&gt;
* &lt;/x&gt;
* &lt;/message&gt;
* </pre>
*
*/
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append('<').append(getElementName()).append(" xmlns=\"").append(getNamespace()).append("\">");
buf.append(item.toXML());
buf.append("</").append(getElementName()).append('>');
return buf.toString();
}
}

View file

@ -1,89 +0,0 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* 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.pep.packet;
import org.jivesoftware.smack.packet.ExtensionElement;
/**
* Represents XMPP Personal Event Protocol packets.<p>
*
* The 'http://jabber.org/protocol/pubsub#event' namespace is used to publish personal events items from one client
* to subscribed clients (See XEP-163).
*
* @author Jeff Williams
*/
public abstract class PEPItem implements ExtensionElement {
String id;
public abstract String getNode();
public abstract String getItemDetailsXML();
/**
* Creates a new PEPItem.
*
*/
public PEPItem(String id) {
super();
this.id = id;
}
/**
* Returns the XML element name of the extension sub-packet root element.
* Always returns "x"
*
* @return the XML element name of the stanza(/packet) extension.
*/
public String getElementName() {
return "item";
}
/**
* Returns the XML namespace of the extension sub-packet root element.
*
* @return the XML namespace of the stanza(/packet) extension.
*/
public String getNamespace() {
return "http://jabber.org/protocol/pubsub";
}
/**
* Returns the XML representation of a Personal Event Publish according the specification.
*
* Usually the XML representation will be inside of a Message XML representation like
* in the following example:
* <pre>
* &lt;message id="MlIpV-4" to="gato1@gato.home" from="gato3@gato.home/Smack"&gt;
* &lt;subject&gt;Any subject you want&lt;/subject&gt;
* &lt;body&gt;This message contains roster items.&lt;/body&gt;
* &lt;x xmlns="jabber:x:roster"&gt;
* &lt;item jid="gato1@gato.home"/&gt;
* &lt;item jid="gato2@gato.home"/&gt;
* &lt;/x&gt;
* &lt;/message&gt;
* </pre>
*
*/
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append('<').append(getElementName()).append(" id=\"").append(id).append("\">");
buf.append(getItemDetailsXML());
buf.append("</").append(getElementName()).append('>');
return buf.toString();
}
}

View file

@ -1,74 +0,0 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* 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.pep.packet;
import org.jivesoftware.smack.packet.IQ;
/**
* Represents XMPP PEP/XEP-163 pubsub packets.<p>
*
* The 'http://jabber.org/protocol/pubsub' namespace is used to publish personal events items from one client
* to subscribed clients (See XEP-163).
*
* @author Jeff Williams
*/
public class PEPPubSub extends IQ {
public static final String ELEMENT = "pubsub";
public static final String NAMESPACE = "http://jabber.org/protocol/pubsub";
private final PEPItem item;
/**
* Creates a new PubSub.
*
*/
public PEPPubSub(PEPItem item) {
super(ELEMENT, NAMESPACE);
this.item = item;
}
/**
* Returns the XML representation of a Personal Event Publish according the specification.
*
* Usually the XML representation will be inside of a Message XML representation like
* in the following example:
* <pre>
* &lt;message id="MlIpV-4" to="gato1@gato.home" from="gato3@gato.home/Smack"&gt;
* &lt;subject&gt;Any subject you want&lt;/subject&gt;
* &lt;body&gt;This message contains roster items.&lt;/body&gt;
* &lt;x xmlns="jabber:x:roster"&gt;
* &lt;item jid="gato1@gato.home"/&gt;
* &lt;item jid="gato2@gato.home"/&gt;
* &lt;/x&gt;
* &lt;/message&gt;
* </pre>
*
*/
@Override
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
buf.rightAngleBracket();
buf.openElement("publish").attribute("node", item.getNode()).rightAngleBracket();
buf.append(item.toXML());
buf.closeElement("publish");
return buf;
}
}

View file

@ -1,21 +0,0 @@
/**
*
* Copyright 2015 Florian Schmaus
*
* 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.
*/
/**
* TODO describe me.
*/
package org.jivesoftware.smackx.pep.packet;

View file

@ -1,84 +0,0 @@
/**
*
* Copyright 2003-2007 Jive Software.
*
* 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.pep.provider;
import java.util.HashMap;
import java.util.Map;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.provider.ExtensionElementProvider;
import org.xmlpull.v1.XmlPullParser;
/**
*
* The PEPProvider parses incoming PEPEvent packets.
* (XEP-163 has a weird asymmetric deal: outbound PEP are <iq> + <pubsub> and inbound are <message> + <event>.
* The provider only deals with inbound, and so it only deals with <message>.
*
* Anyhoo...
*
* The way this works is that PEPxxx classes are generic <pubsub> and <message> providers, and anyone who
* wants to publish/receive PEPs, such as <tune>, <geoloc>, etc., simply need to extend PEPItem and register (here)
* a PacketExtensionProvider that knows how to parse that PEPItem extension.
*
* @author Jeff Williams
*/
public class PEPProvider extends ExtensionElementProvider<ExtensionElement> {
private static final Map<String, ExtensionElementProvider<?>> nodeParsers = new HashMap<String, ExtensionElementProvider<?>>();
public static void registerPEPParserExtension(String node, ExtensionElementProvider<?> pepItemParser) {
nodeParsers.put(node, pepItemParser);
}
/**
* Parses a PEPEvent stanza(/packet) and extracts a PEPItem from it.
* (There is only one per <event>.)
*
* @param parser the XML parser, positioned at the starting element of the extension.
* @return a PacketExtension.
* @throws Exception
*/
@Override
public ExtensionElement parse(XmlPullParser parser, int initialDepth)
throws Exception {
ExtensionElement pepItem = null;
boolean done = false;
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("event")) {
} else if (parser.getName().equals("items")) {
// Figure out the node for this event.
String node = parser.getAttributeValue("", "node");
// Get the parser for this kind of node, and if found then parse the node.
ExtensionElementProvider<?> nodeParser = nodeParsers.get(node);
if (nodeParser != null) {
pepItem = nodeParser.parse(parser);
}
}
} else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals("event")) {
done = true;
}
}
}
return pepItem;
}
}

View file

@ -1,21 +0,0 @@
/**
*
* Copyright 2015 Florian Schmaus
*
* 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.
*/
/**
* TODO describe me.
*/
package org.jivesoftware.smackx.pep.provider;