Skip to content

Commit

Permalink
Remove workspace qualification in WFS3 when using virtual services
Browse files Browse the repository at this point in the history
  • Loading branch information
aaime committed Jan 21, 2019
1 parent 4b6a197 commit 884f69d
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 13 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@


import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.geoserver.catalog.Catalog; import org.geoserver.catalog.Catalog;
import org.geoserver.catalog.LayerInfo; import org.geoserver.catalog.LayerInfo;
import org.geoserver.catalog.NamespaceInfo; import org.geoserver.catalog.NamespaceInfo;
import org.geoserver.catalog.ResourceInfo; import org.geoserver.catalog.ResourceInfo;
import org.geoserver.catalog.WorkspaceInfo;
import org.geoserver.ows.LocalWorkspace;
import org.geotools.util.MapEntry; import org.geotools.util.MapEntry;
import org.geotools.util.logging.Logging; import org.geotools.util.logging.Logging;


Expand All @@ -30,6 +33,10 @@ public static String encode(ResourceInfo resource) {
} }


public static String encode(String workspaceName, String resourceName) { public static String encode(String workspaceName, String resourceName) {
final WorkspaceInfo workspace = LocalWorkspace.get();
if (workspace != null && workspace.getName().equalsIgnoreCase(workspaceName)) {
return resourceName;
}
return workspaceName + DELIMITER + resourceName; return workspaceName + DELIMITER + resourceName;
} }


