Skip to content

Commit

Permalink
GEOS-10006 Seeding GWC doesn't work for layers with a dot in the name
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike authored and aaime committed Mar 20, 2023
1 parent d34c6fe commit e171490
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,23 @@ public ResponseEntity<?> doGet(HttpServletRequest req) {

/** GET method for querying running tasks for the provided layer */
@RequestMapping(
value = "/seed/{layer}.json",
value = "/seed/{layer:.+}.json",
method = RequestMethod.GET,
produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_PLAIN_VALUE})
public ResponseEntity<?> doGet(HttpServletRequest req, @PathVariable String layer) {
return seedService.getRunningLayerTasks(req, layer);
public ResponseEntity<?> doGet(@PathVariable String layer) {
return seedService.getRunningLayerTasks(layer);
}

@RequestMapping(
value = "/seed/{layer:.+}.xml",
method = RequestMethod.GET,
produces = {MediaType.APPLICATION_XML_VALUE, MediaType.TEXT_PLAIN_VALUE})
public ResponseEntity<?> doGetXml(@PathVariable String layer) {
return seedService.getRunningLayerTasksXml(layer);
}

/** GET method for displaying the GeoWebCache UI form. */
@RequestMapping(value = "/seed/{layer}", method = RequestMethod.GET)
@RequestMapping(value = "/seed/{layer:.+}", method = RequestMethod.GET)
public ResponseEntity<?> doFormGet(HttpServletRequest request, @PathVariable String layer) {
return formService.handleGet(request, layer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,42 +79,57 @@ public ResponseEntity<?> getRunningTasks(HttpServletRequest request) {
XStream xs = new GeoWebCacheXStream(new JsonHierarchicalStreamDriver());
long[][] list = seeder.getStatusList();
JSONObject obj = new JSONObject(xs.toXML(list));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return new ResponseEntity<>(obj.toString(), headers, HttpStatus.OK);
return constructResponse(MediaType.APPLICATION_JSON, obj.toString(), HttpStatus.OK);
} catch (JSONException jse) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
return new ResponseEntity<Object>("error", headers, HttpStatus.INTERNAL_SERVER_ERROR);
return constructResponse(
MediaType.TEXT_PLAIN, "error", HttpStatus.INTERNAL_SERVER_ERROR);
}
}

/** GET method for querying running tasks for the provided layer */
public ResponseEntity<?> getRunningLayerTasks(HttpServletRequest request, String layer) {
public ResponseEntity<?> getRunningLayerTasks(String layer) {
try {
XStream xs = new GeoWebCacheXStream(new JsonHierarchicalStreamDriver());
long[][] list;
if (null == layer) {
list = seeder.getStatusList();
} else {
try {
seeder.findTileLayer(layer);
} catch (GeoWebCacheException e) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
return new ResponseEntity<>(e.getMessage(), headers, HttpStatus.BAD_REQUEST);
}
list = seeder.getStatusList(layer);
try {
list = findTileLayer(layer);
} catch (GeoWebCacheException e) {
return constructResponse(
MediaType.TEXT_PLAIN, e.getMessage(), HttpStatus.BAD_REQUEST);
}
JSONObject obj = new JSONObject(xs.toXML(list));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return new ResponseEntity<>(obj.toString(), headers, HttpStatus.OK);
return constructResponse(MediaType.APPLICATION_JSON, obj.toString(), HttpStatus.OK);
} catch (JSONException jse) {
LOG.log(Level.SEVERE, jse.getMessage(), jse);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
return new ResponseEntity<Object>("error", headers, HttpStatus.INTERNAL_SERVER_ERROR);
return constructResponse(
MediaType.TEXT_PLAIN, "error", HttpStatus.INTERNAL_SERVER_ERROR);
}
}

public ResponseEntity<?> getRunningLayerTasksXml(String layer) {
XStream xs = new GeoWebCacheXStream();
long[][] list;
try {
list = findTileLayer(layer);
} catch (GeoWebCacheException e) {
return constructResponse(MediaType.TEXT_PLAIN, e.getMessage(), HttpStatus.BAD_REQUEST);
}
return constructResponse(MediaType.APPLICATION_XML, xs.toXML(list), HttpStatus.OK);
}

private ResponseEntity<?> constructResponse(
MediaType mediaType, String message, HttpStatus status) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(mediaType);
return new ResponseEntity<>(message, headers, status);
}

private long[][] findTileLayer(String layer) throws GeoWebCacheException {
if (null == layer) {
return seeder.getStatusList();
} else {
seeder.findTileLayer(layer);
return seeder.getStatusList(layer);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
*/
package org.geowebcache.rest.controller;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.endsWith;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.doReturn;
Expand All @@ -25,11 +27,14 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import javax.servlet.http.HttpServletRequest;
import org.geowebcache.rest.service.FormService;
import org.geowebcache.rest.service.SeedService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -52,6 +57,8 @@ public class SeedControllerTest {

@Autowired private WebApplicationContext wac;

@Mock @Autowired private SeedService seedService;

@InjectMocks @Autowired SeedController controller;

@Spy @Autowired FormService formService;
Expand Down Expand Up @@ -89,12 +96,70 @@ public void testSeedLayerNameWithDots() throws Exception {
String layerName = "test:mock.layer.name";
// when(formService.handleFormPost(eq(layerName),
// anyMap())).thenReturn(ResponseEntity.ok(null));
testGet(layerName);
testGetJson(layerName);
testGetXml(layerName);
testPost(layerName);
}

private void testPost(String layerName) throws Exception {
doReturn(ResponseEntity.ok(null)).when(formService).handleFormPost(anyString(), anyMap());

mockMvc.perform(
post("/rest/seed/{layer}", layerName)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.accept(MediaType.TEXT_XML))
.andExpect(status().isOk());
verify(formService).handleFormPost(eq(layerName), anyMap());
}

private void testGet(String layerName) throws Exception {
doReturn(ResponseEntity.ok(null))
.when(formService)
.handleGet(any(HttpServletRequest.class), anyString());

mockMvc.perform(
get("/rest/seed/{layer}", layerName)
.contentType(MediaType.TEXT_PLAIN)
.accept(MediaType.TEXT_HTML))
.andExpect(status().isOk());

verify(formService).handleGet(any(HttpServletRequest.class), eq(layerName));
}

private void testGetJson(String layerName) throws Exception {
doReturn(ResponseEntity.ok(null))
.when(formService)
.handleGet(any(HttpServletRequest.class), anyString());

doReturn(ResponseEntity.ok(null))
.when(seedService)
.getRunningLayerTasksXml(endsWith(".json"));

mockMvc.perform(
get("/rest/seed/{layer}.json", layerName)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());

verify(controller.seedService).getRunningLayerTasks(eq(layerName));
}

private void testGetXml(String layerName) throws Exception {
doReturn(ResponseEntity.ok(null))
.when(formService)
.handleGet(any(HttpServletRequest.class), anyString());

doReturn(ResponseEntity.ok(null))
.when(seedService)
.getRunningLayerTasksXml(endsWith(".xml"));

mockMvc.perform(
get("/rest/seed/{layer}.xml", layerName)
.contentType(MediaType.APPLICATION_XML)
.accept(MediaType.APPLICATION_XML))
.andExpect(status().isOk());

verify(controller.seedService).getRunningLayerTasks(eq(layerName));
}
}

0 comments on commit e171490

Please sign in to comment.