From f2c8fd9cc8e718ba2d2b813623d725ca84280d02 Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Thu, 19 Aug 2021 13:55:45 +0200 Subject: [PATCH] Some more cleanup --- .../NormalizedHttpPathConverter.java | 36 +++++++++++++++++++ .../ResteasyServerCommonProcessor.java | 3 ++ .../ResteasyStandaloneBuildStep.java | 26 +++----------- .../http/deployment/VertxHttpProcessor.java | 4 ++- .../http/runtime/HttpBuildTimeConfig.java | 33 ++--------------- .../http/runtime/StaticResourcesRecorder.java | 4 ++- 6 files changed, 52 insertions(+), 54 deletions(-) create mode 100644 core/runtime/src/main/java/io/quarkus/runtime/configuration/NormalizedHttpPathConverter.java diff --git a/core/runtime/src/main/java/io/quarkus/runtime/configuration/NormalizedHttpPathConverter.java b/core/runtime/src/main/java/io/quarkus/runtime/configuration/NormalizedHttpPathConverter.java new file mode 100644 index 0000000000000..19f4f490d9f47 --- /dev/null +++ b/core/runtime/src/main/java/io/quarkus/runtime/configuration/NormalizedHttpPathConverter.java @@ -0,0 +1,36 @@ +package io.quarkus.runtime.configuration; + +import static io.quarkus.runtime.configuration.ConverterSupport.DEFAULT_QUARKUS_CONVERTER_PRIORITY; + +import javax.annotation.Priority; + +import org.eclipse.microprofile.config.spi.Converter; + +/** + * A converter to support locales. + */ +@Priority(DEFAULT_QUARKUS_CONVERTER_PRIORITY) +public class NormalizedHttpPathConverter implements Converter { + + private static final String SLASH = "/"; + + @Override + public String convert(String value) throws IllegalArgumentException, NullPointerException { + if (value == null) { + return SLASH; + } + + value = value.trim(); + if (SLASH.equals(value)) { + return value; + } + if (!value.startsWith(SLASH)) { + value = SLASH + value; + } + if (!value.endsWith(SLASH)) { + value = value + SLASH; + } + + return value; + } +} diff --git a/extensions/resteasy-classic/resteasy-server-common/deployment/src/main/java/io/quarkus/resteasy/server/common/deployment/ResteasyServerCommonProcessor.java b/extensions/resteasy-classic/resteasy-server-common/deployment/src/main/java/io/quarkus/resteasy/server/common/deployment/ResteasyServerCommonProcessor.java index ed472ecaf1eea..06c4eb9acb97e 100755 --- a/extensions/resteasy-classic/resteasy-server-common/deployment/src/main/java/io/quarkus/resteasy/server/common/deployment/ResteasyServerCommonProcessor.java +++ b/extensions/resteasy-classic/resteasy-server-common/deployment/src/main/java/io/quarkus/resteasy/server/common/deployment/ResteasyServerCommonProcessor.java @@ -77,6 +77,8 @@ import io.quarkus.resteasy.server.common.spi.AllowedJaxRsAnnotationPrefixBuildItem; import io.quarkus.runtime.annotations.ConfigItem; import io.quarkus.runtime.annotations.ConfigRoot; +import io.quarkus.runtime.annotations.ConvertWith; +import io.quarkus.runtime.configuration.NormalizedHttpPathConverter; /** * Processor that builds the RESTEasy server configuration. @@ -138,6 +140,7 @@ static final class ResteasyConfig { * annotated application classes. */ @ConfigItem(defaultValue = "/") + @ConvertWith(NormalizedHttpPathConverter.class) String path; /** diff --git a/extensions/resteasy-classic/resteasy/deployment/src/main/java/io/quarkus/resteasy/deployment/ResteasyStandaloneBuildStep.java b/extensions/resteasy-classic/resteasy/deployment/src/main/java/io/quarkus/resteasy/deployment/ResteasyStandaloneBuildStep.java index 3fca6ae74703f..e4beff718041f 100644 --- a/extensions/resteasy-classic/resteasy/deployment/src/main/java/io/quarkus/resteasy/deployment/ResteasyStandaloneBuildStep.java +++ b/extensions/resteasy-classic/resteasy/deployment/src/main/java/io/quarkus/resteasy/deployment/ResteasyStandaloneBuildStep.java @@ -23,9 +23,9 @@ import io.quarkus.resteasy.server.common.deployment.ResteasyDeploymentBuildItem; import io.quarkus.vertx.core.deployment.CoreVertxBuildItem; import io.quarkus.vertx.http.deployment.DefaultRouteBuildItem; +import io.quarkus.vertx.http.deployment.HttpRootPathBuildItem; import io.quarkus.vertx.http.deployment.RequireVirtualHttpBuildItem; import io.quarkus.vertx.http.deployment.RouteBuildItem; -import io.quarkus.vertx.http.runtime.HttpBuildTimeConfig; import io.quarkus.vertx.http.runtime.HttpConfiguration; import io.quarkus.vertx.http.runtime.VertxHttpRecorder; import io.vertx.core.Handler; @@ -50,32 +50,16 @@ public void staticInit(ResteasyStandaloneRecorder recorder, ResteasyDeploymentBuildItem deployment, ApplicationArchivesBuildItem applicationArchivesBuildItem, ResteasyInjectionReadyBuildItem resteasyInjectionReady, - HttpBuildTimeConfig httpConfig, + HttpRootPathBuildItem httpRootPathBuildItem, BuildProducer standalone) throws Exception { if (capabilities.isPresent(Capability.SERVLET)) { return; } - String deploymentRootPath = null; - // The context path + the resources path - String rootPath = httpConfig.rootPath; - if (deployment != null) { - deploymentRootPath = deployment.getRootPath(); - if (rootPath.endsWith("/")) { - if (deploymentRootPath.startsWith("/")) { - rootPath += deploymentRootPath.substring(1); - } else { - rootPath += deploymentRootPath; - } - } else if (!deploymentRootPath.equals("/")) { - if (!deploymentRootPath.startsWith("/")) { - rootPath += "/"; - } - rootPath += deploymentRootPath; - } - recorder.staticInit(deployment.getDeployment(), rootPath); - standalone.produce(new ResteasyStandaloneBuildItem(deploymentRootPath)); + recorder.staticInit(deployment.getDeployment(), + httpRootPathBuildItem.resolvePath(deployment.getRootPath().substring(1))); + standalone.produce(new ResteasyStandaloneBuildItem(deployment.getRootPath())); } } diff --git a/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/VertxHttpProcessor.java b/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/VertxHttpProcessor.java index f2b3ce5928e8a..a1ef76ed0c7c7 100644 --- a/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/VertxHttpProcessor.java +++ b/extensions/vertx-http/deployment/src/main/java/io/quarkus/vertx/http/deployment/VertxHttpProcessor.java @@ -198,6 +198,7 @@ ServiceStartBuildItem finalizeRouter( LaunchModeBuildItem launchMode, List defaultRoutes, List filters, VertxWebRouterBuildItem httpRouteRouter, + HttpRootPathBuildItem httpRootPathBuildItem, NonApplicationRootPathBuildItem nonApplicationRootPathBuildItem, HttpBuildTimeConfig httpBuildTimeConfig, HttpConfiguration httpConfiguration, List requireBodyHandlerBuildItems, @@ -256,7 +257,8 @@ ServiceStartBuildItem finalizeRouter( recorder.finalizeRouter(beanContainer.getValue(), defaultRoute.map(DefaultRouteBuildItem::getRoute).orElse(null), - listOfFilters, vertx.getVertx(), lrc, mainRouter, httpRouteRouter.getHttpRouter(), httpBuildTimeConfig.rootPath, + listOfFilters, vertx.getVertx(), lrc, mainRouter, httpRouteRouter.getHttpRouter(), + httpRootPathBuildItem.getRootPath(), launchMode.getLaunchMode(), !requireBodyHandlerBuildItems.isEmpty(), bodyHandler, httpConfiguration, gracefulShutdownFilter, shutdownConfig, executorBuildItem.getExecutorProxy()); diff --git a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/HttpBuildTimeConfig.java b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/HttpBuildTimeConfig.java index b8184ff5ed53f..019e99e7ea0e9 100644 --- a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/HttpBuildTimeConfig.java +++ b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/HttpBuildTimeConfig.java @@ -2,12 +2,11 @@ import java.time.Duration; -import org.eclipse.microprofile.config.spi.Converter; - import io.quarkus.runtime.annotations.ConfigItem; import io.quarkus.runtime.annotations.ConfigPhase; import io.quarkus.runtime.annotations.ConfigRoot; import io.quarkus.runtime.annotations.ConvertWith; +import io.quarkus.runtime.configuration.NormalizedHttpPathConverter; import io.vertx.core.http.ClientAuth; @ConfigRoot(name = "http", phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED) @@ -17,7 +16,7 @@ public class HttpBuildTimeConfig { * The HTTP root path. All web content will be served relative to this root path. */ @ConfigItem(defaultValue = "/") - @ConvertWith(NormalizeHttpRootPathConverter.class) + @ConvertWith(NormalizedHttpPathConverter.class) public String rootPath; public AuthConfig auth; @@ -58,32 +57,4 @@ public class HttpBuildTimeConfig { */ @ConfigItem(defaultValue = "30s") public Duration testTimeout; - - /** - * Make sure the HTTP root path is fully normalized and always starts and ends with a '/'. - */ - public static class NormalizeHttpRootPathConverter implements Converter { - - private static final String SLASH = "/"; - - @Override - public String convert(String value) throws IllegalArgumentException, NullPointerException { - if (value == null) { - return SLASH; - } - - value = value.trim(); - if (SLASH.equals(value)) { - return value; - } - if (!value.startsWith(SLASH)) { - value = SLASH + value; - } - if (!value.endsWith(SLASH)) { - value = value + SLASH; - } - - return value; - } - } } diff --git a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/StaticResourcesRecorder.java b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/StaticResourcesRecorder.java index b27894d1bb019..5c9c0061e125e 100644 --- a/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/StaticResourcesRecorder.java +++ b/extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/StaticResourcesRecorder.java @@ -56,7 +56,9 @@ public Consumer start() { StaticHandler staticHandler = StaticHandler.create(META_INF_RESOURCES).setDefaultContentEncoding("UTF-8"); handlers.add(ctx -> { String rel = ctx.mountPoint() == null ? ctx.normalisedPath() - : ctx.normalisedPath().substring(ctx.mountPoint().length() - 1); + : ctx.normalisedPath().substring( + // let's be extra careful here in case Vert.x normalizes the mount points at some point + ctx.mountPoint().endsWith("/") ? ctx.mountPoint().length() - 1 : ctx.mountPoint().length()); if (knownPaths.contains(rel)) { staticHandler.handle(ctx); } else {