From e63fe2264729f0d8c975ab4be5a2264e35e89e0b Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 29 Oct 2014 23:47:47 +0100 Subject: [PATCH] Add support for XEP-335: JSON Containers and GCM JSON payloads for Google's GCM Cloud Connection Server. --- documentation/extensions/index.md | 2 + .../smack/provider/ProviderFileLoader.java | 4 ++ .../smackx/gcm/packet/GcmPacketExtension.java | 59 +++++++++++++++++++ .../gcm/provider/GcmExtensionProvider.java | 29 +++++++++ .../packet/AbstractJsonPacketExtension.java | 42 +++++++++++++ .../json/packet/JsonPacketExtension.java | 54 +++++++++++++++++ .../AbstractJsonExtensionProvider.java | 39 ++++++++++++ .../json/provider/JsonExtensionProvider.java | 28 +++++++++ .../experimental.providers | 14 +++++ 9 files changed, 271 insertions(+) create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/gcm/packet/GcmPacketExtension.java create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/gcm/provider/GcmExtensionProvider.java create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/json/packet/AbstractJsonPacketExtension.java create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/json/packet/JsonPacketExtension.java create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/json/provider/AbstractJsonExtensionProvider.java create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/json/provider/JsonExtensionProvider.java diff --git a/documentation/extensions/index.md b/documentation/extensions/index.md index 9ab21ed36..33bcd13da 100644 --- a/documentation/extensions/index.md +++ b/documentation/extensions/index.md @@ -67,6 +67,8 @@ Experimental Smack Extensions and currently supported XEPs by Smack (smack-exper |---------------------------------------------|----------------------------------------------------------|----------------------------------------------------------------------------------------------------------| | Message Carbons | [XEP-0280](http://xmpp.org/extensions/xep-0280.html) | Keep all IM clients for a user engaged in a conversation, by carbon-copy outbound messages to all interested resources. | [HTTP over XMPP transport](hoxt.html) | [XEP-0332](http://xmpp.org/extensions/xep-0332.html) | Allows to transport HTTP communication over XMPP peer-to-peer networks. | +| JSON Containers | [XEP-0335](http://xmpp.org/extensions/xep-0335.html) | Encapsulation of JSON data within XMPP Stanzas. | +| Google GCM JSON payload | n/a | Semantically the same as XEP-0335: JSON Containers | | Client State Indication | [XEP-0352](http://xmpp.org/extensions/xep-0352.html) | A way for the client to indicate its active/inactive state. | diff --git a/smack-core/src/main/java/org/jivesoftware/smack/provider/ProviderFileLoader.java b/smack-core/src/main/java/org/jivesoftware/smack/provider/ProviderFileLoader.java index 5f33842c5..442020bb6 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/provider/ProviderFileLoader.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/provider/ProviderFileLoader.java @@ -125,6 +125,10 @@ public class ProviderFileLoader implements ProviderLoader { LOGGER.log(Level.SEVERE, "Could not find provider class", cnfe); exceptions.add(cnfe); } + catch (InstantiationException ie) { + LOGGER.log(Level.SEVERE, "Could not instanciate " + className, ie); + exceptions.add(ie); + } } } catch (IllegalArgumentException illExc) { diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/gcm/packet/GcmPacketExtension.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/gcm/packet/GcmPacketExtension.java new file mode 100644 index 000000000..06c638f30 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/gcm/packet/GcmPacketExtension.java @@ -0,0 +1,59 @@ +/** + * + * Copyright © 2014 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. + */ +package org.jivesoftware.smackx.gcm.packet; + +import org.jivesoftware.smack.packet.Packet; +import org.jivesoftware.smackx.json.packet.AbstractJsonPacketExtension; + +/** + * XMPP extension elements as used by Google's GCM Cloud Connection Server (XMPP). + *

+ * This extension is semantically the same as {@link org.jivesoftware.smackx.json.packet.JsonPacketExtension}, but with + * a different element and namespace. It is used to exchange message stanzas with a JSON payload as extension element. + *

+ * + * @see GCM Cloud Connection Server (XMPP) + */ +public class GcmPacketExtension extends AbstractJsonPacketExtension { + + public static final String ELEMENT = "gcm"; + public static final String NAMESPACE = "google:mobile:data"; + + public GcmPacketExtension(String json) { + super(json); + } + + @Override + public String getNamespace() { + return NAMESPACE; + } + + @Override + public String getElementName() { + return ELEMENT; + } + + /** + * Retrieve the GCM packet extension from the packet. + * + * @param packet + * @return the GCM packet extension or null. + */ + public static GcmPacketExtension from(Packet packet) { + return packet.getExtension(ELEMENT, NAMESPACE); + } +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/gcm/provider/GcmExtensionProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/gcm/provider/GcmExtensionProvider.java new file mode 100644 index 000000000..2367525bd --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/gcm/provider/GcmExtensionProvider.java @@ -0,0 +1,29 @@ +/** + * + * Copyright © 2014 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. + */ +package org.jivesoftware.smackx.gcm.provider; + +import org.jivesoftware.smackx.gcm.packet.GcmPacketExtension; +import org.jivesoftware.smackx.json.packet.AbstractJsonPacketExtension; +import org.jivesoftware.smackx.json.provider.AbstractJsonExtensionProvider; + +public class GcmExtensionProvider extends AbstractJsonExtensionProvider { + + @Override + public AbstractJsonPacketExtension from(String json) { + return new GcmPacketExtension(json); + } +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/json/packet/AbstractJsonPacketExtension.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/json/packet/AbstractJsonPacketExtension.java new file mode 100644 index 000000000..6cbccbfb0 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/json/packet/AbstractJsonPacketExtension.java @@ -0,0 +1,42 @@ +/** + * + * Copyright © 2014 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. + */ +package org.jivesoftware.smackx.json.packet; + +import org.jivesoftware.smack.packet.PacketExtension; +import org.jivesoftware.smack.util.XmlStringBuilder; + +public abstract class AbstractJsonPacketExtension implements PacketExtension { + + private final String json; + + protected AbstractJsonPacketExtension(String json) { + this.json = json; + } + + public final String getJson() { + return json; + } + + @Override + public final XmlStringBuilder toXML() { + XmlStringBuilder xml = new XmlStringBuilder(this); + xml.rightAngleBracket(); + xml.append(json); + xml.closeElement(this); + return xml; + } +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/json/packet/JsonPacketExtension.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/json/packet/JsonPacketExtension.java new file mode 100644 index 000000000..aeb7edf1a --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/json/packet/JsonPacketExtension.java @@ -0,0 +1,54 @@ +/** + * + * Copyright © 2014 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. + */ +package org.jivesoftware.smackx.json.packet; + +import org.jivesoftware.smack.packet.Packet; + +/** + * XMPP JSON Containers as defined in XEP-0335 + * + * @see XEP-0335: JSON Containers + */ +public class JsonPacketExtension extends AbstractJsonPacketExtension { + + public static final String ELEMENT = "json"; + public static final String NAMESPACE = "urn:xmpp:json:0"; + + public JsonPacketExtension(String json) { + super(json); + } + + @Override + public String getNamespace() { + return NAMESPACE; + } + + @Override + public String getElementName() { + return ELEMENT; + } + + /** + * Retrieve the JSON packet extension from the packet. + * + * @param packet + * @return the JSON packet extension or null. + */ + public static JsonPacketExtension from(Packet packet) { + return packet.getExtension(ELEMENT, NAMESPACE); + } +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/json/provider/AbstractJsonExtensionProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/json/provider/AbstractJsonExtensionProvider.java new file mode 100644 index 000000000..4e2c37d14 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/json/provider/AbstractJsonExtensionProvider.java @@ -0,0 +1,39 @@ +/** + * + * Copyright © 2014 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. + */ +package org.jivesoftware.smackx.json.provider; + +import java.io.IOException; + +import org.jivesoftware.smack.SmackException; +import org.jivesoftware.smack.provider.PacketExtensionProvider; +import org.jivesoftware.smack.util.PacketParserUtils; +import org.jivesoftware.smackx.json.packet.AbstractJsonPacketExtension; +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; + +public abstract class AbstractJsonExtensionProvider extends PacketExtensionProvider { + + @Override + public AbstractJsonPacketExtension parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, + IOException, SmackException { + String json = PacketParserUtils.parseElementText(parser); + parser.next(); + return from(json); + } + + public abstract AbstractJsonPacketExtension from(String json); +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/json/provider/JsonExtensionProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/json/provider/JsonExtensionProvider.java new file mode 100644 index 000000000..aced5235e --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/json/provider/JsonExtensionProvider.java @@ -0,0 +1,28 @@ +/** + * + * Copyright © 2014 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. + */ +package org.jivesoftware.smackx.json.provider; + +import org.jivesoftware.smackx.json.packet.AbstractJsonPacketExtension; +import org.jivesoftware.smackx.json.packet.JsonPacketExtension; + +public class JsonExtensionProvider extends AbstractJsonExtensionProvider { + + @Override + public AbstractJsonPacketExtension from(String json) { + return new JsonPacketExtension(json); + } +} diff --git a/smack-experimental/src/main/resources/org.jivesoftware.smackx/experimental.providers b/smack-experimental/src/main/resources/org.jivesoftware.smackx/experimental.providers index 4aff6fcf3..b78a3111c 100644 --- a/smack-experimental/src/main/resources/org.jivesoftware.smackx/experimental.providers +++ b/smack-experimental/src/main/resources/org.jivesoftware.smackx/experimental.providers @@ -38,4 +38,18 @@ org.jivesoftware.smackx.csi.provider.ClientStateIndicationFeatureProvider + + + json + urn:xmpp:json:0 + org.jivesoftware.smackx.json.provider.JsonExtensionProvider + + + + + gcm + + org.jivesoftware.smackx.gcm.provider.GcmExtensionProvider + +