001/** 002 * 003 * Copyright 2013-2014 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.io.IOException; 020 021import org.jivesoftware.smack.packet.Message; 022import org.jivesoftware.smack.packet.Stanza; 023import org.jivesoftware.smack.packet.ExtensionElement; 024import org.jivesoftware.smack.packet.id.StanzaIdUtil; 025import org.jivesoftware.smack.provider.ExtensionElementProvider; 026import org.xmlpull.v1.XmlPullParser; 027import org.xmlpull.v1.XmlPullParserException; 028 029/** 030 * Represents a <b>message delivery receipt request</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 DeliveryReceiptRequest implements ExtensionElement 036{ 037 public static final String ELEMENT = "request"; 038 039 @Override 040 public String getElementName() 041 { 042 return ELEMENT; 043 } 044 045 @Override 046 public String getNamespace() 047 { 048 return DeliveryReceipt.NAMESPACE; 049 } 050 051 @Override 052 public String toXML() 053 { 054 return "<request xmlns='" + DeliveryReceipt.NAMESPACE + "'/>"; 055 } 056 057 /** 058 * Get the {@link DeliveryReceiptRequest} extension of the packet, if any. 059 * 060 * @param p the packet 061 * @return the {@link DeliveryReceiptRequest} extension or {@code null} 062 * @deprecated use {@link #from(Stanza)} instead 063 */ 064 @Deprecated 065 public static DeliveryReceiptRequest getFrom(Stanza p) { 066 return from(p); 067 } 068 069 /** 070 * Get the {@link DeliveryReceiptRequest} extension of the packet, if any. 071 * 072 * @param packet the packet 073 * @return the {@link DeliveryReceiptRequest} extension or {@code null} 074 */ 075 public static DeliveryReceiptRequest from(Stanza packet) { 076 return packet.getExtension(ELEMENT, DeliveryReceipt.NAMESPACE); 077 } 078 079 /** 080 * Add a delivery receipt request to an outgoing packet. 081 * 082 * Only message packets may contain receipt requests as of XEP-0184, 083 * therefore only allow Message as the parameter type. 084 * 085 * @param message Message object to add a request to 086 * @return the Message ID which will be used as receipt ID 087 */ 088 public static String addTo(Message message) { 089 if (message.getStanzaId() == null) { 090 message.setStanzaId(StanzaIdUtil.newStanzaId()); 091 } 092 message.addExtension(new DeliveryReceiptRequest()); 093 return message.getStanzaId(); 094 } 095 096 /** 097 * This Provider parses and returns DeliveryReceiptRequest packets. 098 */ 099 public static class Provider extends ExtensionElementProvider<DeliveryReceiptRequest> { 100 @Override 101 public DeliveryReceiptRequest parse(XmlPullParser parser, 102 int initialDepth) throws XmlPullParserException, 103 IOException { 104 return new DeliveryReceiptRequest(); 105 } 106 } 107}