001/**
002 *
003 * Copyright 2013-2014 Georg Lukas
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.forward.packet;
018
019import org.jivesoftware.smack.packet.ExtensionElement;
020import org.jivesoftware.smack.packet.Stanza;
021import org.jivesoftware.smack.util.XmlStringBuilder;
022
023import org.jivesoftware.smackx.delay.packet.DelayInformation;
024
025/**
026 * Stanza extension for XEP-0297: Stanza Forwarding.
027 * 
028 * @author Georg Lukas
029 * @see <a href="http://xmpp.org/extensions/xep-0297.html">XEP-0297: Stanza Forwarding</a>
030 */
031public class Forwarded implements ExtensionElement {
032    public static final String NAMESPACE = "urn:xmpp:forward:0";
033    public static final String ELEMENT = "forwarded";
034
035    private final DelayInformation delay;
036    private final Stanza forwardedPacket;
037
038    /**
039     * Creates a new Forwarded stanza(/packet) extension.
040     *
041     * @param delay an optional {@link DelayInformation} timestamp of the packet.
042     * @param fwdPacket the stanza(/packet) that is forwarded (required).
043     */
044    public Forwarded(DelayInformation delay, Stanza fwdPacket) {
045        this.delay = delay;
046        this.forwardedPacket = fwdPacket;
047    }
048
049    /**
050     * Creates a new Forwarded stanza(/packet) extension.
051     *
052     * @param fwdPacket the stanza(/packet) that is forwarded (required).
053     */
054    public Forwarded(Stanza fwdPacket) {
055        this(null, fwdPacket);
056    }
057
058    @Override
059    public String getElementName() {
060        return ELEMENT;
061    }
062
063    @Override
064    public String getNamespace() {
065        return NAMESPACE;
066    }
067
068    @Override
069    public XmlStringBuilder toXML() {
070        XmlStringBuilder xml = new XmlStringBuilder(this);
071        xml.rightAngleBracket();
072        xml.optElement(getDelayInformation());
073        xml.append(forwardedPacket.toXML());
074        xml.closeElement(this);
075        return xml;
076    }
077
078    /**
079     * get the stanza(/packet) forwarded by this stanza.
080     *
081     * @return the {@link Stanza} instance (typically a message) that was forwarded.
082     * @deprecated use @{link {@link #getForwardedStanza()}} instead.
083     */
084    @Deprecated
085    public Stanza getForwardedPacket() {
086        return forwardedPacket;
087    }
088
089    /**
090     * Get the forwarded Stanza found in this extension.
091     *
092     * @return the {@link Stanza} (typically a message) that was forwarded.
093     */
094    public Stanza getForwardedStanza() {
095        return forwardedPacket;
096    }
097
098    /**
099     * get the timestamp of the forwarded packet.
100     *
101     * @return the {@link DelayInformation} representing the time when the original stanza(/packet) was sent. May be null.
102     */
103    public DelayInformation getDelayInformation() {
104        return delay;
105    }
106
107    /**
108     * Get the forwarded extension.
109     * @param packet
110     * @return the Forwarded extension or null
111     */
112    public static Forwarded from(Stanza packet) {
113        return packet.getExtension(ELEMENT, NAMESPACE);
114    }
115}