IceUdpTransportInfo.java

  1package eu.siacs.conversations.xmpp.jingle.stanzas;
  2
  3import com.google.common.base.Preconditions;
  4import com.google.common.collect.ImmutableList;
  5
  6import java.util.List;
  7
  8import eu.siacs.conversations.xml.Element;
  9import eu.siacs.conversations.xml.Namespace;
 10
 11public class IceUdpTransportInfo extends GenericTransportInfo {
 12
 13    private IceUdpTransportInfo(final String name, final String xmlns) {
 14        super(name, xmlns);
 15    }
 16
 17    public Fingerprint getFingerprint() {
 18        final Element fingerprint = this.findChild("fingerprint", Namespace.JINGLE_APPS_DTLS);
 19        return fingerprint == null ? null : Fingerprint.upgrade(fingerprint);
 20    }
 21
 22    public List<Candidate> getCandidates() {
 23        final ImmutableList.Builder<Candidate> builder = new ImmutableList.Builder<>();
 24        for(final Element child : getChildren()) {
 25            if ("candidate".equals(child.getName())) {
 26                builder.add(Candidate.upgrade(child));
 27            }
 28        }
 29        return builder.build();
 30    }
 31
 32    public static IceUdpTransportInfo upgrade(final Element element) {
 33        Preconditions.checkArgument("transport".equals(element.getName()), "Name of provided element is not transport");
 34        Preconditions.checkArgument(Namespace.JINGLE_TRANSPORT_ICE_UDP.equals(element.getNamespace()), "Element does not match ice-udp transport namespace");
 35        final IceUdpTransportInfo transportInfo = new IceUdpTransportInfo("transport", Namespace.JINGLE_TRANSPORT_ICE_UDP);
 36        transportInfo.setAttributes(element.getAttributes());
 37        transportInfo.setChildren(element.getChildren());
 38        return transportInfo;
 39    }
 40
 41    public static class Candidate extends Element {
 42
 43        private Candidate() {
 44            super("candidate");
 45        }
 46
 47        public int getComponent() {
 48            return getAttributeAsInt("component");
 49        }
 50
 51        public int getFoundation() {
 52            return getAttributeAsInt("foundation");
 53        }
 54
 55        public int getGeneration() {
 56            return getAttributeAsInt("generation");
 57        }
 58
 59        public String getId() {
 60            return getAttribute("id");
 61        }
 62
 63        public String getIp() {
 64            return getAttribute("ip");
 65        }
 66
 67        public int getNetwork() {
 68            return getAttributeAsInt("network");
 69        }
 70
 71        public int getPort() {
 72            return getAttributeAsInt("port");
 73        }
 74
 75        public int getPriority() {
 76            return getAttributeAsInt("priority");
 77        }
 78
 79        public String getProtocol() {
 80            return getAttribute("protocol");
 81        }
 82
 83        public String getRelAddr() {
 84            return getAttribute("rel-addr");
 85        }
 86
 87        public int getRelPort() {
 88            return getAttributeAsInt("rel-port");
 89        }
 90
 91        public String getType() { //TODO might be converted to enum
 92            return getAttribute("type");
 93        }
 94
 95        private int getAttributeAsInt(final String name) {
 96            final String value = this.getAttribute(name);
 97            if (value == null) {
 98                return 0;
 99            }
100            try {
101                return Integer.parseInt(value);
102            } catch (NumberFormatException e) {
103                return 0;
104            }
105        }
106
107        public static Candidate upgrade(final Element element) {
108            Preconditions.checkArgument("candidate".equals(element.getName()));
109            final Candidate candidate = new Candidate();
110            candidate.setAttributes(element.getAttributes());
111            candidate.setChildren(element.getChildren());
112            return candidate;
113        }
114    }
115
116
117    public static class Fingerprint extends Element {
118
119        public String getHash() {
120            return this.getAttribute("hash");
121        }
122
123        public String getSetup() {
124            return this.getAttribute("setup");
125        }
126
127        private Fingerprint() {
128            super("fingerprint", Namespace.JINGLE_APPS_DTLS);
129        }
130
131        public static Fingerprint upgrade(final Element element) {
132            Preconditions.checkArgument("fingerprint".equals(element.getName()));
133            Preconditions.checkArgument(Namespace.JINGLE_APPS_DTLS.equals(element.getNamespace()));
134            final Fingerprint fingerprint = new Fingerprint();
135            fingerprint.setAttributes(element.getAttributes());
136            fingerprint.setContent(element.getContent());
137            return fingerprint;
138        }
139    }
140}