Skip to content

Commit bfea71c

Browse files
committed
fix wrong server url that contains unexpected params
1 parent e10c0ca commit bfea71c

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

server/src/main/java/au/org/aodn/ogcapi/server/core/service/wfs/WfsServer.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,15 @@ public WfsServer(Search search,
8484
* Build WFS GetFeature URL
8585
*/
8686
protected String createWfsRequestUrl(String wfsUrl, String layerName, List<String> fields, String cqlFilter, String outputFormat) {
87-
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(wfsUrl)
88-
.scheme("https"); // Force HTTPS to fix redirect
87+
UriComponents components = UriComponentsBuilder.fromUriString(wfsUrl).build();
88+
UriComponentsBuilder builder = UriComponentsBuilder.newInstance()
89+
.scheme("https") // Force HTTPS to fix redirect
90+
.host(components.getHost())
91+
.path(Objects.requireNonNull(components.getPath()));
92+
93+
if (components.getPort() != -1) {
94+
builder.port(components.getPort());
95+
}
8996

9097
Map<String, String> param = new HashMap<>(wfsDefaultParam.getDownload());
9198
param.put("typeName", layerName);
@@ -446,6 +453,7 @@ public Optional<String> getFeatureServerUrlByTitleOrQueryParam(String collection
446453
public Optional<String> getFeatureServerUrl(String collectionId, String layerName) {
447454
Optional<String> url = getFeatureServerUrlByTitleOrQueryParam(collectionId, layerName);
448455
if (url.isPresent()) {
456+
log.debug("Found WFS link by title/query param for collectionId {} with layerName {}: {}", collectionId, layerName, url.get());
449457
return url;
450458
}
451459

server/src/test/java/au/org/aodn/ogcapi/server/core/service/wfs/WfsServerTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,50 @@ void createFeatureFieldQueryUrl_buildsCorrectUrlWithTypename() {
349349
assertTrue(result.contains("REQUEST=DescribeFeatureType")); // original one is replaced
350350
}
351351

352+
@Test
353+
void createWfsRequestUrl_stripsOldParamsFromServerUrl() {
354+
// The server URL already has query params that would cause duplicates
355+
String serverUrlWithParams = "https://geoserver.imas.utas.edu.au/geoserver/seamap/wfs"
356+
+ "?version=1.0.0&request=GetFeature&typeName=SeamapAus_VIC_statewide_habitats_2023&outputFormat=SHAPE-ZIP";
357+
358+
String layerName = "seamap:SeamapAus_VIC_statewide_habitats_2023";
359+
String outputFormat = "text/csv";
360+
361+
WfsServer server = new WfsServer(mockSearch, restTemplate, new RestTemplateUtils(restTemplate), entity, wfsDefaultParam);
362+
String result = server.createWfsRequestUrl(serverUrlWithParams, layerName, null, null, outputFormat);
363+
364+
assertNotNull(result);
365+
366+
// Old param values from the server URL must NOT appear
367+
assertFalse(result.contains("SHAPE-ZIP"), "Old outputFormat value should be removed");
368+
assertFalse(result.contains("typeName=SeamapAus_VIC_statewide_habitats_2023&"),
369+
"Old typeName (without namespace prefix) should be removed");
370+
371+
// New param values must be present
372+
assertTrue(result.contains("typeName=seamap:SeamapAus_VIC_statewide_habitats_2023"),
373+
"New typeName with namespace prefix should be present");
374+
assertTrue(result.contains("outputFormat=text/csv"),
375+
"New outputFormat should be present");
376+
377+
// Default download params from config
378+
assertTrue(result.contains("SERVICE=WFS"), "SERVICE param should be present");
379+
assertTrue(result.contains("VERSION=1.0.0"), "VERSION param should be present");
380+
assertTrue(result.contains("REQUEST=GetFeature"), "REQUEST param should be present");
381+
382+
// No duplicate keys — each param name should appear exactly once
383+
String query = result.substring(result.indexOf('?') + 1);
384+
String[] pairs = query.split("&");
385+
long typeNameCount = java.util.Arrays.stream(pairs).filter(p -> p.toLowerCase().startsWith("typename=")).count();
386+
long outputFormatCount = java.util.Arrays.stream(pairs).filter(p -> p.toLowerCase().startsWith("outputformat=")).count();
387+
long versionCount = java.util.Arrays.stream(pairs).filter(p -> p.toLowerCase().startsWith("version=")).count();
388+
long requestCount = java.util.Arrays.stream(pairs).filter(p -> p.toLowerCase().startsWith("request=")).count();
389+
390+
assertEquals(1, typeNameCount, "typeName should appear exactly once");
391+
assertEquals(1, outputFormatCount, "outputFormat should appear exactly once");
392+
assertEquals(1, versionCount, "VERSION should appear exactly once");
393+
assertEquals(1, requestCount, "REQUEST should appear exactly once");
394+
}
395+
352396
@Test
353397
void createCapabilitiesQueryUrl_buildsCorrectUrl() {
354398
// arrange

0 commit comments

Comments
 (0)