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