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