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 javax.xml.namespace.QName;

  20. import org.jivesoftware.smack.packet.ExtensionElement;
  21. import org.jivesoftware.smack.packet.Message;
  22. import org.jivesoftware.smack.packet.MessageBuilder;
  23. import org.jivesoftware.smack.packet.Stanza;
  24. import org.jivesoftware.smack.packet.XmlEnvironment;
  25. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  26. import org.jivesoftware.smack.xml.XmlPullParser;
  27. import org.jivesoftware.smack.xml.XmlPullParserException;

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

  38.     @Override
  39.     public String getElementName() {
  40.         return ELEMENT;
  41.     }

  42.     @Override
  43.     public String getNamespace() {
  44.         return DeliveryReceipt.NAMESPACE;
  45.     }

  46.     @Override
  47.     public String toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  48.         return "<request xmlns='" + DeliveryReceipt.NAMESPACE + "'/>";
  49.     }

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

  61.     /**
  62.      * Get the {@link DeliveryReceiptRequest} extension of the packet, if any.
  63.      *
  64.      * @param packet the packet
  65.      * @return the {@link DeliveryReceiptRequest} extension or {@code null}
  66.      */
  67.     public static DeliveryReceiptRequest from(Stanza packet) {
  68.         return packet.getExtension(DeliveryReceiptRequest.class);
  69.     }

  70.     /**
  71.      * Add a delivery receipt request to an outgoing packet.
  72.      *
  73.      * Only message packets may contain receipt requests as of XEP-0184,
  74.      * therefore only allow Message as the parameter type.
  75.      *
  76.      * @param message Message object to add a request to
  77.      * @return the Message ID which will be used as receipt ID
  78.      */
  79.     // TODO: Deprecate in favor of addTo(MessageBuilder) once connection's stanza interceptors use StanzaBuilder.
  80.     public static String addTo(Message message) {
  81.         message.throwIfNoStanzaId();

  82.         message.addExtension(new DeliveryReceiptRequest());
  83.         return message.getStanzaId();
  84.     }

  85.     /**
  86.      * Add a delivery receipt request to an outgoing packet.
  87.      *
  88.      * Only message packets may contain receipt requests as of XEP-0184,
  89.      * therefore only allow Message as the parameter type.
  90.      *
  91.      * @param messageBuilder Message object to add a request to
  92.      */
  93.     public static void addTo(MessageBuilder messageBuilder) {
  94.         messageBuilder.throwIfNoStanzaId();

  95.         messageBuilder.overrideExtension(new DeliveryReceiptRequest());
  96.     }

  97.     /**
  98.      * This Provider parses and returns DeliveryReceiptRequest packets.
  99.      */
  100.     public static class Provider extends ExtensionElementProvider<DeliveryReceiptRequest> {
  101.         @Override
  102.         public DeliveryReceiptRequest parse(XmlPullParser parser,
  103.                         int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException,
  104.                         IOException {
  105.             return new DeliveryReceiptRequest();
  106.         }
  107.     }
  108. }