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.Packet;
023import org.jivesoftware.smack.packet.PacketExtension;
024import org.jivesoftware.smack.provider.EmbeddedExtensionProvider;
025
026/**
027 * Represents a <b>message delivery receipt</b> entry as specified by
028 * <a href="http://xmpp.org/extensions/xep-0184.html">Message Delivery Receipts</a>.
029 *
030 * @author Georg Lukas
031 */
032public class DeliveryReceipt implements PacketExtension
033{
034    public static final String NAMESPACE = "urn:xmpp:receipts";
035    public static final String ELEMENT = "received";
036
037    private String id; /// original ID of the delivered message
038
039    public DeliveryReceipt(String id)
040    {
041        this.id = id;
042    }
043
044    public String getId()
045    {
046        return id;
047    }
048
049    @Override
050    public String getElementName()
051    {
052        return ELEMENT;
053    }
054
055    @Override
056    public String getNamespace()
057    {
058        return NAMESPACE;
059    }
060
061    @Override
062    public String toXML()
063    {
064        return "<received xmlns='" + NAMESPACE + "' id='" + id + "'/>";
065    }
066
067    /**
068     * Get the {@link DeliveryReceipt} extension of the packet, if any.
069     *
070     * @param p the packet
071     * @return the {@link DeliveryReceipt} extension or {@code null}
072     */
073    public static DeliveryReceipt getFrom(Packet p) {
074        return p.getExtension(ELEMENT, NAMESPACE);
075    }
076
077    /**
078     * This Provider parses and returns DeliveryReceipt packets.
079     */
080    public static class Provider extends EmbeddedExtensionProvider
081    {
082
083        @Override
084        protected PacketExtension createReturnExtension(String currentElement, String currentNamespace,
085                Map<String, String> attributeMap, List<? extends PacketExtension> content)
086        {
087            return new DeliveryReceipt(attributeMap.get("id"));
088        }
089
090    }
091}