001/**
002 *
003 * Copyright 2014 Andriy Tsykholyas, 2015 Florian Schmaus
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
019/**
020 * Represents Req IQ packet.
021 *
022 * @author Andriy Tsykholyas
023 * @see <a href="http://xmpp.org/extensions/xep-0332.html">XEP-0332: HTTP over XMPP transport</a>
024 */
025public final class HttpOverXmppReq extends AbstractHttpOverXmpp {
026
027    public static final String ELEMENT = "req";
028
029    private HttpOverXmppReq(Builder builder) {
030        super(ELEMENT, builder);
031        this.method = builder.method;
032        this.resource = builder.resource;
033        this.maxChunkSize = builder.maxChunkSize;
034        this.ibb = builder.ibb;
035        this.jingle = builder.jingle;
036        this.sipub = builder.sipub;
037        setType(Type.set);
038    }
039
040    private final HttpMethod method;
041    private final String resource;
042
043    private final int maxChunkSize;
044
045    private final boolean sipub;
046
047    private final boolean ibb;
048    private final boolean jingle;
049
050    @Override
051    protected IQChildElementXmlStringBuilder getIQHoxtChildElementBuilder(IQChildElementXmlStringBuilder builder) {
052        builder.attribute("method", method);
053        builder.attribute("resource", resource);
054        builder.attribute("version", getVersion());
055        builder.optIntAttribute("maxChunkSize", maxChunkSize);
056        builder.optBooleanAttributeDefaultTrue("sipub", sipub);
057        builder.optBooleanAttributeDefaultTrue("ibb", ibb);
058        builder.optBooleanAttributeDefaultTrue("jingle", jingle);
059        builder.rightAngleBracket();
060        return builder;
061    }
062
063    /**
064     * Returns method attribute.
065     *
066     * @return method attribute
067     */
068    public HttpMethod getMethod() {
069        return method;
070    }
071
072    /**
073     * Returns resource attribute.
074     *
075     * @return resource attribute
076     */
077    public String getResource() {
078        return resource;
079    }
080
081    /**
082     * Returns maxChunkSize attribute.
083     *
084     * @return maxChunkSize attribute
085     */
086    public int getMaxChunkSize() {
087        return maxChunkSize;
088    }
089
090    /**
091     * Returns sipub attribute.
092     *
093     * @return sipub attribute
094     */
095    public boolean isSipub() {
096        return sipub;
097    }
098
099    /**
100     * Returns ibb attribute.
101     *
102     * @return ibb attribute
103     */
104    public boolean isIbb() {
105        return ibb;
106    }
107
108    /**
109     * Returns jingle attribute.
110     *
111     * @return jingle attribute
112     */
113    public boolean isJingle() {
114        return jingle;
115    }
116
117    public static Builder builder() {
118        return new Builder();
119    }
120
121    /**
122     * A configuration builder for HttpOverXmppReq. Use {@link HttpOverXmppReq#builder()} to obtain a new instance and
123     * {@link #build} to build the configuration.
124     */
125    public static final class Builder extends AbstractHttpOverXmpp.Builder<Builder, HttpOverXmppReq> {
126
127        private HttpMethod method;
128        private String resource;
129
130        private int maxChunkSize = -1;
131
132        private boolean sipub = true;
133
134        private boolean ibb = true;
135        private boolean jingle = true;
136
137        private Builder() {
138        }
139
140        /**
141         * Sets method attribute.
142         *
143         * @param method attribute
144         *
145         * @return the builder
146         */
147        public Builder setMethod(HttpMethod method) {
148            this.method = method;
149            return this;
150        }
151
152        /**
153         * Sets resource attribute.
154         *
155         * @param resource attribute
156         *
157         * @return the builder
158         */
159        public Builder setResource(String resource) {
160            this.resource = resource;
161            return this;
162        }
163
164        /**
165         * Sets jingle attribute.
166         *
167         * @param jingle jingle attribute
168         *
169         * @return the builder
170         */
171        public Builder setJingle(boolean jingle) {
172            this.jingle = jingle;
173            return this;
174        }
175
176        /**
177         * Sets ibb attribute.
178         *
179         * @param ibb ibb attribute
180         *
181         * @return the builder
182         */
183        public Builder setIbb(boolean ibb) {
184            this.ibb = ibb;
185            return this;
186        }
187
188        /**
189         * Sets sipub attribute.
190         *
191         * @param sipub sipub attribute
192         *
193         * @return the builder
194         */
195        public Builder setSipub(boolean sipub) {
196            this.sipub = sipub;
197            return this;
198        }
199
200        /**
201         * Sets maxChunkSize attribute.
202         *
203         * @param maxChunkSize maxChunkSize attribute
204         *
205         * @return the builder
206         */
207        public Builder setMaxChunkSize(int maxChunkSize) {
208            if (maxChunkSize < 256 || maxChunkSize > 65536) {
209                throw new IllegalArgumentException("maxChunkSize must be within [256, 65536]");
210            }
211            this.maxChunkSize = maxChunkSize;
212            return this;
213        }
214
215        @Override
216        public HttpOverXmppReq build() {
217            if (method == null) {
218                throw new IllegalArgumentException("Method cannot be null");
219            }
220            if (resource == null) {
221                throw new IllegalArgumentException("Resource cannot be null");
222            }
223            return new HttpOverXmppReq(this);
224        }
225
226        @Override
227        protected Builder getThis() {
228            return this;
229        }
230
231    }
232}