From 8a61490e711ca24d444231bb896f6801674d9c32 Mon Sep 17 00:00:00 2001 From: Pierre Mauduit Date: Mon, 29 Jan 2024 14:36:37 +0100 Subject: [PATCH] gwc - handles workspace-based paths (#380) --- src/apps/geoserver/gwc/pom.xml | 13 +++++ .../gwc/app/GeoWebCacheApplicationTest.java | 49 +++++++++++++++++++ .../gwc/GeoWebCacheUIAutoConfiguration.java | 37 ++++++++++++-- .../GeoWebCacheUIAutoConfigurationTest.java | 44 +++++++++++++++++ .../gwc/web/rest/GeoWebCacheController.java | 10 +--- 5 files changed, 140 insertions(+), 13 deletions(-) create mode 100644 src/gwc/autoconfigure/src/test/java/org/geoserver/cloud/autoconfigure/gwc/web/gwc/GeoWebCacheUIAutoConfigurationTest.java diff --git a/src/apps/geoserver/gwc/pom.xml b/src/apps/geoserver/gwc/pom.xml index 2851d456c..4caacfb70 100644 --- a/src/apps/geoserver/gwc/pom.xml +++ b/src/apps/geoserver/gwc/pom.xml @@ -49,6 +49,19 @@ spring-boot-starter-test test + + org.geoserver.cloud.catalog + gs-cloud-catalog-plugin + ${project.version} + test-jar + test + + + * + * + + + diff --git a/src/apps/geoserver/gwc/src/test/java/org/geoserver/cloud/gwc/app/GeoWebCacheApplicationTest.java b/src/apps/geoserver/gwc/src/test/java/org/geoserver/cloud/gwc/app/GeoWebCacheApplicationTest.java index 870631ea3..54197120b 100644 --- a/src/apps/geoserver/gwc/src/test/java/org/geoserver/cloud/gwc/app/GeoWebCacheApplicationTest.java +++ b/src/apps/geoserver/gwc/src/test/java/org/geoserver/cloud/gwc/app/GeoWebCacheApplicationTest.java @@ -11,17 +11,28 @@ import com.google.gson.JsonElement; import com.google.gson.JsonParser; +import org.geoserver.catalog.Catalog; +import org.geoserver.catalog.CatalogTestData; +import org.geoserver.config.GeoServer; +import org.geoserver.gwc.controller.GwcUrlHandlerMapping; import org.json.simple.parser.ParseException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.context.ApplicationContext; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; + +import java.io.File; @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @ActiveProfiles("test") @@ -29,11 +40,29 @@ class GeoWebCacheApplicationTest { @Autowired private TestRestTemplate restTemplate; + @Autowired private Catalog catalog; + @Autowired private GeoServer geoServer; + + @Qualifier("rawCatalog") + @Autowired + Catalog rawCatalog; + + @Autowired private ApplicationContext context; + + @TempDir static File tmpDir; + + @DynamicPropertySource + static void registerPgProperties(DynamicPropertyRegistry registry) { + registry.add("geoserver.backend.data-directory.location", tmpDir::getAbsolutePath); + } + @BeforeEach void before() { restTemplate = restTemplate.withBasicAuth("admin", "geoserver"); String rootUri = restTemplate.getRootUri(); assertThat(rootUri).isNotEmpty(); + CatalogTestData data = + CatalogTestData.initialized(() -> rawCatalog, () -> geoServer).initialize(); } @Test @@ -54,6 +83,26 @@ void testRESTPathExtensionContentNegotiation() { testGetRequestContentType("/gwc/rest/layers.xml", APPLICATION_XML); } + @Test + void testGwcUrlHandlerMappingArePresentInTheClasspathAndLocalWorkspaceUrlsAreHandled() { + assertThat(context.isTypeMatch("gwcDemoUrlHandlerMapping", GwcUrlHandlerMapping.class)) + .as("expected a bean gwcDemoUrlHandlerMapping of type GwcUrlHandlerMapping") + .isTrue(); + assertThat(context.isTypeMatch("gwcRestWebUrlHandlerMapping", GwcUrlHandlerMapping.class)) + .as("expected a bean gwcRestWebUrlHandlerMapping of type GwcUrlHandlerMapping") + .isTrue(); + + ResponseEntity response = + restTemplate.getForEntity("/wsName/gwc/demo/layer1", String.class); + + assertThat(response.getStatusCode()) + .as( + String.format( + "expected a HTTP return code 200, got %s", + response.getStatusCode())) + .isEqualTo(HttpStatus.OK); + } + protected ResponseEntity testGetRequestContentType(String uri, MediaType expected) { ResponseEntity response = restTemplate.getForEntity(uri, String.class); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); diff --git a/src/gwc/autoconfigure/src/main/java/org/geoserver/cloud/autoconfigure/web/gwc/GeoWebCacheUIAutoConfiguration.java b/src/gwc/autoconfigure/src/main/java/org/geoserver/cloud/autoconfigure/web/gwc/GeoWebCacheUIAutoConfiguration.java index 39f5aee81..7e1002e52 100644 --- a/src/gwc/autoconfigure/src/main/java/org/geoserver/cloud/autoconfigure/web/gwc/GeoWebCacheUIAutoConfiguration.java +++ b/src/gwc/autoconfigure/src/main/java/org/geoserver/cloud/autoconfigure/web/gwc/GeoWebCacheUIAutoConfiguration.java @@ -6,9 +6,10 @@ import lombok.extern.slf4j.Slf4j; -import org.geoserver.cloud.autoconfigure.gwc.ConditionalOnGeoWebCacheRestConfigEnabled; +import org.geoserver.catalog.Catalog; import org.geoserver.cloud.autoconfigure.gwc.ConditionalOnWebUIEnabled; import org.geoserver.cloud.gwc.config.core.GeoWebCacheConfigurationProperties; +import org.geoserver.gwc.controller.GwcUrlHandlerMapping; import org.geowebcache.GeoWebCacheDispatcher; import org.geowebcache.rest.controller.ByteStreamController; import org.gwc.web.rest.GeoWebCacheController; @@ -32,13 +33,39 @@ GeoWebCacheController gwcController(GeoWebCacheDispatcher gwcDispatcher) { return new GeoWebCacheController(gwcDispatcher); } - /** - * Provide a handler for static web resources if missing, for example, because {@link - * ConditionalOnGeoWebCacheRestConfigEnabled} is disabled - */ + /** ConditionalOnGeoWebCacheRestConfigEnabled} is disabled */ @Bean @ConditionalOnMissingBean(ByteStreamController.class) ByteStreamController byteStreamController() { return new ByteStreamController(); } + + /** + * GS's src/web/gwc/src/main/java/applicationContext.xml + * + * + * + * + * + * + * + */ + @Bean + GwcUrlHandlerMapping gwcDemoUrlHandlerMapping(Catalog catalog) { + GwcUrlHandlerMapping handler = new GwcUrlHandlerMapping(catalog, "/gwc/demo"); + handler.setAlwaysUseFullPath(true); + handler.setOrder(10); + return handler; + } + + @Bean + GwcUrlHandlerMapping gwcRestWebUrlHandlerMapping(Catalog catalog) { + GwcUrlHandlerMapping handler = new GwcUrlHandlerMapping(catalog, "/gwc/rest/web"); + handler.setAlwaysUseFullPath(true); + handler.setOrder(10); + return handler; + } } diff --git a/src/gwc/autoconfigure/src/test/java/org/geoserver/cloud/autoconfigure/gwc/web/gwc/GeoWebCacheUIAutoConfigurationTest.java b/src/gwc/autoconfigure/src/test/java/org/geoserver/cloud/autoconfigure/gwc/web/gwc/GeoWebCacheUIAutoConfigurationTest.java new file mode 100644 index 000000000..3cc1bfed5 --- /dev/null +++ b/src/gwc/autoconfigure/src/test/java/org/geoserver/cloud/autoconfigure/gwc/web/gwc/GeoWebCacheUIAutoConfigurationTest.java @@ -0,0 +1,44 @@ +package org.geoserver.cloud.autoconfigure.gwc.web.gwc; + +import org.geoserver.cloud.autoconfigure.gwc.GeoWebCacheContextRunner; +import org.geoserver.cloud.autoconfigure.gwc.blobstore.AzureBlobstoreAutoConfiguration; +import org.geoserver.cloud.autoconfigure.web.gwc.GeoWebCacheUIAutoConfiguration; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; +import org.springframework.context.ApplicationContext; + +import java.io.File; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + + +public class GeoWebCacheUIAutoConfigurationTest { + + WebApplicationContextRunner runner; + + @TempDir + File tmpDir; + + @BeforeEach + void setUp() throws Exception { + runner = + GeoWebCacheContextRunner.newMinimalGeoWebCacheContextRunner(tmpDir) + .withPropertyValues("gwc.web-ui=true") + .withConfiguration( + AutoConfigurations.of(GeoWebCacheUIAutoConfiguration.class)); + } + + @Test + void beansForLocalWorkspacePathsHandlingArePresent() { + runner.run(context -> { + assertNotNull(context.getBean("gwcDemoUrlHandlerMapping")); + assertNotNull(context.getBean("gwcRestWebUrlHandlerMapping")); + }); + + } +} diff --git a/src/gwc/services/src/main/java/org/gwc/web/rest/GeoWebCacheController.java b/src/gwc/services/src/main/java/org/gwc/web/rest/GeoWebCacheController.java index 021920b80..5b8cc0400 100644 --- a/src/gwc/services/src/main/java/org/gwc/web/rest/GeoWebCacheController.java +++ b/src/gwc/services/src/main/java/org/gwc/web/rest/GeoWebCacheController.java @@ -25,19 +25,13 @@ *

Copied from {@link GeoServerGWCDispatcherController} */ @Controller -@RequestMapping("/gwc") +@RequestMapping(path = {"/gwc", "/{namespace}/gwc"}) @RequiredArgsConstructor public class GeoWebCacheController { private final @NonNull GeoWebCacheDispatcher gwcDispatcher; - @GetMapping( - path = { - "", - "/home", - "/demo/**", - "/proxy/**", - }) + @GetMapping(path = {"", "/home", "/demo/**", "/proxy/**"}) public void handleGet(HttpServletRequest request, HttpServletResponse response) throws Exception { gwcDispatcher.handleRequest(request, response);