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.provider;
018
019import org.jivesoftware.smackx.hoxt.packet.HttpMethod;
020import org.jivesoftware.smackx.hoxt.packet.HttpOverXmppReq;
021
022import org.xmlpull.v1.XmlPullParser;
023
024/**
025 * Req stanza provider.
026 *
027 * @author Andriy Tsykholyas
028 * @see <a href="http://xmpp.org/extensions/xep-0332.html">XEP-0332: HTTP over XMPP transport</a>
029 */
030public class HttpOverXmppReqProvider extends AbstractHttpOverXmppProvider<HttpOverXmppReq> {
031
032    private static final String ATTRIBUTE_METHOD = "method";
033    private static final String ATTRIBUTE_RESOURCE = "resource";
034    private static final String ATTRIBUTE_MAX_CHUNK_SIZE = "maxChunkSize";
035
036    @Override
037    public HttpOverXmppReq parse(XmlPullParser parser, int initialDepth) throws Exception {
038        HttpOverXmppReq.Builder builder = HttpOverXmppReq.builder();
039        builder.setResource(parser.getAttributeValue("", ATTRIBUTE_RESOURCE));
040        builder.setVersion(parser.getAttributeValue("", ATTRIBUTE_VERSION));
041
042        String method = parser.getAttributeValue("", ATTRIBUTE_METHOD);
043        builder.setMethod(HttpMethod.valueOf(method));
044
045        String sipubStr = parser.getAttributeValue("", AbstractHttpOverXmppProvider.ELEMENT_SIPUB);
046        String ibbStr = parser.getAttributeValue("", AbstractHttpOverXmppProvider.ELEMENT_IBB);
047        String jingleStr = parser.getAttributeValue("", AbstractHttpOverXmppProvider.ELEMENT_JINGLE);
048
049        if (sipubStr != null) {
050            builder.setSipub(Boolean.valueOf(sipubStr));
051        }
052        if (ibbStr != null) {
053            builder.setIbb(Boolean.valueOf(ibbStr));
054        }
055        if (jingleStr != null) {
056            builder.setJingle(Boolean.valueOf(jingleStr));
057        }
058
059        String maxChunkSize = parser.getAttributeValue("", ATTRIBUTE_MAX_CHUNK_SIZE);
060        if (maxChunkSize != null) {
061            builder.setMaxChunkSize(Integer.parseInt(maxChunkSize));
062        }
063
064        builder.setHeaders(parseHeaders(parser));
065        builder.setData(parseData(parser));
066
067        return builder.build();
068    }
069}