AbstractHttpOverXmpp.java

  1. /**
  2.  *
  3.  * Copyright 2014 Andriy Tsykholyas
  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. import org.jivesoftware.smack.packet.IQ;
  19. import org.jivesoftware.smack.packet.NamedElement;
  20. import org.jivesoftware.smack.util.XmlStringBuilder;
  21. import org.jivesoftware.smackx.shim.packet.HeadersExtension;

  22. /**
  23.  * Abstract parent for Req and Resp IQ packets.
  24.  *
  25.  * @author Andriy Tsykholyas
  26.  * @see <a href="http://xmpp.org/extensions/xep-0332.html">XEP-0332: HTTP over XMPP transport</a>
  27.  */
  28. public abstract class AbstractHttpOverXmpp extends IQ {

  29.     public static final String NAMESPACE = "urn:xmpp:http";

  30.     protected AbstractHttpOverXmpp(String element) {
  31.         super(element, NAMESPACE);
  32.     }

  33.     private HeadersExtension headers;
  34.     private Data data;

  35.     protected String version;

  36.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  37.         IQChildElementXmlStringBuilder builder = getIQHoxtChildElementBuilder(xml);
  38.         builder.append(headers.toXML());
  39.         builder.append(data.toXML());

  40.         return builder;
  41.     }

  42.     /**
  43.      * Returns start tag.
  44.      *
  45.      * @return start tag
  46.      */
  47.     protected abstract IQChildElementXmlStringBuilder getIQHoxtChildElementBuilder(IQChildElementXmlStringBuilder xml);

  48.     /**
  49.      * Returns version attribute.
  50.      *
  51.      * @return version attribute
  52.      */
  53.     public String getVersion() {
  54.         return version;
  55.     }

  56.     /**
  57.      * Sets version attribute.
  58.      *
  59.      * @param version version attribute
  60.      */
  61.     public void setVersion(String version) {
  62.         this.version = version;
  63.     }

  64.     /**
  65.      * Returns Headers element.
  66.      *
  67.      * @return Headers element
  68.      */
  69.     public HeadersExtension getHeaders() {
  70.         return headers;
  71.     }

  72.     /**
  73.      * Sets Headers element.
  74.      *
  75.      * @param headers Headers element
  76.      */
  77.     public void setHeaders(HeadersExtension headers) {
  78.         this.headers = headers;
  79.     }

  80.     /**
  81.      * Returns Data element.
  82.      *
  83.      * @return Data element
  84.      */
  85.     public Data getData() {
  86.         return data;
  87.     }

  88.     /**
  89.      * Sets Data element.
  90.      *
  91.      * @param data Headers element
  92.      */
  93.     public void setData(Data data) {
  94.         this.data = data;
  95.     }

  96.     /**
  97.      * Representation of Data element.<p>
  98.      * This class is immutable.
  99.      */
  100.     public static class Data {

  101.         private final NamedElement child;

  102.         /**
  103.          * Creates Data element.
  104.          *
  105.          * @param child element nested by Data
  106.          */
  107.         public Data(NamedElement child) {
  108.             this.child = child;
  109.         }

  110.         /**
  111.          * Returns string containing xml representation of this object.
  112.          *
  113.          * @return xml representation of this object
  114.          */
  115.         public String toXML() {
  116.             StringBuilder builder = new StringBuilder();
  117.             builder.append("<data>");
  118.             builder.append(child.toXML());
  119.             builder.append("</data>");
  120.             return builder.toString();
  121.         }

  122.         /**
  123.          * Returns element nested by Data.
  124.          *
  125.          * @return element nested by Data
  126.          */
  127.         public NamedElement getChild() {
  128.             return child;
  129.         }
  130.     }

  131.     /**
  132.      * Representation of Text element.<p>
  133.      * This class is immutable.
  134.      */
  135.     public static class Text implements NamedElement {

  136.         public static final String ELEMENT = "text";

  137.         private final String text;

  138.         /**
  139.          * Creates this element.
  140.          *
  141.          * @param text value of text
  142.          */
  143.         public Text(String text) {
  144.             this.text = text;
  145.         }

  146.         @Override
  147.         public XmlStringBuilder toXML() {
  148.             XmlStringBuilder xml = new XmlStringBuilder(this);
  149.             xml.rightAngleBracket();
  150.             xml.optAppend(text);
  151.             xml.closeElement(this);
  152.             return xml;
  153.         }

  154.         /**
  155.          * Returns text of this element.
  156.          *
  157.          * @return text
  158.          */
  159.         public String getText() {
  160.             return text;
  161.         }

  162.         @Override
  163.         public String getElementName() {
  164.             return ELEMENT;
  165.         }
  166.     }

  167.     /**
  168.      * Representation of Base64 element.<p>
  169.      * This class is immutable.
  170.      */
  171.     public static class Base64 implements NamedElement {

  172.         public static final String ELEMENT = "base64";

  173.         private final String text;

  174.         /**
  175.          * Creates this element.
  176.          *
  177.          * @param text value of text
  178.          */
  179.         public Base64(String text) {
  180.             this.text = text;
  181.         }

  182.         @Override
  183.         public XmlStringBuilder toXML() {
  184.             XmlStringBuilder xml = new XmlStringBuilder(this);
  185.             xml.rightAngleBracket();
  186.             xml.optAppend(text);
  187.             xml.closeElement(this);
  188.             return xml;
  189.         }

  190.         /**
  191.          * Returns text of this element.
  192.          *
  193.          * @return text
  194.          */
  195.         public String getText() {
  196.             return text;
  197.         }

  198.         @Override
  199.         public String getElementName() {
  200.             return ELEMENT;
  201.         }
  202.     }

  203.     /**
  204.      * Representation of Xml element.<p>
  205.      * This class is immutable.
  206.      */
  207.     public static class Xml implements NamedElement {

  208.         public static final String ELEMENT = "xml";

  209.         private final String text;

  210.         /**
  211.          * Creates this element.builder.toString();
  212.          *
  213.          * @param text value of text
  214.          */
  215.         public Xml(String text) {
  216.             this.text = text;
  217.         }

  218.         @Override
  219.         public XmlStringBuilder toXML() {
  220.             XmlStringBuilder xml = new XmlStringBuilder(this);
  221.             xml.rightAngleBracket();
  222.             xml.optAppend(text);
  223.             xml.closeElement(this);
  224.             return xml;
  225.         }

  226.         /**
  227.          * Returns text of this element.
  228.          *
  229.          * @return text
  230.          */
  231.         public String getText() {
  232.             return text;
  233.         }

  234.         @Override
  235.         public String getElementName() {
  236.             return ELEMENT;
  237.         }
  238.     }

  239.     /**
  240.      * Representation of ChunkedBase64 element.<p>
  241.      * This class is immutable.
  242.      */
  243.     public static class ChunkedBase64 implements NamedElement {

  244.         public static final String ELEMENT = "chunkedBase64";

  245.         private final String streamId;

  246.         /**
  247.          * Creates ChunkedBase86 element.
  248.          *
  249.          * @param streamId streamId attribute
  250.          */
  251.         public ChunkedBase64(String streamId) {
  252.             this.streamId = streamId;
  253.         }

  254.         @Override
  255.         public XmlStringBuilder toXML() {
  256.             XmlStringBuilder xml = new XmlStringBuilder(this);
  257.             xml.attribute("streamId", streamId);
  258.             xml.closeEmptyElement();
  259.             return xml;
  260.         }

  261.         /**
  262.          * Returns streamId attribute.
  263.          *
  264.          * @return streamId attribute
  265.          */
  266.         public String getStreamId() {
  267.             return streamId;
  268.         }

  269.         @Override
  270.         public String getElementName() {
  271.             return ELEMENT;
  272.         }
  273.     }

  274.     /**
  275.      * Representation of Ibb element.<p>
  276.      * This class is immutable.
  277.      */
  278.     public static class Ibb implements NamedElement {

  279.         public static final String ELEMENT = "ibb";

  280.         private final String sid;

  281.         /**
  282.          * Creates Ibb element.
  283.          *
  284.          * @param sid sid attribute
  285.          */
  286.         public Ibb(String sid) {
  287.             this.sid = sid;
  288.         }

  289.         @Override
  290.         public XmlStringBuilder toXML() {
  291.             XmlStringBuilder xml = new XmlStringBuilder(this);
  292.             xml.attribute("sid", sid);
  293.             xml.closeEmptyElement();
  294.             return xml;
  295.         }

  296.         /**
  297.          * Returns sid attribute.
  298.          *
  299.          * @return sid attribute
  300.          */
  301.         public String getSid() {
  302.             return sid;
  303.         }

  304.         @Override
  305.         public String getElementName() {
  306.             return ELEMENT;
  307.         }
  308.     }
  309. }