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