001/** 002 * 003 * Copyright 2017 Paul Schaub 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.jingle.transports.jingle_s5b.provider; 018 019import static org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.Mode.tcp; 020import static org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.Mode.udp; 021import static org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportCandidate.ATTR_CID; 022import static org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportCandidate.ATTR_HOST; 023import static org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportCandidate.ATTR_JID; 024import static org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportCandidate.ATTR_PORT; 025import static org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportCandidate.ATTR_PRIORITY; 026import static org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportCandidate.ATTR_TYPE; 027import static org.xmlpull.v1.XmlPullParser.END_TAG; 028import static org.xmlpull.v1.XmlPullParser.START_TAG; 029 030import org.jivesoftware.smackx.jingle.element.JingleContentTransport; 031import org.jivesoftware.smackx.jingle.provider.JingleContentTransportProvider; 032import org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransport; 033import org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportCandidate; 034import org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportInfo; 035import org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportInfo.JingleS5BCandidateTransportInfo; 036 037import org.xmlpull.v1.XmlPullParser; 038 039/** 040 * Provider for JingleSocks5BytestreamTransport elements. 041 */ 042public class JingleS5BTransportProvider extends JingleContentTransportProvider<JingleS5BTransport> { 043 044 @Override 045 public JingleS5BTransport parse(XmlPullParser parser, int initialDepth) throws Exception { 046 JingleS5BTransport.Builder builder = JingleS5BTransport.getBuilder(); 047 048 String streamId = parser.getAttributeValue(null, JingleS5BTransport.ATTR_SID); 049 builder.setStreamId(streamId); 050 051 String dstAddr = parser.getAttributeValue(null, JingleS5BTransport.ATTR_DSTADDR); 052 builder.setDestinationAddress(dstAddr); 053 054 String mode = parser.getAttributeValue(null, JingleS5BTransport.ATTR_MODE); 055 if (mode != null) { 056 builder.setMode(mode.equals(udp.toString()) ? udp : tcp); 057 } 058 059 JingleS5BTransportCandidate.Builder cb; 060 outerloop: while (true) { 061 int tag = parser.nextTag(); 062 String name = parser.getName(); 063 switch (tag) { 064 case START_TAG: { 065 switch (name) { 066 067 case JingleS5BTransportCandidate.ELEMENT: 068 cb = JingleS5BTransportCandidate.getBuilder(); 069 cb.setCandidateId(parser.getAttributeValue(null, ATTR_CID)); 070 cb.setHost(parser.getAttributeValue(null, ATTR_HOST)); 071 cb.setJid(parser.getAttributeValue(null, ATTR_JID)); 072 cb.setPriority(Integer.parseInt(parser.getAttributeValue(null, ATTR_PRIORITY))); 073 074 String portString = parser.getAttributeValue(null, ATTR_PORT); 075 if (portString != null) { 076 cb.setPort(Integer.parseInt(portString)); 077 } 078 079 String typeString = parser.getAttributeValue(null, ATTR_TYPE); 080 if (typeString != null) { 081 cb.setType(JingleS5BTransportCandidate.Type.fromString(typeString)); 082 } 083 builder.addTransportCandidate(cb.build()); 084 break; 085 086 case JingleS5BTransportInfo.CandidateActivated.ELEMENT: 087 builder.setTransportInfo(new JingleS5BTransportInfo.CandidateActivated( 088 parser.getAttributeValue(null, 089 JingleS5BCandidateTransportInfo.ATTR_CID))); 090 break; 091 092 case JingleS5BTransportInfo.CandidateUsed.ELEMENT: 093 builder.setTransportInfo(new JingleS5BTransportInfo.CandidateUsed( 094 parser.getAttributeValue(null, 095 JingleS5BCandidateTransportInfo.ATTR_CID))); 096 break; 097 098 case JingleS5BTransportInfo.CandidateError.ELEMENT: 099 builder.setTransportInfo(JingleS5BTransportInfo.CandidateError.INSTANCE); 100 break; 101 102 case JingleS5BTransportInfo.ProxyError.ELEMENT: 103 builder.setTransportInfo(JingleS5BTransportInfo.ProxyError.INSTANCE); 104 break; 105 } 106 } 107 break; 108 109 case END_TAG: { 110 switch (name) { 111 case JingleContentTransport.ELEMENT: 112 break outerloop; 113 } 114 } 115 } 116 } 117 return builder.build(); 118 } 119}