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