Skip to content

Commit

Permalink
Improvements on input validation with response messages for the unset…
Browse files Browse the repository at this point in the history
…-retention API
  • Loading branch information
PaulBoon committed May 1, 2024
1 parent 639c162 commit 8f65a46
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/main/java/edu/harvard/iq/dataverse/api/Datasets.java
Original file line number Diff line number Diff line change
Expand Up @@ -1914,22 +1914,35 @@ public Response removeFileRetention(@Context ContainerRequestContext crc, @PathP
return error(Status.BAD_REQUEST, "No Retention periods allowed");
}

JsonObject json = JsonUtil.getJsonObject(jsonBody);
JsonObject json;
try {
json = JsonUtil.getJsonObject(jsonBody);
} catch (JsonException ex) {
return error(Status.BAD_REQUEST, "Invalid JSON; error message: " + ex.getMessage());
}

List<DataFile> datasetFiles = dataset.getFiles();
List<DataFile> retentionFilesToUnset = new LinkedList<>();

// extract fileIds from json, find datafiles and add to list
if (json.containsKey("fileIds")){
JsonArray fileIds = json.getJsonArray("fileIds");
for (JsonValue jsv : fileIds) {
try {
DataFile dataFile = findDataFileOrDie(jsv.toString());
retentionFilesToUnset.add(dataFile);
} catch (WrappedResponse ex) {
return ex.getResponse();
try {
JsonArray fileIds = json.getJsonArray("fileIds");
for (JsonValue jsv : fileIds) {
try {
DataFile dataFile = findDataFileOrDie(jsv.toString());
retentionFilesToUnset.add(dataFile);
} catch (WrappedResponse ex) {
return ex.getResponse();
}
}
} catch (ClassCastException ccex) {
return error(Status.BAD_REQUEST, "fileIds must be an array of id strings");
} catch (NullPointerException npex) {
return error(Status.BAD_REQUEST, "No fileIds specified");
}
} else {
return error(Status.BAD_REQUEST, "No fileIds specified");
}

List<Retention> orphanedRetentions = new ArrayList<Retention>();
Expand Down

0 comments on commit 8f65a46

Please sign in to comment.