Remove Gson, replacing with Jackson. Remove unused DataTermsFacet & unused code in WordHighlightSolrSearch

This commit is contained in:
Tim Donohue
2022-04-06 17:12:57 -05:00
parent 248c175063
commit 11faeefe95
11 changed files with 88 additions and 162 deletions

View File

@@ -687,13 +687,6 @@
<version>1.1.1</version>
</dependency>
<!-- Gson: Java to Json conversion -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>

View File

@@ -1,70 +0,0 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.statistics;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
/**
* A neutral data object to hold data for statistics.
*/
public class DataTermsFacet {
private List<TermsFacet> terms;
public DataTermsFacet() {
terms = new ArrayList<TermsFacet>();
}
public void addTermFacet(TermsFacet termsFacet) {
terms.add(termsFacet);
}
/**
* Render this data object into JSON format.
*
* An example of the output could be of the format:
* [{"term":"247166","count":10},{"term":"247168","count":6}]
*
* @return JSON-formatted data.
*/
public String toJson() {
Gson gson = new Gson();
return gson.toJson(terms);
}
public static class TermsFacet {
private String term;
private Integer count;
public TermsFacet(String term, Integer count) {
setTerm(term);
setCount(count);
}
public String getTerm() {
return term;
}
public void setTerm(String term) {
this.term = term;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
}
}

View File

@@ -12,14 +12,11 @@ import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.validator.routines.UrlValidator;
import org.apache.logging.log4j.Logger;
import org.apache.solr.client.solrj.SolrQuery;
@@ -35,7 +32,6 @@ import org.dspace.app.iiif.model.generator.ContentAsTextGenerator;
import org.dspace.app.iiif.model.generator.ManifestGenerator;
import org.dspace.app.iiif.model.generator.SearchResultGenerator;
import org.dspace.app.iiif.service.utils.IIIFUtils;
import org.dspace.discovery.SolrSearchCore;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -66,9 +62,6 @@ public class WordHighlightSolrSearch implements SearchAnnotationService {
@Autowired
SearchResultGenerator searchResult;
@Autowired
SolrSearchCore solrSearchCore;
@Autowired
ManifestGenerator manifestGenerator;
@@ -167,48 +160,55 @@ public class WordHighlightSolrSearch implements SearchAnnotationService {
private String getAnnotationList(UUID uuid, String json, String query) {
searchResult.setIdentifier(manifestId + "/search?q="
+ URLEncoder.encode(query, StandardCharsets.UTF_8));
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
JsonObject body = gson.fromJson(json, JsonObject.class);
ObjectMapper mapper = new ObjectMapper();
JsonNode body = null;
try {
body = mapper.readTree(json);
} catch (JsonProcessingException e) {
log.error("Unable to process json response.", e);
}
// If error occurred or no body, return immediately
if (body == null) {
log.warn("Unable to process json response.");
return utils.asJson(searchResult.generateResource());
}
// outer ocr highlight element
JsonObject highs = body.getAsJsonObject("ocrHighlighting");
// highlight entries
for (Map.Entry<String, JsonElement> ocrIds: highs.entrySet()) {
// ocr_text
JsonObject ocrObj = ocrIds.getValue().getAsJsonObject().getAsJsonObject("ocr_text");
// snippets array
if (ocrObj != null) {
for (JsonElement snippetArray : ocrObj.getAsJsonObject().get("snippets").getAsJsonArray()) {
String pageId = getCanvasId(snippetArray.getAsJsonObject().get("pages"));
for (JsonElement highlights : snippetArray.getAsJsonObject().getAsJsonArray("highlights")) {
for (JsonElement highlight : highlights.getAsJsonArray()) {
// Example structure of Solr response available at
// https://github.com/dbmdz/solr-ocrhighlighting/blob/main/docs/query.md
// Get the outer ocrHighlighting node
JsonNode highs = body.get("ocrHighlighting");
// Loop through each highlight entry under ocrHighlighting
for (final JsonNode highEntry : highs) {
// Get the ocr_text node under the entry
JsonNode ocrNode = highEntry.get("ocr_text");
// Loop through the snippets array under that
for (final JsonNode snippet : ocrNode.get("snippets")) {
// Get a canvas ID based on snippet's pages
String pageId = getCanvasId(snippet.get("pages"));
// Loop through array of highlights for each snippet.
for (final JsonNode highlight : snippet.get("highlights")) {
// Add annotation associated with each highlight
searchResult.addResource(getAnnotation(highlight, pageId, uuid));
}
}
}
}
}
return utils.asJson(searchResult.generateResource());
}
/**
* Returns the annotation generator for the highlight.
* @param highlight highlight element from solor response
* @param highlight highlight node from Solr response
* @param pageId page id from solr response
* @return generator for a single annotation
*/
private AnnotationGenerator getAnnotation(JsonElement highlight, String pageId, UUID uuid) {
JsonObject hcoords = highlight.getAsJsonObject();
String text = (hcoords.get("text").getAsString());
int ulx = hcoords.get("ulx").getAsInt();
int uly = hcoords.get("uly").getAsInt();
int lrx = hcoords.get("lrx").getAsInt();
int lry = hcoords.get("lry").getAsInt();
private AnnotationGenerator getAnnotation(JsonNode highlight, String pageId, UUID uuid) {
String text = (highlight.get("text").asText());
int ulx = highlight.get("ulx").asInt();
int uly = highlight.get("uly").asInt();
int lrx = highlight.get("lrx").asInt();
int lry = highlight.get("lry").asInt();
String w = Integer.toString(lrx - ulx);
String h = Integer.toString(lry - uly);
String params = ulx + "," + uly + "," + w + "," + h;
@@ -221,13 +221,12 @@ public class WordHighlightSolrSearch implements SearchAnnotationService {
* delimited with a "." and that the integer corresponds to the
* canvas identifier in the manifest. For METS/ALTO documents, the page
* order can be derived from the METS file when loading the solr index.
* @param element the pages element
* @param pagesNode the pages node
* @return canvas id
*/
private String getCanvasId(JsonElement element) {
JsonArray pages = element.getAsJsonArray();
JsonObject page = pages.get(0).getAsJsonObject();
String[] identArr = page.get("id").getAsString().split("\\.");
private String getCanvasId(JsonNode pagesNode) {
JsonNode page = pagesNode.get(0);
String[] identArr = page.get("id").asText().split("\\.");
// the canvas id.
return "c" + identArr[1];
}

View File

@@ -18,9 +18,9 @@ import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger;
import org.dspace.app.rest.Parameter;
@@ -296,9 +296,14 @@ public class MetadataFieldRestRepository extends DSpaceRestRepository<MetadataFi
protected MetadataFieldRest put(Context context, HttpServletRequest request, String apiCategory, String model,
Integer id, JsonNode jsonNode) throws SQLException, AuthorizeException {
MetadataFieldRest metadataFieldRest = new Gson().fromJson(jsonNode.toString(), MetadataFieldRest.class);
MetadataFieldRest metadataFieldRest;
try {
metadataFieldRest = new ObjectMapper().readValue(jsonNode.toString(), MetadataFieldRest.class);
} catch (JsonProcessingException e) {
throw new UnprocessableEntityException("Cannot parse JSON in request body", e);
}
if (isBlank(metadataFieldRest.getElement())) {
if (metadataFieldRest == null || isBlank(metadataFieldRest.getElement())) {
throw new UnprocessableEntityException("metadata element (in request body) cannot be blank");
}

View File

@@ -15,9 +15,9 @@ import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.dspace.app.rest.exception.DSpaceBadRequestException;
import org.dspace.app.rest.exception.UnprocessableEntityException;
import org.dspace.app.rest.model.MetadataSchemaRest;
@@ -138,9 +138,14 @@ public class MetadataSchemaRestRepository extends DSpaceRestRepository<MetadataS
protected MetadataSchemaRest put(Context context, HttpServletRequest request, String apiCategory, String model,
Integer id, JsonNode jsonNode) throws SQLException, AuthorizeException {
MetadataSchemaRest metadataSchemaRest = new Gson().fromJson(jsonNode.toString(), MetadataSchemaRest.class);
MetadataSchemaRest metadataSchemaRest;
try {
metadataSchemaRest = new ObjectMapper().readValue(jsonNode.toString(), MetadataSchemaRest.class);
} catch (JsonProcessingException e) {
throw new UnprocessableEntityException("Cannot parse JSON in request body", e);
}
if (isBlank(metadataSchemaRest.getPrefix())) {
if (metadataSchemaRest == null || isBlank(metadataSchemaRest.getPrefix())) {
throw new UnprocessableEntityException("metadata schema name cannot be blank");
}
if (isBlank(metadataSchemaRest.getNamespace())) {

View File

@@ -28,6 +28,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@@ -35,7 +36,6 @@ import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
@@ -566,13 +566,14 @@ public class RelationshipRestRepositoryIT extends AbstractEntityIntegrationTest
.andExpect(jsonPath("$.leftwardValue", is(nullValue())))
.andExpect(jsonPath("$.rightwardValue", is(nullValue())));
JsonObject contentObj = new JsonObject();
contentObj.addProperty("leftwardValue", leftwardValue);
Map<String, String> map = new HashMap<>();
map.put("leftwardValue", leftwardValue);
String json = new ObjectMapper().writeValueAsString(map);
// Add leftwardValue
getClient(token).perform(put("/api/core/relationships/" + idRef)
.contentType("application/json")
.content(contentObj.toString()))
.content(json))
.andExpect(status().isOk());
// Verify leftwardValue is present and rightwardValue not
@@ -624,14 +625,15 @@ public class RelationshipRestRepositoryIT extends AbstractEntityIntegrationTest
.andExpect(jsonPath("$.leftwardValue", is(nullValue())))
.andExpect(jsonPath("$.rightwardValue", is(nullValue())));
JsonObject contentObj = new JsonObject();
contentObj.addProperty("leftwardValue", leftwardValue);
contentObj.addProperty("rightwardValue", rightwardValue);
Map<String, String> map = new HashMap<>();
map.put("leftwardValue", leftwardValue);
map.put("rightwardValue", rightwardValue);
String json = new ObjectMapper().writeValueAsString(map);
// Add leftwardValue and rightwardValue
getClient(token).perform(put("/api/core/relationships/" + idRef)
.contentType("application/json")
.content(contentObj.toString()))
.content(json))
.andExpect(status().isOk());
// Verify leftwardValue and rightwardValue are present

View File

@@ -30,7 +30,7 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import com.google.gson.Gson;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.dspace.app.rest.converter.DSpaceRunnableParameterConverter;
import org.dspace.app.rest.matcher.BitstreamMatcher;
@@ -277,7 +277,7 @@ public class ScriptRestRepositoryIT extends AbstractControllerIntegrationTest {
try {
getClient(token)
.perform(multipart("/api/system/scripts/mock-script/processes")
.param("properties", new Gson().toJson(list)))
.param("properties", new ObjectMapper().writeValueAsString(list)))
.andExpect(status().isAccepted())
.andExpect(jsonPath("$", is(
ProcessMatcher.matchProcess("mock-script",
@@ -321,7 +321,7 @@ public class ScriptRestRepositoryIT extends AbstractControllerIntegrationTest {
try {
getClient(token)
.perform(multipart("/api/system/scripts/mock-script/processes")
.param("properties", new Gson().toJson(list)))
.param("properties", new ObjectMapper().writeValueAsString(list)))
.andExpect(status().isAccepted())
.andExpect(jsonPath("$", is(
ProcessMatcher.matchProcess("mock-script",
@@ -358,7 +358,7 @@ public class ScriptRestRepositoryIT extends AbstractControllerIntegrationTest {
try {
getClient(token)
.perform(multipart("/api/system/scripts/mock-script/processes")
.param("properties", new Gson().toJson(list)))
.param("properties", new ObjectMapper().writeValueAsString(list)))
.andExpect(status().isAccepted())
.andExpect(jsonPath("$", is(
ProcessMatcher.matchProcess("mock-script",
@@ -466,7 +466,7 @@ public class ScriptRestRepositoryIT extends AbstractControllerIntegrationTest {
.perform(multipart("/api/system/scripts/mock-script/processes")
.file(bitstreamFile)
.characterEncoding("UTF-8")
.param("properties", new Gson().toJson(list)))
.param("properties", new ObjectMapper().writeValueAsString(list)))
.andExpect(status().isAccepted())
.andExpect(jsonPath("$", is(
ProcessMatcher.matchProcess("mock-script",

View File

@@ -18,7 +18,7 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import com.google.gson.Gson;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.dspace.app.rest.converter.DSpaceRunnableParameterConverter;
import org.dspace.app.rest.matcher.ProcessMatcher;
import org.dspace.app.rest.model.ParameterValueRest;
@@ -77,7 +77,7 @@ public class CsvExportIT extends AbstractControllerIntegrationTest {
getClient(token)
.perform(multipart("/api/system/scripts/metadata-export/processes")
.param("properties", new Gson().toJson(list)))
.param("properties", new ObjectMapper().writeValueAsString(list)))
.andExpect(status().isAccepted())
.andExpect(jsonPath("$", is(
ProcessMatcher.matchProcess("metadata-export",
@@ -128,7 +128,7 @@ public class CsvExportIT extends AbstractControllerIntegrationTest {
getClient(token)
.perform(multipart("/api/system/scripts/metadata-export/processes")
.param("properties", new Gson().toJson(list)))
.param("properties", new ObjectMapper().writeValueAsString(list)))
.andExpect(status().isAccepted())
.andExpect(jsonPath("$", is(
ProcessMatcher.matchProcess("metadata-export",

View File

@@ -30,7 +30,7 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import com.google.gson.Gson;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.dspace.app.rest.converter.DSpaceRunnableParameterConverter;
import org.dspace.app.rest.matcher.ProcessMatcher;
import org.dspace.app.rest.matcher.RelationshipMatcher;
@@ -285,8 +285,7 @@ public class CsvImportIT extends AbstractEntityIntegrationTest {
getClient(token)
.perform(multipart("/api/system/scripts/metadata-import/processes").file(bitstreamFile)
.param("properties",
new Gson().toJson(list)))
.param("properties", new ObjectMapper().writeValueAsString(list)))
.andExpect(status().isAccepted())
.andDo(result -> idRef
.set(read(result.getResponse().getContentAsString(), "$.processId")));
@@ -345,8 +344,7 @@ public class CsvImportIT extends AbstractEntityIntegrationTest {
getClient(token)
.perform(multipart("/api/system/scripts/metadata-import/processes").file(bitstreamFile)
.param("properties",
new Gson().toJson(list)))
.param("properties", new ObjectMapper().writeValueAsString(list)))
.andExpect(status().isAccepted())
.andExpect(jsonPath("$", is(
ProcessMatcher.matchProcess("metadata-import",

View File

@@ -19,7 +19,7 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import com.google.gson.Gson;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.dspace.app.rest.converter.DSpaceRunnableParameterConverter;
import org.dspace.app.rest.matcher.ProcessMatcher;
import org.dspace.app.rest.model.ParameterValueRest;
@@ -88,7 +88,7 @@ public class CurationScriptIT extends AbstractControllerIntegrationTest {
// Request with -t <invalidTaskOption>
getClient(token)
.perform(multipart(CURATE_SCRIPT_ENDPOINT)
.param("properties", new Gson().toJson(list)))
.param("properties", new ObjectMapper().writeValueAsString(list)))
// Illegal Argument Exception
.andExpect(status().isBadRequest());
}
@@ -109,7 +109,7 @@ public class CurationScriptIT extends AbstractControllerIntegrationTest {
// Request with missing required -i <handle>
getClient(token)
.perform(multipart(CURATE_SCRIPT_ENDPOINT)
.param("properties", new Gson().toJson(list)))
.param("properties", new ObjectMapper().writeValueAsString(list)))
// Illegal Argument Exception
.andExpect(status().isBadRequest());
}
@@ -131,7 +131,7 @@ public class CurationScriptIT extends AbstractControllerIntegrationTest {
// Request with missing required -i <handle>
getClient(token)
.perform(multipart(CURATE_SCRIPT_ENDPOINT)
.param("properties", new Gson().toJson(list)))
.param("properties", new ObjectMapper().writeValueAsString(list)))
// Illegal Argument Exception
.andExpect(status().isBadRequest());
}
@@ -171,7 +171,7 @@ public class CurationScriptIT extends AbstractControllerIntegrationTest {
// Request without -t <task> or -T <taskFile> (and no -q <queue>)
getClient(token)
.perform(multipart(CURATE_SCRIPT_ENDPOINT)
.param("properties", new Gson().toJson(list)))
.param("properties", new ObjectMapper().writeValueAsString(list)))
// Illegal Argument Exception
.andExpect(status().isBadRequest());
}
@@ -193,7 +193,7 @@ public class CurationScriptIT extends AbstractControllerIntegrationTest {
// Request with invalid -s <scope>; must be object, curation or open
getClient(token)
.perform(multipart(CURATE_SCRIPT_ENDPOINT)
.param("properties", new Gson().toJson(list)))
.param("properties", new ObjectMapper().writeValueAsString(list)))
// Illegal Argument Exception
.andExpect(status().isBadRequest());
}
@@ -215,7 +215,7 @@ public class CurationScriptIT extends AbstractControllerIntegrationTest {
// Request with invalid -s <scope>; must be object, curation or open
getClient(token)
.perform(multipart(CURATE_SCRIPT_ENDPOINT)
.param("properties", new Gson().toJson(list)))
.param("properties", new ObjectMapper().writeValueAsString(list)))
// Illegal Argument Exception
.andExpect(status().isBadRequest());
}
@@ -257,7 +257,7 @@ public class CurationScriptIT extends AbstractControllerIntegrationTest {
try {
getClient(token)
.perform(multipart(CURATE_SCRIPT_ENDPOINT)
.param("properties", new Gson().toJson(list)))
.param("properties", new ObjectMapper().writeValueAsString(list)))
.andExpect(status().isAccepted())
.andExpect(jsonPath("$", is(
ProcessMatcher.matchProcess("curate",
@@ -308,7 +308,7 @@ public class CurationScriptIT extends AbstractControllerIntegrationTest {
try {
getClient(token)
.perform(multipart(CURATE_SCRIPT_ENDPOINT)
.param("properties", new Gson().toJson(list)))
.param("properties", new ObjectMapper().writeValueAsString(list)))
.andExpect(status().isAccepted())
.andExpect(jsonPath("$", is(
ProcessMatcher.matchProcess("curate",
@@ -359,7 +359,7 @@ public class CurationScriptIT extends AbstractControllerIntegrationTest {
getClient(token)
.perform(multipart(CURATE_SCRIPT_ENDPOINT)
.param("properties", new Gson().toJson(list)))
.param("properties", new ObjectMapper().writeValueAsString(list)))
.andExpect(jsonPath("$", is(
ProcessMatcher.matchProcess("curate",
String.valueOf(admin.getID()), parameters,

View File

@@ -1648,12 +1648,6 @@
<version>2.1.210</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
<scope>compile</scope>
</dependency>
<!-- Google Analytics -->
<dependency>
<groupId>com.google.apis</groupId>