Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #28] Prevent merging of CQL filter for multiple layers #29

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/main/java/org/mapfish/print/map/readers/HTTPMapReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.json.JSONArray;
import org.json.JSONException;
import org.mapfish.print.InvalidJsonValueException;
import org.mapfish.print.RenderingContext;
import org.mapfish.print.Transformer;
import org.mapfish.print.map.ParallelMapTileLoader;
import org.mapfish.print.map.renderers.TileRenderer;
import org.mapfish.print.utils.PJsonArray;
import org.mapfish.print.utils.PJsonObject;
import org.pvalsecc.misc.MatchAllSet;
import org.pvalsecc.misc.StringUtils;
Expand All @@ -55,6 +55,13 @@ public abstract class HTTPMapReader extends MapReader {
protected final Map<String, PJsonObject> mergeableParams;
protected final URI baseUrl;
public static final Set<String> OVERRIDE_ALL = new MatchAllSet<String>();
public static final Set<String> NOT_MERGEABLE_PARAMS;

static {
Set<String> temp = new HashSet<>();
temp.add("cql_filter");
NOT_MERGEABLE_PARAMS = Collections.unmodifiableSet(temp);
}

protected HTTPMapReader(RenderingContext context, PJsonObject params) {
super(params);
Expand Down Expand Up @@ -209,8 +216,23 @@ public boolean canMerge(MapReader other) {
HTTPMapReader http = (HTTPMapReader) other;
PJsonObject customParams = params.optJSONObject("customParams");
PJsonObject customParamsOther = http.params.optJSONObject("customParams");
return baseUrl.equals(http.baseUrl) &&
(customParams != null ? customParams.equals(customParamsOther) : customParamsOther == null);
if (!baseUrl.equals(http.baseUrl)) {
return false;
}
if (customParams != null) {
// Do not merge if CQL_FILTER is present because having request for 2 layers with 1 filter is invalid
boolean containsNonMergeableAttribute = false;
Iterator<String> iterator = customParams.keys();

while (iterator.hasNext() && !containsNonMergeableAttribute) {
String next = iterator.next();
if (NOT_MERGEABLE_PARAMS.contains(next.toLowerCase())) {
containsNonMergeableAttribute = true;
}
}
return customParams.equals(customParamsOther) && !containsNonMergeableAttribute;
}
return customParamsOther == null;
} else {
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/org/mapfish/print/MapTestBasic.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public abstract class MapTestBasic {

protected RenderingContext context;

protected RenderingContext emptyContext;

private ThreadResources threadResources;

@Before
Expand Down Expand Up @@ -89,7 +91,9 @@ public void setUp() throws Exception {
hosts.add(HostMatcher.ACCEPT_ALL);
config.setHosts(hosts);
PJsonObject globalParams = createGlobalParams();
PJsonObject emptyParams = new PJsonObject(new JSONObject(), "globalParams");;
context = new RenderingContext(doc, writer, config, globalParams, null, layout, Collections.<String, String>emptyMap());
emptyContext = new RenderingContext(doc, writer, config, emptyParams, null, layout, Collections.<String, String>emptyMap());
} finally {
config.close();
}
Expand Down
39 changes: 36 additions & 3 deletions src/test/java/org/mapfish/print/map/readers/HttpMapReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
package org.mapfish.print.map.readers;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.net.URI;
Expand All @@ -32,6 +34,7 @@
import org.junit.Test;
import org.mapfish.print.FakeHttpd;
import org.mapfish.print.MapTestBasic;
import org.mapfish.print.RenderingContext;
import org.mapfish.print.Transformer;
import org.mapfish.print.map.ParallelMapTileLoader;
import org.mapfish.print.map.renderers.TileRenderer;
Expand All @@ -57,7 +60,17 @@ public void testMergeSomeLayersWithParam() throws Exception {
assertEquals(""+commonURI, "ATTRIBUTE1=1;INCLUDE", parameters.get("CQL_FILTER").get(0));
assertCommonParams(commonURI, parameters, "TRUE");
}


@Test
public void testMergeSomeLayersWithCQLParam() throws Exception {
assertFalse(canBeMerged(loadJson("mergeable/test8.json")));
}

@Test
public void testMergeSomeLayersWithoutCQLParam() throws Exception {
assertTrue(canBeMerged(loadJson("mergeable/test9.json")));
}

@Test
public void testMergeNoLayersWithParam() throws Exception {
URI commonURI = createUri(loadJson("mergeable/test3.json"))[0];
Expand Down Expand Up @@ -118,7 +131,7 @@ private URI[] createUri(PJsonObject jsonParams, FakeHttpd.Route... routes)
PJsonObject layer = layers.getJSONObject(count);
PJsonArray layersInt = layer.getJSONArray("layers");
for(int countInt = 0; countInt < layersInt.size(); countInt++) {
currentReader = createMapReader(layer);
currentReader = createMapReader(layer, context);
if (mapReader == null) {
mapReader = currentReader;
} else {
Expand All @@ -131,8 +144,28 @@ private URI[] createUri(PJsonObject jsonParams, FakeHttpd.Route... routes)
currentReader.createCommonURI(null, "", true) };

}

private boolean canBeMerged(PJsonObject jsonParams, FakeHttpd.Route... routes)
throws JSONException {

PJsonArray layers = jsonParams.getJSONArray("layers");
HTTPMapReader mapReader = null;
HTTPMapReader currentReader = null;
for (int count = 0; count < layers.size(); count++) {
PJsonObject layer = layers.getJSONObject(count);
PJsonArray layersInt = layer.getJSONArray("layers");
for(int countInt = 0; countInt < layersInt.size(); countInt++) {
currentReader = createMapReader(layer, emptyContext);
if (mapReader == null) {
mapReader = currentReader;
}
}
}

return mapReader.testMerge(currentReader);
}

private HTTPMapReader createMapReader(PJsonObject layer) {
private HTTPMapReader createMapReader(PJsonObject layer, RenderingContext context) {
final HTTPMapReader mapReader = new HTTPMapReader(context, layer) {

@Override
Expand Down
44 changes: 44 additions & 0 deletions src/test/resources/mergeable/test8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"layers":[
{
"baseURL":"@@baseURL@@",
"opacity":1,
"type":"WMS",
"layers":[
"layerName1"
],
"format":"image/gif",
"styles":[
"wmsstyle"
],
"singleTile":false,
"customParams":{
"CQL_FILTER":"ATTRIBUTE1=1",
"ENV": "mapstore_language:en",
"EXCEPTIONS": "application/vnd.ogc.se_inimage",
"TILED": true,
"TRANSPARENT": true,
"scaleMethod": "accurate"}
},
{
"baseURL":"@@baseURL@@",
"opacity":1,
"type":"WMS",
"layers":[
"layerName2"
],
"format":"image/gif",
"styles":[
"wmsstyle"
],
"singleTile":false,
"customParams":{
"CQL_FILTER":"ATTRIBUTE1=1",
"ENV": "mapstore_language:en",
"EXCEPTIONS": "application/vnd.ogc.se_inimage",
"TILED": true,
"TRANSPARENT": true,
"scaleMethod": "accurate"}
}
]
}
42 changes: 42 additions & 0 deletions src/test/resources/mergeable/test9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"layers":[
{
"baseURL":"@@baseURL@@",
"opacity":1,
"type":"WMS",
"layers":[
"layerName1"
],
"format":"image/gif",
"styles":[
"wmsstyle"
],
"singleTile":false,
"customParams":{
"ENV": "mapstore_language:en",
"EXCEPTIONS": "application/vnd.ogc.se_inimage",
"TILED": true,
"TRANSPARENT": true,
"scaleMethod": "accurate"}
},
{
"baseURL":"@@baseURL@@",
"opacity":1,
"type":"WMS",
"layers":[
"layerName2"
],
"format":"image/gif",
"styles":[
"wmsstyle"
],
"singleTile":false,
"customParams":{
"ENV": "mapstore_language:en",
"EXCEPTIONS": "application/vnd.ogc.se_inimage",
"TILED": true,
"TRANSPARENT": true,
"scaleMethod": "accurate"}
}
]
}
Loading