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.Stanza;
020import org.jivesoftware.smack.packet.ExtensionElement;
021import org.jivesoftware.smack.util.XmlStringBuilder;
022import org.jivesoftware.smackx.delay.packet.DelayInformation;
023
024/**
025 * Stanza(/Packet) extension for >XEP-0297: Stanza Forwarding.
026 * 
027 * @author Georg Lukas
028 * @see <a href="http://xmpp.org/extensions/xep-0297.html">XEP-0297: Stanza Forwarding</a>
029 */
030public class Forwarded implements ExtensionElement {
031    public static final String NAMESPACE = "urn:xmpp:forward:0";
032    public static final String ELEMENT = "forwarded";
033
034    private final DelayInformation delay;
035    private final Stanza forwardedPacket;
036
037    /**
038     * Creates a new Forwarded stanza(/packet) extension.
039     *
040     * @param delay an optional {@link DelayInformation} timestamp of the packet.
041     * @param fwdPacket the stanza(/packet) that is forwarded (required).
042     */
043    public Forwarded(DelayInformation delay, Stanza fwdPacket) {
044        this.delay = delay;
045        this.forwardedPacket = fwdPacket;
046    }
047
048    /**
049     * Creates a new Forwarded stanza(/packet) extension.
050     *
051     * @param fwdPacket the stanza(/packet) that is forwarded (required).
052     */
053    public Forwarded(Stanza fwdPacket) {
054        this(null, fwdPacket);
055    }
056
057    @Override
058    public String getElementName() {
059        return ELEMENT;
060    }
061
062    @Override
063    public String getNamespace() {
064        return NAMESPACE;
065    }
066
067    @Override
068    public XmlStringBuilder toXML() {
069        XmlStringBuilder xml = new XmlStringBuilder(this);
070        xml.rightAngleBracket();
071        xml.optElement(getDelayInformation());
072        xml.append(forwardedPacket.toXML());
073        xml.closeElement(this);
074        return xml;
075    }
076
077    /**
078     * get the stanza(/packet) forwarded by this stanza.
079     *
080     * @return the {@link Stanza} instance (typically a message) that was forwarded.
081     */
082    public Stanza getForwardedPacket() {
083        return forwardedPacket;
084    }
085
086    /**
087     * get the timestamp of the forwarded packet.
088     *
089     * @return the {@link DelayInformation} representing the time when the original stanza(/packet) was sent. May be null.
090     */
091    public DelayInformation getDelayInformation() {
092        return delay;
093    }
094
095    /**
096     * 
097     * @param packet
098     * @return the Forwarded extension or null
099     */
100    public static Forwarded from(Stanza packet) {
101        return packet.getExtension(ELEMENT, NAMESPACE);
102    }
103}