DeliveryReceipt.java

  1. /**
  2.  *
  3.  * Copyright 2013-2014 the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.receipts;

  18. import java.util.List;
  19. import java.util.Map;

  20. import org.jivesoftware.smack.packet.Message;
  21. import org.jivesoftware.smack.packet.ExtensionElement;
  22. import org.jivesoftware.smack.provider.EmbeddedExtensionProvider;
  23. import org.jivesoftware.smack.util.XmlStringBuilder;

  24. /**
  25.  * Represents a <b>message delivery receipt</b> entry as specified by
  26.  * <a href="http://xmpp.org/extensions/xep-0184.html">Message Delivery Receipts</a>.
  27.  *
  28.  * @author Georg Lukas
  29.  */
  30. public class DeliveryReceipt implements ExtensionElement
  31. {
  32.     public static final String NAMESPACE = "urn:xmpp:receipts";
  33.     public static final String ELEMENT = "received";

  34.     /**
  35.      * original ID of the delivered message
  36.      */
  37.     private final String id;

  38.     public DeliveryReceipt(String id)
  39.     {
  40.         this.id = id;
  41.     }

  42.     public String getId()
  43.     {
  44.         return id;
  45.     }

  46.     @Override
  47.     public String getElementName()
  48.     {
  49.         return ELEMENT;
  50.     }

  51.     @Override
  52.     public String getNamespace()
  53.     {
  54.         return NAMESPACE;
  55.     }

  56.     @Override
  57.     public XmlStringBuilder toXML()
  58.     {
  59.         XmlStringBuilder xml = new XmlStringBuilder(this);
  60.         xml.attribute("id", id);
  61.         xml.closeEmptyElement();
  62.         return xml;
  63.     }

  64.     /**
  65.      * Get the {@link DeliveryReceipt} extension of the packet, if any.
  66.      *
  67.      * @param p the packet
  68.      * @return the {@link DeliveryReceipt} extension or {@code null}
  69.      * @deprecated use {@link #from(Message)} instead
  70.      */
  71.     @Deprecated
  72.     public static DeliveryReceipt getFrom(Message p) {
  73.         return from(p);
  74.     }

  75.     /**
  76.      * Get the {@link DeliveryReceipt} extension of the message, if any.
  77.      *
  78.      * @param message the message.
  79.      * @return the {@link DeliveryReceipt} extension or {@code null}
  80.      */
  81.     public static DeliveryReceipt from(Message message) {
  82.         return message.getExtension(ELEMENT, NAMESPACE);
  83.     }

  84.     /**
  85.      * This Provider parses and returns DeliveryReceipt packets.
  86.      */
  87.     public static class Provider extends EmbeddedExtensionProvider<DeliveryReceipt>
  88.     {

  89.         @Override
  90.         protected DeliveryReceipt createReturnExtension(String currentElement, String currentNamespace,
  91.                 Map<String, String> attributeMap, List<? extends ExtensionElement> content)
  92.         {
  93.             return new DeliveryReceipt(attributeMap.get("id"));
  94.         }

  95.     }
  96. }