Forwarded.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.forward.packet;

  18. import org.jivesoftware.smack.packet.Stanza;
  19. import org.jivesoftware.smack.packet.ExtensionElement;
  20. import org.jivesoftware.smack.util.XmlStringBuilder;
  21. import org.jivesoftware.smackx.delay.packet.DelayInformation;

  22. /**
  23.  * Packet extension for >XEP-0297: Stanza Forwarding.
  24.  *
  25.  * @author Georg Lukas
  26.  * @see <a href="http://xmpp.org/extensions/xep-0297.html">XEP-0297: Stanza Forwarding</a>
  27.  */
  28. public class Forwarded implements ExtensionElement {
  29.     public static final String NAMESPACE = "urn:xmpp:forward:0";
  30.     public static final String ELEMENT = "forwarded";

  31.     private final DelayInformation delay;
  32.     private final Stanza forwardedPacket;

  33.     /**
  34.      * Creates a new Forwarded packet extension.
  35.      *
  36.      * @param delay an optional {@link DelayInformation} timestamp of the packet.
  37.      * @param fwdPacket the packet that is forwarded (required).
  38.      */
  39.     public Forwarded(DelayInformation delay, Stanza fwdPacket) {
  40.         this.delay = delay;
  41.         this.forwardedPacket = fwdPacket;
  42.     }

  43.     /**
  44.      * Creates a new Forwarded packet extension.
  45.      *
  46.      * @param fwdPacket the packet that is forwarded (required).
  47.      */
  48.     public Forwarded(Stanza fwdPacket) {
  49.         this(null, fwdPacket);
  50.     }

  51.     @Override
  52.     public String getElementName() {
  53.         return ELEMENT;
  54.     }

  55.     @Override
  56.     public String getNamespace() {
  57.         return NAMESPACE;
  58.     }

  59.     @Override
  60.     public XmlStringBuilder toXML() {
  61.         XmlStringBuilder xml = new XmlStringBuilder(this);
  62.         xml.rightAngleBracket();
  63.         xml.optElement(getDelayInformation());
  64.         xml.append(forwardedPacket.toXML());
  65.         xml.closeElement(this);
  66.         return xml;
  67.     }

  68.     /**
  69.      * get the packet forwarded by this stanza.
  70.      *
  71.      * @return the {@link Stanza} instance (typically a message) that was forwarded.
  72.      */
  73.     public Stanza getForwardedPacket() {
  74.         return forwardedPacket;
  75.     }

  76.     /**
  77.      * get the timestamp of the forwarded packet.
  78.      *
  79.      * @return the {@link DelayInformation} representing the time when the original packet was sent. May be null.
  80.      */
  81.     public DelayInformation getDelayInformation() {
  82.         return delay;
  83.     }

  84.     /**
  85.      *
  86.      * @param packet
  87.      * @return the Forwarded extension or null
  88.      */
  89.     public static Forwarded from(Stanza packet) {
  90.         return packet.getExtension(ELEMENT, NAMESPACE);
  91.     }
  92. }