JingleS5BTransport.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 java.util.ArrayList;
  19. import java.util.List;

  20. import javax.xml.namespace.QName;

  21. import org.jivesoftware.smack.packet.ExtensionElement;
  22. import org.jivesoftware.smack.util.StringUtils;
  23. import org.jivesoftware.smack.util.XmlStringBuilder;

  24. import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
  25. import org.jivesoftware.smackx.jingle.element.JingleContentTransport;
  26. import org.jivesoftware.smackx.jingle.element.JingleContentTransportCandidate;
  27. import org.jivesoftware.smackx.jingle.element.JingleContentTransportInfo;

  28. /**
  29.  * Socks5Bytestream transport element.
  30.  */
  31. public class JingleS5BTransport extends JingleContentTransport implements ExtensionElement {
  32.     public static final String NAMESPACE_V1 = "urn:xmpp:jingle:transports:s5b:1";
  33.     public static final String ATTR_DSTADDR = "dstaddr";
  34.     public static final String ATTR_MODE = "mode";
  35.     public static final String ATTR_SID = "sid";

  36.     public static final QName QNAME = new QName(NAMESPACE_V1, ELEMENT);

  37.     private final String streamId;
  38.     private final String dstAddr;
  39.     private final Bytestream.Mode mode;

  40.     protected JingleS5BTransport(List<JingleContentTransportCandidate> candidates, JingleContentTransportInfo info, String streamId, String dstAddr, Bytestream.Mode mode) {
  41.         super(candidates, info);
  42.         StringUtils.requireNotNullNorEmpty(streamId, "sid MUST be neither null, nor empty.");
  43.         this.streamId = streamId;
  44.         this.dstAddr = dstAddr;
  45.         this.mode = mode;
  46.     }

  47.     public String getStreamId() {
  48.         return streamId;
  49.     }

  50.     public String getDestinationAddress() {
  51.         return dstAddr;
  52.     }

  53.     public Bytestream.Mode getMode() {
  54.         return mode == null ? Bytestream.Mode.tcp : mode;
  55.     }

  56.     @Override
  57.     public String getNamespace() {
  58.         return QNAME.getNamespaceURI();
  59.     }

  60.     @Override
  61.     protected void addExtraAttributes(XmlStringBuilder xml) {
  62.         xml.optAttribute(ATTR_DSTADDR, dstAddr);
  63.         xml.optAttribute(ATTR_MODE, mode);
  64.         xml.attribute(ATTR_SID, streamId);
  65.     }

  66.     public boolean hasCandidate(String candidateId) {
  67.         return getCandidate(candidateId) != null;
  68.     }

  69.     public JingleS5BTransportCandidate getCandidate(String candidateId) {
  70.         for (JingleContentTransportCandidate c : candidates) {
  71.             JingleS5BTransportCandidate candidate = (JingleS5BTransportCandidate) c;
  72.             if (candidate.getCandidateId().equals(candidateId)) {
  73.                 return candidate;
  74.             }
  75.         }
  76.         return null;
  77.     }

  78.     public static Builder getBuilder() {
  79.         return new Builder();
  80.     }

  81.     public static class Builder {
  82.         private String streamId;
  83.         private String dstAddr;
  84.         private Bytestream.Mode mode;
  85.         private final ArrayList<JingleContentTransportCandidate> candidates = new ArrayList<>();
  86.         private JingleContentTransportInfo info;

  87.         public Builder setStreamId(String sid) {
  88.             this.streamId = sid;
  89.             return this;
  90.         }

  91.         public Builder setDestinationAddress(String dstAddr) {
  92.             this.dstAddr = dstAddr;
  93.             return this;
  94.         }

  95.         public Builder setMode(Bytestream.Mode mode) {
  96.             this.mode = mode;
  97.             return this;
  98.         }

  99.         public Builder addTransportCandidate(JingleS5BTransportCandidate candidate) {
  100.             if (info != null) {
  101.                 throw new IllegalStateException("Builder has already an info set. " +
  102.                         "The transport can only have either an info or transport candidates, not both.");
  103.             }
  104.             this.candidates.add(candidate);
  105.             return this;
  106.         }

  107.         public Builder setTransportInfo(JingleContentTransportInfo info) {
  108.             if (!candidates.isEmpty()) {
  109.                 throw new IllegalStateException("Builder has already at least one candidate set. " +
  110.                         "The transport can only have either an info or transport candidates, not both.");
  111.             }
  112.             if (this.info != null) {
  113.                 throw new IllegalStateException("Builder has already an info set.");
  114.             }
  115.             this.info = info;
  116.             return this;
  117.         }

  118.         public Builder setCandidateUsed(String candidateId) {
  119.             return setTransportInfo(new JingleS5BTransportInfo.CandidateUsed(candidateId));
  120.         }

  121.         public Builder setCandidateActivated(String candidateId) {
  122.             return setTransportInfo(new JingleS5BTransportInfo.CandidateActivated(candidateId));
  123.         }

  124.         public Builder setCandidateError() {
  125.             return setTransportInfo(JingleS5BTransportInfo.CandidateError.INSTANCE);
  126.         }

  127.         public Builder setProxyError() {
  128.             return setTransportInfo(JingleS5BTransportInfo.ProxyError.INSTANCE);
  129.         }

  130.         public JingleS5BTransport build() {
  131.             return new JingleS5BTransport(candidates, info, streamId, dstAddr, mode);
  132.         }
  133.     }
  134. }