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