JingleS5BTransportProvider.java

  1. /**
  2.  *
  3.  * Copyright 2017 Paul Schaub
  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.jingle.transports.jingle_s5b.provider;

  18. import static org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.Mode.tcp;
  19. import static org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.Mode.udp;
  20. import static org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportCandidate.ATTR_CID;
  21. import static org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportCandidate.ATTR_HOST;
  22. import static org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportCandidate.ATTR_JID;
  23. import static org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportCandidate.ATTR_PORT;
  24. import static org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportCandidate.ATTR_PRIORITY;
  25. import static org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportCandidate.ATTR_TYPE;

  26. import java.io.IOException;

  27. import org.jivesoftware.smack.packet.XmlEnvironment;
  28. import org.jivesoftware.smack.util.ParserUtils;
  29. import org.jivesoftware.smack.xml.XmlPullParser;
  30. import org.jivesoftware.smack.xml.XmlPullParserException;

  31. import org.jivesoftware.smackx.jingle.element.JingleContentTransportCandidate;
  32. import org.jivesoftware.smackx.jingle.provider.JingleContentTransportProvider;
  33. import org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransport;
  34. import org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportCandidate;
  35. import org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportInfo;
  36. import org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportInfo.JingleS5BCandidateTransportInfo;

  37. /**
  38.  * Provider for JingleSocks5BytestreamTransport elements.
  39.  */
  40. public class JingleS5BTransportProvider extends JingleContentTransportProvider<JingleS5BTransport> {

  41.     @Override
  42.     public JingleS5BTransport parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
  43.         JingleS5BTransport.Builder builder = JingleS5BTransport.getBuilder();

  44.         String streamId = parser.getAttributeValue(null, JingleS5BTransport.ATTR_SID);
  45.         builder.setStreamId(streamId);

  46.         String dstAddr = parser.getAttributeValue(null, JingleS5BTransport.ATTR_DSTADDR);
  47.         builder.setDestinationAddress(dstAddr);

  48.         String mode = parser.getAttributeValue(null, JingleS5BTransport.ATTR_MODE);
  49.         if (mode != null) {
  50.             builder.setMode(mode.equals(udp.toString()) ? udp : tcp);
  51.         }

  52.         JingleS5BTransportCandidate.Builder cb;
  53.         outerloop: while (true) {
  54.             XmlPullParser.TagEvent tag = parser.nextTag();
  55.             switch (tag) {
  56.                 case START_ELEMENT: {
  57.                     String name = parser.getName();
  58.                     switch (name) {

  59.                         case JingleContentTransportCandidate.ELEMENT:
  60.                             cb = JingleS5BTransportCandidate.getBuilder();
  61.                             cb.setCandidateId(parser.getAttributeValue(null, ATTR_CID));
  62.                             cb.setHost(ParserUtils.getInternetAddressIngoringZoneIdAttribute(parser, ATTR_HOST));
  63.                             cb.setJid(parser.getAttributeValue(null, ATTR_JID));
  64.                             cb.setPriority(Integer.parseInt(parser.getAttributeValue(null, ATTR_PRIORITY)));

  65.                             String portString = parser.getAttributeValue(null, ATTR_PORT);
  66.                             if (portString != null) {
  67.                                 cb.setPort(Integer.parseInt(portString));
  68.                             }

  69.                             String typeString = parser.getAttributeValue(null, ATTR_TYPE);
  70.                             if (typeString != null) {
  71.                                 cb.setType(JingleS5BTransportCandidate.Type.fromString(typeString));
  72.                             }
  73.                             builder.addTransportCandidate(cb.build());
  74.                             break;

  75.                         case JingleS5BTransportInfo.CandidateActivated.ELEMENT:
  76.                             builder.setTransportInfo(new JingleS5BTransportInfo.CandidateActivated(
  77.                                     parser.getAttributeValue(null,
  78.                                             JingleS5BCandidateTransportInfo.ATTR_CID)));
  79.                             break;

  80.                         case JingleS5BTransportInfo.CandidateUsed.ELEMENT:
  81.                             builder.setTransportInfo(new JingleS5BTransportInfo.CandidateUsed(
  82.                                     parser.getAttributeValue(null,
  83.                                             JingleS5BCandidateTransportInfo.ATTR_CID)));
  84.                             break;

  85.                         case JingleS5BTransportInfo.CandidateError.ELEMENT:
  86.                             builder.setTransportInfo(JingleS5BTransportInfo.CandidateError.INSTANCE);
  87.                             break;

  88.                         case JingleS5BTransportInfo.ProxyError.ELEMENT:
  89.                             builder.setTransportInfo(JingleS5BTransportInfo.ProxyError.INSTANCE);
  90.                             break;
  91.                     }
  92.                 }
  93.                 break;

  94.                 case END_ELEMENT: {
  95.                     if (parser.getDepth() == initialDepth) {
  96.                             break outerloop;
  97.                     }
  98.                 }
  99.                     break;
  100.             }
  101.         }
  102.         return builder.build();
  103.     }
  104. }