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