Search in sources :

Example 71 with DataFile

use of edu.harvard.iq.dataverse.DataFile 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)

Example 72 with DataFile

use of edu.harvard.iq.dataverse.DataFile in project dataverse by IQSS.

the class Access method tabularDatafileMetadataPreprocessed.

/*
     * "Preprocessed data" metadata format:
     * (this was previously provided as a "format conversion" option of the 
     * file download form of the access API call)
     */
@Path("datafile/{fileId}/metadata/preprocessed")
@GET
@Produces({ "text/xml" })
public DownloadInstance tabularDatafileMetadataPreprocessed(@PathParam("fileId") Long fileId, @QueryParam("key") String apiToken, @Context UriInfo uriInfo, @Context HttpHeaders headers, @Context HttpServletResponse response) throws ServiceUnavailableException {
    DataFile df = dataFileService.find(fileId);
    if (df == null) {
        logger.warning("Access: datafile service could not locate a DataFile object for id " + fileId + "!");
        throw new NotFoundException();
    }
    if (apiToken == null || apiToken.equals("")) {
        apiToken = headers.getHeaderString(API_KEY_HEADER);
    }
    // This will throw a ForbiddenException if access isn't authorized:
    checkAuthorization(df, apiToken);
    DownloadInfo dInfo = new DownloadInfo(df);
    if (df.isTabularData()) {
        dInfo.addServiceAvailable(new OptionalAccessService("preprocessed", "application/json", "format=prep", "Preprocessed data in JSON"));
    } else {
        throw new ServiceUnavailableException("Preprocessed Content Metadata requested on a non-tabular data file.");
    }
    DownloadInstance downloadInstance = new DownloadInstance(dInfo);
    if (downloadInstance.isDownloadServiceSupported("format", "prep")) {
        logger.fine("Preprocessed data for tabular file " + fileId);
    }
    response.setHeader("Access-Control-Allow-Origin", "*");
    return downloadInstance;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) NotFoundException(javax.ws.rs.NotFoundException) OptionalAccessService(edu.harvard.iq.dataverse.dataaccess.OptionalAccessService) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 73 with DataFile

use of edu.harvard.iq.dataverse.DataFile in project dataverse by IQSS.

the class Access method datafileBundle.

