DeliveryReceiptRequest.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.io.IOException;

  19. import org.jivesoftware.smack.packet.Message;
  20. import org.jivesoftware.smack.packet.Stanza;
  21. import org.jivesoftware.smack.packet.ExtensionElement;
  22. import org.jivesoftware.smack.packet.id.StanzaIdUtil;
  23. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  24. import org.xmlpull.v1.XmlPullParser;
  25. import org.xmlpull.v1.XmlPullParserException;

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

  35.     public String getElementName()
  36.     {
  37.         return ELEMENT;
  38.     }

  39.     public String getNamespace()
  40.     {
  41.         return DeliveryReceipt.NAMESPACE;
  42.     }

  43.     public String toXML()
  44.     {
  45.         return "<request xmlns='" + DeliveryReceipt.NAMESPACE + "'/>";
  46.     }

  47.     /**
  48.      * Get the {@link DeliveryReceiptRequest} extension of the packet, if any.
  49.      *
  50.      * @param p the packet
  51.      * @return the {@link DeliveryReceiptRequest} extension or {@code null}
  52.      * @deprecated use {@link #from(Stanza)} instead
  53.      */
  54.     @Deprecated
  55.     public static DeliveryReceiptRequest getFrom(Stanza p) {
  56.         return from(p);
  57.     }

  58.     /**
  59.      * Get the {@link DeliveryReceiptRequest} extension of the packet, if any.
  60.      *
  61.      * @param packet the packet
  62.      * @return the {@link DeliveryReceiptRequest} extension or {@code null}
  63.      */
  64.     public static DeliveryReceiptRequest from(Stanza packet) {
  65.         return packet.getExtension(ELEMENT, DeliveryReceipt.NAMESPACE);
  66.     }

  67.     /**
  68.      * Add a delivery receipt request to an outgoing packet.
  69.      *
  70.      * Only message packets may contain receipt requests as of XEP-0184,
  71.      * therefore only allow Message as the parameter type.
  72.      *
  73.      * @param message Message object to add a request to
  74.      * @return the Message ID which will be used as receipt ID
  75.      */
  76.     public static String addTo(Message message) {
  77.         if (message.getStanzaId() == null) {
  78.             message.setStanzaId(StanzaIdUtil.newStanzaId());
  79.         }
  80.         message.addExtension(new DeliveryReceiptRequest());
  81.         return message.getStanzaId();
  82.     }

  83.     /**
  84.      * This Provider parses and returns DeliveryReceiptRequest packets.
  85.      */
  86.     public static class Provider extends ExtensionElementProvider<DeliveryReceiptRequest> {
  87.         @Override
  88.         public DeliveryReceiptRequest parse(XmlPullParser parser,
  89.                         int initialDepth) throws XmlPullParserException,
  90.                         IOException {
  91.             return new DeliveryReceiptRequest();
  92.         }
  93.     }
  94. }