001/**
002 *
003 * Copyright © 2016-2020 Florian Schmaus and 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.mam.element;
018
019import java.util.List;
020
021import org.jivesoftware.smack.packet.Element;
022import org.jivesoftware.smack.packet.ExtensionElement;
023import org.jivesoftware.smack.packet.Message;
024import org.jivesoftware.smack.packet.MessageView;
025import org.jivesoftware.smack.packet.XmlElement;
026import org.jivesoftware.smack.util.StringUtils;
027import org.jivesoftware.smack.util.XmlStringBuilder;
028import org.jivesoftware.smackx.forward.packet.Forwarded;
029
030import org.jxmpp.jid.Jid;
031
032/**
033 * MAM elements.
034 *
035 * @see <a href="http://xmpp.org/extensions/xep-0313.html">XEP-0313: Message
036 *      Archive Management</a>
037 * @author Fernando Ramirez and Florian Schmaus
038 *
039 */
040public class MamElements {
041
042    /**
043     * MAM result extension class.
044     *
045     * @see <a href="http://xmpp.org/extensions/xep-0313.html">XEP-0313: Message
046     *      Archive Management</a>
047     *
048     */
049    public abstract static class MamResultExtension implements ExtensionElement {
050
051        /**
052         * result element.
053         */
054        public static final String ELEMENT = "result";
055
056        /**
057         * id of the result.
058         */
059        private final String id;
060
061        /**
062         * the forwarded element.
063         */
064        private final Forwarded<Message> forwarded;
065
066        /**
067         * the query id.
068         */
069        private String queryId;
070
071        protected final MamVersion version;
072
073        /**
074         * MAM result extension constructor.
075         *
076         * @param version TODO javadoc me please
077         * @param queryId TODO javadoc me please
078         * @param id TODO javadoc me please
079         * @param forwarded TODO javadoc me please
080         */
081        public MamResultExtension(MamVersion version, String queryId, String id, Forwarded<Message> forwarded) {
082            if (StringUtils.isEmpty(id)) {
083                throw new IllegalArgumentException("id must not be null or empty");
084            }
085            if (forwarded == null) {
086                throw new IllegalArgumentException("forwarded must no be null");
087            }
088            if (version == null) {
089                throw new IllegalArgumentException("version must not be null");
090            }
091            this.version = version;
092            this.id = id;
093            this.forwarded = forwarded;
094            this.queryId = queryId;
095        }
096
097        /**
098         * Get the id.
099         *
100         * @return the id
101         */
102        public String getId() {
103            return id;
104        }
105
106        /**
107         * Get the forwarded element.
108         *
109         * @return the forwarded element
110         */
111        public Forwarded<Message> getForwarded() {
112            return forwarded;
113        }
114
115        /**
116         * Get query id.
117         *
118         * @return the query id
119         */
120        public final String getQueryId() {
121            return queryId;
122        }
123
124        @Override
125        public String getElementName() {
126            return ELEMENT;
127        }
128
129        @Override
130        public final String getNamespace() {
131            return version.getNamespace();
132        }
133
134        @Override
135        public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
136            XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
137
138            xml.optAttribute("queryid", getQueryId());
139            xml.optAttribute("id", getId());
140            xml.rightAngleBracket();
141
142            xml.append(getForwarded());
143
144            xml.closeElement(this);
145            return xml;
146        }
147
148        public static MamResultExtension from(MessageView message) {
149            for (XmlElement extension : message.getExtensions()) {
150                if (extension instanceof MamResultExtension) {
151                    return (MamResultExtension) extension;
152                }
153            }
154
155            return null;
156        }
157
158    }
159
160    /**
161     * Always JID list element class for the MamPrefsIQ.
162     *
163     */
164    public static class AlwaysJidListElement implements Element {
165
166        /**
167         * list of JIDs.
168         */
169        private final List<Jid> alwaysJids;
170
171        /**
172         * Always JID list element constructor.
173         *
174         * @param alwaysJids TODO javadoc me please
175         */
176        AlwaysJidListElement(List<Jid> alwaysJids) {
177            this.alwaysJids = alwaysJids;
178        }
179
180        @Override
181        public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
182            XmlStringBuilder xml = new XmlStringBuilder();
183            xml.openElement("always");
184
185            for (Jid jid : alwaysJids) {
186                xml.element("jid", jid);
187            }
188
189            xml.closeElement("always");
190            return xml;
191        }
192    }
193
194    /**
195     * Never JID list element class for the MamPrefsIQ.
196     *
197     */
198    public static class NeverJidListElement implements Element {
199
200        /**
201         * list of JIDs
202         */
203        private List<Jid> neverJids;
204
205        /**
206         * Never JID list element constructor.
207         *
208         * @param neverJids TODO javadoc me please
209         */
210        public NeverJidListElement(List<Jid> neverJids) {
211            this.neverJids = neverJids;
212        }
213
214        @Override
215        public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
216            XmlStringBuilder xml = new XmlStringBuilder();
217            xml.openElement("never");
218
219            for (Jid jid : neverJids) {
220                xml.element("jid", jid);
221            }
222
223            xml.closeElement("never");
224            return xml;
225        }
226    }
227
228}