Skip to content

Commit

Permalink
OUPDAUNTLE-90 Make a tomcat-7 implementation for tomcat-embedded
Browse files Browse the repository at this point in the history
  • Loading branch information
thijslemmens committed Oct 27, 2023
1 parent 6d48eb2 commit 78736fc
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 70 deletions.
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
rootProject.name = 'docker-alfresco'

include ':tomcat-base'
include ':tomcat-base:tomcat-embedded-7'
include ':tomcat-base:tomcat-embedded-8.5'
include ':tomcat-base:tomcat-embedded-9'
include ':1alfresco-skeleton'
Expand Down
19 changes: 12 additions & 7 deletions tomcat-base/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ subprojects {
java {
withJavadocJar()
withSourcesJar()
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

if (new File("${project.projectDir}/overload.gradle").exists()) {
apply from: "${project.projectDir}/overload.gradle"
}

ext {
jsonLoggingVersion = '0.0.7'
}
Expand All @@ -37,14 +42,14 @@ subprojects {
implementation "org.apache.tomcat:tomcat-jasper:${tomcatVersion}"
implementation "org.apache.tomcat:tomcat-jasper-el:${tomcatVersion}"
implementation "org.apache.tomcat:tomcat-jsp-api:${tomcatVersion}"
implementation "eu.xenit.logging:json-logging:${jsonLoggingVersion}"
// implementation "eu.xenit.logging:json-logging:${jsonLoggingVersion}"
implementation "com.fasterxml.jackson.core:jackson-databind:2.15.2"
testImplementation "org.junit.jupiter:junit-jupiter-api:5.10.0"
testImplementation("org.junit-pioneer:junit-pioneer:2.0.1")
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.10.0"
sharedLibs("eu.xenit.logging:json-logging:${jsonLoggingVersion}") {
transitive = false
}
// sharedLibs("eu.xenit.logging:json-logging:${jsonLoggingVersion}") {
// transitive = false
// }
}

boolean isRelease = ci.branch?.equals("master")
Expand Down Expand Up @@ -84,7 +89,7 @@ subprojects {
}

createDockerFile {
from 'docker.io/eclipse-temurin:11'
from 'docker.io/eclipse-temurin:8'
smartCopy configurations.sharedLibs, '/usr/local/tomcat/shared/lib/'
smartCopy "${project.parent.projectDir}/src/shared/main/resources/keystore", '/keystore'
smartCopy configurations.runtimeClasspath, '/app/tomcat-embedded/lib'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import eu.xenit.alfresco.tomcat.embedded.config.EnvironmentVariableConfigurationProvider;
import eu.xenit.alfresco.tomcat.embedded.share.tomcat.ShareTomcatCustomizer;
import eu.xenit.alfresco.tomcat.embedded.tomcat.TomcatFactory;
import eu.xenit.logging.json.jul.JsonFormatter;
//import eu.xenit.logging.json.jul.JsonFormatter;
import org.apache.catalina.startup.Tomcat;

import java.nio.file.Files;
Expand All @@ -29,11 +29,11 @@ public static void main(String[] args) {
TomcatFactory tomcatFactory = new TomcatFactory(configuration);
Tomcat tomcat = tomcatFactory.getTomcat();
if (configuration.isAlfrescoEnabled()) {
var alfrescoTomcatCustomizer = new AlfrescoTomcatCustomizer();
AlfrescoTomcatCustomizer alfrescoTomcatCustomizer = new AlfrescoTomcatCustomizer();
alfrescoTomcatCustomizer.customize(tomcat, configuration);
}
if (configuration.isShareEnabled()) {
var shareTomcatCustomizer = new ShareTomcatCustomizer();
ShareTomcatCustomizer shareTomcatCustomizer = new ShareTomcatCustomizer();
shareTomcatCustomizer.customize(tomcat, configuration);
}
//Needs to be done after
Expand Down Expand Up @@ -67,14 +67,14 @@ public static void configureLoggerToJSONStdOut(Logger logger, String component,
}
};

if (json) {
JsonFormatter formatter = new JsonFormatter();
formatter.setType("application");
formatter.setComponent(component);
formatter.setExtractStackTrace("true");
formatter.setFilterStackTrace(true);
customHandler.setFormatter(formatter);
}
// if (json) {
// JsonFormatter formatter = new JsonFormatter();
// formatter.setType("application");
// formatter.setComponent(component);
// formatter.setExtractStackTrace("true");
// formatter.setFilterStackTrace(true);
// customHandler.setFormatter(formatter);
// }

logger.addHandler(customHandler);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package eu.xenit.alfresco.tomcat.embedded.alfresco.tomcat;

import eu.xenit.alfresco.tomcat.embedded.alfresco.config.AlfrescoConfiguration;
import eu.xenit.alfresco.tomcat.embedded.config.TomcatConfiguration;
import eu.xenit.alfresco.tomcat.embedded.tomcat.TomcatFactory;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.net.SSLHostConfig;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -53,7 +53,7 @@ public static void createSSLConnector(Tomcat tomcat, AlfrescoConfiguration alfre
System.exit(1);
}

var tomcatConfiguration = alfrescoConfiguration.getTomcatConfiguration();
TomcatConfiguration tomcatConfiguration = alfrescoConfiguration.getTomcatConfiguration();
Connector connector = TomcatFactory.getConnector(tomcat,
"org.apache.coyote.http11.Http11NioProtocol",
tomcatConfiguration.getTomcatSslPort(),
Expand All @@ -65,20 +65,39 @@ public static void createSSLConnector(Tomcat tomcat, AlfrescoConfiguration alfre
tomcatConfiguration.getTomcatRelaxedQueryChars()
);

SSLHostConfig sslHostConfig = new SSLHostConfig();
sslHostConfig.setCertificateKeystoreFile(alfrescoConfiguration.getTomcatSSLKeystore());
sslHostConfig.setCertificateKeystorePassword(alfrescoConfiguration.getTomcatSSLKeystorePassword());
sslHostConfig.setCertificateKeystoreType("JCEKS");
sslHostConfig.setTruststoreFile(alfrescoConfiguration.getTomcatSSLTruststore());
sslHostConfig.setTruststorePassword(alfrescoConfiguration.getTomcatSSLTruststorePassword());
sslHostConfig.setTruststoreType("JCEKS");
sslHostConfig.setSslProtocol("TLS");
sslHostConfig.setCertificateVerification(SSLHostConfig.CertificateVerification.REQUIRED.name());
connector.addSslHostConfig(sslHostConfig);
addSslHost(alfrescoConfiguration, connector);
connector.setSecure(true);
connector.setProperty("clientAuth", "want");
connector.setProperty("allowUnsafeLegacyRenegotiation", "true");
connector.setMaxSavePostSize(-1);
tomcat.setConnector(connector);
}

private static void addSslHost(AlfrescoConfiguration alfrescoConfiguration, Connector connector) {

// Assuming you already have the Connector object initialized as 'connector'
connector.setScheme("https");
connector.setSecure(true);
connector.setPort(8443); // or any other port you want to use for SSL

// Set the SSL protocol
connector.setProtocol("org.apache.coyote.http11.Http11NioProtocol");
connector.setAttribute("SSLEnabled", true);
connector.setAttribute("sslProtocol", "TLS");
connector.setAttribute("clientAuth", "want");

// Keystore configurations
connector.setAttribute("keystoreFile", alfrescoConfiguration.getTomcatSSLKeystore());
connector.setAttribute("keystorePass", alfrescoConfiguration.getTomcatSSLKeystorePassword());
connector.setAttribute("keystoreType", "JCEKS");

// Truststore configurations
connector.setAttribute("truststoreFile", alfrescoConfiguration.getTomcatSSLTruststore());
connector.setAttribute("truststorePass", alfrescoConfiguration.getTomcatSSLTruststorePassword());
connector.setAttribute("truststoreType", "JCEKS");

// Other properties
connector.setAttribute("allowUnsafeLegacyRenegotiation", "true");
connector.setMaxSavePostSize(-1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

import java.io.IOException;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Version;
import java.net.http.HttpConnectTimeoutException;
Expand All @@ -29,7 +31,7 @@ public static void main(String[] args) throws IOException, InterruptedException
TomcatConfiguration configuration = new EnvironmentVariableConfigurationProvider()
.getConfiguration(new DefaultConfigurationProvider()
.getConfiguration());
var exitCode = 0;
int exitCode = 0;
if (configuration.isAlfrescoEnabled()) {
exitCode = healthCheck(ALFRESCO_DEFAULT_LIVE_PROBE, 0, args);
}
Expand All @@ -40,8 +42,8 @@ public static void main(String[] args) throws IOException, InterruptedException
}

private static int healthCheck(String endpoint, int statusCode, String[] args) throws IOException, InterruptedException {
var spec = HealthCheck.setupHealthCheck(endpoint, statusCode, args);
var exitCode = doHealthCheck(spec);
HealthCheckSpec spec = HealthCheck.setupHealthCheck(endpoint, statusCode, args);
int exitCode = doHealthCheck(spec);
if(exitCode != 0) {
System.exit(exitCode);
}
Expand All @@ -66,24 +68,27 @@ public static HealthCheckSpec setupHealthCheck(String defaultLiveProbe, int stat
}

public static int doHealthCheck(HealthCheckSpec spec)
throws IOException, InterruptedException {
var client = HttpClient.newBuilder()
.version(Version.HTTP_1_1)
.connectTimeout(Duration.ofMillis(spec.getTimeOut()))
.build();
var httpRequest = HttpRequest.newBuilder()
.uri(URI.create(spec.getEndPoint()))
.build();
throws IOException {
HttpURLConnection connection = null;
try {
var response = client.send(httpRequest, BodyHandlers.ofString());
if (response.statusCode() == spec.getStatusCode()) {
URL url = new URL(spec.getEndPoint());
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(spec.getTimeOut());
connection.setRequestMethod("GET");

int responseCode = connection.getResponseCode();
if (responseCode == spec.getStatusCode()) {
return 0;
}
return 1;
} catch (ConnectException e) {
return 2;
} catch (HttpConnectTimeoutException e) {
} catch (java.net.SocketTimeoutException e) {
return 3;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package eu.xenit.alfresco.tomcat.embedded.tomcat;

import eu.xenit.alfresco.tomcat.embedded.config.TomcatConfiguration;
import eu.xenit.logging.json.valve.JsonAccessLogValve;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.Service;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;
import static eu.xenit.alfresco.tomcat.embedded.utils.Utils.redirectLog4j;

import eu.xenit.alfresco.tomcat.embedded.config.TomcatConfiguration;
//import eu.xenit.logging.json.valve.JsonAccessLogValve;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

import static eu.xenit.alfresco.tomcat.embedded.utils.Utils.redirectLog4j;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.Service;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.loader.WebappLoader;
import org.apache.catalina.startup.Tomcat;
import org.apache.naming.resources.VirtualDirContext;

public class TomcatFactory {

Expand Down Expand Up @@ -60,7 +59,7 @@ public Tomcat getTomcat() throws IOException {
addUserWithRole(tomcat, "CN=Alfresco Repository, OU=Unknown, O=Alfresco Software Ltd., L=Maidenhead, ST=UK, C=GB", null, "repository");
Path webapps = Paths.get(getConfiguration().getWebappsPath());
if (Files.exists(webapps)) {
try (var directoryStream = Files.newDirectoryStream(webapps)) {
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(webapps)) {
directoryStream.forEach(path -> addWebapp(tomcat, path));
}
}
Expand All @@ -70,7 +69,7 @@ public Tomcat getTomcat() throws IOException {
protected boolean isEmptyDir(Path path) {
if (Files.isDirectory(path)) {
try (Stream<Path> entries = Files.list(path)) {
return entries.findFirst().isEmpty();
return entries.findFirst().isPresent();
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -91,11 +90,18 @@ private void addWebapp(Tomcat tomcat, Path path) {
ctx.setParentClassLoader(Thread.currentThread().getContextClassLoader());
LifecycleListener lifecycleListener = event -> {
if (event.getType().equals("before_start")) {
WebResourceRoot resources = new StandardRoot(ctx);
resources.setCacheMaxSize(getConfiguration().getTomcatCacheMaxSize());
resources.addPostResources(new DirResourceSet(resources, "/WEB-INF/classes", getConfiguration().getSharedClasspathDir(), "/"));
resources.addPostResources(new DirResourceSet(resources, "/WEB-INF/classes", getConfiguration().getGeneratedClasspathDir(), "/"));
resources.addJarResources(new DirResourceSet(resources, "/WEB-INF/lib", getConfiguration().getSharedLibDir(), "/"));
// Create a standard context (assuming you already have it as 'ctx')
WebappLoader webappLoader = new WebappLoader();

// Add classpath directories
webappLoader.addRepository("file:" + getConfiguration().getSharedClasspathDir() + "/");
webappLoader.addRepository("file:" + getConfiguration().getGeneratedClasspathDir() + "/");
ctx.setLoader(webappLoader);

// Additional resources (like JARs) can be added using a virtual loader
VirtualDirContext resources = new VirtualDirContext();
resources.setExtraResourcePaths("/WEB-INF/lib=" + getConfiguration().getSharedLibDir());

if (getConfiguration().isJsonLogging()) {
redirectLog4j(path, Paths.get(configuration.getGeneratedClasspathDir()));
}
Expand All @@ -107,11 +113,11 @@ private void addWebapp(Tomcat tomcat, Path path) {
};
ctx.addLifecycleListener(lifecycleListener);

if (getConfiguration().isAccessLogging()) {
JsonAccessLogValve valve = new JsonAccessLogValve();
ctx.addValve(valve);
ctx.getAccessLog();
}
// if (getConfiguration().isAccessLogging()) {
// JsonAccessLogValve valve = new JsonAccessLogValve();
// ctx.addValve(valve);
// ctx.getAccessLog();
// }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
Expand Down Expand Up @@ -29,7 +30,7 @@ public static void redirectLog4j(Path webappPath, Path destinationPath) {
}
Properties properties = new Properties();
try {
try (var reader = Files.newBufferedReader(log4JPropertiesPath)) {
try (Reader reader = Files.newBufferedReader(log4JPropertiesPath)) {
properties.load(reader);
properties.setProperty("log4j.rootLogger", "error, Console, jmxlogger1");
properties.setProperty("log4j.appender.Console.layout", "eu.xenit.logging.json.log4j.JsonLayout");
Expand Down
10 changes: 10 additions & 0 deletions tomcat-base/tomcat-embedded-7/overload.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ext {
tomcat = [
version: '7.0.109'
]
}

java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package eu.xenit.alfresco.tomcat.embedded.tomcat.tomcat7;

public class Tomcat7Impl {



}
4 changes: 4 additions & 0 deletions tomcat-base/tomcat-embedded-9/overload.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ ext {
]
}

java {
targetCompatibility = JavaVersion.VERSION_1_8
}

sonarqube {
properties {
property 'sonar.projectBaseDir', "$project.parent.projectDir/src/shared/"
Expand Down

0 comments on commit 78736fc

Please sign in to comment.