From 794857970ff8df2edea0d7a93ffc324521d4b162 Mon Sep 17 00:00:00 2001 From: Philip Durbin Date: Fri, 8 Dec 2017 16:00:01 -0500 Subject: [PATCH] store JSON-PROV file as an aux file properly #4343 --- .../edu/harvard/iq/dataverse/api/ProvApi.java | 56 ++++++------------- .../harvard/iq/dataverse/api/ProvApiIT.java | 2 +- 2 files changed, 17 insertions(+), 41 deletions(-) diff --git a/src/main/java/edu/harvard/iq/dataverse/api/ProvApi.java b/src/main/java/edu/harvard/iq/dataverse/api/ProvApi.java index 7b7d65edf4a..c4ab99d31d7 100644 --- a/src/main/java/edu/harvard/iq/dataverse/api/ProvApi.java +++ b/src/main/java/edu/harvard/iq/dataverse/api/ProvApi.java @@ -2,16 +2,12 @@ import edu.harvard.iq.dataverse.DataFile; import edu.harvard.iq.dataverse.FileMetadata; -import edu.harvard.iq.dataverse.dataaccess.DataAccess; -import edu.harvard.iq.dataverse.dataaccess.DataAccessOption; import edu.harvard.iq.dataverse.dataaccess.StorageIO; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.io.StringReader; -import java.nio.channels.Channel; -import java.nio.channels.Channels; -import java.nio.channels.WritableByteChannel; +import java.nio.charset.StandardCharsets; import java.util.logging.Logger; import javax.json.Json; import javax.json.JsonException; @@ -24,7 +20,6 @@ import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; import static javax.ws.rs.core.Response.Status.BAD_REQUEST; -import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; import static javax.ws.rs.core.Response.Status.NOT_FOUND; @Path("files") @@ -63,43 +58,24 @@ public Response addProvJson(String body, @PathParam("id") Long idSupplied) { * https://www.w3.org/Submission/prov-json/schema */ // Write to StorageIO. - StorageIO storageIO = null; + // "json.json" looks a little redundand but the standard is called PROV-JSON and it's nice to see .json on disk. + final String provJsonExtension = "prov-json.json"; try { - storageIO = DataAccess.createNewStorageIO(dataFile, "prov-json"); - Channel outputChannel = storageIO.openAuxChannel(dataFile.getId() + ".json", DataAccessOption.WRITE_ACCESS); - OutputStream outputStream = Channels.newOutputStream((WritableByteChannel) outputChannel); - logger.info("writing body: " + body); - outputStream.write(body.toString().getBytes("UTF8")); - // FIXME: Investigate why two file are being saved: - // - 10.5072/FK2/TZF120/prov-json (empty) - // - 10.5072/FK2/TZF120/prov-json.165.json (looks good) - logger.info("done writing body"); + StorageIO storageIO = dataFile.getStorageIO(); + InputStream inputStream = new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8.name())); + storageIO.saveInputStreamAsAux(inputStream, provJsonExtension); } catch (IOException ex) { logger.warning("Exception caught writing body: " + ex); } - // Read from StorageIO. FIXME: This isn't working. The idea was to make sure we can read back the bits we persisted to StorageIO above. - boolean readBackFromStorageIo = false; - if (readBackFromStorageIo) { - StorageIO dataAccess = null; - try { - dataAccess = DataAccess.getStorageIO(dataFile); - } catch (IOException ex) { - logger.info("Exception caught in DataAccess.getStorageIO(dataFile): " + ex); - } - InputStream cachedExportInputStream = null; - try { - cachedExportInputStream = dataAccess.getAuxFileAsInputStream("prov-json" + dataFile.getId() + ".json"); - } catch (IOException ex) { - logger.info("Exception caught in dataAccess.getAuxFileAsInputStream: " + ex); - } - if (cachedExportInputStream == null) { - return error(INTERNAL_SERVER_ERROR, "cachedExportInputStream was null."); - } - JsonReader jsonReader = Json.createReader(cachedExportInputStream); - JsonObject jsonObj2 = jsonReader.readObject(); - response.add("message", "PROV-JSON provenance data saved: " + jsonObj2.toString()); - } else { - response.add("message", "PROV-JSON provenance data saved."); + // Read from StorageIO and show it to the user. + try { + StorageIO dataAccess = dataFile.getStorageIO(); + InputStream inputStream = dataAccess.getAuxFileAsInputStream(provJsonExtension); + JsonReader jsonReader = Json.createReader(inputStream); + JsonObject jsonObject = jsonReader.readObject(); + response.add("message", "PROV-JSON provenance data saved: " + jsonObject.toString()); + } catch (IOException ex) { + logger.info("Exception caught in DataAccess.getStorageIO(dataFile): " + ex); } return ok(response); } diff --git a/src/test/java/edu/harvard/iq/dataverse/api/ProvApiIT.java b/src/test/java/edu/harvard/iq/dataverse/api/ProvApiIT.java index a998bb66f9d..8b14a1b5987 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/ProvApiIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/ProvApiIT.java @@ -64,7 +64,7 @@ public void testAddProvFile() { Response uploadProvJson = UtilIT.uploadProvJson(dataFileId.toString(), provJsonGood, apiTokenForDepositor); uploadProvJson.prettyPrint(); uploadProvJson.then().assertThat() - .body("data.message", equalTo("PROV-JSON provenance data saved.")) + .body("data.message", equalTo("PROV-JSON provenance data saved: {\"prov\":true,\"foo\":\"bar\"}")) .statusCode(OK.getStatusCode()); JsonObject provFreeFormGood = Json.createObjectBuilder()