001/**
002 *
003 * Copyright 2014 Andriy Tsykholyas
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.hoxt.packet;
018
019import org.jivesoftware.smack.packet.IQ;
020import org.jivesoftware.smack.packet.NamedElement;
021import org.jivesoftware.smack.util.XmlStringBuilder;
022import org.jivesoftware.smackx.shim.packet.HeadersExtension;
023
024/**
025 * Abstract parent for Req and Resp IQ packets.
026 *
027 * @author Andriy Tsykholyas
028 * @see <a href="http://xmpp.org/extensions/xep-0332.html">XEP-0332: HTTP over XMPP transport</a>
029 */
030public abstract class AbstractHttpOverXmpp extends IQ {
031
032    public static final String NAMESPACE = "urn:xmpp:http";
033
034    protected AbstractHttpOverXmpp(String element) {
035        super(element, NAMESPACE);
036    }
037
038    private HeadersExtension headers;
039    private Data data;
040
041    protected String version;
042
043    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
044        IQChildElementXmlStringBuilder builder = getIQHoxtChildElementBuilder(xml);
045        builder.append(headers.toXML());
046        builder.append(data.toXML());
047
048        return builder;
049    }
050
051    /**
052     * Returns start tag.
053     *
054     * @return start tag
055     */
056    protected abstract IQChildElementXmlStringBuilder getIQHoxtChildElementBuilder(IQChildElementXmlStringBuilder xml);
057
058    /**
059     * Returns version attribute.
060     *
061     * @return version attribute
062     */
063    public String getVersion() {
064        return version;
065    }
066
067    /**
068     * Sets version attribute.
069     *
070     * @param version version attribute
071     */
072    public void setVersion(String version) {
073        this.version = version;
074    }
075
076    /**
077     * Returns Headers element.
078     *
079     * @return Headers element
080     */
081    public HeadersExtension getHeaders() {
082        return headers;
083    }
084
085    /**
086     * Sets Headers element.
087     *
088     * @param headers Headers element
089     */
090    public void setHeaders(HeadersExtension headers) {
091        this.headers = headers;
092    }
093
094    /**
095     * Returns Data element.
096     *
097     * @return Data element
098     */
099    public Data getData() {
100        return data;
101    }
102
103    /**
104     * Sets Data element.
105     *
106     * @param data Headers element
107     */
108    public void setData(Data data) {
109        this.data = data;
110    }
111
112    /**
113     * Representation of Data element.<p>
114     * This class is immutable.
115     */
116    public static class Data {
117
118        private final NamedElement child;
119
120        /**
121         * Creates Data element.
122         *
123         * @param child element nested by Data
124         */
125        public Data(NamedElement child) {
126            this.child = child;
127        }
128
129        /**
130         * Returns string containing xml representation of this object.
131         *
132         * @return xml representation of this object
133         */
134        public String toXML() {
135            StringBuilder builder = new StringBuilder();
136            builder.append("<data>");
137            builder.append(child.toXML());
138            builder.append("</data>");
139            return builder.toString();
140        }
141
142        /**
143         * Returns element nested by Data.
144         *
145         * @return element nested by Data
146         */
147        public NamedElement getChild() {
148            return child;
149        }
150    }
151
152    /**
153     * Representation of Text element.<p>
154     * This class is immutable.
155     */
156    public static class Text implements NamedElement {
157
158        public static final String ELEMENT = "text";
159
160        private final String text;
161
162        /**
163         * Creates this element.
164         *
165         * @param text value of text
166         */
167        public Text(String text) {
168            this.text = text;
169        }
170
171        @Override
172        public XmlStringBuilder toXML() {
173            XmlStringBuilder xml = new XmlStringBuilder(this);
174            xml.rightAngleBracket();
175            xml.optAppend(text);
176            xml.closeElement(this);
177            return xml;
178        }
179
180        /**
181         * Returns text of this element.
182         *
183         * @return text
184         */
185        public String getText() {
186            return text;
187        }
188
189        @Override
190        public String getElementName() {
191            return ELEMENT;
192        }
193    }
194
195    /**
196     * Representation of Base64 element.<p>
197     * This class is immutable.
198     */
199    public static class Base64 implements NamedElement {
200
201        public static final String ELEMENT = "base64";
202
203        private final String text;
204
205        /**
206         * Creates this element.
207         *
208         * @param text value of text
209         */
210        public Base64(String text) {
211            this.text = text;
212        }
213
214        @Override
215        public XmlStringBuilder toXML() {
216            XmlStringBuilder xml = new XmlStringBuilder(this);
217            xml.rightAngleBracket();
218            xml.optAppend(text);
219            xml.closeElement(this);
220            return xml;
221        }
222
223        /**
224         * Returns text of this element.
225         *
226         * @return text
227         */
228        public String getText() {
229            return text;
230        }
231
232        @Override
233        public String getElementName() {
234            return ELEMENT;
235        }
236    }
237
238    /**
239     * Representation of Xml element.<p>
240     * This class is immutable.
241     */
242    public static class Xml implements NamedElement {
243
244        public static final String ELEMENT = "xml";
245
246        private final String text;
247
248        /**
249         * Creates this element.builder.toString();
250         *
251         * @param text value of text
252         */
253        public Xml(String text) {
254            this.text = text;
255        }
256
257        @Override
258        public XmlStringBuilder toXML() {
259            XmlStringBuilder xml = new XmlStringBuilder(this);
260            xml.rightAngleBracket();
261            xml.optAppend(text);
262            xml.closeElement(this);
263            return xml;
264        }
265
266        /**
267         * Returns text of this element.
268         *
269         * @return text
270         */
271        public String getText() {
272            return text;
273        }
274
275        @Override
276        public String getElementName() {
277            return ELEMENT;
278        }
279    }
280
281    /**
282     * Representation of ChunkedBase64 element.<p>
283     * This class is immutable.
284     */
285    public static class ChunkedBase64 implements NamedElement {
286
287        public static final String ELEMENT = "chunkedBase64";
288
289        private final String streamId;
290
291        /**
292         * Creates ChunkedBase86 element.
293         *
294         * @param streamId streamId attribute
295         */
296        public ChunkedBase64(String streamId) {
297            this.streamId = streamId;
298        }
299
300        @Override
301        public XmlStringBuilder toXML() {
302            XmlStringBuilder xml = new XmlStringBuilder(this);
303            xml.attribute("streamId", streamId);
304            xml.closeEmptyElement();
305            return xml;
306        }
307
308        /**
309         * Returns streamId attribute.
310         *
311         * @return streamId attribute
312         */
313        public String getStreamId() {
314            return streamId;
315        }
316
317        @Override
318        public String getElementName() {
319            return ELEMENT;
320        }
321    }
322
323    /**
324     * Representation of Ibb element.<p>
325     * This class is immutable.
326     */
327    public static class Ibb implements NamedElement {
328
329        public static final String ELEMENT = "ibb";
330
331        private final String sid;
332
333        /**
334         * Creates Ibb element.
335         *
336         * @param sid sid attribute
337         */
338        public Ibb(String sid) {
339            this.sid = sid;
340        }
341
342        @Override
343        public XmlStringBuilder toXML() {
344            XmlStringBuilder xml = new XmlStringBuilder(this);
345            xml.attribute("sid", sid);
346            xml.closeEmptyElement();
347            return xml;
348        }
349
350        /**
351         * Returns sid attribute.
352         *
353         * @return sid attribute
354         */
355        public String getSid() {
356            return sid;
357        }
358
359        @Override
360        public String getElementName() {
361            return ELEMENT;
362        }
363    }
364}