Skip to content

Commit 22e50e0

Browse files
dromagnoliaaime
authored andcommitted
Follow up on ticket-941-tilejson
1 parent a607c19 commit 22e50e0

5 files changed

Lines changed: 114 additions & 9 deletions

File tree

geowebcache/wmts/src/main/java/org/geowebcache/service/wmts/WMTSGetCapabilities.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ private void layerResourceUrls(
752752
TileJSONProvider provider = (TileJSONProvider) layer;
753753
String format = "json";
754754
if (provider.supportsTileJSON()) {
755-
String template = baseTemplate + "/tilejson/{style}?format=" + format;
755+
String template = baseTemplate + "/{style}/tilejson?format=" + format;
756756
layerResourceUrlsGen(xml, format, "TileJSON", template);
757757
}
758758
}

geowebcache/wmts/src/main/java/org/geowebcache/service/wmts/WMTSService.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public class WMTSService extends Service {
6363
public static final String GET_TILE = "gettile";
6464
public static final String GET_TILEJSON = "gettilejson";
6565

66+
private static final String STYLE_HINT = ";style=";
67+
6668
enum RequestType {
6769
TILE,
6870
CAPABILITIES,
@@ -345,7 +347,13 @@ public Conveyor getKvpConveyor(
345347
return tile;
346348
} else if (req.equals(GET_TILEJSON)) {
347349
ConveyorTile tile = new ConveyorTile(sb, values.get("layer"), request, response);
348-
tile.setHint(req);
350+
String hint = req;
351+
// I Will need the style when setting up the TileJSON tiles url
352+
String style = values.get("style");
353+
if (style != null) {
354+
hint += (STYLE_HINT + style);
355+
}
356+
tile.setHint(hint);
349357
tile.setRequestHandler(ConveyorTile.RequestHandler.SERVICE);
350358
return tile;
351359
} else {
@@ -583,10 +591,17 @@ public void handleRequest(Conveyor conv) throws OWSException, GeoWebCacheExcepti
583591
ConveyorTile convTile = (ConveyorTile) conv;
584592
WMTSGetFeatureInfo wmsGFI = new WMTSGetFeatureInfo(convTile);
585593
wmsGFI.writeResponse(stats);
586-
} else if (tile.getHint().equals(GET_TILEJSON)) {
594+
} else if (tile.getHint().startsWith(GET_TILEJSON)) {
587595
getSecurityDispatcher().checkSecurity(tile);
588596
ConveyorTile convTile = (ConveyorTile) conv;
589597
TileLayer layer = convTile.getLayer();
598+
String hint = tile.getHint();
599+
String style = null;
600+
int styleIndex = hint.indexOf(STYLE_HINT);
601+
if (styleIndex != -1) {
602+
style = hint.substring(styleIndex + STYLE_HINT.length());
603+
}
604+
590605
if (layer instanceof TileJSONProvider) {
591606
// in GetCapabilities we are adding a TileJSON resource URL
592607
// only when the layer supports TileJSON.
@@ -596,7 +611,7 @@ public void handleRequest(Conveyor conv) throws OWSException, GeoWebCacheExcepti
596611
throw new HttpErrorCodeException(404, "TileJSON Not supported");
597612
}
598613
WMTSTileJSON wmtsTileJSON =
599-
new WMTSTileJSON(convTile, servletBase, context, urlMangler);
614+
new WMTSTileJSON(convTile, servletBase, context, style, urlMangler);
600615
wmtsTileJSON.writeResponse(layer);
601616
}
602617
}

geowebcache/wmts/src/main/java/org/geowebcache/service/wmts/WMTSTileJSON.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,26 @@ public class WMTSTileJSON {
3939

4040
private static Log log = LogFactory.getLog(org.geowebcache.service.wmts.WMTSTileJSON.class);
4141
private final String restBaseUrl;
42+
private String style;
4243
private ConveyorTile convTile;
4344
private static final String ENDING_DIGITS_REGEX = "([0-9]+)$";
4445
private static final Pattern PATTERN_DIGITS = Pattern.compile(ENDING_DIGITS_REGEX);
4546

4647
public WMTSTileJSON(
47-
ConveyorTile convTile, String baseUrl, String contextPath, URLMangler urlMangler) {
48+
ConveyorTile convTile,
49+
String baseUrl,
50+
String contextPath,
51+
String style,
52+
URLMangler urlMangler) {
4853
this.convTile = convTile;
54+
this.style = style;
4955
this.restBaseUrl = urlMangler.buildURL(baseUrl, contextPath, WMTSService.REST_PATH);
5056
}
5157

5258
public void writeResponse(TileLayer layer) {
5359
TileJSONProvider provider = (TileJSONProvider) layer;
5460
TileJSON json = provider.getTileJSON();
55-
// "/{layer}/{tileMatrixSet}/{tileZoom}/{tileRow}/{tileCol}"
61+
5662
List<String> urls = new ArrayList<>();
5763
for (MimeType mimeType : layer.getMimeTypes()) {
5864
Set<String> gridSubSets = layer.getGridSubsets();
@@ -107,11 +113,12 @@ private void addTileUrl(
107113
+ "/"
108114
+ layer.getName()
109115
+ "/"
116+
+ (style != null ? (style + "/") : "")
110117
+ gridSubSet
111118
+ "/"
112119
+ zoomLevelPrefix
113-
+ "{tileZoom}"
114-
+ "/{tileRow}/{tileColumn}"
120+
+ "{z}"
121+
+ "/{y}/{x}"
115122
+ "?format="
116123
+ mimeType;
117124
urls.add(tileUrl);

geowebcache/wmts/src/test/java/org/geowebcache/service/wmts/WMTSRestTest.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
import static org.custommonkey.xmlunit.XMLAssert.assertXpathExists;
1919
import static org.junit.Assert.assertArrayEquals;
2020
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertTrue;
2122
import static org.mockito.ArgumentMatchers.any;
2223
import static org.mockito.ArgumentMatchers.anyInt;
2324
import static org.mockito.ArgumentMatchers.eq;
2425
import static org.mockito.Mockito.mock;
2526
import static org.mockito.Mockito.when;
27+
import static org.mockito.Mockito.withSettings;
2628

2729
import java.io.File;
2830
import java.io.IOException;
@@ -33,10 +35,12 @@
3335
import java.util.HashMap;
3436
import java.util.List;
3537
import java.util.Map;
38+
import java.util.Objects;
3639
import org.apache.commons.httpclient.HttpStatus;
3740
import org.apache.commons.io.FileUtils;
3841
import org.custommonkey.xmlunit.SimpleNamespaceContext;
3942
import org.custommonkey.xmlunit.XMLUnit;
43+
import org.geowebcache.GeoWebCacheException;
4044
import org.geowebcache.config.DefaultGridsets;
4145
import org.geowebcache.config.ServerConfiguration;
4246
import org.geowebcache.config.XMLGridSubset;
@@ -53,8 +57,10 @@
5357
import org.geowebcache.grid.SRS;
5458
import org.geowebcache.io.ByteArrayResource;
5559
import org.geowebcache.io.Resource;
60+
import org.geowebcache.layer.TileJSONProvider;
5661
import org.geowebcache.layer.TileLayer;
5762
import org.geowebcache.layer.TileLayerDispatcher;
63+
import org.geowebcache.layer.meta.TileJSON;
5864
import org.geowebcache.mime.MimeType;
5965
import org.geowebcache.service.OWSException;
6066
import org.geowebcache.stats.RuntimeStats;
@@ -153,12 +159,89 @@ public void testGetTileWithStyle() throws Exception {
153159
assertArrayEquals(getSampleTileContent().getContents(), resp.getContentAsByteArray());
154160
}
155161

162+
@Test
163+
public void testGetTileJSONWithStyle() throws Exception {
164+
addTileLayerJsonMock();
165+
166+
MockHttpServletRequest req = new MockHttpServletRequest();
167+
req.setPathInfo("geowebcache/service/wmts/rest/mockLayerTileJSON/style-a/tilejson");
168+
req.addParameter("format", "json");
169+
MockHttpServletResponse resp = dispatch(req);
170+
171+
assertEquals(200, resp.getStatus());
172+
String content = resp.getContentAsString();
173+
174+
// Checking the response contains a tileUrl with the style
175+
assertTrue(
176+
content.contains(
177+
"\"tiles\":[\"http://localhost/service/wmts/rest/mockLayerTileJSON/style-a/EPSG:900913/EPSG:900913:{z}/{y}/{x}?format=image/png\"]"));
178+
}
179+
180+
@Test
181+
public void testGetTileJSONWithoutStyle() throws Exception {
182+
addTileLayerJsonMock();
183+
184+
MockHttpServletRequest req = new MockHttpServletRequest();
185+
req.setPathInfo("geowebcache/service/wmts/rest/mockLayerTileJSON/tilejson");
186+
req.addParameter("format", "json");
187+
MockHttpServletResponse resp = dispatch(req);
188+
189+
assertEquals(200, resp.getStatus());
190+
String content = resp.getContentAsString();
191+
192+
// Checking the response contains a tileUrl without the style
193+
assertTrue(
194+
content.contains(
195+
"\"tiles\":[\"http://localhost/service/wmts/rest/mockLayerTileJSON/EPSG:900913/EPSG:900913:{z}/{y}/{x}?format=image/png\"]"));
196+
}
197+
198+
private void addTileLayerJsonMock() throws GeoWebCacheException {
199+
200+
final MimeType mimeType1 = MimeType.createFromFormat("image/png");
201+
202+
String layerNameJson = "mockLayerTileJSON";
203+
TileLayer tileLayerJson =
204+
mock(TileLayer.class, withSettings().extraInterfaces(TileJSONProvider.class));
205+
when(tileLayerDispatcher.getTileLayer(eq(layerNameJson))).thenReturn(tileLayerJson);
206+
when(tileLayerJson.getName()).thenReturn(layerNameJson);
207+
when(tileLayerJson.isEnabled()).thenReturn(true);
208+
when(tileLayerJson.isAdvertised()).thenReturn(true);
209+
when(tileLayerJson.getMimeTypes()).thenReturn(Arrays.asList(mimeType1));
210+
TileJSONProvider tileJSONProvider = (TileJSONProvider) tileLayerJson;
211+
when(tileJSONProvider.supportsTileJSON()).thenReturn(true);
212+
when(tileJSONProvider.getTileJSON()).thenReturn(new TileJSON());
213+
214+
String googleMercator = "EPSG:900913";
215+
when(tileLayerJson.getGridSubsets()).thenReturn(Collections.singleton(googleMercator));
216+
GridSubset subset = mock(GridSubset.class);
217+
when(subset.getGridNames()).thenReturn(new String[] {googleMercator + ":1"});
218+
when(tileLayerJson.getGridSubset(eq(googleMercator))).thenReturn(subset);
219+
220+
when(tileLayerDispatcher.getLayerList()).thenReturn(Arrays.asList(tileLayerJson));
221+
}
222+
156223
public MockHttpServletResponse dispatch(MockHttpServletRequest req) throws Exception {
157224
MockHttpServletResponse resp = new MockHttpServletResponse();
158225
try {
159226
final Conveyor conveyor = wmtsService.getConveyor(req, resp);
160227

161228
if (conveyor.reqHandler == Conveyor.RequestHandler.SERVICE) {
229+
final String layerName = conveyor.getLayerId();
230+
231+
final TileLayer layer;
232+
if (Objects.nonNull(layerName)) {
233+
layer = tileLayerDispatcher.getTileLayer(layerName);
234+
if (layer != null && !layer.isEnabled()) {
235+
throw new OWSException(
236+
400,
237+
"InvalidParameterValue",
238+
"LAYERS",
239+
"Layer '" + layerName + "' is disabled");
240+
}
241+
if (conveyor instanceof ConveyorTile) {
242+
((ConveyorTile) conveyor).setTileLayer(layer);
243+
}
244+
}
162245
wmtsService.handleRequest(conveyor);
163246
} else {
164247
ResponseUtils.writeTile(

geowebcache/wmts/src/test/java/org/geowebcache/service/wmts/WMTSServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1560,7 +1560,7 @@ public void testGetCapWithTileJSON() throws Exception {
15601560
+ "[@format='json']"
15611561
+ "[@template='http://localhost:8080/geowebcache"
15621562
+ WMTSService.REST_PATH
1563-
+ "/mockLayer/tilejson/{style}?format=json'])",
1563+
+ "/mockLayer/{style}/tilejson?format=json'])",
15641564
doc));
15651565
}
15661566
}

0 commit comments

Comments
 (0)