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