Merge pull request #244 from vanitasvitae/EmeImprovements

Add EME convenience methods
This commit is contained in:
Florian Schmaus 2018-07-03 09:52:39 +02:00 committed by GitHub
commit 8011ba96bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 46 additions and 0 deletions

View File

@ -17,6 +17,7 @@
package org.jivesoftware.smackx.eme.element;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jivesoftware.smack.packet.ExtensionElement;
@ -145,4 +146,49 @@ public class ExplicitMessageEncryptionElement implements ExtensionElement {
public static ExplicitMessageEncryptionElement from(Message message) {
return message.getExtension(ELEMENT, NAMESPACE);
}
/**
* Return true, if the {@code message} already contains an EME element with the specified {@code protocolNamespace}.
*
* @param message message
* @param protocolNamespace namespace
* @return true if message has EME element for that namespace, otherwise false
*/
public static boolean hasProtocol(Message message, String protocolNamespace) {
List<ExplicitMessageEncryptionElement> emeElements = message.getExtension(
ExplicitMessageEncryptionElement.ELEMENT,
ExplicitMessageEncryptionElement.NAMESPACE);
for (ExplicitMessageEncryptionElement e : emeElements) {
if (e.getEncryptionNamespace().equals(protocolNamespace)) {
return true;
}
}
return false;
}
/**
* Return true, if the {@code message} already contains an EME element with the specified protocol namespace.
*
* @param message message
* @param protocol protocol
* @return true if message has EME element for that namespace, otherwise false
*/
public static boolean hasProtocol(Message message, ExplicitMessageEncryptionProtocol protocol) {
return hasProtocol(message, protocol.namespace);
}
/**
* Add an EME element containing the specified {@code protocol} namespace to the message.
* In case there is already an element with that protocol, we do nothing.
*
* @param message message
* @param protocol encryption protocol
*/
public static void set(Message message, ExplicitMessageEncryptionProtocol protocol) {
if (!hasProtocol(message, protocol.namespace)) {
message.addExtension(new ExplicitMessageEncryptionElement(protocol));
}
}
}