// @EJB
// TODO:
// versions? -- L.A. 4.0 beta 10
@Path("datafile/bundle/{fileId}")
@GET
@Produces({ "application/zip" })
public BundleDownloadInstance datafileBundle(@PathParam("fileId") Long fileId, @QueryParam("gbrecs") Boolean gbrecs, @QueryParam("key") String apiToken, @Context UriInfo uriInfo, @Context HttpHeaders headers, @Context HttpServletResponse response) /*throws NotFoundException, ServiceUnavailableException, PermissionDeniedException, AuthorizationRequiredException*/
{
    DataFile df = dataFileService.find(fileId);
    GuestbookResponse gbr = null;
    if (df == null) {
        logger.warning("Access: datafile service could not locate a DataFile object for id " + fileId + "!");
        throw new NotFoundException();
    }
    if (apiToken == null || apiToken.equals("")) {
        apiToken = headers.getHeaderString(API_KEY_HEADER);
    }
    // This will throw a ForbiddenException if access isn't authorized:
    checkAuthorization(df, apiToken);
    if (gbrecs == null && df.isReleased()) {
        // Write Guestbook record if not done previously and file is released
        User apiTokenUser = findAPITokenUser(apiToken);
        gbr = guestbookResponseService.initAPIGuestbookResponse(df.getOwner(), df, session, apiTokenUser);
        guestbookResponseService.save(gbr);
    }
    DownloadInfo dInfo = new DownloadInfo(df);
    BundleDownloadInstance downloadInstance = new BundleDownloadInstance(dInfo);
    FileMetadata fileMetadata = df.getFileMetadata();
    DatasetVersion datasetVersion = df.getOwner().getLatestVersion();
    downloadInstance.setFileCitationEndNote(datasetService.createCitationXML(datasetVersion, fileMetadata));
    downloadInstance.setFileCitationRIS(datasetService.createCitationRIS(datasetVersion, fileMetadata));
    downloadInstance.setFileCitationBibtex(new BibtexCitation(datasetVersion).toString());
    ByteArrayOutputStream outStream = null;
    outStream = new ByteArrayOutputStream();
    try {
        ddiExportService.exportDataFile(fileId, outStream, null, null);
        downloadInstance.setFileDDIXML(outStream.toString());
    } catch (Exception ex) {
    // if we can't generate the DDI, it's ok;
    // we'll just generate the bundle without it.
    }
    return downloadInstance;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) 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) FileMetadata(edu.harvard.iq.dataverse.FileMetadata) NotFoundException(javax.ws.rs.NotFoundException) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) BibtexCitation(edu.harvard.iq.dataverse.BibtexCitation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BadRequestException(javax.ws.rs.BadRequestException) NotFoundException(javax.ws.rs.NotFoundException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) WebApplicationException(javax.ws.rs.WebApplicationException) ForbiddenException(javax.ws.rs.ForbiddenException) IOException(java.io.IOException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 74 with DataFile

use of edu.harvard.iq.dataverse.DataFile in project dataverse by IQSS.

the class Access method tabularDatafileMetadataDDI.

/* 
     * This has been moved here, under /api/access, from the /api/meta hierarchy
     * which we are going to retire.
     */
@Path("datafile/{fileId}/metadata/ddi")
@GET
@Produces({ "text/xml" })
public String tabularDatafileMetadataDDI(@PathParam("fileId") Long fileId, @QueryParam("exclude") String exclude, @QueryParam("include") String include, @Context HttpHeaders header, @Context HttpServletResponse response) throws NotFoundException, ServiceUnavailableException /*, PermissionDeniedException, AuthorizationRequiredException*/
{
    String retValue = "";
    DataFile dataFile = null;
    // httpHeaders.add("Content-disposition", "attachment; filename=\"dataverse_files.zip\"");
    // httpHeaders.add("Content-Type", "application/zip; name=\"dataverse_files.zip\"");
    response.setHeader("Content-disposition", "attachment; filename=\"dataverse_files.zip\"");
    dataFile = dataFileService.find(fileId);
    if (dataFile == null) {
        throw new NotFoundException();
    }
    String fileName = dataFile.getFileMetadata().getLabel().replaceAll("\\.tab$", "-ddi.xml");
    response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "\"");
    response.setHeader("Content-Type", "application/xml; name=\"" + fileName + "\"");
    ByteArrayOutputStream outStream = null;
    outStream = new ByteArrayOutputStream();
    try {
        ddiExportService.exportDataFile(fileId, outStream, exclude, include);
        retValue = outStream.toString();
    } catch (Exception e) {
        // We return Service Unavailable.
        throw new ServiceUnavailableException();
    }
    response.setHeader("Access-Control-Allow-Origin", "*");
    return retValue;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) NotFoundException(javax.ws.rs.NotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) BadRequestException(javax.ws.rs.BadRequestException) NotFoundException(javax.ws.rs.NotFoundException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) WebApplicationException(javax.ws.rs.WebApplicationException) ForbiddenException(javax.ws.rs.ForbiddenException) IOException(java.io.IOException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 75 with DataFile

use of edu.harvard.iq.dataverse.DataFile in project dataverse by IQSS.

the class Access method dsCardImage.

// Note:
// the Dataverse page is no longer using this method.
@Path("dsCardImage/{versionId}")
@GET
@Produces({ "image/png" })
public InputStream dsCardImage(@PathParam("versionId") Long versionId, @Context UriInfo uriInfo, @Context HttpHeaders headers, @Context HttpServletResponse response) /*throws NotFoundException, ServiceUnavailableException, PermissionDeniedException, AuthorizationRequiredException*/
{
    DatasetVersion datasetVersion = versionService.find(versionId);
    if (datasetVersion == null) {
        logger.warning("Preview: Version service could not locate a DatasetVersion object for id " + versionId + "!");
        return null;
    }
    // String imageThumbFileName = null;
    StorageIO thumbnailDataAccess = null;
    if (datasetVersion.getDataset() != null) {
        DataFile logoDataFile = datasetVersion.getDataset().getThumbnailFile();
        if (logoDataFile != null) {
            try {
                StorageIO<DataFile> dataAccess = logoDataFile.getStorageIO();
                if (dataAccess != null) {
                    // && dataAccess.isLocalFile()) {
                    dataAccess.open();
                    thumbnailDataAccess = ImageThumbConverter.getImageThumbnailAsInputStream(dataAccess, 48);
                }
            } catch (IOException ioEx) {
                thumbnailDataAccess = null;
            }
        }
        if (thumbnailDataAccess != null && thumbnailDataAccess.getInputStream() != null) {
            return thumbnailDataAccess.getInputStream();
        }
    }
    return null;
}
Also used : DataFile(edu.harvard.iq.dataverse.DataFile) DatasetVersion(edu.harvard.iq.dataverse.DatasetVersion) StorageIO(edu.harvard.iq.dataverse.dataaccess.StorageIO) IOException(java.io.IOException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

DataFile (edu.harvard.iq.dataverse.DataFile)111 Dataset (edu.harvard.iq.dataverse.Dataset)39 IOException (java.io.IOException)39 FileMetadata (edu.harvard.iq.dataverse.FileMetadata)30 ArrayList (java.util.ArrayList)25 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)20 File (java.io.File)20 FileNotFoundException (java.io.FileNotFoundException)18 Path (javax.ws.rs.Path)18 Dataverse (edu.harvard.iq.dataverse.Dataverse)17 FileInputStream (java.io.FileInputStream)16 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)14 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)13 Date (java.util.Date)13 GET (javax.ws.rs.GET)13 Test (org.junit.Test)13 Timestamp (java.sql.Timestamp)11 InputStream (java.io.InputStream)10 DataVariable (edu.harvard.iq.dataverse.datavariable.DataVariable)8 FileOutputStream (java.io.FileOutputStream)8