mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-12-09 12:31:08 +01:00
Introduce StanzaBuilder
As first step to immutable Stanza types.
This commit is contained in:
parent
926c5892ad
commit
5db6191110
134 changed files with 2576 additions and 764 deletions
|
|
@ -88,10 +88,11 @@ public final class DnsOverXmppManager extends Manager {
|
|||
try {
|
||||
response = resolver.resolve(query);
|
||||
} catch (IOException exception) {
|
||||
StanzaError.Builder errorBuilder = StanzaError.getBuilder()
|
||||
StanzaError errorBuilder = StanzaError.getBuilder()
|
||||
.setType(Type.CANCEL)
|
||||
.setCondition(Condition.internal_server_error)
|
||||
.setDescriptiveEnText("Exception while resolving your DNS query", exception)
|
||||
.build()
|
||||
;
|
||||
|
||||
IQ errorResponse = IQ.createErrorResponse(iqRequest, errorBuilder);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2017 Florian Schmaus
|
||||
* Copyright 2017-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.
|
||||
|
|
@ -20,8 +20,12 @@ import java.util.HashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
|
||||
import org.jivesoftware.smack.packet.ExtensionElement;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.packet.MessageBuilder;
|
||||
import org.jivesoftware.smack.packet.MessageView;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
|
||||
|
|
@ -33,6 +37,8 @@ public class ExplicitMessageEncryptionElement implements ExtensionElement {
|
|||
|
||||
public static final String NAMESPACE = "urn:xmpp:eme:0";
|
||||
|
||||
public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
|
||||
|
||||
public enum ExplicitMessageEncryptionProtocol {
|
||||
|
||||
/**
|
||||
|
|
@ -154,14 +160,12 @@ public class ExplicitMessageEncryptionElement implements ExtensionElement {
|
|||
* @param protocolNamespace namespace
|
||||
* @return true if message has EME element for that namespace, otherwise false
|
||||
*/
|
||||
public static boolean hasProtocol(Message message, String protocolNamespace) {
|
||||
List<ExtensionElement> extensionElements = message.getExtensions(
|
||||
ExplicitMessageEncryptionElement.ELEMENT,
|
||||
ExplicitMessageEncryptionElement.NAMESPACE);
|
||||
public static boolean hasProtocol(MessageView message, String protocolNamespace) {
|
||||
List<ExplicitMessageEncryptionElement> emeElements = message
|
||||
.getExtensions(ExplicitMessageEncryptionElement.class);
|
||||
|
||||
for (ExtensionElement extensionElement : extensionElements) {
|
||||
ExplicitMessageEncryptionElement e = (ExplicitMessageEncryptionElement) extensionElement;
|
||||
if (e.getEncryptionNamespace().equals(protocolNamespace)) {
|
||||
for (ExplicitMessageEncryptionElement emeElement : emeElements) {
|
||||
if (emeElement.getEncryptionNamespace().equals(protocolNamespace)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -176,7 +180,7 @@ public class ExplicitMessageEncryptionElement implements ExtensionElement {
|
|||
* @param protocol protocol
|
||||
* @return true if message has EME element for that namespace, otherwise false
|
||||
*/
|
||||
public static boolean hasProtocol(Message message, ExplicitMessageEncryptionProtocol protocol) {
|
||||
public static boolean hasProtocol(MessageView message, ExplicitMessageEncryptionProtocol protocol) {
|
||||
return hasProtocol(message, protocol.namespace);
|
||||
}
|
||||
|
||||
|
|
@ -184,10 +188,10 @@ public class ExplicitMessageEncryptionElement implements ExtensionElement {
|
|||
* 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 message a message builder.
|
||||
* @param protocol encryption protocol
|
||||
*/
|
||||
public static void set(Message message, ExplicitMessageEncryptionProtocol protocol) {
|
||||
public static void set(MessageBuilder message, ExplicitMessageEncryptionProtocol protocol) {
|
||||
if (!hasProtocol(message, protocol.namespace)) {
|
||||
message.addExtension(new ExplicitMessageEncryptionElement(protocol));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2017 Florian Schmaus
|
||||
* Copyright 2017-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,7 +16,10 @@
|
|||
*/
|
||||
package org.jivesoftware.smackx.hints.element;
|
||||
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import javax.xml.namespace.QName;
|
||||
|
||||
import org.jivesoftware.smack.packet.MessageBuilder;
|
||||
import org.jivesoftware.smack.packet.MessageView;
|
||||
|
||||
/**
|
||||
* A "store" hint. Messages with this hint should be stored in permanent stores or archives.
|
||||
|
|
@ -29,6 +32,8 @@ public final class StoreHint extends MessageProcessingHint {
|
|||
|
||||
public static final String ELEMENT = "store";
|
||||
|
||||
public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
|
||||
|
||||
private StoreHint() {
|
||||
}
|
||||
|
||||
|
|
@ -47,15 +52,15 @@ public final class StoreHint extends MessageProcessingHint {
|
|||
return MessageProcessingHintType.store;
|
||||
}
|
||||
|
||||
public static StoreHint from(Message message) {
|
||||
return message.getExtension(ELEMENT, NAMESPACE);
|
||||
public static StoreHint from(MessageView message) {
|
||||
return message.getExtension(QNAME);
|
||||
}
|
||||
|
||||
public static boolean hasHint(Message message) {
|
||||
public static boolean hasHint(MessageView message) {
|
||||
return from(message) != null;
|
||||
}
|
||||
|
||||
public static void set(Message message) {
|
||||
public static void set(MessageBuilder message) {
|
||||
message.overrideExtension(INSTANCE);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright © 2016 Florian Schmaus
|
||||
* Copyright © 2016-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.
|
||||
|
|
@ -17,19 +17,25 @@
|
|||
package org.jivesoftware.smackx.iot.control.element;
|
||||
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.IqBuilder;
|
||||
import org.jivesoftware.smack.packet.StanzaBuilder;
|
||||
|
||||
public class IoTSetResponse extends IQ {
|
||||
|
||||
public static final String ELEMENT = "setResponse";
|
||||
public static final String NAMESPACE = Constants.IOT_CONTROL_NAMESPACE;
|
||||
|
||||
public IoTSetResponse(IqBuilder iqBuilder) {
|
||||
super(iqBuilder, ELEMENT, NAMESPACE);
|
||||
}
|
||||
|
||||
// TODO: Deprecate when stanza build is ready.
|
||||
public IoTSetResponse() {
|
||||
super(ELEMENT, NAMESPACE);
|
||||
}
|
||||
|
||||
public IoTSetResponse(IoTSetRequest iotSetRequest) {
|
||||
this();
|
||||
initializeAsResultFor(iotSetRequest);
|
||||
this(StanzaBuilder.buildIqResultFor(iotSetRequest));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -125,10 +125,14 @@ public final class IoTDataManager extends IoTManager {
|
|||
@Override
|
||||
public void momentaryReadOut(List<? extends IoTDataField> results) {
|
||||
IoTFieldsExtension iotFieldsExtension = IoTFieldsExtension.buildFor(dataRequest.getSequenceNr(), true, thing.getNodeInfo(), results);
|
||||
Message message = new Message(dataRequest.getFrom());
|
||||
message.addExtension(iotFieldsExtension);
|
||||
|
||||
XMPPConnection connection = connection();
|
||||
Message message = connection.getStanzaFactory().buildMessageStanza()
|
||||
.to(dataRequest.getFrom())
|
||||
.addExtension(iotFieldsExtension)
|
||||
.build();
|
||||
try {
|
||||
connection().sendStanza(message);
|
||||
connection.sendStanza(message);
|
||||
}
|
||||
catch (NotConnectedException | InterruptedException e) {
|
||||
LOGGER.log(Level.SEVERE, "Could not send read-out response " + message, e);
|
||||
|
|
|
|||
|
|
@ -140,8 +140,10 @@ public final class IoTProvisioningManager extends Manager {
|
|||
+ " is already not subscribed to our presence.");
|
||||
return;
|
||||
}
|
||||
Presence unsubscribed = new Presence(Presence.Type.unsubscribed);
|
||||
unsubscribed.setTo(unfriendJid);
|
||||
Presence unsubscribed = connection.getStanzaFactory().buildPresenceStanza()
|
||||
.ofType(Presence.Type.unsubscribed)
|
||||
.to(unfriendJid)
|
||||
.build();
|
||||
connection.sendStanza(unsubscribed);
|
||||
}
|
||||
}, UNFRIEND_MESSAGE);
|
||||
|
|
@ -162,7 +164,10 @@ public final class IoTProvisioningManager extends Manager {
|
|||
// friendship requests.
|
||||
final XMPPConnection connection = connection();
|
||||
Friend friendNotification = new Friend(connection.getUser().asBareJid());
|
||||
Message notificationMessage = new Message(friendJid, friendNotification);
|
||||
Message notificationMessage = connection.getStanzaFactory().buildMessageStanza()
|
||||
.to(friendJid)
|
||||
.addExtension(friendNotification)
|
||||
.build();
|
||||
connection.sendStanza(notificationMessage);
|
||||
} else {
|
||||
// Check is the message was send from a thing we previously
|
||||
|
|
@ -359,8 +364,11 @@ public final class IoTProvisioningManager extends Manager {
|
|||
}
|
||||
|
||||
public void sendFriendshipRequest(BareJid bareJid) throws NotConnectedException, InterruptedException {
|
||||
Presence presence = new Presence(Presence.Type.subscribe);
|
||||
presence.setTo(bareJid);
|
||||
XMPPConnection connection = connection();
|
||||
Presence presence = connection.getStanzaFactory().buildPresenceStanza()
|
||||
.ofType(Presence.Type.subscribe)
|
||||
.to(bareJid)
|
||||
.build();
|
||||
|
||||
friendshipRequestedCache.put(bareJid, null);
|
||||
|
||||
|
|
@ -379,9 +387,12 @@ public final class IoTProvisioningManager extends Manager {
|
|||
|
||||
public void unfriend(Jid friend) throws NotConnectedException, InterruptedException {
|
||||
if (isMyFriend(friend)) {
|
||||
Presence presence = new Presence(Presence.Type.unsubscribed);
|
||||
presence.setTo(friend);
|
||||
connection().sendStanza(presence);
|
||||
XMPPConnection connection = connection();
|
||||
Presence presence = connection.getStanzaFactory().buildPresenceStanza()
|
||||
.ofType(Presence.Type.unsubscribed)
|
||||
.to(friend)
|
||||
.build();
|
||||
connection.sendStanza(presence);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -35,8 +35,8 @@ import org.jivesoftware.smack.filter.MessageTypeFilter;
|
|||
import org.jivesoftware.smack.filter.StanzaFilter;
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.packet.MessageBuilder;
|
||||
import org.jivesoftware.smack.packet.Stanza;
|
||||
|
||||
import org.jivesoftware.smackx.muclight.element.MUCLightAffiliationsIQ;
|
||||
import org.jivesoftware.smackx.muclight.element.MUCLightChangeAffiliationsIQ;
|
||||
import org.jivesoftware.smackx.muclight.element.MUCLightConfigurationIQ;
|
||||
|
|
@ -126,9 +126,9 @@ public class MultiUserChatLight {
|
|||
* @throws InterruptedException if the calling thread was interrupted.
|
||||
*/
|
||||
public void sendMessage(String text) throws NotConnectedException, InterruptedException {
|
||||
Message message = createMessage();
|
||||
MessageBuilder message = buildMessage();
|
||||
message.setBody(text);
|
||||
connection.sendStanza(message);
|
||||
connection.sendStanza(message.build());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -157,9 +157,28 @@ public class MultiUserChatLight {
|
|||
* Creates a new Message to send to the chat room.
|
||||
*
|
||||
* @return a new Message addressed to the chat room.
|
||||
* @deprecated use {@link #buildMessage()} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
// TODO: Remove when stanza builder is ready.
|
||||
public Message createMessage() {
|
||||
return new Message(room, Message.Type.groupchat);
|
||||
return connection.getStanzaFactory().buildMessageStanza()
|
||||
.ofType(Message.Type.groupchat)
|
||||
.to(room)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new message builder for messages send to this MUC room.
|
||||
*
|
||||
* @return a new message builder.
|
||||
*/
|
||||
public MessageBuilder buildMessage() {
|
||||
return connection.getStanzaFactory()
|
||||
.buildMessageStanza()
|
||||
.ofType(Message.Type.groupchat)
|
||||
.to(room)
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -169,10 +188,23 @@ public class MultiUserChatLight {
|
|||
* the message.
|
||||
* @throws NotConnectedException if the XMPP connection is not connected.
|
||||
* @throws InterruptedException if the calling thread was interrupted.
|
||||
* @deprecated use {@link #sendMessage(MessageBuilder)} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
// TODO: Remove in Smack 4.5.
|
||||
public void sendMessage(Message message) throws NotConnectedException, InterruptedException {
|
||||
message.setTo(room);
|
||||
message.setType(Message.Type.groupchat);
|
||||
sendMessage(message.asBuilder());
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a Message to the chat room.
|
||||
*
|
||||
* @param messageBuilder the message.
|
||||
* @throws NotConnectedException if the XMPP connection is not connected.
|
||||
* @throws InterruptedException if the calling thread was interrupted.
|
||||
*/
|
||||
public void sendMessage(MessageBuilder messageBuilder) throws NotConnectedException, InterruptedException {
|
||||
Message message = messageBuilder.to(room).ofType(Message.Type.groupchat).build();
|
||||
connection.sendStanza(message);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -50,7 +50,9 @@ public class RemoteDisablingProvider extends ExtensionElementProvider<RemoteDisa
|
|||
|
||||
String affiliation = parser.getAttributeValue("", "affiliation");
|
||||
if (affiliation == null || !affiliation.equals("none")) {
|
||||
return null;
|
||||
// TODO: Is this correct? We previously returned null here, but was certainly wrong, as
|
||||
// providers should always return an element or throw.
|
||||
throw new IOException("Invalid affiliation: " + affiliation);
|
||||
}
|
||||
}
|
||||
} else if (eventType == XmlPullParser.Event.END_ELEMENT) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue