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