use of edu.harvard.iq.dataverse.dataaccess.DataFileZipper in project dataverse by IQSS.
the class Access method datafiles.
/*
* API method for downloading zipped bundles of multiple files:
*/
@Path("datafiles/{fileIds}")
@GET
@Produces({ "application/zip" })
public Response datafiles(@PathParam("fileIds") String fileIds, @QueryParam("gbrecs") Boolean gbrecs, @QueryParam("key") String apiTokenParam, @Context UriInfo uriInfo, @Context HttpHeaders headers, @Context HttpServletResponse response) throws WebApplicationException /*throws NotFoundException, ServiceUnavailableException, PermissionDeniedException, AuthorizationRequiredException*/
{
long setLimit = systemConfig.getZipDownloadLimit();
if (!(setLimit > 0L)) {
setLimit = DataFileZipper.DEFAULT_ZIPFILE_LIMIT;
}
long zipDownloadSizeLimit = setLimit;
logger.fine("setting zip download size limit to " + zipDownloadSizeLimit + " bytes.");
if (fileIds == null || fileIds.equals("")) {
throw new BadRequestException();
}
String apiToken = (apiTokenParam == null || apiTokenParam.equals("")) ? headers.getHeaderString(API_KEY_HEADER) : apiTokenParam;
// for use in adding gb records if necessary
User apiTokenUser = findAPITokenUser(apiToken);
StreamingOutput stream = new StreamingOutput() {
@Override
public void write(OutputStream os) throws IOException, WebApplicationException {
String[] fileIdParams = fileIds.split(",");
DataFileZipper zipper = null;
boolean accessToUnrestrictedFileAuthorized = false;
String fileManifest = "";
long sizeTotal = 0L;
if (fileIdParams != null && fileIdParams.length > 0) {
logger.fine(fileIdParams.length + " tokens;");
for (int i = 0; i < fileIdParams.length; i++) {
logger.fine("token: " + fileIdParams[i]);
Long fileId = null;
try {
fileId = new Long(fileIdParams[i]);
} catch (NumberFormatException nfe) {
fileId = null;
}
if (fileId != null) {
logger.fine("attempting to look up file id " + fileId);
DataFile file = dataFileService.find(fileId);
if (file != null) {
if ((accessToUnrestrictedFileAuthorized && !file.isRestricted()) || isAccessAuthorized(file, apiToken)) {
if (!file.isRestricted()) {
accessToUnrestrictedFileAuthorized = true;
}
logger.fine("adding datafile (id=" + file.getId() + ") to the download list of the ZippedDownloadInstance.");
// downloadInstance.addDataFile(file);
if (gbrecs == null && file.isReleased()) {
GuestbookResponse gbr = guestbookResponseService.initAPIGuestbookResponse(file.getOwner(), file, session, apiTokenUser);
guestbookResponseService.save(gbr);
}
if (zipper == null) {
// This is the first file we can serve - so we now know that we are going to be able
// to produce some output.
zipper = new DataFileZipper(os);
zipper.setFileManifest(fileManifest);
response.setHeader("Content-disposition", "attachment; filename=\"dataverse_files.zip\"");
response.setHeader("Content-Type", "application/zip; name=\"dataverse_files.zip\"");
}
if (sizeTotal + file.getFilesize() < zipDownloadSizeLimit) {
sizeTotal += zipper.addFileToZipStream(file);
} else {
String fileName = file.getFileMetadata().getLabel();
String mimeType = file.getContentType();
zipper.addToManifest(fileName + " (" + mimeType + ") " + " skipped because the total size of the download bundle exceeded the limit of " + zipDownloadSizeLimit + " bytes.\r\n");
}
} else {
if (zipper == null) {
fileManifest = fileManifest + file.getFileMetadata().getLabel() + " IS RESTRICTED AND CANNOT BE DOWNLOADED\r\n";
} else {
zipper.addToManifest(file.getFileMetadata().getLabel() + " IS RESTRICTED AND CANNOT BE DOWNLOADED\r\n");
}
}
} else {
// Or should we just drop it and make a note in the Manifest?
throw new NotFoundException();
}
}
}
} else {
throw new BadRequestException();
}
if (zipper == null) {
// just give them a 403:
throw new ForbiddenException();
}
// This will add the generated File Manifest to the zipped output,
// then flush and close the stream:
zipper.finalizeZipStream();
// os.flush();
// os.close();
}
};
return Response.ok(stream).build();
}
Aggregations