001/**
002 *
003 * Copyright the original author or authors
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.bytestreams.socks5.provider;
018
019import java.io.IOException;
020
021import org.jivesoftware.smack.packet.XmlEnvironment;
022import org.jivesoftware.smack.provider.IQProvider;
023import org.jivesoftware.smack.util.ParserUtils;
024import org.jivesoftware.smack.xml.XmlPullParser;
025import org.jivesoftware.smack.xml.XmlPullParserException;
026
027import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
028import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.Mode;
029
030import org.jxmpp.jid.Jid;
031
032/**
033 * Parses a bytestream packet.
034 *
035 * @author Alexander Wenckus
036 */
037public class BytestreamsProvider extends IQProvider<Bytestream> {
038
039    @Override
040    public Bytestream parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
041                    throws XmlPullParserException, IOException {
042        boolean done = false;
043
044        Bytestream toReturn = new Bytestream();
045
046        String id = parser.getAttributeValue("", "sid");
047        String mode = parser.getAttributeValue("", "mode");
048
049        // streamhost
050        Jid JID = null;
051        String host = null;
052        String port = null;
053
054        XmlPullParser.Event eventType;
055        String elementName;
056        while (!done) {
057            eventType = parser.next();
058            elementName = parser.getName();
059            if (eventType == XmlPullParser.Event.START_ELEMENT) {
060                if (elementName.equals(Bytestream.StreamHost.ELEMENTNAME)) {
061                    JID = ParserUtils.getJidAttribute(parser);
062                    host = parser.getAttributeValue("", "host");
063                    port = parser.getAttributeValue("", "port");
064                }
065                else if (elementName.equals(Bytestream.StreamHostUsed.ELEMENTNAME)) {
066                    toReturn.setUsedHost(ParserUtils.getJidAttribute(parser));
067                }
068                else if (elementName.equals(Bytestream.Activate.ELEMENTNAME)) {
069                    toReturn.setToActivate(ParserUtils.getJidAttribute(parser));
070                }
071            }
072            else if (eventType == XmlPullParser.Event.END_ELEMENT) {
073                if (elementName.equals("streamhost")) {
074                    if (port == null) {
075                        toReturn.addStreamHost(JID, host);
076                    }
077                    else {
078                        toReturn.addStreamHost(JID, host, Integer.parseInt(port));
079                    }
080                    JID = null;
081                    host = null;
082                    port = null;
083                }
084                else if (elementName.equals("query")) {
085                    done = true;
086                }
087            }
088        }
089
090        if (mode == null) {
091            toReturn.setMode(Mode.tcp);
092        } else {
093            toReturn.setMode(Bytestream.Mode.fromName(mode));
094        }
095        toReturn.setSessionID(id);
096        return toReturn;
097    }
098
099}