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 java.util.ArrayList;
020import java.util.Collection;
021import java.util.List;
022
023import org.jivesoftware.smack.packet.ExtensionElement;
024import org.jivesoftware.smack.packet.Message;
025import org.jivesoftware.smack.packet.Stanza;
026import org.jivesoftware.smack.util.XmlStringBuilder;
027
028import org.jivesoftware.smackx.delay.packet.DelayInformation;
029
030/**
031 * Stanza extension for XEP-0297: Stanza Forwarding.
032 *
033 * @author Georg Lukas
034 * @see <a href="http://xmpp.org/extensions/xep-0297.html">XEP-0297: Stanza Forwarding</a>
035 */
036public class Forwarded implements ExtensionElement {
037    public static final String NAMESPACE = "urn:xmpp:forward:0";
038    public static final String ELEMENT = "forwarded";
039
040    private final DelayInformation delay;
041    private final Stanza forwardedPacket;
042
043    /**
044     * Creates a new Forwarded stanza extension.
045     *
046     * @param delay an optional {@link DelayInformation} timestamp of the packet.
047     * @param fwdPacket the stanza that is forwarded (required).
048     */
049    public Forwarded(DelayInformation delay, Stanza fwdPacket) {
050        this.delay = delay;
051        this.forwardedPacket = fwdPacket;
052    }
053
054    /**
055     * Creates a new Forwarded stanza extension.
056     *
057     * @param fwdPacket the stanza that is forwarded (required).
058     */
059    public Forwarded(Stanza fwdPacket) {
060        this(null, fwdPacket);
061    }
062
063    @Override
064    public String getElementName() {
065        return ELEMENT;
066    }
067
068    @Override
069    public String getNamespace() {
070        return NAMESPACE;
071    }
072
073    @Override
074    public XmlStringBuilder toXML(String enclosingNamespace) {
075        XmlStringBuilder xml = new XmlStringBuilder(this);
076        xml.rightAngleBracket();
077        xml.optElement(getDelayInformation());
078        xml.append(forwardedPacket.toXML(NAMESPACE));
079        xml.closeElement(this);
080        return xml;
081    }
082
083    /**
084     * get the stanza forwarded by this stanza.
085     *
086     * @return the {@link Stanza} instance (typically a message) that was forwarded.
087     * @deprecated use @{link {@link #getForwardedStanza()}} instead.
088     */
089    @Deprecated
090    public Stanza getForwardedPacket() {
091        return forwardedPacket;
092    }
093
094    /**
095     * Get the forwarded Stanza found in this extension.
096     *
097     * @return the {@link Stanza} (typically a message) that was forwarded.
098     */
099    public Stanza getForwardedStanza() {
100        return forwardedPacket;
101    }
102
103    /**
104     * get the timestamp of the forwarded packet.
105     *
106     * @return the {@link DelayInformation} representing the time when the original stanza was sent. May be null.
107     */
108    public DelayInformation getDelayInformation() {
109        return delay;
110    }
111
112    /**
113     * Get the forwarded extension.
114     * @param packet
115     * @return the Forwarded extension or null
116     */
117    public static Forwarded from(Stanza packet) {
118        return packet.getExtension(ELEMENT, NAMESPACE);
119    }
120
121    /**
122     * Extract messages in a collection of forwarded elements. Note that it is required that the {@link Forwarded} in
123     * the given collection only contain {@link Message} stanzas.
124     *
125     * @param forwardedCollection the collection to extract from.
126     * @return a list a the extracted messages.
127     * @since 4.3.0
128     */
129    public static List<Message> extractMessagesFrom(Collection<Forwarded> forwardedCollection) {
130        List<Message> res = new ArrayList<>(forwardedCollection.size());
131        for (Forwarded forwarded : forwardedCollection) {
132            Message message =  (Message) forwarded.forwardedPacket;
133            res.add(message);
134        }
135        return res;
136    }
137}