EnablePushNotificationsIQ.java

  1. /**
  2.  *
  3.  * Copyright © 2016 Fernando Ramirez
  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.push_notifications.element;

  18. import java.util.HashMap;
  19. import java.util.Iterator;
  20. import java.util.Map;

  21. import org.jivesoftware.smack.packet.IQ;

  22. import org.jivesoftware.smackx.pubsub.packet.PubSub;
  23. import org.jivesoftware.smackx.xdata.FormField;
  24. import org.jivesoftware.smackx.xdata.TextSingleFormField;
  25. import org.jivesoftware.smackx.xdata.packet.DataForm;

  26. import org.jxmpp.jid.Jid;

  27. /**
  28.  * Enable Push Notifications IQ.
  29.  *
  30.  * @see <a href="http://xmpp.org/extensions/xep-0357.html">XEP-0357: Push
  31.  *      Notifications</a>
  32.  * @author Fernando Ramirez
  33.  *
  34.  */
  35. public class EnablePushNotificationsIQ extends IQ {

  36.     /**
  37.      * enable element.
  38.      */
  39.     public static final String ELEMENT = "enable";

  40.     /**
  41.      * the IQ NAMESPACE.
  42.      */
  43.     public static final String NAMESPACE = PushNotificationsElements.NAMESPACE;

  44.     private final Jid jid;
  45.     private final String node;
  46.     private final HashMap<String, String> publishOptions;

  47.     public EnablePushNotificationsIQ(Jid jid, String node, HashMap<String, String> publishOptions) {
  48.         super(ELEMENT, NAMESPACE);
  49.         this.jid = jid;
  50.         this.node = node;
  51.         this.publishOptions = publishOptions;
  52.         this.setType(Type.set);
  53.     }

  54.     public EnablePushNotificationsIQ(Jid jid, String node) {
  55.         this(jid, node, null);
  56.     }

  57.     /**
  58.      * Get the JID.
  59.      *
  60.      * @return the JID
  61.      */
  62.     public Jid getJid() {
  63.         return jid;
  64.     }

  65.     /**
  66.      * Get the node.
  67.      *
  68.      * @return the node
  69.      */
  70.     public String getNode() {
  71.         return node;
  72.     }

  73.     /**
  74.      * Get the publish options.
  75.      *
  76.      * @return the publish options
  77.      */
  78.     public HashMap<String, String> getPublishOptions() {
  79.         return publishOptions;
  80.     }

  81.     @Override
  82.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  83.         xml.attribute("jid", jid);
  84.         xml.attribute("node", node);
  85.         xml.rightAngleBracket();

  86.         if (publishOptions != null) {
  87.             DataForm.Builder dataForm = DataForm.builder();

  88.             // TODO: Shouldn't this use some potentially existing PubSub API? Also FORM_TYPE fields are usually of type
  89.             // 'hidden', but the examples in XEP-0357 do also not set the value to hidden and FORM_TYPE itself appears
  90.             // to be more convention than specification.
  91.             FormField formTypeField = FormField.buildHiddenFormType(PubSub.NAMESPACE + "#publish-options");
  92.             dataForm.addField(formTypeField);

  93.             Iterator<Map.Entry<String, String>> publishOptionsIterator = publishOptions.entrySet().iterator();
  94.             while (publishOptionsIterator.hasNext()) {
  95.                 Map.Entry<String, String> pairVariableValue = publishOptionsIterator.next();
  96.                 TextSingleFormField.Builder field = FormField.builder(pairVariableValue.getKey());
  97.                 field.setValue(pairVariableValue.getValue());
  98.                 dataForm.addField(field.build());
  99.             }

  100.             xml.append(dataForm.build());
  101.         }

  102.         return xml;
  103.     }

  104. }