Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(deps): update managed.jetty to v12 (major) #838

Merged
merged 10 commits into from
Nov 25, 2024
6 changes: 3 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ undertow = '2.3.18.Final'
tomcat = '11.0.0'
bcpkix = "1.70"

managed-jetty = '11.0.24'
managed-apache-http-core5 = "5.3.1"
managed-jetty = '12.0.15'
micronaut-reactor = "3.6.0"
micronaut-security = "4.11.1"
micronaut-serde = "2.12.0"
Expand Down Expand Up @@ -49,8 +49,8 @@ kotlin-reflect = { module = 'org.jetbrains.kotlin:kotlin-reflect' }
apache-http-core5 = { module = 'org.apache.httpcomponents.core5:httpcore5', version.ref = 'managed-apache-http-core5' }
tomcat-embed-core = { module = 'org.apache.tomcat.embed:tomcat-embed-core', version.ref = 'tomcat' }
undertow-servlet = { module = 'io.undertow:undertow-servlet', version.ref = 'undertow' }
jetty-servlet = { module = 'org.eclipse.jetty:jetty-servlet', version.ref = 'managed-jetty' }
jetty-http2-server = { module = 'org.eclipse.jetty.http2:http2-server', version.ref = 'managed-jetty' }
jetty-servlet = { module = 'org.eclipse.jetty.ee10:jetty-ee10-servlet', version.ref = 'managed-jetty' }
jetty-http2-server = { module = 'org.eclipse.jetty.http2:jetty-http2-server', version.ref = 'managed-jetty' }
jetty-alpn-server = { module = 'org.eclipse.jetty:jetty-alpn-server', version.ref = 'managed-jetty' }
jetty-alpn-conscrypt-server = { module = 'org.eclipse.jetty:jetty-alpn-conscrypt-server', version.ref = 'managed-jetty' }
kotest-runner = { module = 'io.kotest:kotest-runner-junit5', version.ref = 'kotest-runner' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public JettyConfiguration(@Nullable MultipartConfiguration multipartConfiguratio
/**
* Default constructor.
* @param multipartConfiguration The multipart configuration.
* @param requestLog The request log configuration
*/
@Inject
public JettyConfiguration(@Nullable MultipartConfiguration multipartConfiguration, @Nullable JettyRequestLog requestLog) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
import io.micronaut.web.router.Router;
import jakarta.inject.Singleton;
import jakarta.servlet.ServletContainerInitializer;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.stream.Stream;
import org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory;
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.http2.server.HTTP2CServerConnectionFactory;
import org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory;
Expand All @@ -56,11 +56,10 @@
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SslConnectionFactory;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceCollection;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;

Expand Down Expand Up @@ -158,12 +157,14 @@ protected Server jettyServer(
});

final ServletContextHandler contextHandler = newJettyContext(server, contextPath);
server.setHandler(contextHandler);
configureServletInitializer(server, contextHandler, servletContainerInitializers);
ResourceFactory resourceFactory = ResourceFactory.of(server);

final SslConfiguration sslConfiguration = getSslConfiguration();
ServerConnector https = null;
if (sslConfiguration.isEnabled()) {
https = newHttpsConnector(server, sslConfiguration, jettySslConfiguration);
https = newHttpsConnector(server, sslConfiguration, jettySslConfiguration, resourceFactory);

}
final ServerConnector http = newHttpConnector(server, host, port);
Expand Down Expand Up @@ -202,12 +203,13 @@ protected Server jettyServer(
* @param server The server
* @param sslConfiguration The SSL configuration
* @param jettySslConfiguration The Jetty SSL configuration
* @param resourceFactory
* @return The server connector
*/
protected @NonNull ServerConnector newHttpsConnector(
@NonNull Server server,
@NonNull SslConfiguration sslConfiguration,
@NonNull JettyConfiguration.JettySslConfiguration jettySslConfiguration) {
@NonNull JettyConfiguration.JettySslConfiguration jettySslConfiguration, ResourceFactory resourceFactory) {
ServerConnector https;
final HttpConfiguration httpConfig = jettyConfiguration.getHttpConfiguration();
int securePort = sslConfiguration.getPort();
Expand Down Expand Up @@ -236,7 +238,7 @@ protected Server jettyServer(
keyStoreConfig.getPath().ifPresent(path -> {
if (path.startsWith(ServletStaticResourceConfiguration.CLASSPATH_PREFIX)) {
String cp = path.substring(ServletStaticResourceConfiguration.CLASSPATH_PREFIX.length());
sslContextFactory.setKeyStorePath(Resource.newClassPathResource(cp).getURI().toString());
sslContextFactory.setKeyStorePath(resourceFactory.newClassLoaderResource(cp).getURI().toString());
} else {
sslContextFactory.setKeyStorePath(path);
}
Expand All @@ -249,7 +251,7 @@ protected Server jettyServer(
trustStore.getPath().ifPresent(path -> {
if (path.startsWith(ServletStaticResourceConfiguration.CLASSPATH_PREFIX)) {
String cp = path.substring(ServletStaticResourceConfiguration.CLASSPATH_PREFIX.length());
sslContextFactory.setTrustStorePath(Resource.newClassPathResource(cp).getURI().toString());
sslContextFactory.setTrustStorePath(resourceFactory.newClassLoaderResource(cp).getURI().toString());
} else {
sslContextFactory.setTrustStorePath(path);
}
Expand Down Expand Up @@ -298,12 +300,14 @@ protected void configureServletInitializer(Server server, ServletContextHandler
}

List<ContextHandler> resourceHandlers = Stream.concat(
getStaticResourceConfigurations().stream().map(this::toHandler),
Stream.of(contextHandler)
Stream.of(contextHandler),
getStaticResourceConfigurations().stream().map(servletStaticResourceConfiguration -> toHandler(servletStaticResourceConfiguration, ResourceFactory.of(contextHandler)))
).toList();

HandlerList handlerList = new HandlerList(resourceHandlers.toArray(new ContextHandler[0]));
server.setHandler(handlerList);
ContextHandlerCollection contextHandlerCollection = new ContextHandlerCollection(
resourceHandlers.toArray(new ContextHandler[0])
);
server.setHandler(contextHandlerCollection);
}

/**
Expand All @@ -314,7 +318,11 @@ protected void configureServletInitializer(Server server, ServletContextHandler
* @return The handler
*/
protected @NonNull ServletContextHandler newJettyContext(@NonNull Server server, @NonNull String contextPath) {
return new ServletContextHandler(server, contextPath, false, false);
return new ServletContextHandler(
contextPath,
false,
false
);
}

/**
Expand Down Expand Up @@ -390,16 +398,17 @@ private void applyAdditionalPorts(Server server, ServerConnector serverConnector
* @param config The static resource configuration
* @return the context handler
*/
private ContextHandler toHandler(ServletStaticResourceConfiguration config) {
private ContextHandler toHandler(ServletStaticResourceConfiguration config, ResourceFactory resourceFactory) {
ResourceHandler resourceHandler = new ResourceHandler();
Resource[] resourceArray = config.getPaths().stream()
.map(path -> {
if (path.startsWith(ServletStaticResourceConfiguration.CLASSPATH_PREFIX)) {
String cp = path.substring(ServletStaticResourceConfiguration.CLASSPATH_PREFIX.length());
return Resource.newClassPathResource(cp);
return resourceFactory.newClassLoaderResource(cp);
} else {
try {
return Resource.newResource(path);
} catch (IOException e) {
return resourceFactory.newResource(path);
} catch (Exception e) {
throw new ConfigurationException("Static resource path doesn't exist: " + path, e);
}
}
Expand All @@ -412,23 +421,14 @@ private ContextHandler toHandler(ServletStaticResourceConfiguration config) {

final String mapping = path;

ResourceCollection mappedResourceCollection = new ResourceCollection(resourceArray) {
@Override
public Resource addPath(String path) throws IOException {
return super.addPath(path.substring(mapping.length()));
}
};

ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setBaseResource(mappedResourceCollection);
Resource combined = ResourceFactory.combine(resourceArray);
resourceHandler.setBaseResource(combined);
resourceHandler.setDirAllowed(false);
resourceHandler.setDirectoriesListed(false);
if (!isEmpty(config.getCacheControl())) {
resourceHandler.setCacheControl(config.getCacheControl());
}

ContextHandler contextHandler = new ContextHandler(path);
contextHandler.setContextPath("/");
contextHandler.setHandler(resourceHandler);
contextHandler.setDisplayName("Static Resources " + mapping);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class JettyParameterBinding2Spec extends Specification {
expect:
response.status() == HttpStatus.OK
response.contentType.get() == MediaType.TEXT_PLAIN_TYPE
response.body() == 'Hello micronaut /'
response.body() == 'Hello micronaut ROOT'
}

void "test request and response"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ch.qos.logback.classic.Level
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.AppenderBase
import io.micronaut.context.ApplicationContext
import io.micronaut.context.annotation.Property
import io.micronaut.context.env.Environment
import io.micronaut.context.exceptions.BeanInstantiationException
import io.micronaut.http.HttpRequest
Expand Down Expand Up @@ -227,7 +228,7 @@ class JettyStaticResourceResolutionSpec extends Specification implements TestPro
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer, [
'micronaut.router.static-resources.default.paths': ['classpath:public'],
'micronaut.router.static-resources.default.mapping': '/static/**',
'micronaut.router.static-resources.default.cache-control': '', // clear the cache control header
'micronaut.router.static-resources.default.cache-control': 'no-cache', // clear the cache control header
])
HttpClient rxClient = embeddedServer.applicationContext.createBean(HttpClient, embeddedServer.getURL())

Expand All @@ -246,7 +247,7 @@ class JettyStaticResourceResolutionSpec extends Specification implements TestPro
response.body() == "<html><head></head><body>HTML Page from resources/foo</body></html>"

and: 'the cache control header is not set'
!response.headers.contains(CACHE_CONTROL)
response.header(CACHE_CONTROL) == 'no-cache'

cleanup:
embeddedServer.stop()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.net.*;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Implementation of {@link AbstractServletServer} for Undertow.
Expand Down
Loading
Loading