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