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 org.jivesoftware.smack.packet.IQ;
020import org.jivesoftware.smack.provider.IQProvider;
021import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
022import org.xmlpull.v1.XmlPullParser;
023
024/**
025 * Parses a bytestream packet.
026 * 
027 * @author Alexander Wenckus
028 */
029public class BytestreamsProvider implements IQProvider {
030
031    public IQ parseIQ(XmlPullParser parser) throws Exception {
032        boolean done = false;
033
034        Bytestream toReturn = new Bytestream();
035
036        String id = parser.getAttributeValue("", "sid");
037        String mode = parser.getAttributeValue("", "mode");
038
039        // streamhost
040        String JID = null;
041        String host = null;
042        String port = null;
043
044        int eventType;
045        String elementName;
046        while (!done) {
047            eventType = parser.next();
048            elementName = parser.getName();
049            if (eventType == XmlPullParser.START_TAG) {
050                if (elementName.equals(Bytestream.StreamHost.ELEMENTNAME)) {
051                    JID = parser.getAttributeValue("", "jid");
052                    host = parser.getAttributeValue("", "host");
053                    port = parser.getAttributeValue("", "port");
054                }
055                else if (elementName.equals(Bytestream.StreamHostUsed.ELEMENTNAME)) {
056                    toReturn.setUsedHost(parser.getAttributeValue("", "jid"));
057                }
058                else if (elementName.equals(Bytestream.Activate.ELEMENTNAME)) {
059                    toReturn.setToActivate(parser.getAttributeValue("", "jid"));
060                }
061            }
062            else if (eventType == XmlPullParser.END_TAG) {
063                if (elementName.equals("streamhost")) {
064                    if (port == null) {
065                        toReturn.addStreamHost(JID, host);
066                    }
067                    else {
068                        toReturn.addStreamHost(JID, host, Integer.parseInt(port));
069                    }
070                    JID = null;
071                    host = null;
072                    port = null;
073                }
074                else if (elementName.equals("query")) {
075                    done = true;
076                }
077            }
078        }
079
080        toReturn.setMode((Bytestream.Mode.fromName(mode)));
081        toReturn.setSessionID(id);
082        return toReturn;
083    }
084
085}