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.util.StringUtils;
020import org.jivesoftware.smackx.hoxt.HOXTManager;
021
022/**
023 * Represents Req IQ packet.
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 class HttpOverXmppReq extends AbstractHttpOverXmpp {
029
030    private Req req;
031
032    @Override
033    public String getChildElementXML() {
034        return req.toXML();
035    }
036
037    /**
038     * Returns Req element.
039     *
040     * @return Req element
041     */
042    public Req getReq() {
043        return req;
044    }
045
046    /**
047     * Sets Req element.
048     *
049     * @param req Req element
050     */
051    public void setReq(Req req) {
052        this.req = req;
053    }
054
055    /**
056     * Represents Req element.
057     */
058    public static class Req extends AbstractBody {
059
060        private HttpMethod method;
061        private String resource;
062
063        // TODO: validate:  xs:minInclusive value='256' xs:maxInclusive value='65536'
064        private int maxChunkSize = 0; // 0 means not set
065
066        private boolean sipub = true;
067
068        private boolean ibb = true;
069        private boolean jingle = true;
070
071        /**
072         * Creates this object.
073         *
074         * @param method   method attribute
075         * @param resource resource attribute
076         */
077        public Req(HttpMethod method, String resource) {
078            this.method = method;
079            this.resource = resource;
080        }
081
082        @Override
083        protected String getStartTag() {
084            StringBuilder builder = new StringBuilder();
085            builder.append("<req");
086            builder.append(" ");
087            builder.append("xmlns='").append(HOXTManager.NAMESPACE).append("'");
088            builder.append(" ");
089            builder.append("method='").append(method.toString()).append("'");
090            builder.append(" ");
091            builder.append("resource='").append(StringUtils.escapeForXML(resource)).append("'");
092            builder.append(" ");
093            builder.append("version='").append(StringUtils.escapeForXML(version)).append("'");
094            if (maxChunkSize != 0) {
095                builder.append(" ");
096                builder.append("maxChunkSize='").append(Integer.toString(maxChunkSize)).append("'");
097            }
098            builder.append(" ");
099            builder.append("sipub='").append(Boolean.toString(sipub)).append("'");
100            builder.append(" ");
101            builder.append("ibb='").append(Boolean.toString(ibb)).append("'");
102            builder.append(" ");
103            builder.append("jingle='").append(Boolean.toString(jingle)).append("'");
104            builder.append(">");
105            return builder.toString();
106        }
107
108        @Override
109        protected String getEndTag() {
110            return "</req>";
111        }
112
113        /**
114         * Returns method attribute.
115         *
116         * @return method attribute
117         */
118        public HttpMethod getMethod() {
119            return method;
120        }
121
122        /**
123         * Returns resource attribute.
124         *
125         * @return resource attribute
126         */
127        public String getResource() {
128            return resource;
129        }
130
131        /**
132         * Returns maxChunkSize attribute.
133         *
134         * @return maxChunkSize attribute
135         */
136        public int getMaxChunkSize() {
137            return maxChunkSize;
138        }
139
140        /**
141         * Sets maxChunkSize attribute.
142         *
143         * @param maxChunkSize maxChunkSize attribute
144         */
145        public void setMaxChunkSize(int maxChunkSize) {
146            this.maxChunkSize = maxChunkSize;
147        }
148
149        /**
150         * Returns sipub attribute.
151         *
152         * @return sipub attribute
153         */
154        public boolean isSipub() {
155            return sipub;
156        }
157
158        /**
159         * Sets sipub attribute.
160         *
161         * @param sipub sipub attribute
162         */
163        public void setSipub(boolean sipub) {
164            this.sipub = sipub;
165        }
166
167        /**
168         * Returns ibb attribute.
169         *
170         * @return ibb attribute
171         */
172        public boolean isIbb() {
173            return ibb;
174        }
175
176        /**
177         * Sets ibb attribute.
178         *
179         * @param ibb ibb attribute
180         */
181        public void setIbb(boolean ibb) {
182            this.ibb = ibb;
183        }
184
185        /**
186         * Returns jingle attribute.
187         *
188         * @return jingle attribute
189         */
190        public boolean isJingle() {
191            return jingle;
192        }
193
194        /**
195         * Sets jingle attribute.
196         *
197         * @param jingle jingle attribute
198         */
199        public void setJingle(boolean jingle) {
200            this.jingle = jingle;
201        }
202    }
203}