Skip to content

Commit

Permalink
OGC API Records code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
josegar74 authored and fxprunayre committed Apr 27, 2023
1 parent c4992ff commit be4c7da
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

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

import com.google.common.collect.ImmutableList;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -51,6 +49,9 @@
@Controller
public class CapabilitiesApiController {

public static final String CONFORMANCE_REL = "conformance";
private static final String SOURCE_ATTR = "source";

@Value("${gn.baseurl}")
String baseUrl;

Expand Down Expand Up @@ -97,7 +98,7 @@ public class CapabilitiesApiController {
public ResponseEntity<Root> getLandingPage(@ApiIgnore HttpServletRequest request,
@ApiIgnore HttpServletResponse response,
@ApiIgnore Model model) throws Exception {
String baseUrl = request.getRequestURL().toString();
String requestBaseUrl = request.getRequestURL().toString();

MediaType mediaType = mediaTypeUtil.calculatePriorityMediaTypeFromRequest(request);

Expand All @@ -113,26 +114,24 @@ public ResponseEntity<Root> getLandingPage(@ApiIgnore HttpServletRequest request
}

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

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

addOpenApiLinks(root, baseUrl);
addConformanceLinks(root, baseUrl);
addOpenApiLinks(root, requestBaseUrl);
addConformanceLinks(root, requestBaseUrl);

return ResponseEntity.ok(root);
} else {
List<Source> sources = sourceRepository.findAll();
XsltModel modelSource = new XsltModel();
modelSource.setOutputFormats(configuration.getFormats(Operations.collections));
modelSource.setCollections(sources);
model.addAttribute("source", modelSource.toSource());
model.addAttribute(SOURCE_ATTR, modelSource.toSource());
Locale locale = LocaleContextHolder.getLocale();
viewUtility.addi18n(model, locale, request);

Expand Down Expand Up @@ -160,14 +159,14 @@ private void addOpenApiLinks(Root root, String baseUrl) {
private void addConformanceLinks(Root root, String baseUrl) {
String title = "The Conformance classes";
root.addLinksItem(new Link()
.href(baseUrl + "conformance")
.href(baseUrl + CONFORMANCE_REL)
.title(title)
.rel("conformance").type(MediaType.TEXT_HTML_VALUE));
.rel(CONFORMANCE_REL).type(MediaType.TEXT_HTML_VALUE));

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


Expand Down Expand Up @@ -195,7 +194,7 @@ public ResponseEntity<Conformance> conformanceDeclaration(@ApiIgnore HttpServlet
MediaType mediaType = mediaTypeUtil.calculatePriorityMediaTypeFromRequest(request);

Conformance conformance = new Conformance();
conformance.setConformsTo(ImmutableList.of(
conformance.setConformsTo(List.of(
"http://www.opengis.net/spec/ogcapi-records-1/1.0/conf/core",
"http://www.opengis.net/spec/ogcapi-records-1/1.0/conf/searchable-catalogue",
"http://www.opengis.net/spec/ogcapi-records-1/1.0/conf/record-collection",
Expand All @@ -213,7 +212,7 @@ public ResponseEntity<Conformance> conformanceDeclaration(@ApiIgnore HttpServlet
modelSource.setOutputFormats(configuration.getFormats(Operations.conformance));
modelSource.setCollections(sources);
modelSource.setConformance(conformance);
model.addAttribute("source", modelSource.toSource());
model.addAttribute(SOURCE_ATTR, modelSource.toSource());
viewUtility.addi18n(model, locale, request);
View view = viewResolver.resolveViewName("ogcapir/conformance", locale);
view.render(model.asMap(), request, response);
Expand Down Expand Up @@ -252,28 +251,26 @@ public ResponseEntity<Content> describeCollections(@ApiIgnore HttpServletRequest
if (!mediaType.equals(MediaType.TEXT_HTML)) {
Content content = new Content();

String baseUrl = request.getRequestURL()
String requestBaseUrl = request.getRequestURL()
.toString();

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

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

return ResponseEntity.ok(content);
} else {
List<Source> sources = sourceRepository.findAll();
XsltModel modelSource = new XsltModel();
modelSource.setOutputFormats(configuration.getFormats(Operations.collections));
modelSource.setCollections(sources);
model.addAttribute("source", modelSource.toSource());
model.addAttribute(SOURCE_ATTR, modelSource.toSource());
viewUtility.addi18n(model, locale, request);

View view = viewResolver.resolveViewName("ogcapir/collections", locale);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.io.StringWriter;
import java.math.BigInteger;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -125,11 +123,11 @@ public ResponseEntity<CollectionInfo> describeCollection(

if (MediaTypeUtil.defaultSupportedMediaTypes.contains(mediaType)) {
if (!mediaType.equals(MediaType.TEXT_HTML)) {
String baseUrl = request.getRequestURL()
String requestBaseUrl = request.getRequestURL()
.toString().replace(collectionId, "");

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

return ResponseEntity.ok(collectionInfo);
Expand Down Expand Up @@ -179,7 +177,6 @@ public ResponseEntity<CollectionInfo> describeCollection(
// return openSearchDescription;
try {
JAXBContext context = JAXBContext.newInstance(OpenSearchDescription.class);
StringWriter sw = new StringWriter();
Marshaller marshaller = context.createMarshaller();
response.setContentType(GnMediaType.APPLICATION_OPENSEARCH_XML_VALUE);
response.setHeader(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public String retrieveCollectionFilter(Source source) {
public List<String> getSortables(Source source) {
List<String> sortables = new ArrayList<>();

Optional<UiSetting> uiSetting = null;
Optional<UiSetting> uiSetting = Optional.empty();
if (source.getType() == SourceType.portal) {
uiSetting = uiSettingsRepository.findById("srv");
} else if (source.getUiConfig() != null) {
Expand All @@ -92,12 +92,12 @@ public List<String> getSortables(Source source) {

if (uiSetting.isPresent()) {
UiSetting uiSettingValue = uiSetting.get();
String configuration = uiSettingValue.getConfiguration();
String uiSettingValueConfiguration = uiSettingValue.getConfiguration();

try {
ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = mapper.getFactory();
JsonParser parser = factory.createParser(configuration);
JsonParser parser = factory.createParser(uiSettingValueConfiguration);
JsonNode actualObj = mapper.readTree(parser);

JsonNode sortbyValues = actualObj.get("mods").get("search").get("sortbyValues");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

public class CollectionInfoBuilder {

private CollectionInfoBuilder() {
throw new IllegalStateException("Utility class");
}

/**
* Build Collection info from source table.
*/
Expand Down Expand Up @@ -66,7 +70,7 @@ public static CollectionInfo buildFromSource(Source source, String language,

List<Link> linkList = LinksItemsBuilder.build(
format, collectionUri.toString(), language, configuration);
linkList.forEach(l -> collectionInfo.addLinksItem(l));
linkList.forEach(collectionInfo::addLinksItem);

return collectionInfo;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
import org.fao.geonet.common.search.SearchConfiguration;
import org.fao.geonet.common.search.SearchConfiguration.Format;
import org.fao.geonet.ogcapi.records.controller.model.Link;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;

public class LinksItemsBuilder {

private LinksItemsBuilder() {
throw new IllegalStateException("Utility class");
}

/**
* Build items.
*/
Expand All @@ -27,7 +31,7 @@ public static List<Link> build(
links.add(currentDoc);

for (MediaType supportedMediaType : MediaTypeUtil.defaultSupportedMediaTypes) {
if (!supportedMediaType.equals(linkFormat.getMimeType())) {
if (!supportedMediaType.toString().equals(linkFormat.getMimeType())) {
Optional<Format> f = configuration.getFormat(supportedMediaType);
Link alternateDoc = new Link();
alternateDoc.setRel("alternate");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package org.fao.geonet.ogcapi.records.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -26,27 +25,27 @@ public class MediaTypeUtil {
SearchConfiguration searchConfiguration;

public static final List<MediaType> defaultSupportedMediaTypes =
Arrays.asList(
List.of(
MediaType.APPLICATION_JSON,
MediaType.APPLICATION_XML,
MediaType.TEXT_HTML);

public static final List<MediaType> openSearchSupportedMediaTypes =
Arrays.asList(
List.of(
MediaType.APPLICATION_RSS_XML,
MediaType.APPLICATION_ATOM_XML,
GnMediaType.APPLICATION_OPENSEARCH_XML);

public static final List<MediaType> ldSupportedMediaTypes =
Arrays.asList(
List.of(
GnMediaType.APPLICATION_JSON_LD,
GnMediaType.TEXT_TURTLE,
GnMediaType.APPLICATION_RDF_XML,
GnMediaType.APPLICATION_DCAT2_XML);


public static final List<MediaType> xmlMediaTypes =
Arrays.asList(MediaType.APPLICATION_XML);
List.of(MediaType.APPLICATION_XML);


public MediaType calculatePriorityMediaTypeFromRequest(HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public String buildQuery(
}));
}

Set<String> sources = new HashSet(defaultSources);
Set<String> sources = new HashSet<>(defaultSources);
if (sourceFields != null) {
sources.addAll(sourceFields);
} else {
Expand All @@ -116,7 +116,7 @@ public String buildQuery(
QueryBuilders.queryStringQuery(buildFullTextSearchQuery(q));
boolQuery.must(fullTextQuery);

if (externalids != null && externalids.size() > 0) {
if (externalids != null && !externalids.isEmpty()) {
boolQuery.must(
QueryBuilders.termsQuery(
IndexRecordFieldNames.uuid,
Expand Down Expand Up @@ -155,7 +155,7 @@ public String buildQuery(

private String buildFullTextSearchQuery(List<String> q) {
String queryString = "*:*";
if (q != null && q.size() > 0) {
if (q != null && !q.isEmpty()) {
String values = q.stream().collect(Collectors.joining(" AND "));
if (StringUtils.isNotEmpty(configuration.getQueryBase())) {
queryString = configuration.getQueryBase().replaceAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@

public class XmlUtil {

private XmlUtil() {
throw new IllegalStateException("Utility class");
}

/**
* Retrieves the content of a org.w3c.dom.Node as a string.
*/
public static String getNodeString(Node node) throws TransformerException {
StringWriter writer = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(node), new StreamResult(writer));
String output = writer.toString();
return output;
return writer.toString();
}


Expand Down

0 comments on commit be4c7da

Please sign in to comment.