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.smack.packet.IQ;
020import org.jivesoftware.smackx.hoxt.packet.HttpMethod;
021import org.jivesoftware.smackx.hoxt.packet.HttpOverXmppReq;
022import org.xmlpull.v1.XmlPullParser;
023
024/**
025 * Req packet 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 {
031
032    private static final String ELEMENT_REQ = "req";
033
034    private static final String ATTRIBUTE_METHOD = "method";
035    private static final String ATTRIBUTE_RESOURCE = "resource";
036    private static final String ATTRIBUTE_MAX_CHUNK_SIZE = "maxChunkSize";
037
038    /**
039     * Mandatory no argument constructor.
040     */
041    public HttpOverXmppReqProvider() {
042    }
043
044    @Override
045    public IQ parseIQ(XmlPullParser parser) throws Exception {
046        String method = parser.getAttributeValue("", ATTRIBUTE_METHOD);
047        String resource = parser.getAttributeValue("", ATTRIBUTE_RESOURCE);
048        String version = parser.getAttributeValue("", ATTRIBUTE_VERSION);
049        String maxChunkSize = parser.getAttributeValue("", ATTRIBUTE_MAX_CHUNK_SIZE);
050
051        HttpMethod reqMethod = HttpMethod.valueOf(method);
052        HttpOverXmppReq.Req req = new HttpOverXmppReq.Req(reqMethod, resource);
053        req.setVersion(version);
054
055        Boolean sipub = true;
056        Boolean jingle = true;
057        Boolean ibb = true;
058
059        String sipubStr = parser.getAttributeValue("", AbstractHttpOverXmppProvider.ELEMENT_SIPUB);
060        String ibbStr = parser.getAttributeValue("", AbstractHttpOverXmppProvider.ELEMENT_IBB);
061        String jingleStr = parser.getAttributeValue("", AbstractHttpOverXmppProvider.ELEMENT_JINGLE);
062
063        if (sipubStr != null) {
064            sipub = Boolean.valueOf(sipubStr);
065        }
066        if (ibbStr != null) {
067            ibb = Boolean.valueOf(ibbStr);
068        }
069        if (jingleStr != null) {
070            jingle = Boolean.valueOf(jingleStr);
071        }
072
073        req.setIbb(ibb);
074        req.setSipub(sipub);
075        req.setJingle(jingle);
076
077        if (maxChunkSize != null) {
078            int maxChunkSizeValue = Integer.parseInt(maxChunkSize);
079            req.setMaxChunkSize(maxChunkSizeValue);
080        }
081
082        parseHeadersAndData(parser, ELEMENT_REQ, req);
083        HttpOverXmppReq packet = new HttpOverXmppReq();
084        packet.setReq(req);
085        return packet;
086    }
087}