Expand All @@ -42,6 +49,11 @@ public static String encode(String workspaceName, String resourceName) {
* decoded. * decoded.
*/ */
public static List<LayerInfo> getLayers(Catalog catalog, String encodedResourceId) { public static List<LayerInfo> getLayers(Catalog catalog, String encodedResourceId) {
final WorkspaceInfo workspace = LocalWorkspace.get();
if (workspace != null) {
encodedResourceId = workspace.getName() + DELIMITER + encodedResourceId;
}

List<MapEntry<String, String>> decodedList = decode(encodedResourceId); List<MapEntry<String, String>> decodedList = decode(encodedResourceId);
if (decodedList.isEmpty()) { if (decodedList.isEmpty()) {
LOGGER.info("Could not decode id '" + encodedResourceId + "'"); LOGGER.info("Could not decode id '" + encodedResourceId + "'");
Expand All @@ -55,31 +67,31 @@ public static List<LayerInfo> getLayers(Catalog catalog, String encodedResourceI
for (MapEntry<String, String> mapEntry : decodedList) { for (MapEntry<String, String> mapEntry : decodedList) {


String namespace = mapEntry.getKey(); String namespace = mapEntry.getKey();
String covName = mapEntry.getValue(); String localName = mapEntry.getValue();


if (namespace == null || namespace.isEmpty()) { if (namespace == null || namespace.isEmpty()) {
LOGGER.info(" Checking coverage name " + covName); LOGGER.log(Level.FINE, " Checking coverage name {0}", localName);


LayerInfo layer = catalog.getLayerByName(covName); LayerInfo layer = catalog.getLayerByName(localName);
if (layer != null) { if (layer != null) {
LOGGER.info(" - Collecting layer " + layer.prefixedName()); LOGGER.log(Level.FINE, " - Collecting layer {0}", layer.prefixedName());
ret.add(layer); ret.add(layer);
} else { } else {
LOGGER.info(" - Ignoring layer " + covName); LOGGER.log(Level.FINE, " - Ignoring layer {0}", localName);
} }
} else { } else {
LOGGER.info(" Checking pair " + namespace + " : " + covName); LOGGER.info(" Checking pair " + namespace + " : " + localName);


String fullName = namespace + ":" + covName; String fullName = namespace + ":" + localName;
NamespaceInfo nsInfo = catalog.getNamespaceByPrefix(namespace); NamespaceInfo nsInfo = catalog.getNamespaceByPrefix(namespace);
if (nsInfo != null) { if (nsInfo != null) {
LOGGER.info(" - Namespace found " + namespace); LOGGER.log(Level.FINE, " - Namespace found {0}", namespace);
LayerInfo layer = catalog.getLayerByName(fullName); LayerInfo layer = catalog.getLayerByName(fullName);
if (layer != null) { if (layer != null) {
LOGGER.info(" - Collecting layer " + layer.prefixedName()); LOGGER.log(Level.FINE, " - Collecting layer {0} ", layer.prefixedName());
ret.add(layer); ret.add(layer);
} else { } else {
LOGGER.info(" - Ignoring layer " + fullName); LOGGER.log(Level.FINE, " - Ignoring layer {0} " + fullName);
} }
} else { } else {
LOGGER.info(" - Namespace not found " + namespace); LOGGER.info(" - Namespace not found " + namespace);
Expand Down
25 changes: 25 additions & 0 deletions src/community/wfs3/src/test/java/org/geoserver/wfs3/ApiTest.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -194,4 +194,29 @@ private void validateApi(OpenAPI api) {
assertEquals(wfs.getMaxFeatures(), limitSchema.getMaximum().intValue()); assertEquals(wfs.getMaxFeatures(), limitSchema.getMaximum().intValue());
assertEquals(wfs.getMaxFeatures(), ((Number) limitSchema.getDefault()).intValue()); assertEquals(wfs.getMaxFeatures(), ((Number) limitSchema.getDefault()).intValue());
} }

@Test
public void testWorkspaceQualifiedAPI() throws Exception {
MockHttpServletRequest request = createRequest("cdf/wfs3/api");
request.setMethod("GET");
request.setContent(new byte[] {});
request.addHeader(HttpHeaders.ACCEPT, "foo/bar, application/x-yaml, text/html");
MockHttpServletResponse response = dispatch(request);
assertEquals(200, response.getStatus());
assertEquals("application/x-yaml", response.getContentType());
String yaml = string(new ByteArrayInputStream(response.getContentAsString().getBytes()));

ObjectMapper mapper = Yaml.mapper();
OpenAPI api = mapper.readValue(yaml, OpenAPI.class);
Map<String, Parameter> params = api.getComponents().getParameters();
Parameter collectionId = params.get("collectionId");
List<String> collectionIdValues = collectionId.getSchema().getEnum();
List<String> expectedCollectionIds =
getCatalog()
.getFeatureTypesByNamespace(getCatalog().getNamespaceByPrefix("cdf"))
.stream()
.map(ft -> ft.getName())
.collect(Collectors.toList());
assertThat(collectionIdValues, equalTo(expectedCollectionIds));
}
} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -64,6 +64,45 @@ public void testCollectionJson() throws Exception {
tiles.get("href")); tiles.get("href"));
} }


@Test
public void testCollectionVirtualWorkspace() throws Exception {
String roadSegments = MockData.ROAD_SEGMENTS.getLocalPart();
DocumentContext json = getAsJSONPath("cite/wfs3/collections/" + roadSegments, 200);

assertEquals("RoadSegments", json.read("$.name", String.class));
assertEquals("RoadSegments", json.read("$.title", String.class));

// check we have the expected number of links and they all use the right "rel" relation
List<String> formats =
DefaultWebFeatureService30.getAvailableFormats(FeatureCollectionResponse.class);
assertThat(
(int) json.read("$.links.length()", Integer.class),
Matchers.greaterThanOrEqualTo(formats.size()));
for (String format : formats) {
// check title and rel.
List items = json.read("$.links[?(@.type=='" + format + "')]", List.class);
Map item = (Map) items.get(0);
assertEquals("RoadSegments items as " + format, item.get("title"));
assertEquals("item", item.get("rel"));
}
// the WFS3 specific GML3.2 output format is available
assertNotNull(json.read("$.links[?(@.type=='" + GML32WFS3OutputFormat.FORMAT + "')]"));

// tiling scheme extension
Map tilingScheme = (Map) json.read("links[?(@.rel=='tilingScheme')]", List.class).get(0);
assertEquals(
"http://localhost:8080/geoserver/cite/wfs3/collections/"
+ roadSegments
+ "/tiles/{tilingSchemeId}",
tilingScheme.get("href"));
Map tiles = (Map) json.read("links[?(@.rel=='tiles')]", List.class).get(0);
assertEquals(
"http://localhost:8080/geoserver/cite/wfs3/collections/"
+ roadSegments
+ "/tiles/{tilingSchemeId}/{level}/{row}/{col}",
tiles.get("href"));
}

@Test @Test
public void testCollectionsXML() throws Exception { public void testCollectionsXML() throws Exception {
Document dom = Document dom =
Expand All @@ -87,6 +126,6 @@ public void testCollectionYaml() throws Exception {
"wfs3/collections/" "wfs3/collections/"
+ getEncodedName(MockData.ROAD_SEGMENTS) + getEncodedName(MockData.ROAD_SEGMENTS)
+ "?f=application/x-yaml"); + "?f=application/x-yaml");
System.out.println(yaml); // System.out.println(yaml);
} }
} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
*/ */
package org.geoserver.wfs3; package org.geoserver.wfs3;


import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.lessThanOrEqualTo; import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
Expand All @@ -13,6 +15,7 @@
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import net.minidev.json.JSONArray;
import org.geoserver.catalog.FeatureTypeInfo; import org.geoserver.catalog.FeatureTypeInfo;
import org.geoserver.data.test.MockData; import org.geoserver.data.test.MockData;
import org.geoserver.data.test.SystemTestData; import org.geoserver.data.test.SystemTestData;
Expand Down Expand Up @@ -77,7 +80,17 @@ public void testCollectionsWorkspaceSpecificJson() throws Exception {
.stream() .stream()
.filter(ft -> "cdf".equals(ft.getStore().getWorkspace().getName())) .filter(ft -> "cdf".equals(ft.getStore().getWorkspace().getName()))
.count(); .count();
// check the filtering
assertEquals(expected, (int) json.read("collections.length()", Integer.class)); assertEquals(expected, (int) json.read("collections.length()", Integer.class));
// check the workspace prefixes have been removed
assertThat(json.read("collections[?(@.name=='Deletes')]"), not(empty()));
assertThat(json.read("collections[?(@.name=='cdf__Deletes')]"), empty());
// check the url points to a ws qualified url
final String deleteHrefPath =
"collections[?(@.name=='Deletes')].links[?(@.rel=='item' && @.type=='application/geo+json')].href";
assertEquals(
"http://localhost:8080/geoserver/cdf/wfs3/collections/Deletes/items?f=application%2Fgeo%2Bjson",
((JSONArray) json.read(deleteHrefPath)).get(0));
} }


@Test @Test
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void testGetLayerAsGeoJson() throws Exception {


@Test @Test
public void testWorkspaceQualified() throws Exception { public void testWorkspaceQualified() throws Exception {
String roadSegments = getEncodedName(MockData.ROAD_SEGMENTS); String roadSegments = MockData.ROAD_SEGMENTS.getLocalPart();
DocumentContext json = DocumentContext json =
getAsJSONPath( getAsJSONPath(
MockData.ROAD_SEGMENTS.getPrefix() MockData.ROAD_SEGMENTS.getPrefix()
Expand All @@ -75,6 +75,15 @@ public void testWorkspaceQualified() throws Exception {
assertEquals(2, alternatefRels.size()); assertEquals(2, alternatefRels.size());
assertEquals("alternate", alternatefRels.get(0)); assertEquals("alternate", alternatefRels.get(0));
assertEquals("collection", alternatefRels.get(1)); assertEquals("collection", alternatefRels.get(1));
// check collection link
List selfLink = json.read("links[?(@.rel == 'collection')].href");
assertThat(selfLink.size(), greaterThan(0));
assertThat(
(String) selfLink.get(0),
startsWith(
"http://localhost:8080/geoserver/cite/wfs3/collections/"
+ roadSegments
+ "?"));
} }


@Test @Test
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void testLandingPageXML() throws Exception {
@Test @Test
public void testLandingPageYaml() throws Exception { public void testLandingPageYaml() throws Exception {
String yaml = getAsString("wfs3/?f=application/x-yaml"); String yaml = getAsString("wfs3/?f=application/x-yaml");
System.out.println(yaml); // System.out.println(yaml);
// TODO: add actual tests in here // TODO: add actual tests in here
} }


Expand Down

0 comments on commit 884f69d

Please sign in to comment.