001/**
002 *
003 * Copyright 2019-2020 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack.tcp;
018
019import java.util.HashSet;
020import java.util.Set;
021
022import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration;
023import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionModuleDescriptor;
024import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
025import org.jivesoftware.smack.fsm.StateDescriptor;
026import org.jivesoftware.smack.tcp.XmppTcpTransportModule.EstablishTlsStateDescriptor;
027import org.jivesoftware.smack.tcp.XmppTcpTransportModule.EstablishingTcpConnectionStateDescriptor;
028
029public class XmppTcpTransportModuleDescriptor extends ModularXmppClientToServerConnectionModuleDescriptor {
030
031    private final boolean startTls;
032    private final boolean directTls;
033
034    public XmppTcpTransportModuleDescriptor(Builder builder) {
035        startTls = builder.startTls;
036        directTls = builder.directTls;
037    }
038
039    @Override
040    protected Set<Class<? extends StateDescriptor>> getStateDescriptors() {
041        Set<Class<? extends StateDescriptor>> res = new HashSet<>();
042        res.add(EstablishingTcpConnectionStateDescriptor.class);
043        if (startTls) {
044            res.add(EstablishTlsStateDescriptor.class);
045        }
046        if (directTls) {
047            // TODO: Add direct TLS.
048            throw new IllegalArgumentException("DirectTLS is not implemented yet");
049        }
050        return res;
051    }
052
053    @Override
054    protected XmppTcpTransportModule constructXmppConnectionModule(ModularXmppClientToServerConnectionInternal connectionInternal) {
055        return new XmppTcpTransportModule(this, connectionInternal);
056    }
057
058    public boolean isStartTlsEnabled() {
059        return startTls;
060    }
061
062    public boolean isDirectTlsEnabled() {
063        return directTls;
064    }
065
066    public static final class Builder extends ModularXmppClientToServerConnectionModuleDescriptor.Builder {
067
068        private Builder(ModularXmppClientToServerConnectionConfiguration.Builder connectionConfigurationBuilder) {
069            super(connectionConfigurationBuilder);
070        }
071
072        private boolean startTls = true;
073
074        private boolean directTls = false;
075
076        public Builder disableDirectTls() {
077            directTls = false;
078            return this;
079        }
080
081        public Builder disableStartTls() {
082            startTls = false;
083            return this;
084        }
085
086        @Override
087        protected XmppTcpTransportModuleDescriptor build() {
088            return new XmppTcpTransportModuleDescriptor(this);
089        }
090    }
091}