Skip to content

Commit

Permalink
gwc - handles workspace-based paths (geoserver#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmauduit committed Aug 23, 2024
1 parent 6bf7536 commit 54b61cb
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 13 deletions.
13 changes: 13 additions & 0 deletions src/apps/geoserver/gwc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.geoserver.cloud.catalog</groupId>
<artifactId>gs-cloud-catalog-plugin</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,66 @@
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.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.autoconfigure.web.servlet.WebMvcRegistrations;
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")
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);
registry.add("geoserver.backend.data-directory.enabled", () -> true);
registry.add("spring.cloud.config.enabled", () -> false);
registry.add("spring.cloud.config.discovery.enabled", () -> false);
registry.add("spring.cloud.discovery.enabled", () -> false);
registry.add("spring.cloud.bus.enabled", () -> false);
registry.add("eureka.client.enabled", () -> false);
registry.add("gwc.web-ui", () -> true);
}

@BeforeEach
void before() {
restTemplate = restTemplate.withBasicAuth("admin", "geoserver");
String rootUri = restTemplate.getRootUri();
assertThat(rootUri).isNotEmpty();
CatalogTestData data =
CatalogTestData.initialized(() -> rawCatalog, () -> geoServer).initialize();
}

@Test
Expand All @@ -53,6 +91,26 @@ void testRESTPathExtensionContentNegotiation() {
testGetRequestContentType("/gwc/rest/layers.xml", APPLICATION_XML);
}

@Test
void testGwcUrlHandlerMappingArePresentInTheClasspathAndLocalWorkspaceUrlsAreHandled() {
assertThat(context.isTypeMatch("gwcDemoUrlHandlerMapping", WebMvcRegistrations.class))
.as("expected a bean gwcDemoUrlHandlerMapping of type GwcUrlHandlerMapping")
.isTrue();
assertThat(context.isTypeMatch("gwcRestWebUrlHandlerMapping", WebMvcRegistrations.class))
.as("expected a bean gwcRestWebUrlHandlerMapping of type GwcUrlHandlerMapping")
.isTrue();

ResponseEntity<String> 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<String> testGetRequestContentType(String uri, MediaType expected) {
ResponseEntity<String> response = restTemplate.getForEntity(uri, String.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@

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;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.annotation.PostConstruct;

Expand All @@ -32,13 +35,51 @@ 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
* <!-- Used for workspace-based demo requests so the requests to GS stay workspace-based -->
* <bean id="gwcDemoUrlHandlerMapping"
* class="org.geoserver.gwc.controller.GwcUrlHandlerMapping"> <constructor-arg ref="catalog" />
* <constructor-arg value="/gwc/demo"/> <property name="alwaysUseFullPath" value="true" />
* <property name="order" value="10" /> </bean>
* <!-- Used for workspace-based web requests (i.e. for rest/web/openlayer/ol.js) -->
* <bean id="gwcRestWebUrlHandlerMapping"
* class="org.geoserver.gwc.controller.GwcUrlHandlerMapping"> <constructor-arg ref="catalog" />
* <constructor-arg type="java.lang.String" value="/gwc/rest/web"/> <property
* name="alwaysUseFullPath" value="true" /> <property name="order" value="10" /> </bean>
*/
@Bean
WebMvcRegistrations gwcDemoUrlHandlerMapping(Catalog catalog) {
GwcUrlHandlerMapping handler = new GwcUrlHandlerMapping(catalog, "/gwc/demo");
handler.setAlwaysUseFullPath(true);
handler.setOrder(10);

return new WebMvcRegistrations() {
@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
return handler;
}
};
}

@Bean
WebMvcRegistrations gwcRestWebUrlHandlerMapping(Catalog catalog) {
GwcUrlHandlerMapping handler = new GwcUrlHandlerMapping(catalog, "/gwc/rest/web");
handler.setAlwaysUseFullPath(true);
handler.setOrder(10);

return new WebMvcRegistrations() {
@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
return handler;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.geoserver.cloud.autoconfigure.gwc.web.gwc;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.geoserver.cloud.autoconfigure.gwc.GeoWebCacheContextRunner;
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.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;

import java.io.File;

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"));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,13 @@
* <p>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);
Expand Down

0 comments on commit 54b61cb

Please sign in to comment.