1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 12:02:05 +01:00

Add support for XEP-0328: JID Prep

Fixes SMACK-878.
This commit is contained in:
Florian Schmaus 2019-09-08 18:29:46 +02:00
parent 09e7368313
commit 9b186f2541
8 changed files with 246 additions and 0 deletions

View file

@ -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. |

View file

@ -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<XMPPConnection, JidPrepManager> 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);
}
}

View file

@ -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;
}
}

View file

@ -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;

View file

@ -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 <a href="https://xmpp.org/extensions/xep-0328.html">XEP-0328: JID Prep</a>
*/
package org.jivesoftware.smackx.jid_prep;

View file

@ -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<JidPrepIq> {
@Override
public JidPrepIq parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException {
String jid = parser.nextText();
return new JidPrepIq(jid);
}
}

View file

@ -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;

View file

@ -324,4 +324,11 @@
<className>org.jivesoftware.smackx.dox.provider.DnsIqProvider</className>
</iqProvider>
<!-- XEP-0328: JID Prep -->
<iqProvider>
<elementName>jid</elementName>
<namespace>urn:xmpp:jidprep:0</namespace>
<className>org.jivesoftware.smackx.jid_prep.provider.JidPrepIqProvider</className>
</iqProvider>
</smackProviders>