CarbonExtension.java

  1. /**
  2.  *
  3.  * Copyright 2013-2014 Georg Lukas
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.carbons.packet;

  18. import org.jivesoftware.smack.packet.Message;
  19. import org.jivesoftware.smack.packet.ExtensionElement;
  20. import org.jivesoftware.smack.util.XmlStringBuilder;
  21. import org.jivesoftware.smackx.forward.packet.Forwarded;

  22. /**
  23.  * Packet extension for XEP-0280: Message Carbons. The extension
  24.  * <a href="http://xmpp.org/extensions/xep-0280.html">XEP-0280</a> is
  25.  * meant to synchronize a message flow to multiple presences of a user.
  26.  *
  27.  * <p>
  28.  * It accomplishes this by wrapping a {@link Forwarded} packet in a <b>sent</b>
  29.  * or <b>received</b> element
  30.  *
  31.  * @author Georg Lukas
  32.  */
  33. public class CarbonExtension implements ExtensionElement {
  34.     public static final String NAMESPACE = Carbon.NAMESPACE;

  35.     private final Direction dir;
  36.     private final Forwarded fwd;

  37.     /**
  38.      * Construct a Carbon message extension.
  39.      *
  40.      * @param dir Determines if the carbon is being sent/received
  41.      * @param fwd The forwarded message.
  42.      */
  43.     public CarbonExtension(Direction dir, Forwarded fwd) {
  44.         this.dir = dir;
  45.         this.fwd = fwd;
  46.     }

  47.     /**
  48.      * Get the direction (sent or received) of the carbon.
  49.      *
  50.      * @return the {@link Direction} of the carbon.
  51.      */
  52.     public Direction getDirection() {
  53.         return dir;
  54.     }

  55.     /**
  56.      * Get the forwarded packet.
  57.      *
  58.      * @return the {@link Forwarded} message contained in this Carbon.
  59.      */
  60.     public Forwarded getForwarded() {
  61.         return fwd;
  62.     }

  63.     @Override
  64.     public String getElementName() {
  65.         return dir.name();
  66.     }

  67.     @Override
  68.     public String getNamespace() {
  69.         return NAMESPACE;
  70.     }

  71.     @Override
  72.     public XmlStringBuilder toXML() {
  73.         XmlStringBuilder xml = new XmlStringBuilder(this);
  74.         xml.rightAngleBracket();
  75.         xml.append(fwd.toXML());
  76.         xml.closeElement(this);
  77.         return xml;
  78.     }

  79.     /**
  80.      * Obtain a Carbon from a message, if available.
  81.      * <p>
  82.      * Only {@link Message} instances can contain a Carbon extensions.
  83.      * </p>
  84.      *
  85.      * @param msg Message object to check for carbons
  86.      *
  87.      * @return a Carbon if available, null otherwise.
  88.      * @deprecated use {@link #from(Message)} instead
  89.      */
  90.     @Deprecated
  91.     public static CarbonExtension getFrom(Message msg) {
  92.         return from(msg);
  93.     }

  94.     /**
  95.      * Obtain a Carbon from a message, if available.
  96.      * <p>
  97.      * Only {@link Message} instances can contain a Carbon extensions.
  98.      * </p>
  99.      *
  100.      * @param msg Message object to check for carbons
  101.      *
  102.      * @return a Carbon if available, null otherwise.
  103.      */
  104.     public static CarbonExtension from(Message msg) {
  105.         CarbonExtension cc = msg.getExtension(Direction.received.name(), NAMESPACE);
  106.         if (cc == null)
  107.             cc = msg.getExtension(Direction.sent.name(), NAMESPACE);
  108.         return cc;
  109.     }

  110.     /**
  111.      * Defines the direction of a {@link CarbonExtension} message.
  112.      */
  113.     public static enum Direction {
  114.         received,
  115.         sent
  116.     }

  117.     /**
  118.      * Packet extension indicating that a message may not be carbon-copied.  Adding this
  119.      * extension to any message will disallow that message from being copied.
  120.      */
  121.     public static class Private implements ExtensionElement {
  122.         public static final Private INSTANCE = new Private();
  123.         public static final String ELEMENT = "private";

  124.         private Private() {
  125.         }

  126.         @Override
  127.         public String getElementName() {
  128.             return ELEMENT;
  129.         }

  130.         @Override
  131.         public String getNamespace() {
  132.             return NAMESPACE;
  133.         }

  134.         @Override
  135.         public String toXML() {
  136.             return "<" + ELEMENT + " xmlns='" + NAMESPACE + "'/>";
  137.         }

  138.         /**
  139.          * Marks a message "private", so that it will not be carbon-copied, by adding private packet
  140.          * extension to the message.
  141.          *
  142.          * @param message the message to add the private extension to
  143.          */
  144.         public static void addTo(Message message) {
  145.             message.addExtension(INSTANCE);
  146.         }
  147.     }
  148. }