BytestreamsProvider.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.bytestreams.socks5.provider;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.provider.IQProvider;
  20. import org.jivesoftware.smack.util.ParserUtils;
  21. import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
  22. import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.Mode;
  23. import org.jxmpp.jid.Jid;
  24. import org.xmlpull.v1.XmlPullParser;
  25. import org.xmlpull.v1.XmlPullParserException;

  26. /**
  27.  * Parses a bytestream packet.
  28.  *
  29.  * @author Alexander Wenckus
  30.  */
  31. public class BytestreamsProvider extends IQProvider<Bytestream> {

  32.     @Override
  33.     public Bytestream parse(XmlPullParser parser, int initialDepth)
  34.                     throws XmlPullParserException, IOException {
  35.         boolean done = false;

  36.         Bytestream toReturn = new Bytestream();

  37.         String id = parser.getAttributeValue("", "sid");
  38.         String mode = parser.getAttributeValue("", "mode");

  39.         // streamhost
  40.         Jid JID = null;
  41.         String host = null;
  42.         String port = null;

  43.         int eventType;
  44.         String elementName;
  45.         while (!done) {
  46.             eventType = parser.next();
  47.             elementName = parser.getName();
  48.             if (eventType == XmlPullParser.START_TAG) {
  49.                 if (elementName.equals(Bytestream.StreamHost.ELEMENTNAME)) {
  50.                     JID = ParserUtils.getJidAttribute(parser);
  51.                     host = parser.getAttributeValue("", "host");
  52.                     port = parser.getAttributeValue("", "port");
  53.                 }
  54.                 else if (elementName.equals(Bytestream.StreamHostUsed.ELEMENTNAME)) {
  55.                     toReturn.setUsedHost(ParserUtils.getJidAttribute(parser));
  56.                 }
  57.                 else if (elementName.equals(Bytestream.Activate.ELEMENTNAME)) {
  58.                     toReturn.setToActivate(ParserUtils.getJidAttribute(parser));
  59.                 }
  60.             }
  61.             else if (eventType == XmlPullParser.END_TAG) {
  62.                 if (elementName.equals("streamhost")) {
  63.                     if (port == null) {
  64.                         toReturn.addStreamHost(JID, host);
  65.                     }
  66.                     else {
  67.                         toReturn.addStreamHost(JID, host, Integer.parseInt(port));
  68.                     }
  69.                     JID = null;
  70.                     host = null;
  71.                     port = null;
  72.                 }
  73.                 else if (elementName.equals("query")) {
  74.                     done = true;
  75.                 }
  76.             }
  77.         }

  78.         if (mode == null) {
  79.             toReturn.setMode(Mode.tcp);
  80.         } else {
  81.             toReturn.setMode((Bytestream.Mode.fromName(mode)));
  82.         }
  83.         toReturn.setSessionID(id);
  84.         return toReturn;
  85.     }

  86. }