Search in sources :

Example 1 with DataFileZipper

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();
}
Also used : DataFileZipper(edu.harvard.iq.dataverse.dataaccess.DataFileZipper) ForbiddenException(javax.ws.rs.ForbiddenException) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) User(edu.harvard.iq.dataverse.authorization.users.User) PrivateUrlUser(edu.harvard.iq.dataverse.authorization.users.PrivateUrlUser) GuestUser(edu.harvard.iq.dataverse.authorization.users.GuestUser) GuestbookResponse(edu.harvard.iq.dataverse.GuestbookResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) NotFoundException(javax.ws.rs.NotFoundException) StreamingOutput(javax.ws.rs.core.StreamingOutput) DataFile(edu.harvard.iq.dataverse.DataFile) BadRequestException(javax.ws.rs.BadRequestException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

DataFile (edu.harvard.iq.dataverse.DataFile)1 GuestbookResponse (edu.harvard.iq.dataverse.GuestbookResponse)1 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)1 GuestUser (edu.harvard.iq.dataverse.authorization.users.GuestUser)1 PrivateUrlUser (edu.harvard.iq.dataverse.authorization.users.PrivateUrlUser)1 User (edu.harvard.iq.dataverse.authorization.users.User)1 DataFileZipper (edu.harvard.iq.dataverse.dataaccess.DataFileZipper)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 BadRequestException (javax.ws.rs.BadRequestException)1 ForbiddenException (javax.ws.rs.ForbiddenException)1 GET (javax.ws.rs.GET)1 NotFoundException (javax.ws.rs.NotFoundException)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 StreamingOutput (javax.ws.rs.core.StreamingOutput)1