JingleS5BTransportCandidate.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.elements;

  18. import org.jivesoftware.smack.packet.XmlEnvironment;
  19. import org.jivesoftware.smack.util.InternetAddress;
  20. import org.jivesoftware.smack.util.Objects;
  21. import org.jivesoftware.smack.util.StringUtils;
  22. import org.jivesoftware.smack.util.XmlStringBuilder;

  23. import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
  24. import org.jivesoftware.smackx.jingle.element.JingleContentTransportCandidate;

  25. import org.jxmpp.jid.Jid;
  26. import org.jxmpp.jid.impl.JidCreate;
  27. import org.jxmpp.stringprep.XmppStringprepException;

  28. /**
  29.  * TransportCandidate for Jingle Socks5Bytestream transports.
  30.  */
  31. public final class JingleS5BTransportCandidate extends JingleContentTransportCandidate {

  32.     public static final String NAMESPACE = JingleS5BTransport.NAMESPACE_V1;

  33.     public static final String ATTR_CID = "cid";
  34.     public static final String ATTR_HOST = "host";
  35.     public static final String ATTR_JID = "jid";
  36.     public static final String ATTR_PORT = "port";
  37.     public static final String ATTR_PRIORITY = "priority";
  38.     public static final String ATTR_TYPE = "type";

  39.     private final String cid;
  40.     private final InternetAddress host;
  41.     private final Jid jid;
  42.     private final int port;
  43.     private final int priority;
  44.     private final Type type;

  45.     public JingleS5BTransportCandidate(String candidateId, String hostString, Jid jid, int port, int priority, Type type) {
  46.         this(candidateId, InternetAddress.fromIgnoringZoneId(hostString), jid, port, priority, type);
  47.     }

  48.     public JingleS5BTransportCandidate(String candidateId, InternetAddress host, Jid jid, int port, int priority, Type type) {
  49.         Objects.requireNonNull(candidateId);
  50.         Objects.requireNonNull(host);
  51.         Objects.requireNonNull(jid);

  52.         if (priority < 0) {
  53.             throw new IllegalArgumentException("Priority MUST NOT be less than 0.");
  54.         }
  55.         if (port < 0) {
  56.             throw new IllegalArgumentException("Port MUST NOT be less than 0.");
  57.         }

  58.         this.cid = candidateId;
  59.         this.host = host;
  60.         this.jid = jid;
  61.         this.port = port;
  62.         this.priority = priority;
  63.         this.type = type;
  64.     }

  65.     public JingleS5BTransportCandidate(Bytestream.StreamHost streamHost, int priority, Type type) {
  66.         this(StringUtils.randomString(24), streamHost.getAddress(), streamHost.getJID(), streamHost.getPort(), priority, type);
  67.     }

  68.     public enum Type {
  69.         assisted (120),
  70.         direct (126),
  71.         proxy (10),
  72.         tunnel (110),
  73.         ;

  74.         private final int weight;

  75.         public int getWeight() {
  76.             return weight;
  77.         }

  78.         Type(int weight) {
  79.             this.weight = weight;
  80.         }

  81.         public static Type fromString(String name) {
  82.             for (Type t : Type.values()) {
  83.                 if (t.toString().equals(name)) {
  84.                     return t;
  85.                 }
  86.             }
  87.             throw new IllegalArgumentException("Illegal type: " + name);
  88.         }
  89.     }

  90.     public String getCandidateId() {
  91.         return cid;
  92.     }

  93.     public InternetAddress getHost() {
  94.         return host;
  95.     }

  96.     public Jid getJid() {
  97.         return jid;
  98.     }

  99.     public int getPort() {
  100.         return port;
  101.     }

  102.     public int getPriority() {
  103.         return priority;
  104.     }

  105.     public Type getType() {
  106.         return type;
  107.     }

  108.     public Bytestream.StreamHost getStreamHost() {
  109.         return new Bytestream.StreamHost(jid, host, port);
  110.     }


  111.     @Override
  112.     public String getNamespace() {
  113.         return NAMESPACE;
  114.     }

  115.     @Override
  116.     public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) {
  117.         XmlStringBuilder xml = new XmlStringBuilder(this, enclosingXmlEnvironment);
  118.         xml.attribute(ATTR_CID, cid);
  119.         xml.attribute(ATTR_HOST, host);
  120.         xml.attribute(ATTR_JID, jid);
  121.         if (port >= 0) {
  122.             xml.attribute(ATTR_PORT, port);
  123.         }
  124.         xml.attribute(ATTR_PRIORITY, priority);
  125.         xml.optAttribute(ATTR_TYPE, type);

  126.         xml.closeEmptyElement();
  127.         return xml;
  128.     }

  129.     public static Builder getBuilder() {
  130.         return new Builder();
  131.     }

  132.     public static final class Builder {
  133.         private String cid;
  134.         private InternetAddress host;
  135.         private Jid jid;
  136.         private int port = -1;
  137.         private int priority = -1;
  138.         private Type type;

  139.         private Builder() {
  140.         }

  141.         public Builder setCandidateId(String cid) {
  142.             this.cid = cid;
  143.             return this;
  144.         }

  145.         public Builder setHost(String host)  {
  146.             InternetAddress inetAddress = InternetAddress.fromIgnoringZoneId(host);
  147.             return setHost(inetAddress);
  148.         }

  149.         public Builder setHost(InternetAddress host) {
  150.             this.host = host;
  151.             return this;
  152.         }

  153.         public Builder setJid(String jid) throws XmppStringprepException {
  154.             this.jid = JidCreate.from(jid);
  155.             return this;
  156.         }

  157.         public Builder setPort(int port) {
  158.             if (port < 0) {
  159.                 throw new IllegalArgumentException("Port MUST NOT be less than 0.");
  160.             }
  161.             this.port = port;
  162.             return this;
  163.         }

  164.         public Builder setPriority(int priority) {
  165.             if (priority < 0) {
  166.                 throw new IllegalArgumentException("Priority MUST NOT be less than 0.");
  167.             }
  168.             this.priority = priority;
  169.             return this;
  170.         }

  171.         public Builder setType(Type type) {
  172.             this.type = type;
  173.             return this;
  174.         }

  175.         public JingleS5BTransportCandidate build() {
  176.             return new JingleS5BTransportCandidate(cid, host, jid, port, priority, type);
  177.         }
  178.     }

  179. }