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.Iterator;
020import java.util.Map;
021
022import org.jivesoftware.smack.packet.IQ;
023
024import org.jivesoftware.smackx.pubsub.packet.PubSub;
025import org.jivesoftware.smackx.xdata.FormField;
026import org.jivesoftware.smackx.xdata.TextSingleFormField;
027import org.jivesoftware.smackx.xdata.packet.DataForm;
028
029import org.jxmpp.jid.Jid;
030
031/**
032 * Enable Push Notifications IQ.
033 *
034 * @see <a href="http://xmpp.org/extensions/xep-0357.html">XEP-0357: Push
035 *      Notifications</a>
036 * @author Fernando Ramirez
037 *
038 */
039public class EnablePushNotificationsIQ extends IQ {
040
041    /**
042     * enable element.
043     */
044    public static final String ELEMENT = "enable";
045
046    /**
047     * the IQ NAMESPACE.
048     */
049    public static final String NAMESPACE = PushNotificationsElements.NAMESPACE;
050
051    private final Jid jid;
052    private final String node;
053    private final Map<String, String> publishOptions;
054
055    @SuppressWarnings("this-escape")
056    public EnablePushNotificationsIQ(Jid jid, String node, Map<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 Map<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}