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

Ogcapi records - better catalog objects #1

Open
wants to merge 19 commits into
base: origin_main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
import org.fao.geonet.common.search.SearchConfiguration.Operations;
import org.fao.geonet.domain.Source;
import org.fao.geonet.domain.SourceType;
import org.fao.geonet.ogcapi.records.controller.model.CollectionInfo;
import org.fao.geonet.ogcapi.records.controller.model.Conformance;
import org.fao.geonet.ogcapi.records.controller.model.Content;
import org.fao.geonet.ogcapi.records.controller.model.Link;
import org.fao.geonet.ogcapi.records.controller.model.Root;
import org.fao.geonet.ogcapi.records.model.OgcApiLink;
import org.fao.geonet.ogcapi.records.model.XsltModel;
import org.fao.geonet.ogcapi.records.service.RecordService;
import org.fao.geonet.ogcapi.records.util.CollectionInfoBuilder;
import org.fao.geonet.ogcapi.records.util.LinksItemsBuilder;
import org.fao.geonet.ogcapi.records.util.MediaTypeUtil;
Expand Down Expand Up @@ -77,6 +79,12 @@ public class CapabilitiesApiController {
@Autowired
MediaTypeUtil mediaTypeUtil;

@Autowired
CollectionInfoBuilder collectionInfoBuilder;

@Autowired
RecordService recordService;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems not used.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed


/**
* Landing page end-point.
*
Expand Down Expand Up @@ -111,16 +119,28 @@ public ResponseEntity<Root> getLandingPage(@ApiIgnore HttpServletRequest request
String label = source.getLabel("eng");
root.title(StringUtils.isEmpty(label) ? source.getName() : label);
root.description("");

Locale locale = LocaleContextHolder.getLocale();

var language = locale.getISO3Language();

CollectionInfo collectionInfo = collectionInfoBuilder
.buildFromSource(source, language, requestBaseUrl,
configuration.getFormat(mediaType), configuration,request);
root.setSystemInfo(collectionInfo);
}

root.addLinksItem(new Link()
root.addLinksItem(new OgcApiLink()
.href(requestBaseUrl)
.rel("self").type(MediaType.APPLICATION_JSON.toString()));
.rel("self")
.type(MediaType.APPLICATION_JSON.toString()));

configuration.getFormats(Operations.root).forEach(f -> root.addLinksItem(new Link()
configuration.getFormats(Operations.root).forEach(f ->
root.addLinksItem(new OgcApiLink()
.href(requestBaseUrl + "collections?f=" + f.getName())
.type("Catalogue collections")
.rel("self").type(f.getMimeType())));
.rel("self")
.type(f.getMimeType())));

addOpenApiLinks(root, requestBaseUrl);
addConformanceLinks(root, requestBaseUrl);
Expand All @@ -145,28 +165,32 @@ public ResponseEntity<Root> getLandingPage(@ApiIgnore HttpServletRequest request

private void addOpenApiLinks(Root root, String baseUrl) {
String title = "The OpenAPI Documentation";
root.addLinksItem(new Link()
root.addLinksItem(new OgcApiLink()
.href(baseUrl + "openapi")
.title(title)
.rel("service-doc").type(MediaType.TEXT_HTML_VALUE));

root.addLinksItem(new Link()
root.addLinksItem(new OgcApiLink()
.href(baseUrl + "openapi?f=json")
.title(title + " as JSON")
.rel("service-desc").type(MediaType.APPLICATION_JSON_VALUE));
}

private void addConformanceLinks(Root root, String baseUrl) {
String title = "The Conformance classes";
root.addLinksItem(new Link()

root.addLinksItem(new OgcApiLink()
.href(baseUrl + CONFORMANCE_REL)
.title(title)
.rel(CONFORMANCE_REL).type(MediaType.TEXT_HTML_VALUE));
.rel(CONFORMANCE_REL)
.type(MediaType.TEXT_HTML_VALUE));


root.addLinksItem(new Link()
root.addLinksItem(new OgcApiLink()
.href(baseUrl + CONFORMANCE_REL + "?f=json")
.title(title + " as JSON")
.rel(CONFORMANCE_REL).type(MediaType.APPLICATION_JSON_VALUE));
.rel(CONFORMANCE_REL)
.type(MediaType.APPLICATION_JSON_VALUE));
}


Expand Down Expand Up @@ -256,11 +280,12 @@ public ResponseEntity<Content> describeCollections(@ApiIgnore HttpServletRequest

List<Source> sources = sourceRepository.findAll();
sources.forEach(s -> content.addCollectionsItem(
CollectionInfoBuilder.buildFromSource(
s, language, requestBaseUrl, configuration.getFormat(mediaType), configuration)));
collectionInfoBuilder.buildFromSource(
s, language, requestBaseUrl, configuration.getFormat(mediaType),
configuration,request)));

// TODO: Accept format parameter.
List<Link> linkList = LinksItemsBuilder.build(
List<OgcApiLink> linkList = LinksItemsBuilder.build(
configuration.getFormat(mediaType), requestBaseUrl, language, configuration);
linkList.forEach(content::addLinksItem);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* (c) 2020 Open Source Geospatial Foundation - all rights reserved This code is licensed under the
* GPL 2.0 license, available at the root application directory.
*/

package org.fao.geonet.ogcapi.records.controller;

import io.swagger.annotations.Api;
Expand Down Expand Up @@ -75,6 +80,9 @@ public class CollectionApiController {
@Autowired
MediaTypeUtil mediaTypeUtil;

@Autowired
CollectionInfoBuilder collectionInfoBuilder;

/**
* Describe a collection.
*/
Expand Down Expand Up @@ -126,9 +134,9 @@ public ResponseEntity<CollectionInfo> describeCollection(
String requestBaseUrl = request.getRequestURL()
.toString().replace(collectionId, "");

CollectionInfo collectionInfo = CollectionInfoBuilder
CollectionInfo collectionInfo = collectionInfoBuilder
.buildFromSource(source, language, requestBaseUrl,
configuration.getFormat(mediaType), configuration);
configuration.getFormat(mediaType), configuration,request);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
configuration.getFormat(mediaType), configuration,request);
configuration.getFormat(mediaType), configuration, request);

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank for the review, jose. I've run the code through an intellij reformat "cleanup". The first time I did this, it changed my javadocs and the checkstyle got very upset. However, I just re-ran it and it didn't make changes - all these types of problems should be fixed now.


