001/**
002 *
003 * Copyright © 2016 Fernando Ramirez
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.push_notifications.element;
018
019import java.util.HashMap;
020import java.util.Iterator;
021import java.util.Map;
022
023import org.jivesoftware.smack.packet.IQ;
024
025import org.jivesoftware.smackx.pubsub.packet.PubSub;
026import org.jivesoftware.smackx.xdata.FormField;
027import org.jivesoftware.smackx.xdata.TextSingleFormField;
028import org.jivesoftware.smackx.xdata.packet.DataForm;
029
030import org.jxmpp.jid.Jid;
031
032/**
033 * Enable Push Notifications IQ.
034 *
035 * @see <a href="http://xmpp.org/extensions/xep-0357.html">XEP-0357: Push
036 *      Notifications</a>
037 * @author Fernando Ramirez
038 *
039 */
040public class EnablePushNotificationsIQ extends IQ {
041
042    /**
043     * enable element.
044     */
045    public static final String ELEMENT = "enable";
046
047    /**
048     * the IQ NAMESPACE.
049     */
050    public static final String NAMESPACE = PushNotificationsElements.NAMESPACE;
051
052    private final Jid jid;
053    private final String node;
054    private final HashMap<String, String> publishOptions;
055
056    public EnablePushNotificationsIQ(Jid jid, String node, HashMap<String, String> publishOptions) {
057        super(ELEMENT, NAMESPACE);
058        this.jid = jid;
059        this.node = node;
060        this.publishOptions = publishOptions;
061        this.setType(Type.set);
062    }
063
064    public EnablePushNotificationsIQ(Jid jid, String node) {
065        this(jid, node, null);
066    }
067
068    /**
069     * Get the JID.
070     *
071     * @return the JID
072     */
073    public Jid getJid() {
074        return jid;
075    }
076
077    /**
078     * Get the node.
079     *
080     * @return the node
081     */
082    public String getNode() {
083        return node;
084    }
085
086    /**
087     * Get the publish options.
088     *
089     * @return the publish options
090     */
091    public HashMap<String, String> getPublishOptions() {
092        return publishOptions;
093    }
094
095    @Override
096    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
097        xml.attribute("jid", jid);
098        xml.attribute("node", node);
099        xml.rightAngleBracket();
100
101        if (publishOptions != null) {
102            DataForm.Builder dataForm = DataForm.builder();
103
104            // TODO: Shouldn't this use some potentially existing PubSub API? Also FORM_TYPE fields are usually of type
105            // 'hidden', but the examples in XEP-0357 do also not set the value to hidden and FORM_TYPE itself appears
106            // to be more convention than specification.
107            FormField formTypeField = FormField.buildHiddenFormType(PubSub.NAMESPACE + "#publish-options");
108            dataForm.addField(formTypeField);
109
110            Iterator<Map.Entry<String, String>> publishOptionsIterator = publishOptions.entrySet().iterator();
111            while (publishOptionsIterator.hasNext()) {
112                Map.Entry<String, String> pairVariableValue = publishOptionsIterator.next();
113                TextSingleFormField.Builder field = FormField.builder(pairVariableValue.getKey());
114                field.setValue(pairVariableValue.getValue());
115                dataForm.addField(field.build());
116            }
117
118            xml.append(dataForm.build());
119        }
120
121        return xml;
122    }
123
124}