diff --git a/src/main/java/edu/harvard/iq/dataverse/DatasetVersionServiceBean.java b/src/main/java/edu/harvard/iq/dataverse/DatasetVersionServiceBean.java index e4eb6aac88e..3c1ae3abf38 100644 --- a/src/main/java/edu/harvard/iq/dataverse/DatasetVersionServiceBean.java +++ b/src/main/java/edu/harvard/iq/dataverse/DatasetVersionServiceBean.java @@ -648,13 +648,8 @@ public RetrieveDatasetVersionResponse selectRequestedVersion(List handleLatestPublished() { } })); if (dsv == null) { + // (A "Not Found" would be more appropriate here, I believe, than a "Bad Request". + // But we've been using the latter for a while, and it's a popular API... + // and this return code is expected by our tests - so I'm choosing it to keep + // -- L.A.) return error(BAD_REQUEST, BundleUtil.getStringFromBundle("access.api.exception.version.not.found")); } String fileIds = getFileIdsAsCommaSeparated(dsv.getFileMetadatas()); + // We don't want downloads from Draft versions to be counted, + // so we are setting the gbrecs (aka "do not write guestbook response") + // variable accordingly: + if (dsv.isDraft()) { + gbrecs = true; + } return downloadDatafiles(fileIds, gbrecs, apiTokenParam, uriInfo, headers, response); } catch (WrappedResponse wr) { return wr.getResponse(); @@ -701,7 +737,7 @@ public Response datafiles(@PathParam("fileIds") String fileIds, @QueryParam("gbr return downloadDatafiles(fileIds, gbrecs, apiTokenParam, uriInfo, headers, response); } - private Response downloadDatafiles(String rawFileIds, boolean gbrecs, String apiTokenParam, UriInfo uriInfo, HttpHeaders headers, HttpServletResponse response) throws WebApplicationException /* throws NotFoundException, ServiceUnavailableException, PermissionDeniedException, AuthorizationRequiredException*/ { + private Response downloadDatafiles(String rawFileIds, boolean donotwriteGBResponse, String apiTokenParam, UriInfo uriInfo, HttpHeaders headers, HttpServletResponse response) throws WebApplicationException /* throws NotFoundException, ServiceUnavailableException, PermissionDeniedException, AuthorizationRequiredException*/ { long setLimit = systemConfig.getZipDownloadLimit(); if (!(setLimit > 0L)) { setLimit = DataFileZipper.DEFAULT_ZIPFILE_LIMIT; @@ -745,7 +781,7 @@ private Response downloadDatafiles(String rawFileIds, boolean gbrecs, String api if (useCustomZipService) { URI redirect_uri = null; try { - redirect_uri = handleCustomZipDownload(customZipServiceUrl, fileIds, apiToken, apiTokenUser, uriInfo, headers, gbrecs, true); + redirect_uri = handleCustomZipDownload(customZipServiceUrl, fileIds, apiToken, apiTokenUser, uriInfo, headers, donotwriteGBResponse, true); } catch (WebApplicationException wae) { throw wae; } @@ -789,7 +825,7 @@ public void write(OutputStream os) throws IOException, logger.fine("adding datafile (id=" + file.getId() + ") to the download list of the ZippedDownloadInstance."); //downloadInstance.addDataFile(file); - if (gbrecs != true && file.isReleased()){ + if (donotwriteGBResponse != true && file.isReleased()){ GuestbookResponse gbr = guestbookResponseService.initAPIGuestbookResponse(file.getOwner(), file, session, apiTokenUser); guestbookResponseService.save(gbr); MakeDataCountEntry entry = new MakeDataCountEntry(uriInfo, headers, dvRequestService, file); @@ -1874,7 +1910,8 @@ private User findAPITokenUser(String apiToken) { return apiTokenUser; } - private URI handleCustomZipDownload(String customZipServiceUrl, String fileIds, String apiToken, User apiTokenUser, UriInfo uriInfo, HttpHeaders headers, boolean gbrecs, boolean orig) throws WebApplicationException { + private URI handleCustomZipDownload(String customZipServiceUrl, String fileIds, String apiToken, User apiTokenUser, UriInfo uriInfo, HttpHeaders headers, boolean donotwriteGBResponse, boolean orig) throws WebApplicationException { + String zipServiceKey = null; Timestamp timestamp = null; @@ -1901,7 +1938,7 @@ private URI handleCustomZipDownload(String customZipServiceUrl, String fileIds, validFileCount++; if (isAccessAuthorized(file, apiToken)) { logger.fine("adding datafile (id=" + file.getId() + ") to the download list of the ZippedDownloadInstance."); - if (gbrecs != true && file.isReleased()) { + if (donotwriteGBResponse != true && file.isReleased()) { GuestbookResponse gbr = guestbookResponseService.initAPIGuestbookResponse(file.getOwner(), file, session, apiTokenUser); guestbookResponseService.save(gbr); MakeDataCountEntry entry = new MakeDataCountEntry(uriInfo, headers, dvRequestService, file); diff --git a/src/main/java/propertyFiles/Bundle.properties b/src/main/java/propertyFiles/Bundle.properties index d22f6f6537f..ad556123b98 100644 --- a/src/main/java/propertyFiles/Bundle.properties +++ b/src/main/java/propertyFiles/Bundle.properties @@ -2395,6 +2395,7 @@ access.api.requestList.noRequestsFound=There are no access requests for this fil access.api.exception.metadata.not.available.for.nontabular.file=This type of metadata is only available for tabular files. access.api.exception.metadata.restricted.no.permission=You do not have permission to download this file. access.api.exception.version.not.found=Could not find requested dataset version. +access.api.exception.dataset.not.found=Could not find requested dataset. #permission permission.AddDataverse.label=AddDataverse diff --git a/src/test/java/edu/harvard/iq/dataverse/api/DownloadFilesIT.java b/src/test/java/edu/harvard/iq/dataverse/api/DownloadFilesIT.java index 96bd5be6ca5..7d5adf95507 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/DownloadFilesIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/DownloadFilesIT.java @@ -20,6 +20,7 @@ import static javax.ws.rs.core.Response.Status.FORBIDDEN; import static javax.ws.rs.core.Response.Status.OK; import static javax.ws.rs.core.Response.Status.UNAUTHORIZED; +import static javax.ws.rs.core.Response.Status.BAD_REQUEST; import static org.hamcrest.CoreMatchers.equalTo; import org.junit.Assert; import static org.junit.Assert.assertTrue; @@ -92,10 +93,12 @@ public void downloadAllFilesByVersion() throws IOException { Assert.assertEquals(expectedFiles1, filenamesFound1); // A guest user can't download unpublished files. + // (a guest user cannot even see that the draft version actually exists; + // so they are going to get a "BAD REQUEST", not "UNAUTHORIZED":) Response downloadFiles2 = UtilIT.downloadFiles(datasetPid, null); downloadFiles2.prettyPrint(); downloadFiles2.then().assertThat() - .statusCode(UNAUTHORIZED.getStatusCode()) + .statusCode(BAD_REQUEST.getStatusCode()) .body("status", equalTo("ERROR")); UtilIT.publishDataverseViaNativeApi(dataverseAlias, apiToken)