1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-21 22:02:06 +01:00

Replace 'key' with QName

This commit is contained in:
Florian Schmaus 2019-05-08 21:10:39 +02:00
parent 7c6d1f4340
commit 49f4de0cdb
5 changed files with 54 additions and 53 deletions

View file

@ -65,6 +65,7 @@ import javax.net.ssl.X509TrustManager;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.PasswordCallback;
import javax.xml.namespace.QName;
import org.jivesoftware.smack.ConnectionConfiguration.DnssecMode;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
@ -234,7 +235,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
private XmlEnvironment incomingStreamXmlEnvironment;
final Map<String, NonzaCallback> nonzaCallbacks = new HashMap<>();
final Map<QName, NonzaCallback> nonzaCallbacks = new HashMap<>();
protected final Lock connectionLock = new ReentrantLock();
@ -1214,7 +1215,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
protected final void parseAndProcessNonza(XmlPullParser parser) throws IOException, XmlPullParserException, SmackParsingException {
final String element = parser.getName();
final String namespace = parser.getNamespace();
final String key = XmppStringUtils.generateKey(element, namespace);
final QName key = new QName(namespace, element);
NonzaProvider<? extends Nonza> nonzaProvider = ProviderManager.getNonzaProvider(key);
if (nonzaProvider == null) {

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2018 Florian Schmaus
* Copyright 2018-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.
@ -19,18 +19,18 @@ package org.jivesoftware.smack;
import java.util.HashMap;
import java.util.Map;
import javax.xml.namespace.QName;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPException.FailedNonzaException;
import org.jivesoftware.smack.packet.Nonza;
import org.jivesoftware.smack.util.XmppElementUtil;
import org.jxmpp.util.XmppStringUtils;
public class NonzaCallback {
protected final AbstractXMPPConnection connection;
protected final Map<String, GenericElementListener<? extends Nonza>> filterAndListeners;
protected final Map<QName, GenericElementListener<? extends Nonza>> filterAndListeners;
private NonzaCallback(Builder builder) {
this.connection = builder.connection;
@ -39,7 +39,7 @@ public class NonzaCallback {
}
void onNonzaReceived(Nonza nonza) {
String key = XmppStringUtils.generateKey(nonza.getElementName(), nonza.getNamespace());
QName key = nonza.getQName();
GenericElementListener<? extends Nonza> nonzaListener = filterAndListeners.get(key);
nonzaListener.processElement(nonza);
@ -47,8 +47,8 @@ public class NonzaCallback {
public void cancel() {
synchronized (connection.nonzaCallbacks) {
for (Map.Entry<String, GenericElementListener<? extends Nonza>> entry : filterAndListeners.entrySet()) {
String filterKey = entry.getKey();
for (Map.Entry<QName, GenericElementListener<? extends Nonza>> entry : filterAndListeners.entrySet()) {
QName filterKey = entry.getKey();
NonzaCallback installedCallback = connection.nonzaCallbacks.get(filterKey);
if (equals(installedCallback)) {
connection.nonzaCallbacks.remove(filterKey);
@ -63,7 +63,7 @@ public class NonzaCallback {
}
synchronized (connection.nonzaCallbacks) {
for (String key : filterAndListeners.keySet()) {
for (QName key : filterAndListeners.keySet()) {
connection.nonzaCallbacks.put(key, this);
}
}
@ -78,8 +78,8 @@ public class NonzaCallback {
Builder builder) {
super(builder);
final String successNonzaKey = XmppElementUtil.getKeyFor(successNonzaClass);
final String failedNonzaKey = XmppElementUtil.getKeyFor(failedNonzaClass);
final QName successNonzaKey = XmppElementUtil.getQNameFor(successNonzaClass);
final QName failedNonzaKey = XmppElementUtil.getQNameFor(failedNonzaClass);
final GenericElementListener<SN> successListener = new GenericElementListener<SN>(successNonzaClass) {
@Override
@ -139,14 +139,14 @@ public class NonzaCallback {
public static final class Builder {
private final AbstractXMPPConnection connection;
private Map<String, GenericElementListener<? extends Nonza>> filterAndListeners = new HashMap<>();
private Map<QName, GenericElementListener<? extends Nonza>> filterAndListeners = new HashMap<>();
Builder(AbstractXMPPConnection connection) {
this.connection = connection;
}
public <N extends Nonza> Builder listenFor(Class<? extends N> nonza, GenericElementListener<? extends N> nonzaListener) {
String key = XmppElementUtil.getKeyFor(nonza);
QName key = XmppElementUtil.getQNameFor(nonza);
filterAndListeners.put(key, nonzaListener);
return this;
}

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2018 Florian Schmaus
* Copyright 2018-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.
@ -16,6 +16,8 @@
*/
package org.jivesoftware.smack.packet;
import javax.xml.namespace.QName;
public interface FullyQualifiedElement extends NamedElement {
/**
@ -25,4 +27,9 @@ public interface FullyQualifiedElement extends NamedElement {
*/
String getNamespace();
default QName getQName() {
String namespaceURI = getNamespace();
String localPart = getElementName();
return new QName(namespaceURI, localPart);
}
}

View file

@ -22,6 +22,8 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.xml.namespace.QName;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.packet.IQ;
@ -29,8 +31,6 @@ import org.jivesoftware.smack.packet.Nonza;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmppElementUtil;
import org.jxmpp.util.XmppStringUtils;
/**
* Manages providers for parsing custom XML sub-documents of XMPP packets. Two types of
* providers exist:<ul>
@ -112,10 +112,10 @@ import org.jxmpp.util.XmppStringUtils;
*/
public final class ProviderManager {
private static final Map<String, ExtensionElementProvider<ExtensionElement>> extensionProviders = new ConcurrentHashMap<String, ExtensionElementProvider<ExtensionElement>>();
private static final Map<String, IQProvider<IQ>> iqProviders = new ConcurrentHashMap<String, IQProvider<IQ>>();
private static final Map<String, ExtensionElementProvider<ExtensionElement>> streamFeatureProviders = new ConcurrentHashMap<String, ExtensionElementProvider<ExtensionElement>>();
private static final Map<String, NonzaProvider<? extends Nonza>> nonzaProviders = new ConcurrentHashMap<>();
private static final Map<QName, ExtensionElementProvider<ExtensionElement>> extensionProviders = new ConcurrentHashMap<>();
private static final Map<QName, IQProvider<IQ>> iqProviders = new ConcurrentHashMap<>();
private static final Map<QName, ExtensionElementProvider<ExtensionElement>> streamFeatureProviders = new ConcurrentHashMap<>();
private static final Map<QName, NonzaProvider<? extends Nonza>> nonzaProviders = new ConcurrentHashMap<>();
static {
// Ensure that Smack is initialized by calling getVersion, so that user
@ -168,7 +168,7 @@ public final class ProviderManager {
* @return the IQ provider.
*/
public static IQProvider<IQ> getIQProvider(String elementName, String namespace) {
String key = getKey(elementName, namespace);
QName key = getQName(elementName, namespace);
return iqProviders.get(key);
}
@ -199,7 +199,7 @@ public final class ProviderManager {
Object provider) {
validate(elementName, namespace);
// First remove existing providers
String key = removeIQProvider(elementName, namespace);
QName key = removeIQProvider(elementName, namespace);
if (provider instanceof IQProvider) {
iqProviders.put(key, (IQProvider<IQ>) provider);
} else {
@ -214,10 +214,10 @@ public final class ProviderManager {
*
* @param elementName the XML element name.
* @param namespace the XML namespace.
* @return the key of the removed IQ Provider
* @return the QName of the removed provider
*/
public static String removeIQProvider(String elementName, String namespace) {
String key = getKey(elementName, namespace);
public static QName removeIQProvider(String elementName, String namespace) {
QName key = getQName(elementName, namespace);
iqProviders.remove(key);
return key;
}
@ -242,7 +242,7 @@ public final class ProviderManager {
* @return the extension provider.
*/
public static ExtensionElementProvider<ExtensionElement> getExtensionProvider(String elementName, String namespace) {
String key = getKey(elementName, namespace);
QName key = getQName(elementName, namespace);
return extensionProviders.get(key);
}
@ -260,7 +260,7 @@ public final class ProviderManager {
Object provider) {
validate(elementName, namespace);
// First remove existing providers
String key = removeExtensionProvider(elementName, namespace);
QName key = removeExtensionProvider(elementName, namespace);
if (provider instanceof ExtensionElementProvider) {
extensionProviders.put(key, (ExtensionElementProvider<ExtensionElement>) provider);
} else {
@ -275,10 +275,10 @@ public final class ProviderManager {
*
* @param elementName the XML element name.
* @param namespace the XML namespace.
* @return the key of the removed stanza extension provider
* @return the QName of the removed stanza extension provider
*/
public static String removeExtensionProvider(String elementName, String namespace) {
String key = getKey(elementName, namespace);
public static QName removeExtensionProvider(String elementName, String namespace) {
QName key = getQName(elementName, namespace);
extensionProviders.remove(key);
return key;
}
@ -297,48 +297,48 @@ public final class ProviderManager {
}
public static ExtensionElementProvider<ExtensionElement> getStreamFeatureProvider(String elementName, String namespace) {
String key = getKey(elementName, namespace);
QName key = getQName(elementName, namespace);
return streamFeatureProviders.get(key);
}
public static void addStreamFeatureProvider(String elementName, String namespace, ExtensionElementProvider<ExtensionElement> provider) {
validate(elementName, namespace);
String key = getKey(elementName, namespace);
QName key = getQName(elementName, namespace);
streamFeatureProviders.put(key, provider);
}
public static void removeStreamFeatureProvider(String elementName, String namespace) {
String key = getKey(elementName, namespace);
QName key = getQName(elementName, namespace);
streamFeatureProviders.remove(key);
}
public static NonzaProvider<? extends Nonza> getNonzaProvider(String elementName, String namespace) {
String key = getKey(elementName, namespace);
QName key = getQName(elementName, namespace);
return getNonzaProvider(key);
}
public static NonzaProvider<? extends Nonza> getNonzaProvider(String key) {
public static NonzaProvider<? extends Nonza> getNonzaProvider(QName key) {
return nonzaProviders.get(key);
}
public static void addNonzaProvider(NonzaProvider<? extends Nonza> nonzaProvider) {
Class<? extends Nonza> nonzaClass = nonzaProvider.getElementClass();
String key = XmppElementUtil.getKeyFor(nonzaClass);
QName key = XmppElementUtil.getQNameFor(nonzaClass);
nonzaProviders.put(key, nonzaProvider);
}
public static void removeNonzaProvider(Class<? extends Nonza> nonzaClass) {
String key = XmppElementUtil.getKeyFor(nonzaClass);
QName key = XmppElementUtil.getQNameFor(nonzaClass);
nonzaProviders.remove(key);
}
public static void removeNonzaProvider(String elementName, String namespace) {
String key = getKey(elementName, namespace);
QName key = getQName(elementName, namespace);
nonzaProviders.remove(key);
}
private static String getKey(String elementName, String namespace) {
return XmppStringUtils.generateKey(elementName, namespace);
private static QName getQName(String elementName, String namespace) {
return new QName(namespace, elementName);
}
private static void validate(String elementName, String namespace) {

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2018 Florian Schmaus
* Copyright 2018-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.
@ -16,13 +16,13 @@
*/
package org.jivesoftware.smack.util;
import org.jivesoftware.smack.packet.FullyQualifiedElement;
import javax.xml.namespace.QName;
import org.jxmpp.util.XmppStringUtils;
import org.jivesoftware.smack.packet.FullyQualifiedElement;
public class XmppElementUtil {
public static String getKeyFor(Class<? extends FullyQualifiedElement> fullyQualifiedElement) {
public static QName getQNameFor(Class<? extends FullyQualifiedElement> fullyQualifiedElement) {
String element, namespace;
try {
element = (String) fullyQualifiedElement.getField("ELEMENT").get(null);
@ -32,14 +32,7 @@ public class XmppElementUtil {
throw new IllegalArgumentException(e);
}
String key = XmppStringUtils.generateKey(element, namespace);
return key;
return new QName(namespace, element);
}
public static String getKeyFor(FullyQualifiedElement fullyQualifiedElement) {
String element = fullyQualifiedElement.getElementName();
String namespace = fullyQualifiedElement.getNamespace();
String key = XmppStringUtils.generateKey(element, namespace);
return key;
}
}