Skip to content

Commit

Permalink
OpenSearch REST API collections controller, read part
Browse files Browse the repository at this point in the history
  • Loading branch information
aaime committed Jun 19, 2017
1 parent 9784e5e commit fbaf104
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ paths:
* description.html: the HTML description for the product
* metadata.xml: the ISO metadata for the collection
* thumbnail.png: the collection thumbnail (ignored at the time of writing)
The JSON format is the same as the one returned by a GET on a collection, the "*HRef" properties should be omitted
The JSON format is the same as the one returned by a GET on a collection, the "*HRef" properties should be omitted.
A creation with ZIP is recommend.
parameters:
- name: ogc_publish
in: query
Expand Down Expand Up @@ -94,7 +95,7 @@ paths:
- application/json
responses:
200:
description: Success. The response contains cross links to ogcLinks/metadata/description/thumnail sub-resources, when the same JSON is used to create a new collection the associated can be omitted, or if everything needs to be put in the JSON, the associated representations can be inlined instead (the thumbnail image can be BASE64 encded). A creation with ZIP is recommend
description: Success. The response contains cross links to ogcLinks/metadata/description/thumnail sub-resources.
examples:
application/json: |
{
Expand Down
10 changes: 10 additions & 0 deletions src/community/oseo/oseo-rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
<artifactId>gs-rest</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.geoserver</groupId>
<artifactId>gs-rest</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>${gt.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.geoserver.opensearch.eo.OpenSearchAccessProvider;
import org.geoserver.opensearch.eo.store.OpenSearchAccess;
import org.geoserver.rest.RestBaseController;
import org.geoserver.rest.RestException;
import org.springframework.http.HttpStatus;

/**
* Base class for OpenSearch related REST controllers
Expand All @@ -26,5 +28,18 @@ public AbstractOpenSearchController(OpenSearchAccessProvider accessProvider) {
protected OpenSearchAccess getOpenSearchAccess() throws IOException {
return accessProvider.getOpenSearchAccess();
}

protected void validateMin(Integer value, int min, String name) {
if(value != null && value < min) {
throw new RestException("Invalid parameter " + name + ", should be at least " + min, HttpStatus.BAD_REQUEST);
}
}

protected void validateMax(Integer value, int max, String name) {
if(value != null && value > max) {
throw new RestException("Invalid parameter " + name + ", should be at most " + max, HttpStatus.BAD_REQUEST);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
import org.geoserver.opensearch.eo.OpenSearchAccessProvider;
import org.geoserver.opensearch.eo.store.OpenSearchAccess;
import org.geoserver.ows.URLMangler.URLType;
import org.geoserver.ows.util.RequestUtils;
import org.geoserver.ows.util.ResponseUtils;
import org.geoserver.platform.ServiceException;
import org.geoserver.rest.ResourceNotFoundException;
import org.geoserver.rest.RestBaseController;
import org.geoserver.rest.RestException;
import org.geotools.data.FeatureSource;
import org.geotools.data.Query;
import org.geotools.factory.CommonFactoryFinder;
Expand All @@ -28,11 +26,10 @@
import org.opengis.filter.FilterFactory2;
import org.opengis.filter.sort.SortBy;
import org.opengis.filter.sort.SortOrder;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
Expand All @@ -47,24 +44,12 @@
@ControllerAdvice
@RequestMapping(path = RestBaseController.ROOT_PATH + "/oseo/collections")
public class CollectionsController extends AbstractOpenSearchController {

static final FilterFactory2 FF = CommonFactoryFinder.getFilterFactory2();

public CollectionsController(OpenSearchAccessProvider accessProvider) {
super(accessProvider);
}

private void validateMin(Integer value, int min, String name) {
if(value != null && value < min) {
throw new RestException("Invalid parameter " + name + ", should be at least " + min, HttpStatus.BAD_REQUEST);
}
}

private void validateMax(Integer value, int max, String name) {
if(value != null && value > max) {
throw new RestException("Invalid parameter " + name + ", should be at most " + max, HttpStatus.BAD_REQUEST);
}
}

@GetMapping(produces = { MediaType.APPLICATION_JSON_VALUE })
@ResponseBody
Expand All @@ -89,10 +74,10 @@ public CollectionReferences getCollections(HttpServletRequest request,
}
query.setSortBy(new SortBy[] {FF.sort("name", SortOrder.ASCENDING)});
query.setPropertyNames(new String[] { "name" });
FeatureCollection<FeatureType, Feature> features = fs.getFeatures(query);

// map to java beans for JSON encoding
String baseURL = ResponseUtils.baseURL(request);
FeatureCollection<FeatureType, Feature> features = fs.getFeatures(query);
List<CollectionReference> list = new ArrayList<>();
features.accepts(f -> {
String name = (String) f.getProperty("name").getValue();
Expand All @@ -104,4 +89,21 @@ public CollectionReferences getCollections(HttpServletRequest request,
return new CollectionReferences(list);
}

@GetMapping(path = "{collection}", produces = { MediaType.APPLICATION_JSON_VALUE })
@ResponseBody
public FeatureCollection getCollection(@PathVariable(name="collection", required=true) String collection) throws IOException {
// query the collections for their names
OpenSearchAccess access = accessProvider.getOpenSearchAccess();
FeatureSource<FeatureType, Feature> fs = access.getCollectionSource();
Query query = new Query();
query.setFilter(FF.equal(FF.property("name"), FF.literal("collection"), true));
FeatureCollection<FeatureType, Feature> fc = fs.getFeatures(query);
if(fc.isEmpty()) {
throw new ResourceNotFoundException("Could not find a collection named '" + collection + "'");
}

return fc;
}


}

0 comments on commit fbaf104

Please sign in to comment.