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