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