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