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

Use JsonUtil.getJsonObject in AbstractApiBean, MakeDataCountApi #10062

Merged
merged 12 commits into from
Nov 13, 2023
22 changes: 10 additions & 12 deletions src/main/java/edu/harvard/iq/dataverse/api/AbstractApiBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
import edu.harvard.iq.dataverse.util.BundleUtil;
import edu.harvard.iq.dataverse.util.SystemConfig;
import edu.harvard.iq.dataverse.util.json.JsonParser;
import edu.harvard.iq.dataverse.util.json.JsonUtil;
import edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder;
import edu.harvard.iq.dataverse.validation.PasswordValidatorServiceBean;
import java.io.StringReader;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -62,9 +62,9 @@
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonException;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.json.JsonReader;
import jakarta.json.JsonValue;
import jakarta.json.JsonValue.ValueType;
import jakarta.persistence.EntityManager;
Expand Down Expand Up @@ -132,23 +132,21 @@ public Response refineResponse( String message ) {
* In the common case of the wrapped response being of type JSON,
* return the message field it has (if any).
* @return the content of a message field, or {@code null}.
* @throws JsonException when JSON parsing fails.
*/
String getWrappedMessageWhenJson() {
if ( response.getMediaType().equals(MediaType.APPLICATION_JSON_TYPE) ) {
Object entity = response.getEntity();
if ( entity == null ) return null;

String json = entity.toString();
try ( StringReader rdr = new StringReader(json) ){
JsonReader jrdr = Json.createReader(rdr);
JsonObject obj = jrdr.readObject();
if ( obj.containsKey("message") ) {
JsonValue message = obj.get("message");
return message.getValueType() == ValueType.STRING ? obj.getString("message") : message.toString();
} else {
return null;
}
JsonObject obj = JsonUtil.getJsonObject(entity.toString());
if ( obj.containsKey("message") ) {
JsonValue message = obj.get("message");
return message.getValueType() == ValueType.STRING ? obj.getString("message") : message.toString();
} else {
return null;
}

} else {
return null;
}
Expand Down
42 changes: 17 additions & 25 deletions src/main/java/edu/harvard/iq/dataverse/api/MakeDataCountApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
import edu.harvard.iq.dataverse.makedatacount.DatasetMetricsServiceBean;
import edu.harvard.iq.dataverse.settings.JvmSettings;
import edu.harvard.iq.dataverse.util.SystemConfig;
import edu.harvard.iq.dataverse.util.json.JsonUtil;

import java.io.FileReader;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -83,26 +82,21 @@ public Response sendDataToHub() {
@Path("{id}/addUsageMetricsFromSushiReport")
public Response addUsageMetricsFromSushiReport(@PathParam("id") String id, @QueryParam("reportOnDisk") String reportOnDisk) {

JsonObject report;

try (FileReader reader = new FileReader(reportOnDisk)) {
report = Json.createReader(reader).readObject();
Dataset dataset;
try {
dataset = findDatasetOrDie(id);
List<DatasetMetrics> datasetMetrics = datasetMetricsService.parseSushiReport(report, dataset);
if (!datasetMetrics.isEmpty()) {
for (DatasetMetrics dm : datasetMetrics) {
datasetMetricsService.save(dm);
}
try {
JsonObject report = JsonUtil.getJsonObjectFromFile(reportOnDisk);
Dataset dataset = findDatasetOrDie(id);
List<DatasetMetrics> datasetMetrics = datasetMetricsService.parseSushiReport(report, dataset);
if (!datasetMetrics.isEmpty()) {
for (DatasetMetrics dm : datasetMetrics) {
datasetMetricsService.save(dm);
}
} catch (WrappedResponse ex) {
Logger.getLogger(MakeDataCountApi.class.getName()).log(Level.SEVERE, null, ex);
return error(Status.BAD_REQUEST, "Wrapped response: " + ex.getLocalizedMessage());
}
} catch (WrappedResponse ex) {
logger.log(Level.SEVERE, null, ex);
return error(Status.BAD_REQUEST, "Wrapped response: " + ex.getLocalizedMessage());

} catch (IOException ex) {
System.out.print(ex.getMessage());
logger.log(Level.WARNING, ex.getMessage());
return error(Status.BAD_REQUEST, "IOException: " + ex.getLocalizedMessage());
}
String msg = "Dummy Data has been added to dataset " + id;
Expand All @@ -113,10 +107,8 @@ public Response addUsageMetricsFromSushiReport(@PathParam("id") String id, @Quer
@Path("/addUsageMetricsFromSushiReport")
public Response addUsageMetricsFromSushiReportAll(@PathParam("id") String id, @QueryParam("reportOnDisk") String reportOnDisk) {

JsonObject report;

try (FileReader reader = new FileReader(reportOnDisk)) {
report = Json.createReader(reader).readObject();
try {
JsonObject report = JsonUtil.getJsonObjectFromFile(reportOnDisk);

List<DatasetMetrics> datasetMetrics = datasetMetricsService.parseSushiReport(report, null);
if (!datasetMetrics.isEmpty()) {
Expand All @@ -126,7 +118,7 @@ public Response addUsageMetricsFromSushiReportAll(@PathParam("id") String id, @Q
}

} catch (IOException ex) {
System.out.print(ex.getMessage());
logger.log(Level.WARNING, ex.getMessage());
return error(Status.BAD_REQUEST, "IOException: " + ex.getLocalizedMessage());
}
String msg = "Usage Metrics Data has been added to all datasets from file " + reportOnDisk;
Expand All @@ -135,7 +127,7 @@ public Response addUsageMetricsFromSushiReportAll(@PathParam("id") String id, @Q

@POST
@Path("{id}/updateCitationsForDataset")
public Response updateCitationsForDataset(@PathParam("id") String id) throws MalformedURLException, IOException {
public Response updateCitationsForDataset(@PathParam("id") String id) throws IOException {
try {
Dataset dataset = findDatasetOrDie(id);
String persistentId = dataset.getGlobalId().toString();
Expand All @@ -158,7 +150,7 @@ public Response updateCitationsForDataset(@PathParam("id") String id) throws Mal
logger.warning("Failed to get citations from " + url.toString());
return error(Status.fromStatusCode(status), "Failed to get citations from " + url.toString());
}
JsonObject report = Json.createReader(connection.getInputStream()).readObject();
JsonObject report = JsonUtil.getJsonObject(connection.getInputStream());
bencomp marked this conversation as resolved.
Show resolved Hide resolved
JsonObject links = report.getJsonObject("links");
JsonArray data = report.getJsonArray("data");
Iterator<JsonValue> iter = data.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import edu.harvard.iq.dataverse.engine.command.RequiredPermissions;
import edu.harvard.iq.dataverse.engine.command.exception.CommandException;
import edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException;
import edu.harvard.iq.dataverse.util.json.JsonUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;
import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonReader;

@RequiredPermissions(Permission.EditDataset)
public class GetProvJsonCommand extends AbstractCommand<JsonObject> {
Expand All @@ -37,9 +37,8 @@ public JsonObject execute(CommandContext ctxt) throws CommandException {
StorageIO<DataFile> dataAccess = dataFile.getStorageIO();
InputStream inputStream = dataAccess.getAuxFileAsInputStream(provJsonExtension);
bencomp marked this conversation as resolved.
Show resolved Hide resolved
JsonObject jsonObject = null;
if(null != inputStream) {
JsonReader jsonReader = Json.createReader(inputStream);
jsonObject = jsonReader.readObject();
if (null != inputStream) {
jsonObject = JsonUtil.getJsonObject(inputStream);
}
return jsonObject;
} catch (IOException ex) {
Expand Down
52 changes: 40 additions & 12 deletions src/main/java/edu/harvard/iq/dataverse/util/json/JsonUtil.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package edu.harvard.iq.dataverse.util.json;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.JsonReader;
import jakarta.json.JsonWriter;
import jakarta.json.JsonWriterFactory;
Expand All @@ -20,17 +21,19 @@ public class JsonUtil {

private static final Logger logger = Logger.getLogger(JsonUtil.class.getCanonicalName());

private JsonUtil() {}

/**
* Make an attempt at pretty printing a String but will return the original
* string if it isn't JSON or if there is any exception.
*/
public static String prettyPrint(String jsonString) {
try {
com.google.gson.JsonParser jsonParser = new com.google.gson.JsonParser();
JsonObject jsonObject = jsonParser.parse(jsonString).getAsJsonObject();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String prettyJson = gson.toJson(jsonObject);
return prettyJson;
if (jsonString.trim().startsWith("{")) {
return prettyPrint(getJsonObject(jsonString));
} else {
return prettyPrint(getJsonArray(jsonString));
}
} catch (Exception ex) {
logger.info("Returning original string due to exception: " + ex);
return jsonString;
Expand All @@ -48,7 +51,7 @@ public static String prettyPrint(JsonArray jsonArray) {
return stringWriter.toString();
}

public static String prettyPrint(jakarta.json.JsonObject jsonObject) {
public static String prettyPrint(JsonObject jsonObject) {
Map<String, Boolean> config = new HashMap<>();
config.put(JsonGenerator.PRETTY_PRINTING, true);
JsonWriterFactory jsonWriterFactory = Json.createWriterFactory(config);
Expand All @@ -59,15 +62,40 @@ public static String prettyPrint(jakarta.json.JsonObject jsonObject) {
return stringWriter.toString();
}

public static jakarta.json.JsonObject getJsonObject(String serializedJson) {
public static JsonObject getJsonObject(String serializedJson) {
try (StringReader rdr = new StringReader(serializedJson)) {
try (JsonReader jsonReader = Json.createReader(rdr)) {
return jsonReader.readObject();
}
}
}

public static jakarta.json.JsonArray getJsonArray(String serializedJson) {
/**
* Return the contents of the {@link InputStream} as a JSON object.
* @param stream the input stream to read from
* @throws JsonException when parsing fails.
*/
public static JsonObject getJsonObject(InputStream stream) {
try (JsonReader jsonReader = Json.createReader(stream)) {
return jsonReader.readObject();
}
}

/**
* Return the contents of the file as a JSON object.
* @param fileName the name of the file to read from
* @throws FileNotFoundException when the file cannot be opened for reading
* @throws JsonException when parsing fails.
*/
public static JsonObject getJsonObjectFromFile(String fileName) throws IOException {
try (FileReader rdr = new FileReader(fileName)) {
try (JsonReader jsonReader = Json.createReader(rdr)) {
return jsonReader.readObject();
}
}
}

public static JsonArray getJsonArray(String serializedJson) {
try (StringReader rdr = new StringReader(serializedJson)) {
try (JsonReader jsonReader = Json.createReader(rdr)) {
return jsonReader.readArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

public class JsonUtilTest {
class JsonUtilTest {

@Test
public void testPrettyPrint() {
JsonUtil jsonUtil = new JsonUtil();
void testPrettyPrint() {
String nullString = null;
assertEquals(null, JsonUtil.prettyPrint(nullString));
assertEquals("", JsonUtil.prettyPrint(""));
assertEquals("junk", JsonUtil.prettyPrint("junk"));
assertEquals("{}", JsonUtil.prettyPrint("{}"));
assertEquals("{\n" + " \"foo\": \"bar\"\n" + "}", JsonUtil.prettyPrint("{\"foo\": \"bar\"}"));
assertEquals("{\n}", JsonUtil.prettyPrint("{}"));
assertEquals("[\n \"junk\"\n]", JsonUtil.prettyPrint("[\"junk\"]"));
assertEquals("{\n" + " \"foo\": \"bar\"\n" + "}", JsonUtil.prettyPrint("{\"foo\": \"bar\"}"));
}

}
Loading