XmppTcpTransportModuleDescriptor.java

  1. /**
  2.  *
  3.  * Copyright 2019-2020 Florian Schmaus
  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.smack.tcp;

  18. import java.util.HashSet;
  19. import java.util.Set;

  20. import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration;
  21. import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionModuleDescriptor;
  22. import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
  23. import org.jivesoftware.smack.fsm.StateDescriptor;
  24. import org.jivesoftware.smack.tcp.XmppTcpTransportModule.EstablishTlsStateDescriptor;
  25. import org.jivesoftware.smack.tcp.XmppTcpTransportModule.EstablishingTcpConnectionStateDescriptor;

  26. public class XmppTcpTransportModuleDescriptor extends ModularXmppClientToServerConnectionModuleDescriptor {

  27.     private final boolean startTls;
  28.     private final boolean directTls;

  29.     public XmppTcpTransportModuleDescriptor(Builder builder) {
  30.         startTls = builder.startTls;
  31.         directTls = builder.directTls;
  32.     }

  33.     @Override
  34.     protected Set<Class<? extends StateDescriptor>> getStateDescriptors() {
  35.         Set<Class<? extends StateDescriptor>> res = new HashSet<>();
  36.         res.add(EstablishingTcpConnectionStateDescriptor.class);
  37.         if (startTls) {
  38.             res.add(EstablishTlsStateDescriptor.class);
  39.         }
  40.         if (directTls) {
  41.             // TODO: Add direct TLS.
  42.             throw new IllegalArgumentException("DirectTLS is not implemented yet");
  43.         }
  44.         return res;
  45.     }

  46.     @Override
  47.     protected XmppTcpTransportModule constructXmppConnectionModule(ModularXmppClientToServerConnectionInternal connectionInternal) {
  48.         return new XmppTcpTransportModule(this, connectionInternal);
  49.     }

  50.     public boolean isStartTlsEnabled() {
  51.         return startTls;
  52.     }

  53.     public boolean isDirectTlsEnabled() {
  54.         return directTls;
  55.     }

  56.     public static final class Builder extends ModularXmppClientToServerConnectionModuleDescriptor.Builder {

  57.         private Builder(ModularXmppClientToServerConnectionConfiguration.Builder connectionConfigurationBuilder) {
  58.             super(connectionConfigurationBuilder);
  59.         }

  60.         private boolean startTls = true;

  61.         private boolean directTls = false;

  62.         public Builder disableDirectTls() {
  63.             directTls = false;
  64.             return this;
  65.         }

  66.         public Builder disableStartTls() {
  67.             startTls = false;
  68.             return this;
  69.         }

  70.         @Override
  71.         protected XmppTcpTransportModuleDescriptor build() {
  72.             return new XmppTcpTransportModuleDescriptor(this);
  73.         }
  74.     }
  75. }