Skip to content

Commit

Permalink
Extract REST schema/interface classes into rest package. Add pretty p…
Browse files Browse the repository at this point in the history
…rinting config (by default off).
  • Loading branch information
Querela committed Sep 3, 2024
1 parent 93f121f commit 000f34c
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 86 deletions.
2 changes: 2 additions & 0 deletions aggregator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ aggregatorParams:
- es
- pl

prettyPrintJSON: false

piwikConfig:
enabled: ${PIWIK_ENABLED:-true}
url: ${PIWIK_URL:-https://stats.clarin.eu/}
Expand Down
2 changes: 2 additions & 0 deletions aggregator_devel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ aggregatorParams:
- es
- pl

prettyPrintJSON: true

piwikConfig:
enabled: ${PIWIK_ENABLED:-false}
url: ${PIWIK_URL:-https://stats.clarin.eu/}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/eu/clarin/sru/fcs/aggregator/rest/InitSchema.java
Original file line number Diff line number Diff line change
@@ -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<Resource> resources;

@JsonProperty(required = true)
List<String> languages;

@JsonProperty(required = true)
List<String> weblichtLanguages;

@JsonProperty
String query;

@JsonProperty
String mode;

@JsonProperty("x-aggregation-context")
Map<String, List<String>> contextString;
}
Original file line number Diff line number Diff line change
@@ -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<MetaOnlyResult> results;

public JsonMetaOnlySearch(List<MetaOnlyResult> results) {
this.results = results;
}

public static JsonMetaOnlySearch fromJsonSearch(JsonSearch search) {
final List<MetaOnlyResult> results = search.results.stream().map(r -> new MetaOnlyResult(r))
.collect(Collectors.toList());
final JsonMetaOnlySearch js = new JsonMetaOnlySearch(results);
js.inProgress = search.inProgress;
return js;
}
}
19 changes: 19 additions & 0 deletions src/main/java/eu/clarin/sru/fcs/aggregator/rest/JsonSearch.java
Original file line number Diff line number Diff line change
@@ -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<Result> results;

public JsonSearch(List<Result> results) {
this.results = results;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package eu.clarin.sru.fcs.aggregator.rest;

import java.util.Map;

public abstract class LanguageMap implements Map<String, String> {}
90 changes: 4 additions & 86 deletions src/main/java/eu/clarin/sru/fcs/aggregator/rest/RestService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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")
Expand All @@ -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<Resource> resources = Aggregator.getInstance().getResources().getResources();
return Response.ok(toJson(resources)).build();
}

private static abstract class LanguageMap implements Map<String, String> {
return Response.ok(resources).build();
}

@GET
Expand All @@ -111,22 +95,7 @@ public Response getLanguages() throws IOException {
Set<String> codes = Aggregator.getInstance().getResources().getLanguages();
log.info("get language codes: {}", codes);
Map<String, String> languages = LanguagesISO693.getInstance().getLanguageMap(codes);
return Response.ok(toJson(languages)).build();
}

private static class InitSchema {
@JsonProperty(required = true)
List<Resource> resources;
@JsonProperty(required = true)
List<String> languages;
@JsonProperty(required = true)
List<String> weblichtLanguages;
@JsonProperty
String query;
@JsonProperty
String mode;
@JsonProperty("x-aggregation-context")
Map<String, List<String>> contextString;
return Response.ok(languages).build();
}

@GET
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<Result> results;

public JsonSearch(List<Result> results) {
this.results = results;
}
}

@GET
@Produces({ MediaType.APPLICATION_JSON })
@Path("search/{id}")
Expand All @@ -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<MetaOnlyResult> results;

public JsonMetaOnlySearch(List<MetaOnlyResult> results) {
this.results = results;
}

public static JsonMetaOnlySearch fromJsonSearch(JsonSearch search) {
final List<MetaOnlyResult> 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")
Expand Down Expand Up @@ -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<String, Map<String, EndpointStats>> 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")
Expand Down Expand Up @@ -520,7 +438,7 @@ public Response getStatistics() throws IOException {
});
}
};
return Response.ok(toJson(j)).build();
return Response.ok(j).build();
}

}
Original file line number Diff line number Diff line change
@@ -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<String, Map<String, EndpointStats>> institutions;

@JsonProperty(required = true)
Date date;
}

@JsonProperty(value = "Last Scan", required = true)
StatisticsSchema scans;

@JsonProperty(value = "Recent Searches", required = true)
StatisticsSchema searches;
}

0 comments on commit 000f34c

Please sign in to comment.