001/**
002 *
003 * Copyright 2013-2014 the original author or authors
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.receipts;
018
019import java.util.List;
020import java.util.Map;
021
022import org.jivesoftware.smack.packet.Message;
023import org.jivesoftware.smack.packet.ExtensionElement;
024import org.jivesoftware.smack.provider.EmbeddedExtensionProvider;
025import org.jivesoftware.smack.util.XmlStringBuilder;
026
027/**
028 * Represents a <b>message delivery receipt</b> entry as specified by
029 * <a href="http://xmpp.org/extensions/xep-0184.html">Message Delivery Receipts</a>.
030 *
031 * @author Georg Lukas
032 */
033public class DeliveryReceipt implements ExtensionElement
034{
035    public static final String NAMESPACE = "urn:xmpp:receipts";
036    public static final String ELEMENT = "received";
037
038    /**
039     * original ID of the delivered message
040     */
041    private final String id;
042
043    public DeliveryReceipt(String id)
044    {
045        this.id = id;
046    }
047
048    public String getId()
049    {
050        return id;
051    }
052
053    @Override
054    public String getElementName()
055    {
056        return ELEMENT;
057    }
058
059    @Override
060    public String getNamespace()
061    {
062        return NAMESPACE;
063    }
064
065    @Override
066    public XmlStringBuilder toXML()
067    {
068        XmlStringBuilder xml = new XmlStringBuilder(this);
069        xml.attribute("id", id);
070        xml.closeEmptyElement();
071        return xml;
072    }
073
074    /**
075     * Get the {@link DeliveryReceipt} extension of the packet, if any.
076     *
077     * @param p the packet
078     * @return the {@link DeliveryReceipt} extension or {@code null}
079     * @deprecated use {@link #from(Message)} instead
080     */
081    @Deprecated
082    public static DeliveryReceipt getFrom(Message p) {
083        return from(p);
084    }
085
086    /**
087     * Get the {@link DeliveryReceipt} extension of the message, if any.
088     *
089     * @param message the message.
090     * @return the {@link DeliveryReceipt} extension or {@code null}
091     */
092    public static DeliveryReceipt from(Message message) {
093        return message.getExtension(ELEMENT, NAMESPACE);
094    }
095
096    /**
097     * This Provider parses and returns DeliveryReceipt packets.
098     */
099    public static class Provider extends EmbeddedExtensionProvider<DeliveryReceipt>
100    {
101
102        @Override
103        protected DeliveryReceipt createReturnExtension(String currentElement, String currentNamespace,
104                Map<String, String> attributeMap, List<? extends ExtensionElement> content)
105        {
106            return new DeliveryReceipt(attributeMap.get("id"));
107        }
108
109    }
110}