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