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