HttpOverXmppReq.java

  1. /**
  2.  *
  3.  * Copyright 2014 Andriy Tsykholyas, 2015 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.hoxt.packet;

  18. /**
  19.  * Represents Req IQ packet.
  20.  *
  21.  * @author Andriy Tsykholyas
  22.  * @see <a href="http://xmpp.org/extensions/xep-0332.html">XEP-0332: HTTP over XMPP transport</a>
  23.  */
  24. public final class HttpOverXmppReq extends AbstractHttpOverXmpp {

  25.     public static final String ELEMENT = "req";

  26.     private HttpOverXmppReq(Builder builder) {
  27.         super(ELEMENT, builder);
  28.         this.method = builder.method;
  29.         this.resource = builder.resource;
  30.         this.maxChunkSize = builder.maxChunkSize;
  31.         this.ibb = builder.ibb;
  32.         this.jingle = builder.jingle;
  33.         this.sipub = builder.sipub;
  34.         setType(Type.set);
  35.     }

  36.     private final HttpMethod method;
  37.     private final String resource;

  38.     private final int maxChunkSize;

  39.     private final boolean sipub;

  40.     private final boolean ibb;
  41.     private final boolean jingle;

  42.     @Override
  43.     protected IQChildElementXmlStringBuilder getIQHoxtChildElementBuilder(IQChildElementXmlStringBuilder builder) {
  44.         builder.attribute("method", method);
  45.         builder.attribute("resource", resource);
  46.         builder.attribute("version", getVersion());
  47.         builder.optIntAttribute("maxChunkSize", maxChunkSize);
  48.         builder.optBooleanAttributeDefaultTrue("sipub", sipub);
  49.         builder.optBooleanAttributeDefaultTrue("ibb", ibb);
  50.         builder.optBooleanAttributeDefaultTrue("jingle", jingle);
  51.         builder.rightAngleBracket();
  52.         return builder;
  53.     }

  54.     /**
  55.      * Returns method attribute.
  56.      *
  57.      * @return method attribute
  58.      */
  59.     public HttpMethod getMethod() {
  60.         return method;
  61.     }

  62.     /**
  63.      * Returns resource attribute.
  64.      *
  65.      * @return resource attribute
  66.      */
  67.     public String getResource() {
  68.         return resource;
  69.     }

  70.     /**
  71.      * Returns maxChunkSize attribute.
  72.      *
  73.      * @return maxChunkSize attribute
  74.      */
  75.     public int getMaxChunkSize() {
  76.         return maxChunkSize;
  77.     }

  78.     /**
  79.      * Returns sipub attribute.
  80.      *
  81.      * @return sipub attribute
  82.      */
  83.     public boolean isSipub() {
  84.         return sipub;
  85.     }

  86.     /**
  87.      * Returns ibb attribute.
  88.      *
  89.      * @return ibb attribute
  90.      */
  91.     public boolean isIbb() {
  92.         return ibb;
  93.     }

  94.     /**
  95.      * Returns jingle attribute.
  96.      *
  97.      * @return jingle attribute
  98.      */
  99.     public boolean isJingle() {
  100.         return jingle;
  101.     }

  102.     public static Builder builder() {
  103.         return new Builder();
  104.     }

  105.     /**
  106.      * A configuration builder for HttpOverXmppReq. Use {@link HttpOverXmppReq#builder()} to obtain a new instance and
  107.      * {@link #build} to build the configuration.
  108.      */
  109.     public static final class Builder extends AbstractHttpOverXmpp.Builder<Builder, HttpOverXmppReq> {

  110.         private HttpMethod method;
  111.         private String resource;

  112.         private int maxChunkSize = -1;

  113.         private boolean sipub = true;

  114.         private boolean ibb = true;
  115.         private boolean jingle = true;

  116.         private Builder() {
  117.         }

  118.         /**
  119.          * Sets method attribute.
  120.          *
  121.          * @param method attribute
  122.          *
  123.          * @return the builder
  124.          */
  125.         public Builder setMethod(HttpMethod method) {
  126.             this.method = method;
  127.             return this;
  128.         }

  129.         /**
  130.          * Sets resource attribute.
  131.          *
  132.          * @param resource attribute
  133.          *
  134.          * @return the builder
  135.          */
  136.         public Builder setResource(String resource) {
  137.             this.resource = resource;
  138.             return this;
  139.         }

  140.         /**
  141.          * Sets jingle attribute.
  142.          *
  143.          * @param jingle jingle attribute
  144.          *
  145.          * @return the builder
  146.          */
  147.         public Builder setJingle(boolean jingle) {
  148.             this.jingle = jingle;
  149.             return this;
  150.         }

  151.         /**
  152.          * Sets ibb attribute.
  153.          *
  154.          * @param ibb ibb attribute
  155.          *
  156.          * @return the builder
  157.          */
  158.         public Builder setIbb(boolean ibb) {
  159.             this.ibb = ibb;
  160.             return this;
  161.         }

  162.         /**
  163.          * Sets sipub attribute.
  164.          *
  165.          * @param sipub sipub attribute
  166.          *
  167.          * @return the builder
  168.          */
  169.         public Builder setSipub(boolean sipub) {
  170.             this.sipub = sipub;
  171.             return this;
  172.         }

  173.         /**
  174.          * Sets maxChunkSize attribute.
  175.          *
  176.          * @param maxChunkSize maxChunkSize attribute
  177.          *
  178.          * @return the builder
  179.          */
  180.         public Builder setMaxChunkSize(int maxChunkSize) {
  181.             if (maxChunkSize < 256 || maxChunkSize > 65536) {
  182.                 throw new IllegalArgumentException("maxChunkSize must be within [256, 65536]");
  183.             }
  184.             this.maxChunkSize = maxChunkSize;
  185.             return this;
  186.         }

  187.         @Override
  188.         public HttpOverXmppReq build() {
  189.             if (method == null) {
  190.                 throw new IllegalArgumentException("Method cannot be null");
  191.             }
  192.             if (resource == null) {
  193.                 throw new IllegalArgumentException("Resource cannot be null");
  194.             }
  195.             return new HttpOverXmppReq(this);
  196.         }

  197.         @Override
  198.         protected Builder getThis() {
  199.             return this;
  200.         }

  201.     }
  202. }