From bd333bd1ac34563e26eca13b988481a9884f3489 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 13 Aug 2014 12:10:18 +0200 Subject: [PATCH] Add Enable/Disable Carbon IQs --- .../smackx/carbons/CarbonManager.java | 20 ++++--- .../smackx/carbons/packet/Carbon.java | 52 +++++++++++++++++++ .../carbons/packet/CarbonExtension.java | 24 +++++++-- 3 files changed, 83 insertions(+), 13 deletions(-) create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/packet/Carbon.java diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/CarbonManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/CarbonManager.java index b7ccce08e..b56a64b36 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/CarbonManager.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/CarbonManager.java @@ -34,6 +34,8 @@ import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smackx.carbons.packet.CarbonExtension; +import org.jivesoftware.smackx.carbons.packet.Carbon; +import org.jivesoftware.smackx.carbons.packet.CarbonExtension.Private; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; /** @@ -85,13 +87,13 @@ public class CarbonManager extends Manager { } private static IQ carbonsEnabledIQ(final boolean new_state) { - IQ setIQ = new IQ() { - public String getChildElementXML() { - return "<" + (new_state? "enable" : "disable") + " xmlns='" + CarbonExtension.NAMESPACE + "'/>"; - } - }; - setIQ.setType(IQ.Type.set); - return setIQ; + IQ request; + if (new_state) { + request = new Carbon.Enable(); + } else { + request = new Carbon.Disable(); + } + return request; } /** @@ -187,8 +189,10 @@ public class CarbonManager extends Manager { * Mark a message as "private", so it will not be carbon-copied. * * @param msg Message object to mark private + * @deprecated use {@link Private#addTo(Message)} */ + @Deprecated public static void disableCarbons(Message msg) { - msg.addExtension(new CarbonExtension.Private()); + msg.addExtension(Private.INSTANCE); } } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/packet/Carbon.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/packet/Carbon.java new file mode 100644 index 000000000..9dbd21990 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/packet/Carbon.java @@ -0,0 +1,52 @@ +/** + * + * Copyright © 2014 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.carbons.packet; + +import org.jivesoftware.smack.packet.IQ; + +/** + * Carbon IQs + */ +public class Carbon { + public static final String NAMESPACE = "urn:xmpp:carbons:2"; + + public static class Enable extends IQ { + public static final String ELEMENT = "enable"; + + public Enable() { + setType(Type.set); + } + + @Override + public String getChildElementXML() { + return "<" + ELEMENT + " xmlns='" + NAMESPACE + "'/>"; + } + } + + public static class Disable extends IQ { + public static final String ELEMENT = "disable"; + + public Disable() { + setType(Type.set); + } + + @Override + public String getChildElementXML() { + return "<" + ELEMENT + " xmlns='" + NAMESPACE + "'/>"; + } + } +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/packet/CarbonExtension.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/packet/CarbonExtension.java index 2a5a9daea..88248e074 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/packet/CarbonExtension.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/packet/CarbonExtension.java @@ -33,7 +33,7 @@ import org.jivesoftware.smackx.forward.Forwarded; * @author Georg Lukas */ public class CarbonExtension implements PacketExtension { - public static final String NAMESPACE = "urn:xmpp:carbons:2"; + public static final String NAMESPACE = Carbon.NAMESPACE; private final Direction dir; private final Forwarded fwd; @@ -97,9 +97,9 @@ public class CarbonExtension implements PacketExtension { * @return a Carbon if available, null otherwise. */ public static CarbonExtension getFrom(Message msg) { - CarbonExtension cc = msg.getExtension(Direction.received.name(), CarbonExtension.NAMESPACE); + CarbonExtension cc = msg.getExtension(Direction.received.name(), NAMESPACE); if (cc == null) - cc = msg.getExtension(Direction.sent.name(), CarbonExtension.NAMESPACE); + cc = msg.getExtension(Direction.sent.name(), NAMESPACE); return cc; } @@ -116,8 +116,12 @@ public class CarbonExtension implements PacketExtension { * extension to any message will disallow that message from being copied. */ public static class Private implements PacketExtension { + public static final Private INSTANCE = new Private(); public static final String ELEMENT = "private"; + private Private() { + } + @Override public String getElementName() { return ELEMENT; @@ -125,12 +129,22 @@ public class CarbonExtension implements PacketExtension { @Override public String getNamespace() { - return CarbonExtension.NAMESPACE; + return NAMESPACE; } @Override public String toXML() { - return "<" + ELEMENT + " xmlns='" + CarbonExtension.NAMESPACE + "'/>"; + return "<" + ELEMENT + " xmlns='" + NAMESPACE + "'/>"; + } + + /** + * Marks a message "private", so that it will not be carbon-copied, by adding private packet + * extension to the message. + * + * @param message the message to add the private extension to + */ + public static void addTo(Message message) { + message.addExtension(INSTANCE); } } }