From 9b186f254144025a7c113eb95beaa326f0a23567 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 8 Sep 2019 18:29:46 +0200 Subject: [PATCH] Add support for XEP-0328: JID Prep Fixes SMACK-878. --- documentation/extensions/index.md | 1 + .../smackx/jid_prep/JidPrepManager.java | 91 +++++++++++++++++++ .../smackx/jid_prep/element/JidPrepIq.java | 44 +++++++++ .../smackx/jid_prep/element/package-info.java | 21 +++++ .../smackx/jid_prep/package-info.java | 23 +++++ .../jid_prep/provider/JidPrepIqProvider.java | 38 ++++++++ .../jid_prep/provider/package-info.java | 21 +++++ .../experimental.providers | 7 ++ 8 files changed, 246 insertions(+) create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/JidPrepManager.java create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/element/JidPrepIq.java create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/element/package-info.java create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/package-info.java create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/provider/JidPrepIqProvider.java create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/provider/package-info.java diff --git a/documentation/extensions/index.md b/documentation/extensions/index.md index 2adcb5170..b5c279f7d 100644 --- a/documentation/extensions/index.md +++ b/documentation/extensions/index.md @@ -90,6 +90,7 @@ Experimental Smack Extensions and currently supported XEPs of smack-experimental | [Internet of Things - Sensor Data](iot.md) | [XEP-0323](https://xmpp.org/extensions/xep-0323.html) | n/a | Sensor data interchange over XMPP. | | [Internet of Things - Provisioning](iot.md) | [XEP-0324](https://xmpp.org/extensions/xep-0324.html) | n/a | Provisioning, access rights and user privileges for the Internet of Things. | | [Internet of Things - Control](iot.md) | [XEP-0325](https://xmpp.org/extensions/xep-0325.html) | n/a | Describes how to control devices or actuators in an XMPP-based sensor network. | +| Jid Prep | [XEP-0328](https://xmpp.org/extensions/xep-0328.html) | 0.1 | Describes a way for an XMPP client to request an XMPP server to prep and normalize a given JID. | | [HTTP over XMPP transport](hoxt.md) | [XEP-0332](https://xmpp.org/extensions/xep-0332.html) | n/a | Allows to transport HTTP communication over XMPP peer-to-peer networks. | | Chat Markers | [XEP-0333](https://xmpp.org/extensions/xep-0333.html) | n/a | A solution of marking the last received, displayed and acknowledged message in a chat. | | Message Processing Hints | [XEP-0334](https://xmpp.org/extensions/xep-0334.html) | n/a | Hints to entities routing or receiving a message. | diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/JidPrepManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/JidPrepManager.java new file mode 100644 index 000000000..b5ebc8ce7 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/JidPrepManager.java @@ -0,0 +1,91 @@ +/** + * + * Copyright 2019 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.jid_prep; + +import java.util.Map; +import java.util.WeakHashMap; + +import org.jivesoftware.smack.Manager; +import org.jivesoftware.smack.SmackException.NoResponseException; +import org.jivesoftware.smack.SmackException.NotConnectedException; +import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smack.XMPPException.XMPPErrorException; +import org.jivesoftware.smack.packet.StanzaError; + +import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; +import org.jivesoftware.smackx.jid_prep.element.JidPrepIq; + +import org.jxmpp.jid.DomainBareJid; +import org.jxmpp.jid.Jid; + +public class JidPrepManager extends Manager { + + public static final String NAMESPACE = JidPrepIq.NAMESPACE; + + private static final Map INSTANCES = new WeakHashMap<>(); + + private final ServiceDiscoveryManager serviceDiscoveryManager; + + public static synchronized JidPrepManager getInstanceFor(XMPPConnection connection) { + JidPrepManager manager = INSTANCES.get(connection); + if (manager == null) { + manager = new JidPrepManager(connection); + INSTANCES.put(connection, manager); + } + return manager; + } + + public JidPrepManager(XMPPConnection connection) { + super(connection); + + serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection); + } + + public String requestJidPrep(String jidToBePrepped) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + DomainBareJid serviceAddress = serviceDiscoveryManager.findService(NAMESPACE, true); + return requestJidPrep(serviceAddress, jidToBePrepped); + } + + public String requestJidPrep(Jid jidPrepService, String jidToBePrepped) + throws NoResponseException, NotConnectedException, InterruptedException, XMPPErrorException { + JidPrepIq jidPrepRequest = new JidPrepIq(jidToBePrepped); + jidPrepRequest.setTo(jidPrepService); + + JidPrepIq jidPrepResponse; + try { + jidPrepResponse = connection().sendIqRequestAndWaitForResponse(jidPrepRequest); + } catch (XMPPErrorException e) { + StanzaError stanzaError = e.getStanzaError(); + if (stanzaError.getCondition() == StanzaError.Condition.jid_malformed) { + // jid-malformed is, sadly, returned if the jid can not be normalized. This means we can not distinguish + // if the error is returned because e.g. the IQ's 'to' address was malformed (c.f. RFC 6120 ยง 8.3.3.8) + // or if the JID to prep was malformed. Assume the later is the case and return 'null'. + return null; + } + + throw e; + } + + return jidPrepResponse.getJid(); + } + + public boolean isSupported(Jid jid) + throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { + return serviceDiscoveryManager.supportsFeature(jid, NAMESPACE); + } +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/element/JidPrepIq.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/element/JidPrepIq.java new file mode 100644 index 000000000..2e9e6c4b2 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/element/JidPrepIq.java @@ -0,0 +1,44 @@ +/** + * + * Copyright 2019 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.jid_prep.element; + +import org.jivesoftware.smack.packet.IQ; + +public class JidPrepIq extends IQ { + + public static final String ELEMENT = "jid"; + public static final String NAMESPACE = "urn:xmpp:jidprep:0"; + + private final String jid; + + public JidPrepIq(String jid) { + super(ELEMENT, NAMESPACE); + this.jid = jid; + } + + public String getJid() { + return jid; + } + + @Override + protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { + xml.rightAngleBracket(); + xml.escape(jid); + + return xml; + } +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/element/package-info.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/element/package-info.java new file mode 100644 index 000000000..9606c202d --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/element/package-info.java @@ -0,0 +1,21 @@ +/** + * + * Copyright 2019 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. + */ + +/** + * XML elements for XEP-0328: JID Prep. + */ +package org.jivesoftware.smackx.jid_prep.element; diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/package-info.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/package-info.java new file mode 100644 index 000000000..47b1cae3b --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/package-info.java @@ -0,0 +1,23 @@ +/** + * + * Copyright 2019 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. + */ + +/** + * Smack's API for XEP-0328: JID Prep. + * + * @see XEP-0328: JID Prep + */ +package org.jivesoftware.smackx.jid_prep; diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/provider/JidPrepIqProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/provider/JidPrepIqProvider.java new file mode 100644 index 000000000..82f81f1a9 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/provider/JidPrepIqProvider.java @@ -0,0 +1,38 @@ +/** + * + * Copyright 2019 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.jid_prep.provider; + +import java.io.IOException; + +import org.jivesoftware.smack.packet.XmlEnvironment; +import org.jivesoftware.smack.parsing.SmackParsingException; +import org.jivesoftware.smack.provider.IQProvider; +import org.jivesoftware.smack.xml.XmlPullParser; +import org.jivesoftware.smack.xml.XmlPullParserException; +import org.jivesoftware.smackx.jid_prep.element.JidPrepIq; + +public class JidPrepIqProvider extends IQProvider { + + @Override + public JidPrepIq parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) + throws XmlPullParserException, IOException, SmackParsingException { + String jid = parser.nextText(); + + return new JidPrepIq(jid); + } + +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/provider/package-info.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/provider/package-info.java new file mode 100644 index 000000000..7399e97f2 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jid_prep/provider/package-info.java @@ -0,0 +1,21 @@ +/** + * + * Copyright 2019 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. + */ + +/** + * Providers for XEP-0328: JID Prep. + */ +package org.jivesoftware.smackx.jid_prep.provider; diff --git a/smack-experimental/src/main/resources/org.jivesoftware.smack.experimental/experimental.providers b/smack-experimental/src/main/resources/org.jivesoftware.smack.experimental/experimental.providers index 974c149f2..9f42e0f57 100644 --- a/smack-experimental/src/main/resources/org.jivesoftware.smack.experimental/experimental.providers +++ b/smack-experimental/src/main/resources/org.jivesoftware.smack.experimental/experimental.providers @@ -324,4 +324,11 @@ org.jivesoftware.smackx.dox.provider.DnsIqProvider + + + jid + urn:xmpp:jidprep:0 + org.jivesoftware.smackx.jid_prep.provider.JidPrepIqProvider + +