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 java.io.IOException;
020
021import org.jivesoftware.smack.SmackException;
022import org.jivesoftware.smackx.hoxt.packet.HttpMethod;
023import org.jivesoftware.smackx.hoxt.packet.HttpOverXmppReq;
024import org.xmlpull.v1.XmlPullParser;
025import org.xmlpull.v1.XmlPullParserException;
026
027/**
028 * Req stanza(/packet) provider.
029 *
030 * @author Andriy Tsykholyas
031 * @see <a href="http://xmpp.org/extensions/xep-0332.html">XEP-0332: HTTP over XMPP transport</a>
032 */
033public class HttpOverXmppReqProvider extends AbstractHttpOverXmppProvider<HttpOverXmppReq> {
034
035    private static final String ATTRIBUTE_METHOD = "method";
036    private static final String ATTRIBUTE_RESOURCE = "resource";
037    private static final String ATTRIBUTE_MAX_CHUNK_SIZE = "maxChunkSize";
038
039    @Override
040    public HttpOverXmppReq parse(XmlPullParser parser, int initialDepth)
041                    throws XmlPullParserException, IOException, SmackException {
042        String method = parser.getAttributeValue("", ATTRIBUTE_METHOD);
043        String resource = parser.getAttributeValue("", ATTRIBUTE_RESOURCE);
044        String version = parser.getAttributeValue("", ATTRIBUTE_VERSION);
045        String maxChunkSize = parser.getAttributeValue("", ATTRIBUTE_MAX_CHUNK_SIZE);
046
047        HttpMethod reqMethod = HttpMethod.valueOf(method);
048        HttpOverXmppReq req = new HttpOverXmppReq(reqMethod, resource);
049        req.setVersion(version);
050
051        Boolean sipub = true;
052        Boolean jingle = true;
053        Boolean ibb = true;
054
055        String sipubStr = parser.getAttributeValue("", AbstractHttpOverXmppProvider.ELEMENT_SIPUB);
056        String ibbStr = parser.getAttributeValue("", AbstractHttpOverXmppProvider.ELEMENT_IBB);
057        String jingleStr = parser.getAttributeValue("", AbstractHttpOverXmppProvider.ELEMENT_JINGLE);
058
059        if (sipubStr != null) {
060            sipub = Boolean.valueOf(sipubStr);
061        }
062        if (ibbStr != null) {
063            ibb = Boolean.valueOf(ibbStr);
064        }
065        if (jingleStr != null) {
066            jingle = Boolean.valueOf(jingleStr);
067        }
068
069        req.setIbb(ibb);
070        req.setSipub(sipub);
071        req.setJingle(jingle);
072
073        if (maxChunkSize != null) {
074            int maxChunkSizeValue = Integer.parseInt(maxChunkSize);
075            req.setMaxChunkSize(maxChunkSizeValue);
076        }
077
078        parseHeadersAndData(parser, HttpOverXmppReq.ELEMENT, req);
079        return req;
080    }
081}