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            if (eventType == XmlPullParser.Event.START_ELEMENT) {
059                elementName = parser.getName();
060                if (elementName.equals(Bytestream.StreamHost.ELEMENT)) {
061                    JID = ParserUtils.getJidAttribute(parser);
062                    host = parser.getAttributeValue("", "host");
063                    port = parser.getAttributeValue("", "port");
064                }
065                else if (elementName.equals(Bytestream.StreamHostUsed.ELEMENT)) {
066                    toReturn.setUsedHost(ParserUtils.getJidAttribute(parser));
067                }
068                else if (elementName.equals(Bytestream.Activate.ELEMENT)) {
069                    toReturn.setToActivate(ParserUtils.getJidAttribute(parser));
070                }
071            }
072            else if (eventType == XmlPullParser.Event.END_ELEMENT) {
073                elementName = parser.getName();
074                if (elementName.equals("streamhost")) {
075                    if (port == null) {
076                        toReturn.addStreamHost(JID, host);
077                    }
078                    else {
079                        toReturn.addStreamHost(JID, host, Integer.parseInt(port));
080                    }
081                    JID = null;
082                    host = null;
083                    port = null;
084                }
085                else if (elementName.equals("query")) {
086                    done = true;
087                }
088            }
089        }
090
091        if (mode == null) {
092            toReturn.setMode(Mode.tcp);
093        } else {
094            toReturn.setMode(Bytestream.Mode.fromName(mode));
095        }
096        toReturn.setSessionID(id);
097        return toReturn;
098    }
099
100}