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