return ResponseEntity.ok(collectionInfo);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.fao.geonet.ogcapi.records.model.Item;
import org.fao.geonet.ogcapi.records.model.XsltModel;
import org.fao.geonet.ogcapi.records.service.CollectionService;
import org.fao.geonet.ogcapi.records.service.RecordService;
import org.fao.geonet.ogcapi.records.util.MediaTypeUtil;
import org.fao.geonet.ogcapi.records.util.RecordsEsQueryBuilder;
import org.fao.geonet.ogcapi.records.util.XmlUtil;
Expand Down Expand Up @@ -111,6 +112,8 @@ public class ItemApiController {
MediaTypeUtil mediaTypeUtil;
@Autowired
DcatConverter dcatConverter;
@Autowired
RecordService recordService;

/**
* Describe a collection item.
Expand Down Expand Up @@ -167,7 +170,8 @@ public ResponseEntity<Void> collectionsCollectionIdItemsRecordIdGet(
|| mediaType.equals(GnMediaType.APPLICATION_GEOJSON)) {
try {
String type = mediaType.equals(MediaType.APPLICATION_JSON) ? "json" : "geojson";
JsonNode recordAsJson = getRecordAsJson(collectionId, recordId, request, source, type);
JsonNode recordAsJson = recordService.getRecordAsJson(collectionId, recordId,
request, source, type);

streamResult(response,
recordAsJson.toPrettyString(),
Expand Down Expand Up @@ -307,7 +311,7 @@ private ResponseEntity<Void> collectionsCollectionIdItemsRecordIdGetAsJsonLd(
|| GnMediaType.APPLICATION_RDF_XML_VALUE.equals(acceptHeader);
boolean isLinkedData = (isTurtle || isRdfXml || isDcat);

JsonNode recordAsJson = getRecordAsJson(collectionId, recordId, request, source,
JsonNode recordAsJson = recordService.getRecordAsJson(collectionId, recordId, request, source,
isLinkedData ? "json" : "schema.org");

if (isLinkedData) {
Expand Down Expand Up @@ -372,9 +376,11 @@ private ResponseEntity<Void> collectionsCollectionIdItemsRecordIdGetAsXml(

try {
String collectionFilter = collectionService.retrieveCollectionFilter(source, true);
String query = recordsEsQueryBuilder.buildQuerySingleRecord(recordId, collectionFilter, null);
String query = recordsEsQueryBuilder.buildQuerySingleRecord(recordId,
collectionFilter, null);

String queryResponse = proxy.searchAndGetResult(request.getSession(), request, query, null);
String queryResponse = proxy.searchAndGetResult(request.getSession(),
request, query, null);

Document queryResult = XmlUtil.parseXmlString(queryResponse);
String total = queryResult.getChildNodes().item(0).getAttributes().getNamedItem("total")
Expand Down Expand Up @@ -417,7 +423,8 @@ private ResponseEntity<Void> collectionsCollectionIdItemsRecordIdGetAsHtml(
}

try {
JsonNode recordAsJson = getRecordAsJson(collectionId, recordId, request, source, "json");
JsonNode recordAsJson = recordService.getRecordAsJson(collectionId, recordId,
request, source, "json");

Metadata metadataRecord = metadataRepository.findOneByUuid(recordId);
if (metadataRecord == null) {
Expand Down Expand Up @@ -463,43 +470,6 @@ private ResponseEntity<Void> collectionsCollectionIdItemsRecordIdGetAsHtml(
}


private JsonNode getRecordAsJson(
String collectionId,
String recordId,
HttpServletRequest request,
Source source,
String type) throws Exception {
String collectionFilter = collectionService.retrieveCollectionFilter(source, true);
String query = recordsEsQueryBuilder.buildQuerySingleRecord(recordId, collectionFilter, null);

String queryResponse = proxy.searchAndGetResult(request.getSession(), request, query, null);

ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = mapper.getFactory();
JsonParser parser = factory.createParser(queryResponse);
JsonNode actualObj = mapper.readTree(parser);

JsonNode totalValue =
"json".equals(type)
? actualObj.get("hits").get("total").get("value")
: actualObj.get("size");

if ((totalValue == null) || (totalValue.intValue() == 0)) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
messages.getMessage(EXCEPTION_COLLECTION_ITEM_NOT_FOUND,
new String[]{recordId, collectionId},
request.getLocale()));
}

if ("json".equals(type)) {
return actualObj.get("hits").get("hits").get(0);
} else {
String elementName = "schema.org".equals(type) ? "dataFeedElement" : "features";
return actualObj.get(elementName).get(0);
}
}


private List<String> setDefaultRssSortBy(List<String> sortby, HttpServletRequest request) {
boolean isRss = "rss".equals(request.getParameter("f"))
|| (request.getHeader(HttpHeaders.ACCEPT) != null
Expand Down
Loading