HttpOverXmppReqProvider.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.provider;

  18. import org.jivesoftware.smackx.hoxt.packet.HttpMethod;
  19. import org.jivesoftware.smackx.hoxt.packet.HttpOverXmppReq;
  20. import org.xmlpull.v1.XmlPullParser;

  21. /**
  22.  * Req packet provider.
  23.  *
  24.  * @author Andriy Tsykholyas
  25.  * @see <a href="http://xmpp.org/extensions/xep-0332.html">XEP-0332: HTTP over XMPP transport</a>
  26.  */
  27. public class HttpOverXmppReqProvider extends AbstractHttpOverXmppProvider<HttpOverXmppReq> {

  28.     private static final String ATTRIBUTE_METHOD = "method";
  29.     private static final String ATTRIBUTE_RESOURCE = "resource";
  30.     private static final String ATTRIBUTE_MAX_CHUNK_SIZE = "maxChunkSize";

  31.     @Override
  32.     public HttpOverXmppReq parse(XmlPullParser parser, int initialDepth)
  33.                     throws Exception {
  34.         String method = parser.getAttributeValue("", ATTRIBUTE_METHOD);
  35.         String resource = parser.getAttributeValue("", ATTRIBUTE_RESOURCE);
  36.         String version = parser.getAttributeValue("", ATTRIBUTE_VERSION);
  37.         String maxChunkSize = parser.getAttributeValue("", ATTRIBUTE_MAX_CHUNK_SIZE);

  38.         HttpMethod reqMethod = HttpMethod.valueOf(method);
  39.         HttpOverXmppReq req = new HttpOverXmppReq(reqMethod, resource);
  40.         req.setVersion(version);

  41.         Boolean sipub = true;
  42.         Boolean jingle = true;
  43.         Boolean ibb = true;

  44.         String sipubStr = parser.getAttributeValue("", AbstractHttpOverXmppProvider.ELEMENT_SIPUB);
  45.         String ibbStr = parser.getAttributeValue("", AbstractHttpOverXmppProvider.ELEMENT_IBB);
  46.         String jingleStr = parser.getAttributeValue("", AbstractHttpOverXmppProvider.ELEMENT_JINGLE);

  47.         if (sipubStr != null) {
  48.             sipub = Boolean.valueOf(sipubStr);
  49.         }
  50.         if (ibbStr != null) {
  51.             ibb = Boolean.valueOf(ibbStr);
  52.         }
  53.         if (jingleStr != null) {
  54.             jingle = Boolean.valueOf(jingleStr);
  55.         }

  56.         req.setIbb(ibb);
  57.         req.setSipub(sipub);
  58.         req.setJingle(jingle);

  59.         if (maxChunkSize != null) {
  60.             int maxChunkSizeValue = Integer.parseInt(maxChunkSize);
  61.             req.setMaxChunkSize(maxChunkSizeValue);
  62.         }

  63.         parseHeadersAndData(parser, HttpOverXmppReq.ELEMENT, req);
  64.         return req;
  65.     }
  66. }