TomcatConnectorFactory.java

  1. /*
  2.  * Copyright (C) 2020-2024 by Savoir-faire Linux
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 3 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  16.  */
  17. package net.jami.jams.server.core;

  18. import lombok.extern.slf4j.Slf4j;

  19. import org.apache.catalina.connector.Connector;
  20. import org.apache.tomcat.util.net.SSLHostConfig;
  21. import org.apache.tomcat.util.net.SSLHostConfigCertificate;

  22. import java.io.File;

  23. @Slf4j
  24. public class TomcatConnectorFactory {

  25.     public static Connector getSSLConnectorWithTrustStore(
  26.             String certificateFile, String keyFile, int port) {
  27.         log.info(System.getProperty("user.dir") + File.separator + "keystore.jks");
  28.         Connector connector = getSSLConnectorWithoutTrustStore(certificateFile, keyFile, port);
  29.         connector.findSslHostConfigs()[0].setTruststoreFile(
  30.                 System.getProperty("user.dir") + File.separator + "keystore.jks");
  31.         connector.findSslHostConfigs()[0].setTruststorePassword("changeit");
  32.         connector.findSslHostConfigs()[0].setCertificateVerification("optional");
  33.         return connector;
  34.     }

  35.     public static Connector getSSLConnectorWithoutTrustStore(
  36.             String certificateFile, String keyFile, int port) {
  37.         // Check if trust store exists or create it if necessary.
  38.         Connector connector = new Connector();
  39.         SSLHostConfig sslConfig = new SSLHostConfig();
  40.         SSLHostConfigCertificate sslHostConfigCertificate =
  41.                 new SSLHostConfigCertificate(sslConfig, SSLHostConfigCertificate.Type.RSA);
  42.         sslHostConfigCertificate.setCertificateChainFile(
  43.                 System.getProperty("user.dir") + File.separator + certificateFile);
  44.         sslHostConfigCertificate.setCertificateFile(
  45.                 System.getProperty("user.dir") + File.separator + certificateFile);
  46.         sslHostConfigCertificate.setCertificateKeyFile(
  47.                 System.getProperty("user.dir") + File.separator + keyFile);
  48.         sslConfig.addCertificate(sslHostConfigCertificate);
  49.         sslConfig.setProtocols("TLSv1,TLSv1.2,TLSv1.3");
  50.         connector.addSslHostConfig(sslConfig);
  51.         connector.setPort(port);
  52.         connector.setSecure(true);
  53.         connector.setScheme("https");
  54.         connector.setProperty("SSLEnabled", "true");
  55.         return connector;
  56.     }

  57.     public static Connector getNoSSLConnector(int port) {
  58.         Connector connector = new Connector();
  59.         connector.setPort(port);
  60.         connector.setScheme("http");
  61.         return connector;
  62.     }
  63. }