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