Skip to content

Commit

Permalink
[GEOS-9003] Added javadoc and more unit tests for getting the raw lay…
Browse files Browse the repository at this point in the history
…er index.
  • Loading branch information
sikeoka committed Nov 19, 2018
1 parent 49d3465 commit 3431d61
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 43 deletions.
49 changes: 32 additions & 17 deletions src/wms/src/main/java/org/geoserver/wms/WMSRequests.java
Original file line number Diff line number Diff line change
Expand Up @@ -437,28 +437,43 @@ static LinkedHashMap<String, String> getGetMapParams(
return params;
}

/**
* Layer groups are expanded into their component layers very early in the WMS GetMap request
* handling process. This method is intended to reverse that process and find the index in the
* raw layers KVP parameter that corresponds to a layer index after layer groups are expanded.
*
* @param req the WMS GetMap request
* @param layerIndex the layer index in the expanded layers list
* @return the layer index in the raw layers list
* @throws IllegalArgumentException if unable to determine the raw layer index
*/
@SuppressWarnings("unchecked")
private static int getRawLayerIndex(GetMapRequest req, int layerIndex) {
List<String> names = KvpUtils.readFlat(req.getRawKvp().get("layers"));
if (names.size() != req.getLayers().size()) {
// Finds the index in the raw layers KVP parameter that corresponds
// to the layer index after layer groups are expanded.
List<?> layers =
new LayerParser(WMS.get())
.parseLayers(names, req.getRemoteOwsURL(), req.getRemoteOwsType());
int numLayers = 0;
for (int index = 0; index < layers.size(); index++) {
if (layers.get(index) instanceof LayerGroupInfo) {
numLayers += ((LayerGroupInfo) layers.get(index)).layers().size();
} else {
numLayers++;
}
if (numLayers > layerIndex) {
return index;
}
if (names.size() == 1) {
// single layer or layer group
return 0;
} else if (names.size() == req.getLayers().size()) {
// multiple layers without any layer groups
return layerIndex;
}
// layer group and one or more additional layers and/or layer groups
List<?> layers =
new LayerParser(WMS.get())
.parseLayers(names, req.getRemoteOwsURL(), req.getRemoteOwsType());
int numLayers = 0;
for (int index = 0; index < layers.size(); index++) {
if (layers.get(index) instanceof LayerGroupInfo) {
numLayers += ((LayerGroupInfo) layers.get(index)).layers().size();
} else {
numLayers++;
}
if (numLayers > layerIndex) {
return index;
}
}
return layerIndex;
throw new IllegalArgumentException(
"Unable to determine raw index for " + layerIndex + " in " + names);
}

/**
Expand Down
109 changes: 83 additions & 26 deletions src/wms/src/test/java/org/geoserver/wms/WMSRequestsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
*/
package org.geoserver.wms;

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

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.xml.namespace.QName;
import org.geoserver.data.test.MockData;
import org.geoserver.data.test.SystemTestData;
import org.geoserver.ows.util.KvpMap;
import org.geoserver.ows.util.ResponseUtils;
import org.junit.Test;

public class WMSRequestsTest extends WMSTestSupport {
Expand Down Expand Up @@ -48,33 +51,62 @@ public void testGetGetMapUrlWithDimensions() throws Exception {
url.contains("&dim_my_dimension=010"));
}

@SuppressWarnings("unchecked")
@Test
public void testGetGetMapUrlWithLayerGroupCqlFilter() throws Exception {
QName[] names =
new QName[] {
MockData.LAKES, MockData.LAKES, MockData.FORESTS, MockData.TASMANIA_DEM
};
GetMapRequest request = createGetMapRequest(names);
KvpMap rawKvp = new KvpMap(request.getRawKvp());
rawKvp.put(
"layers",
request.getLayers().get(0).getName()
+ ','
+ NATURE_GROUP
+ ','
+ request.getLayers().get(3).getName());
rawKvp.put("cql_filter", "fid='123';name LIKE 'BLUE%';INCLUDE");
request.setRawKvp(rawKvp);
request.setFormat(DefaultWebMapService.FORMAT);
DefaultWebMapService.autoSetBoundsAndSize(request);
List<String> urls = new ArrayList<>();
for (int i = 0; i < request.getLayers().size(); i++) {
String url =
WMSRequests.getGetMapUrl(
request, request.getLayers().get(i).getName(), i, null, null, null);
urls.add(URLDecoder.decode(url, "UTF-8"));
}
public void testGetGetMapUrlWithSingleLayer() {
GetMapRequest request = initGetMapRequest(MockData.LAKES);
request.getRawKvp().put("cql_filter", "fid='123'");
List<String> urls = getGetMapUrls(request);
assertEquals(1, urls.size());
assertTrue(
"Incorrect cql_filter in GetMap URL: " + urls.get(0),
urls.get(0).contains("&cql_filter=fid='123'&"));
}

@Test
public void testGetGetMapUrlWithMultipleLayers() {
GetMapRequest request = initGetMapRequest(MockData.LAKES, MockData.TASMANIA_DEM);
request.getRawKvp().put("cql_filter", "fid='123';INCLUDE");
List<String> urls = getGetMapUrls(request);
assertEquals(2, urls.size());
assertTrue(
"Incorrect cql_filter in GetMap URL: " + urls.get(0),
urls.get(0).contains("&cql_filter=fid='123'&"));
assertTrue(
"Incorrect cql_filter in GetMap URL: " + urls.get(1),
urls.get(1).contains("&cql_filter=INCLUDE&"));
}

@Test
public void testGetGetMapUrlWithSingleLayerGroup() {
GetMapRequest request = initGetMapRequest(MockData.LAKES, MockData.FORESTS);
request.getRawKvp().put("layers", NATURE_GROUP);
request.getRawKvp().put("cql_filter", "name LIKE 'BLUE%'");
List<String> urls = getGetMapUrls(request);
assertEquals(2, urls.size());
assertTrue(
"Incorrect cql_filter in GetMap URL: " + urls.get(0),
urls.get(0).contains("&cql_filter=name LIKE 'BLUE%'&"));
assertTrue(
"Incorrect cql_filter in GetMap URL: " + urls.get(1),
urls.get(1).contains("&cql_filter=name LIKE 'BLUE%'&"));
}

@Test
public void testGetGetMapUrlWithLayerGroupAndLayers() {
GetMapRequest request =
initGetMapRequest(
MockData.LAKES, MockData.LAKES, MockData.FORESTS, MockData.TASMANIA_DEM);
request.getRawKvp()
.put(
"layers",
request.getLayers().get(0).getName()
+ ','
+ NATURE_GROUP
+ ','
+ request.getLayers().get(3).getName());
request.getRawKvp().put("cql_filter", "fid='123';name LIKE 'BLUE%';INCLUDE");
List<String> urls = getGetMapUrls(request);
assertEquals(4, urls.size());
assertTrue(
"Incorrect cql_filter in GetMap URL: " + urls.get(0),
urls.get(0).contains("&cql_filter=fid='123'&"));
Expand All @@ -88,4 +120,29 @@ public void testGetGetMapUrlWithLayerGroupCqlFilter() throws Exception {
"Incorrect cql_filter in GetMap URL: " + urls.get(3),
urls.get(3).contains("&cql_filter=INCLUDE&"));
}

@SuppressWarnings("unchecked")
private GetMapRequest initGetMapRequest(QName... names) {
GetMapRequest request = createGetMapRequest(names);
request.setRawKvp(new KvpMap(request.getRawKvp()));
String layers =
request.getLayers()
.stream()
.map(MapLayerInfo::getName)
.collect(Collectors.joining(","));
request.getRawKvp().put("layers", layers);
request.setFormat(DefaultWebMapService.FORMAT);
DefaultWebMapService.autoSetBoundsAndSize(request);
return request;
}

private static List<String> getGetMapUrls(GetMapRequest request) {
List<String> urls = new ArrayList<>(request.getLayers().size());
for (int i = 0; i < request.getLayers().size(); i++) {
String name = request.getLayers().get(i).getName();
String url = WMSRequests.getGetMapUrl(request, name, i, null, null, null);
urls.add(ResponseUtils.urlDecode(url));
}
return urls;
}
}

0 comments on commit 3431d61

Please sign in to comment.