MamElements.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 java.util.List;

  19. import org.jivesoftware.smack.packet.Element;
  20. import org.jivesoftware.smack.packet.ExtensionElement;
  21. import org.jivesoftware.smack.packet.Message;
  22. import org.jivesoftware.smack.packet.MessageView;
  23. import org.jivesoftware.smack.packet.XmlElement;
  24. import org.jivesoftware.smack.util.StringUtils;
  25. import org.jivesoftware.smack.util.XmlStringBuilder;
  26. import org.jivesoftware.smackx.forward.packet.Forwarded;

  27. import org.jxmpp.jid.Jid;

  28. /**
  29.  * MAM elements.
  30.  *
  31.  * @see <a href="http://xmpp.org/extensions/xep-0313.html">XEP-0313: Message
  32.  *      Archive Management</a>
  33.  * @author Fernando Ramirez and Florian Schmaus
  34.  *
  35.  */
  36. public class MamElements {

  37.     /**
  38.      * MAM result extension class.
  39.      *
  40.      * @see <a href="http://xmpp.org/extensions/xep-0313.html">XEP-0313: Message
  41.      *      Archive Management</a>
  42.      *
  43.      */
  44.     public abstract static class MamResultExtension implements ExtensionElement {

  45.         /**
  46.          * result element.
  47.          */
  48.         public static final String ELEMENT = "result";

  49.         /**
  50.          * id of the result.
  51.          */
  52.         private final String id;

  53.         /**
  54.          * the forwarded element.
  55.          */
  56.         private final Forwarded<Message> forwarded;

  57.         /**
  58.          * the query id.
  59.          */
  60.         private String queryId;

  61.         protected final MamVersion version;

  62.         /**
  63.          * MAM result extension constructor.
  64.          *
  65.          * @param version TODO javadoc me please
  66.          * @param queryId TODO javadoc me please
  67.          * @param id TODO javadoc me please
  68.          * @param forwarded TODO javadoc me please
  69.          */
  70.         public MamResultExtension(MamVersion version, String queryId, String id, Forwarded<Message> forwarded) {
  71.             if (StringUtils.isEmpty(id)) {
  72.                 throw new IllegalArgumentException("id must not be null or empty");
  73.             }
  74.             if (forwarded == null) {
  75.                 throw new IllegalArgumentException("forwarded must no be null");
  76.             }
  77.             if (version == null) {
  78.                 throw new IllegalArgumentException("version must not be null");
  79.             }
  80.             this.version = version;
  81.             this.id = id;
  82.             this.forwarded = forwarded;
  83.             this.queryId = queryId;
  84.         }

  85.         /**
  86.          * Get the id.
  87.          *
  88.          * @return the id
  89.          */
  90.         public String getId() {
  91.             return id;
  92.         }

  93.         /**
  94.          * Get the forwarded element.
  95.          *
  96.          * @return the forwarded element
  97.          */
  98.         public Forwarded<Message> getForwarded() {
  99.             return forwarded;
  100.         }

  101.         /**
  102.          * Get query id.
  103.          *
  104.          * @return the query id
  105.          */
  106.         public final String getQueryId() {
  107.             return queryId;
  108.         }

  109.         @Override
  110.         public String getElementName() {
  111.             return ELEMENT;
  112.         }

  113.         @Override
  114.         public final String getNamespace() {
  115.             return version.getNamespace();
  116.         }

  117.         @Override
  118.         public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  119.             XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);

  120.             xml.optAttribute("queryid", getQueryId());
  121.             xml.optAttribute("id", getId());
  122.             xml.rightAngleBracket();

  123.             xml.append(getForwarded());

  124.             xml.closeElement(this);
  125.             return xml;
  126.         }

  127.         public static MamResultExtension from(MessageView message) {
  128.             for (XmlElement extension : message.getExtensions()) {
  129.                 if (extension instanceof MamResultExtension) {
  130.                     return (MamResultExtension) extension;
  131.                 }
  132.             }

  133.             return null;
  134.         }

  135.     }

  136.     /**
  137.      * Always JID list element class for the MamPrefsIQ.
  138.      *
  139.      */
  140.     public static class AlwaysJidListElement implements Element {

  141.         /**
  142.          * list of JIDs.
  143.          */
  144.         private final List<Jid> alwaysJids;

  145.         /**
  146.          * Always JID list element constructor.
  147.          *
  148.          * @param alwaysJids TODO javadoc me please
  149.          */
  150.         AlwaysJidListElement(List<Jid> alwaysJids) {
  151.             this.alwaysJids = alwaysJids;
  152.         }

  153.         @Override
  154.         public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  155.             XmlStringBuilder xml = new XmlStringBuilder();
  156.             xml.openElement("always");

  157.             for (Jid jid : alwaysJids) {
  158.                 xml.element("jid", jid);
  159.             }

  160.             xml.closeElement("always");
  161.             return xml;
  162.         }
  163.     }

  164.     /**
  165.      * Never JID list element class for the MamPrefsIQ.
  166.      *
  167.      */
  168.     public static class NeverJidListElement implements Element {

  169.         /**
  170.          * list of JIDs
  171.          */
  172.         private List<Jid> neverJids;

  173.         /**
  174.          * Never JID list element constructor.
  175.          *
  176.          * @param neverJids TODO javadoc me please
  177.          */
  178.         public NeverJidListElement(List<Jid> neverJids) {
  179.             this.neverJids = neverJids;
  180.         }

  181.         @Override
  182.         public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  183.             XmlStringBuilder xml = new XmlStringBuilder();
  184.             xml.openElement("never");

  185.             for (Jid jid : neverJids) {
  186.                 xml.element("jid", jid);
  187.             }

  188.             xml.closeElement("never");
  189.             return xml;
  190.         }
  191.     }

  192. }