1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-06-16 00:24:49 +02:00
Smack/smack-jingle-old/src/main/java/org/jivesoftware/smackx/jingleold/provider/JingleTransportProvider.java
Florian Schmaus 4133eb175c Replace XPP3 by XmlPullParser interface wrapping StAX and XPP3
Introducing Smack's own XmlPullParser interface which tries to stay as
compatible as possible to XPP3. The interface is used to either wrap
StAX's XMLStreamReader if Smack is used on Java SE, and XPP3's
XmlPullParser if Smack is used on on Android.

Fixes SMACK-591.

Also introduce JUnit 5 and non-strict javadoc projects.
2019-05-06 22:10:50 +02:00

263 lines
7.9 KiB
Java

/**
*
* Copyright 2003-2005 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.jingleold.provider;
import java.io.IOException;
import org.jivesoftware.smack.packet.XmlEnvironment;
import org.jivesoftware.smack.provider.ExtensionElementProvider;
import org.jivesoftware.smack.xml.XmlPullParser;
import org.jivesoftware.smack.xml.XmlPullParserException;
import org.jivesoftware.smackx.jingleold.nat.ICECandidate;
import org.jivesoftware.smackx.jingleold.nat.TransportCandidate;
import org.jivesoftware.smackx.jingleold.packet.JingleTransport;
import org.jivesoftware.smackx.jingleold.packet.JingleTransport.JingleTransportCandidate;
/**
* Provider for a Jingle transport element.
*
* @author Alvaro Saurin
*/
public abstract class JingleTransportProvider extends ExtensionElementProvider<JingleTransport> {
/**
* Obtain the corresponding TransportNegotiator instance.
*
* @return a new TransportNegotiator instance
*/
protected JingleTransport getInstance() {
return new JingleTransport();
}
/**
* Parse a iq/jingle/transport element.
*
* @param parser the structure to parse
* @return a transport element.
* @throws IOException
* @throws XmlPullParserException
*/
@Override
public JingleTransport parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
boolean done = false;
JingleTransport trans = getInstance();
while (!done) {
XmlPullParser.Event eventType = parser.next();
String name = parser.getName();
if (eventType == XmlPullParser.Event.START_ELEMENT) {
if (name.equals(JingleTransportCandidate.NODENAME)) {
JingleTransportCandidate jtc = parseCandidate(parser);
if (jtc != null) trans.addCandidate(jtc);
}
else {
// TODO: Should be SmackParseException.
throw new IOException("Unknown tag \"" + name + "\" in transport element.");
}
}
else if (eventType == XmlPullParser.Event.END_ELEMENT) {
if (name.equals(JingleTransport.NODENAME)) {
done = true;
}
}
}
return trans;
}
protected abstract JingleTransportCandidate parseCandidate(XmlPullParser parser);
/**
* RTP-ICE profile.
*/
public static class Ice extends JingleTransportProvider {
/**
* Defauls constructor.
*/
public Ice() {
super();
}
/**
* Obtain the corresponding TransportNegotiator.Ice instance.
*
* @return a new TransportNegotiator.Ice instance
*/
@Override
protected JingleTransport getInstance() {
return new JingleTransport.Ice();
}
/**
* Parse a iq/jingle/transport/candidate element.
*
* @param parser the structure to parse
* @return a candidate element
*/
@Override
protected JingleTransportCandidate parseCandidate(XmlPullParser parser) {
ICECandidate mt = new ICECandidate();
String channel = parser.getAttributeValue("", "channel");
String generation = parser.getAttributeValue("", "generation");
String ip = parser.getAttributeValue("", "ip");
String name = parser.getAttributeValue("", "name");
String network = parser.getAttributeValue("", "network");
String username = parser.getAttributeValue("", "username");
String password = parser.getAttributeValue("", "password");
String port = parser.getAttributeValue("", "port");
String preference = parser.getAttributeValue("", "preference");
String proto = parser.getAttributeValue("", "proto");
String type = parser.getAttributeValue("", "type");
if (channel != null) {
mt.setChannel(new TransportCandidate.Channel(channel));
}
if (generation != null) {
try {
mt.setGeneration(Integer.parseInt(generation));
}
catch (Exception e) {
}
}
if (ip != null) {
mt.setIp(ip);
}
else {
return null;
}
if (name != null) {
mt.setName(name);
}
if (network != null) {
try {
mt.setNetwork(Integer.parseInt(network));
}
catch (Exception e) {
}
}
if (username != null) {
mt.setUsername(username);
}
if (password != null) {
mt.setPassword(password);
}
if (port != null) {
try {
mt.setPort(Integer.parseInt(port));
}
catch (Exception e) {
}
}
if (preference != null) {
try {
mt.setPreference(Integer.parseInt(preference));
}
catch (Exception e) {
}
}
if (proto != null) {
mt.setProto(new TransportCandidate.Protocol(proto));
}
if (type != null) {
mt.setType(ICECandidate.Type.valueOf(type));
}
return new JingleTransport.Ice.Candidate(mt);
}
}
/**
* Raw UDP profile.
*/
public static class RawUdp extends JingleTransportProvider {
/**
* Defauls constructor.
*/
public RawUdp() {
super();
}
/**
* Obtain the corresponding TransportNegotiator.RawUdp instance.
*
* @return a new TransportNegotiator.RawUdp instance
*/
@Override
protected JingleTransport getInstance() {
return new JingleTransport.RawUdp();
}
/**
* Parse a iq/jingle/transport/candidate element.
*
* @param parser the structure to parse
* @return a candidate element
*/
@Override
protected JingleTransportCandidate parseCandidate(XmlPullParser parser) {
TransportCandidate.Fixed mt = new TransportCandidate.Fixed();
String generation = parser.getAttributeValue("", "generation");
String ip = parser.getAttributeValue("", "ip");
String name = parser.getAttributeValue("", "name");
String port = parser.getAttributeValue("", "port");
// LOGGER.debug();
if (generation != null) {
try {
mt.setGeneration(Integer.parseInt(generation));
}
catch (Exception e) {
}
}
if (ip != null) {
mt.setIp(ip);
}
if (name != null) {
mt.setName(name);
}
if (port != null) {
try {
mt.setPort(Integer.parseInt(port));
}
catch (Exception e) {
}
}
return new JingleTransport.RawUdp.Candidate(mt);
}
}
}