Skip to content

Commit d598f02

Browse files
committed
HTM-1927: Fix styled legend requests for proxied layer are not using proxied urls
1 parent d462f69 commit d598f02

2 files changed

Lines changed: 47 additions & 18 deletions

File tree

src/main/java/org/tailormap/api/persistence/helper/ApplicationHelper.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.slf4j.LoggerFactory;
3232
import org.springframework.stereotype.Service;
3333
import org.springframework.transaction.annotation.Transactional;
34+
import org.springframework.web.util.UriComponentsBuilder;
3435
import org.tailormap.api.controller.GeoServiceProxyController;
3536
import org.tailormap.api.persistence.Application;
3637
import org.tailormap.api.persistence.Configuration;
@@ -48,6 +49,7 @@
4849
import org.tailormap.api.persistence.json.GeoServiceLayerSettings;
4950
import org.tailormap.api.persistence.json.ServicePublishingSettings;
5051
import org.tailormap.api.persistence.json.TileLayerHiDpiMode;
52+
import org.tailormap.api.persistence.json.WMSStyle;
5153
import org.tailormap.api.repository.ApplicationRepository;
5254
import org.tailormap.api.repository.ConfigurationRepository;
5355
import org.tailormap.api.repository.FeatureSourceRepository;
@@ -191,6 +193,23 @@ private String getLegendProxyUrl(Application application, AppTreeLayerNode appTr
191193
+ "/" + LEGEND.getValue();
192194
}
193195

196+
private List<WMSStyle> getProxiedLegendStyles(
197+
Application application, AppTreeLayerNode appTreeLayerNode, List<WMSStyle> legendStyles) {
198+
String legendProxyUrl = getLegendProxyUrl(application, appTreeLayerNode);
199+
return legendStyles.stream()
200+
.map(style ->
201+
// Create a copy of the style so we don't mutate configuration objects
202+
new WMSStyle(
203+
style.getName(),
204+
style.getTitle(),
205+
style.getAbstractText(),
206+
UriComponentsBuilder.fromUriString(legendProxyUrl)
207+
.queryParam("STYLE", style.getName())
208+
.build(true)
209+
.toUri()))
210+
.toList();
211+
}
212+
194213
private class MapResponseLayerBuilder {
195214
private final Application app;
196215
private final MapResponse mapResponse;
@@ -404,6 +423,13 @@ private boolean addAppLayerItem(AppTreeLayerNode layerRef) {
404423
}
405424
}
406425

426+
List<WMSStyle> legendStyles = appLayerSettings.getSelectedStyles();
427+
if (proxied && legendStyles != null) {
428+
// when proxied the urls must be passed through getProxiedLegendStyles to be accessible,
429+
// so don't pass the original style URLs to the frontend
430+
legendStyles = getProxiedLegendStyles(app, layerRef, legendStyles);
431+
}
432+
407433
SearchIndex searchIndex = null;
408434
if (appLayerSettings.getSearchIndexId() != null) {
409435
searchIndex = searchIndexRepository
@@ -450,7 +476,7 @@ private boolean addAppLayerItem(AppTreeLayerNode layerRef) {
450476
.webMercatorAvailable(webMercatorAvailable)
451477
.tileset3dStyle(appLayerSettings.getTileset3dStyle())
452478
.hiddenFunctionality(appLayerSettings.getHiddenFunctionality())
453-
.styles(appLayerSettings.getSelectedStyles()));
479+
.styles(legendStyles));
454480

455481
return true;
456482
}

src/test/java/org/tailormap/api/controller/ViewerControllerIntegrationTest.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.tailormap.api.controller;
77

88
import static org.hamcrest.Matchers.contains;
9+
import static org.hamcrest.Matchers.containsString;
910
import static org.hamcrest.Matchers.endsWith;
1011
import static org.hamcrest.Matchers.nullValue;
1112
import static org.hamcrest.Matchers.startsWith;
@@ -110,22 +111,13 @@ void should_contain_proxy_url() throws Exception {
110111
contains(
111112
endsWith(
112113
"/app/default/layer/lyr%3Apdok-kadaster-bestuurlijkegebieden%3AProvinciegebied/proxy/wms"))))
113-
114-
// Backend does not save legendImageUrl from capabilities, and therefore also does not
115-
// replace it with a proxied version. Frontend will use normal URL to create standard WMS
116-
// GetLegendGraphic request, which usually works fine
117-
// Old 10.0 code:
118-
// https://github.com/Tailormap/tailormap-api/blob/tailormap-api-10.0.0/src/main/java/nl/b3p/tailormap/api/controller/MapController.java#L440
119-
// .andExpect(
120-
// jsonPath("$.appLayers[?(@.id ===
121-
// 'lyr:pdok-kadaster-bestuurlijkegebieden:Provinciegebied')].legendImageUrl")
122-
// .value(
123-
// contains(
124-
// allOf(
125-
//
126-
// containsString("/app/default/layer/lyr%3Apdok-kadaster-bestuurlijkegebieden%3AProvinciegebied/proxy/wms"),
127-
// containsString("request=GetLegendGraphic")))))
128-
;
114+
.andExpect(
115+
jsonPath(
116+
"$.appLayers[?(@.id === 'lyr:pdok-kadaster-bestuurlijkegebieden:Provinciegebied')].legendImageUrl")
117+
.value(
118+
contains(
119+
containsString(
120+
"/app/default/layer/lyr%3Apdok-kadaster-bestuurlijkegebieden%3AProvinciegebied/proxy/legend"))));
129121
}
130122

131123
@Test
@@ -156,7 +148,18 @@ void should_contain_proxied_secured_service_layer_with_styles() throws Exception
156148
.value("purple_polygon"))
157149
.andExpect(jsonPath(
158150
"$.appLayers[?(@.id === 'lyr:snapshot-geoserver-proxied:postgis:begroeidterreindeel')].styles[0].name")
159-
.value("purple_polygon"));
151+
.value("purple_polygon"))
152+
.andExpect(
153+
jsonPath(
154+
"$.appLayers[?(@.id === 'lyr:snapshot-geoserver-proxied:postgis:begroeidterreindeel')].styles[0].legendUrl")
155+
.value(
156+
// Need to use (extra) contains() because jsonPath() returns an array even when
157+
// the expression resolves to a single scalar property
158+
// see
159+
// https://github.com/authorjapps/zerocode/wiki/When-JSON-Path-Matching-returns-value-or-values-as-an-array
160+
contains(
161+
containsString(
162+
"/app/secured/layer/lyr%3Asnapshot-geoserver-proxied%3Apostgis%3Abegroeidterreindeel/proxy/legend?STYLE=purple_polygon"))));
160163
}
161164

162165
@Test

0 commit comments

Comments
 (0)