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