MamQueryIQ.java

  1. /**
  2.  *
  3.  * Copyright © 2016-2020 Florian Schmaus and 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.mam.element;

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

  19. import org.jivesoftware.smackx.xdata.packet.DataForm;

  20. /**
  21.  * MAM Query IQ class.
  22.  *
  23.  * @see <a href="http://xmpp.org/extensions/xep-0313.html">XEP-0313: Message
  24.  *      Archive Management</a>
  25.  * @author Fernando Ramirez and Florian Schmaus
  26.  *
  27.  */
  28. public class MamQueryIQ extends IQ {

  29.     /**
  30.      * the MAM query IQ element.
  31.      */
  32.     public static final String ELEMENT = QUERY_ELEMENT;

  33.     private final String queryId;
  34.     private final String node;
  35.     private final DataForm dataForm;

  36.     /**
  37.      * MAM query IQ constructor.
  38.      *
  39.      * @param version TODO javadoc me please
  40.      * @param queryId TODO javadoc me please
  41.      */
  42.     public MamQueryIQ(MamVersion version, String queryId) {
  43.         this(version, queryId, null, null);
  44.         setType(IQ.Type.get);
  45.     }

  46.     /**
  47.      * MAM query IQ constructor.
  48.      *
  49.      * @param version TODO javadoc me please
  50.      * @param form TODO javadoc me please
  51.      */
  52.     public MamQueryIQ(MamVersion version, DataForm form) {
  53.         this(version, null, null, form);
  54.     }

  55.     /**
  56.      * MAM query IQ constructor.
  57.      *
  58.      * @param version TODO javadoc me please
  59.      * @param queryId TODO javadoc me please
  60.      * @param form TODO javadoc me please
  61.      */
  62.     public MamQueryIQ(MamVersion version, String queryId, DataForm form) {
  63.         this(version, queryId, null, form);
  64.     }

  65.     /**
  66.      * MAM query IQ constructor.
  67.      *
  68.      * @param version TODO javadoc me please
  69.      * @param queryId TODO javadoc me please
  70.      * @param node TODO javadoc me please
  71.      * @param dataForm TODO javadoc me please
  72.      */
  73.     public MamQueryIQ(MamVersion version, String queryId, String node, DataForm dataForm) {
  74.         super(ELEMENT, version.getNamespace());
  75.         this.queryId = queryId;
  76.         this.node = node;
  77.         this.dataForm = dataForm;

  78.         if (dataForm != null) {
  79.             String formType = dataForm.getFormType();
  80.             if (formType == null) {
  81.                 throw new IllegalArgumentException("If a data form is given it must posses a hidden form type field");
  82.             }
  83.             if (!formType.equals(version.getNamespace())) {
  84.                 throw new IllegalArgumentException(
  85.                         "Value of the hidden form type field must be '" + version.getNamespace() + "'");
  86.             }
  87.             addExtension(dataForm);
  88.         }
  89.     }

  90.     /**
  91.      * Get query id.
  92.      *
  93.      * @return the query id
  94.      */
  95.     public String getQueryId() {
  96.         return queryId;
  97.     }

  98.     /**
  99.      * Get the Node name.
  100.      *
  101.      * @return the node
  102.      */
  103.     public String getNode() {
  104.       return node;
  105.     }

  106.     /**
  107.      * Get the data form.
  108.      *
  109.      * @return the data form
  110.      */
  111.     public DataForm getDataForm() {
  112.         return dataForm;
  113.     }

  114.     @Override
  115.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  116.         xml.optAttribute("queryid", queryId);
  117.         xml.optAttribute("node", node);
  118.         xml.rightAngleBracket();
  119.         return xml;
  120.     }

  121. }