From 000f34ca29f5d34d325dd508dfef0b38a7b73ef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20K=C3=B6rner?= Date: Mon, 24 Jun 2024 15:07:57 +0200 Subject: [PATCH] Extract REST schema/interface classes into rest package. Add pretty printing config (by default off). --- aggregator.yml | 2 + aggregator_devel.yml | 2 + .../sru/fcs/aggregator/app/Aggregator.java | 6 ++ .../app/AggregatorConfiguration.java | 8 ++ .../sru/fcs/aggregator/rest/InitSchema.java | 28 ++++++ .../aggregator/rest/JsonMetaOnlySearch.java | 28 ++++++ .../sru/fcs/aggregator/rest/JsonSearch.java | 19 ++++ .../sru/fcs/aggregator/rest/LanguageMap.java | 5 ++ .../sru/fcs/aggregator/rest/RestService.java | 90 +------------------ .../rest/ScanSearchStatisticsSchema.java | 30 +++++++ 10 files changed, 132 insertions(+), 86 deletions(-) create mode 100644 src/main/java/eu/clarin/sru/fcs/aggregator/rest/InitSchema.java create mode 100644 src/main/java/eu/clarin/sru/fcs/aggregator/rest/JsonMetaOnlySearch.java create mode 100644 src/main/java/eu/clarin/sru/fcs/aggregator/rest/JsonSearch.java create mode 100644 src/main/java/eu/clarin/sru/fcs/aggregator/rest/LanguageMap.java create mode 100644 src/main/java/eu/clarin/sru/fcs/aggregator/rest/ScanSearchStatisticsSchema.java diff --git a/aggregator.yml b/aggregator.yml index f97141a..802234f 100644 --- a/aggregator.yml +++ b/aggregator.yml @@ -34,6 +34,8 @@ aggregatorParams: - es - pl + prettyPrintJSON: false + piwikConfig: enabled: ${PIWIK_ENABLED:-true} url: ${PIWIK_URL:-https://stats.clarin.eu/} diff --git a/aggregator_devel.yml b/aggregator_devel.yml index 6fcdc82..a2eaf0b 100644 --- a/aggregator_devel.yml +++ b/aggregator_devel.yml @@ -48,6 +48,8 @@ aggregatorParams: - es - pl + prettyPrintJSON: true + piwikConfig: enabled: ${PIWIK_ENABLED:-false} url: ${PIWIK_URL:-https://stats.clarin.eu/} diff --git a/src/main/java/eu/clarin/sru/fcs/aggregator/app/Aggregator.java b/src/main/java/eu/clarin/sru/fcs/aggregator/app/Aggregator.java index a20ae1e..27250f4 100644 --- a/src/main/java/eu/clarin/sru/fcs/aggregator/app/Aggregator.java +++ b/src/main/java/eu/clarin/sru/fcs/aggregator/app/Aggregator.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; import com.optimaize.langdetect.LanguageDetector; import com.optimaize.langdetect.LanguageDetectorBuilder; import com.optimaize.langdetect.ngram.NgramExtractors; @@ -209,6 +210,11 @@ public void run(AggregatorConfiguration config, Environment environment) throws environment.jersey().register(new IndexResource()); environment.jersey().register(new RestService()); + // pretty printing + if (config.aggregatorParams.prettyPrintJSON) { + environment.getObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, true); + } + // swagger if (config.aggregatorParams.openapiEnabled) { final String[] resourceClasses = { "eu.clarin.sru.fcs.aggregator.app.IndexResource", diff --git a/src/main/java/eu/clarin/sru/fcs/aggregator/app/AggregatorConfiguration.java b/src/main/java/eu/clarin/sru/fcs/aggregator/app/AggregatorConfiguration.java index d41ada7..09897f1 100644 --- a/src/main/java/eu/clarin/sru/fcs/aggregator/app/AggregatorConfiguration.java +++ b/src/main/java/eu/clarin/sru/fcs/aggregator/app/AggregatorConfiguration.java @@ -182,6 +182,14 @@ public int getSEARCH_MAX_CONCURRENT_REQUESTS_PER_SLOW_ENDPOINT() { return SEARCH_MAX_CONCURRENT_REQUESTS_PER_SLOW_ENDPOINT; } + @JsonProperty + boolean prettyPrintJSON; + + @JsonIgnore + public boolean getPrettyPrintJSON() { + return prettyPrintJSON; + } + public static class PiwikConfig { @JsonProperty boolean enabled; diff --git a/src/main/java/eu/clarin/sru/fcs/aggregator/rest/InitSchema.java b/src/main/java/eu/clarin/sru/fcs/aggregator/rest/InitSchema.java new file mode 100644 index 0000000..f8b8a73 --- /dev/null +++ b/src/main/java/eu/clarin/sru/fcs/aggregator/rest/InitSchema.java @@ -0,0 +1,28 @@ +package eu.clarin.sru.fcs.aggregator.rest; + +import java.util.List; +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import eu.clarin.sru.fcs.aggregator.scan.Resource; + +public class InitSchema { + @JsonProperty(required = true) + List resources; + + @JsonProperty(required = true) + List languages; + + @JsonProperty(required = true) + List weblichtLanguages; + + @JsonProperty + String query; + + @JsonProperty + String mode; + + @JsonProperty("x-aggregation-context") + Map> contextString; +} diff --git a/src/main/java/eu/clarin/sru/fcs/aggregator/rest/JsonMetaOnlySearch.java b/src/main/java/eu/clarin/sru/fcs/aggregator/rest/JsonMetaOnlySearch.java new file mode 100644 index 0000000..e47076f --- /dev/null +++ b/src/main/java/eu/clarin/sru/fcs/aggregator/rest/JsonMetaOnlySearch.java @@ -0,0 +1,28 @@ +package eu.clarin.sru.fcs.aggregator.rest; + +import java.util.List; +import java.util.stream.Collectors; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import eu.clarin.sru.fcs.aggregator.search.MetaOnlyResult; + +public class JsonMetaOnlySearch { + @JsonProperty(required = true) + int inProgress = 0; + + @JsonProperty(required = true) + List results; + + public JsonMetaOnlySearch(List results) { + this.results = results; + } + + public static JsonMetaOnlySearch fromJsonSearch(JsonSearch search) { + final List results = search.results.stream().map(r -> new MetaOnlyResult(r)) + .collect(Collectors.toList()); + final JsonMetaOnlySearch js = new JsonMetaOnlySearch(results); + js.inProgress = search.inProgress; + return js; + } +} diff --git a/src/main/java/eu/clarin/sru/fcs/aggregator/rest/JsonSearch.java b/src/main/java/eu/clarin/sru/fcs/aggregator/rest/JsonSearch.java new file mode 100644 index 0000000..8288aac --- /dev/null +++ b/src/main/java/eu/clarin/sru/fcs/aggregator/rest/JsonSearch.java @@ -0,0 +1,19 @@ +package eu.clarin.sru.fcs.aggregator.rest; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import eu.clarin.sru.fcs.aggregator.search.Result; + +public class JsonSearch { + @JsonProperty(required = true) + int inProgress = 0; + + @JsonProperty(required = true) + List results; + + public JsonSearch(List results) { + this.results = results; + } +} diff --git a/src/main/java/eu/clarin/sru/fcs/aggregator/rest/LanguageMap.java b/src/main/java/eu/clarin/sru/fcs/aggregator/rest/LanguageMap.java new file mode 100644 index 0000000..035a77f --- /dev/null +++ b/src/main/java/eu/clarin/sru/fcs/aggregator/rest/LanguageMap.java @@ -0,0 +1,5 @@ +package eu.clarin.sru.fcs.aggregator.rest; + +import java.util.Map; + +public abstract class LanguageMap implements Map {} diff --git a/src/main/java/eu/clarin/sru/fcs/aggregator/rest/RestService.java b/src/main/java/eu/clarin/sru/fcs/aggregator/rest/RestService.java index 17f2d9c..aaeef0f 100644 --- a/src/main/java/eu/clarin/sru/fcs/aggregator/rest/RestService.java +++ b/src/main/java/eu/clarin/sru/fcs/aggregator/rest/RestService.java @@ -1,9 +1,6 @@ package eu.clarin.sru.fcs.aggregator.rest; -import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.ObjectWriter; import eu.clarin.sru.client.SRUVersion; import eu.clarin.sru.fcs.aggregator.app.Aggregator; import eu.clarin.sru.fcs.aggregator.app.AggregatorConfiguration; @@ -13,12 +10,10 @@ import eu.clarin.sru.fcs.aggregator.scan.FCSProtocolVersion; import eu.clarin.sru.fcs.aggregator.scan.FCSSearchCapabilities; import eu.clarin.sru.fcs.aggregator.scan.Statistics; -import eu.clarin.sru.fcs.aggregator.scan.Statistics.EndpointStats; import eu.clarin.sru.fcs.aggregator.search.Result; import eu.clarin.sru.fcs.aggregator.search.Search; import eu.clarin.sru.fcs.aggregator.util.LanguagesISO693; import eu.clarin.sru.fcs.aggregator.search.Exports; -import eu.clarin.sru.fcs.aggregator.search.MetaOnlyResult; import io.swagger.v3.oas.annotations.OpenAPIDefinition; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.info.Info; @@ -34,13 +29,11 @@ import java.io.IOException; import java.net.URI; import java.util.ArrayList; -import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.stream.Collectors; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.Consumes; @@ -82,12 +75,6 @@ public class RestService { private static final org.slf4j.Logger log = LoggerFactory.getLogger(RestService.class); - ObjectWriter ow = new ObjectMapper().writerWithDefaultPrettyPrinter(); - - private String toJson(Object o) throws JsonProcessingException { - return ow.writeValueAsString(o); - } - @GET @Produces({ MediaType.APPLICATION_JSON }) @Path("resources") @@ -96,10 +83,7 @@ private String toJson(Object o) throws JsonProcessingException { @Content(mediaType = MediaType.APPLICATION_JSON, array = @ArraySchema(schema = @Schema(implementation = Resource.class))) }) }) public Response getResources() throws IOException { List resources = Aggregator.getInstance().getResources().getResources(); - return Response.ok(toJson(resources)).build(); - } - - private static abstract class LanguageMap implements Map { + return Response.ok(resources).build(); } @GET @@ -111,22 +95,7 @@ public Response getLanguages() throws IOException { Set codes = Aggregator.getInstance().getResources().getLanguages(); log.info("get language codes: {}", codes); Map languages = LanguagesISO693.getInstance().getLanguageMap(codes); - return Response.ok(toJson(languages)).build(); - } - - private static class InitSchema { - @JsonProperty(required = true) - List resources; - @JsonProperty(required = true) - List languages; - @JsonProperty(required = true) - List weblichtLanguages; - @JsonProperty - String query; - @JsonProperty - String mode; - @JsonProperty("x-aggregation-context") - Map> contextString; + return Response.ok(languages).build(); } @GET @@ -163,7 +132,7 @@ public Response getInit(@Context final HttpServletRequest request) throws IOExce Aggregator.getInstance().getParams().getWeblichtConfig().getAcceptedTcfLanguages()); } }; - return Response.ok(toJson(j)).build(); + return Response.ok(j).build(); } @POST @@ -233,18 +202,6 @@ public Response postSearch( return Response.created(uri).entity(search.getId()).build(); } - public static class JsonSearch { - - @JsonProperty(required = true) - int inProgress = 0; - @JsonProperty(required = true) - List results; - - public JsonSearch(List results) { - this.results = results; - } - } - @GET @Produces({ MediaType.APPLICATION_JSON }) @Path("search/{id}") @@ -268,27 +225,6 @@ public Response getSearch(@PathParam("id") Long searchId, return Response.ok(js).build(); } - public static class JsonMetaOnlySearch { - - @JsonProperty(required = true) - int inProgress = 0; - - @JsonProperty(required = true) - List results; - - public JsonMetaOnlySearch(List results) { - this.results = results; - } - - public static JsonMetaOnlySearch fromJsonSearch(JsonSearch search) { - final List results = search.results.stream().map(r -> new MetaOnlyResult(r)) - .collect(Collectors.toList()); - final JsonMetaOnlySearch js = new JsonMetaOnlySearch(results); - js.inProgress = search.inProgress; - return js; - } - } - @GET @Produces({ MediaType.APPLICATION_JSON }) @Path("search/{id}/metaonly") @@ -472,24 +408,6 @@ public Response sendSearchResultsToWeblicht(@PathParam("id") Long searchId, : Response.seeOther(weblichtUri).entity(weblichtUri).build(); } - private static class ScanSearchStatisticsSchema { - private static class StatisticsSchema { - @JsonProperty(required = true) - int timeout; - @JsonProperty(required = true) - Boolean isScan; - @JsonProperty(required = true) - Map> institutions; - @JsonProperty(required = true) - Date date; - } - - @JsonProperty(value = "Last Scan", required = true) - StatisticsSchema scans; - @JsonProperty(value = "Recent Searches", required = true) - StatisticsSchema searches; - } - @GET @Produces({ MediaType.APPLICATION_JSON }) @Path("statistics") @@ -520,7 +438,7 @@ public Response getStatistics() throws IOException { }); } }; - return Response.ok(toJson(j)).build(); + return Response.ok(j).build(); } } diff --git a/src/main/java/eu/clarin/sru/fcs/aggregator/rest/ScanSearchStatisticsSchema.java b/src/main/java/eu/clarin/sru/fcs/aggregator/rest/ScanSearchStatisticsSchema.java new file mode 100644 index 0000000..7f75924 --- /dev/null +++ b/src/main/java/eu/clarin/sru/fcs/aggregator/rest/ScanSearchStatisticsSchema.java @@ -0,0 +1,30 @@ +package eu.clarin.sru.fcs.aggregator.rest; + +import java.util.Date; +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import eu.clarin.sru.fcs.aggregator.scan.Statistics.EndpointStats; + +public class ScanSearchStatisticsSchema { + public static class StatisticsSchema { + @JsonProperty(required = true) + int timeout; + + @JsonProperty(required = true) + Boolean isScan; + + @JsonProperty(required = true) + Map> institutions; + + @JsonProperty(required = true) + Date date; + } + + @JsonProperty(value = "Last Scan", required = true) + StatisticsSchema scans; + + @JsonProperty(value = "Recent Searches", required = true) + StatisticsSchema searches; +}