Search in sources :

Example 1 with MediaResource

use of org.swordapp.server.MediaResource in project dataverse by IQSS.

the class MediaResourceManagerImpl method getMediaResourceRepresentation.

@Override
public MediaResource getMediaResourceRepresentation(String uri, Map<String, String> map, AuthCredentials authCredentials, SwordConfiguration swordConfiguration) throws SwordError, SwordServerException, SwordAuthException {
    AuthenticatedUser user = swordAuth.auth(authCredentials);
    DataverseRequest dvReq = new DataverseRequest(user, httpRequest);
    urlManager.processUrl(uri);
    String globalId = urlManager.getTargetIdentifier();
    if (urlManager.getTargetType().equals("study") && globalId != null) {
        logger.fine("looking up dataset with globalId " + globalId);
        Dataset dataset = datasetService.findByGlobalId(globalId);
        if (dataset != null) {
            /**
             * @todo: support downloading of files (SWORD 2.0 Profile 6.4. -
             * Retrieving the content)
             * http://swordapp.github.io/SWORDv2-Profile/SWORDProfile.html#protocoloperations_retrievingcontent
             *
             * This ticket is mostly about terms of use:
             * https://github.com/IQSS/dataverse/issues/183
             */
            boolean getMediaResourceRepresentationSupported = false;
            if (getMediaResourceRepresentationSupported) {
                Dataverse dvThatOwnsDataset = dataset.getOwner();
                /**
                 * @todo Add Dataverse 4 style permission check here. Is
                 * there a Command we use for downloading files as zip?
                 */
                boolean authorized = false;
                if (authorized) {
                    /**
                     * @todo Zip file download is being implemented in
                     * https://github.com/IQSS/dataverse/issues/338
                     */
                    InputStream fixmeInputStream = new ByteArrayInputStream("FIXME: replace with zip of all dataset files".getBytes());
                    String contentType = "application/zip";
                    String packaging = UriRegistry.PACKAGE_SIMPLE_ZIP;
                    boolean isPackaged = true;
                    MediaResource mediaResource = new MediaResource(fixmeInputStream, contentType, packaging, isPackaged);
                    return mediaResource;
                } else {
                    throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "user " + user.getDisplayInfo().getTitle() + " is not authorized to get a media resource representation of the dataset with global ID " + dataset.getGlobalId());
                }
            } else {
                throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Downloading files via the SWORD-based Dataverse Data Deposit API is not (yet) supported: https://github.com/IQSS/dataverse/issues/183");
            }
        } else {
            throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Couldn't find dataset with global ID of " + globalId);
        }
    } else {
        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, "Couldn't dermine target type or identifier from URL: " + uri);
    }
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) SwordError(org.swordapp.server.SwordError) Dataset(edu.harvard.iq.dataverse.Dataset) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MediaResource(org.swordapp.server.MediaResource) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Dataverse(edu.harvard.iq.dataverse.Dataverse)

Example 2 with MediaResource

use of org.swordapp.server.MediaResource in project mycore by MyCoRe-Org.

the class MCRSwordMediaHandler method getMediaResourceRepresentation.

public MediaResource getMediaResourceRepresentation(String derivateID, String requestFilePath, Map<String, String> accept) throws SwordError, SwordServerException {
    MediaResource resultRessource;
    if (!MCRAccessManager.checkPermission(derivateID, MCRAccessManager.PERMISSION_READ)) {
        throw new SwordError(UriRegistry.ERROR_METHOD_NOT_ALLOWED, "You dont have the right to read from the derivate!");
    }
    if (requestFilePath != null && isValidFilePath(requestFilePath)) {
        final MCRPath path = MCRPath.getPath(derivateID, requestFilePath);
        checkFile(path);
        InputStream is = null;
        try {
            // MediaResource/Sword2 api should close the stream.
            is = Files.newInputStream(path);
            resultRessource = new MediaResource(is, Files.probeContentType(path), UriRegistry.PACKAGE_BINARY);
        } catch (IOException e) {
            LOGGER.error("Error while opening File: {}", path, e);
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e1) {
                    LOGGER.error("Could not close Stream after error. ", e);
                }
            }
            throw new SwordError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    } else {
        // if there is no file path or file is just "/" or "" then send the zipped Derivate
        resultRessource = MCRSwordUtil.getZippedDerivateMediaResource(derivateID);
    }
    MCRSessionMgr.getCurrentSession().commitTransaction();
    return resultRessource;
}
Also used : SwordError(org.swordapp.server.SwordError) InputStream(java.io.InputStream) MediaResource(org.swordapp.server.MediaResource) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 3 with MediaResource

use of org.swordapp.server.MediaResource in project mycore by MyCoRe-Org.

the class MCRSwordUtil method getZippedDerivateMediaResource.

public static MediaResource getZippedDerivateMediaResource(String object) {
    final Path tempFile;
    try {
        tempFile = Files.createTempFile("swordv2_", ".temp.zip");
    } catch (IOException e) {
        throw new MCRException("Could not create temp file!", e);
    }
    try (final OutputStream tempFileStream = Files.newOutputStream(tempFile)) {
        final ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(tempFileStream);
        zipOutputStream.setLevel(Deflater.BEST_COMPRESSION);
        final MCRPath root = MCRPath.getPath(object, "/");
        addDirectoryToZip(zipOutputStream, root);
        zipOutputStream.close();
    } catch (IOException e) {
        throw new MCRException(e);
    }
    MediaResource resultRessource;
    InputStream is;
    try {
        is = new MCRDeleteFileOnCloseFilterInputStream(Files.newInputStream(tempFile), tempFile);
        resultRessource = new MediaResource(is, MCRSwordConstants.MIME_TYPE_APPLICATION_ZIP, UriRegistry.PACKAGE_SIMPLE_ZIP);
    } catch (IOException e) {
        throw new MCRException("could not read from temp file!", e);
    }
    return resultRessource;
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRException(org.mycore.common.MCRException) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) OutputStream(java.io.OutputStream) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) MediaResource(org.swordapp.server.MediaResource) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Aggregations

InputStream (java.io.InputStream)3 MediaResource (org.swordapp.server.MediaResource)3 IOException (java.io.IOException)2 MCRPath (org.mycore.datamodel.niofs.MCRPath)2 SwordError (org.swordapp.server.SwordError)2 Dataset (edu.harvard.iq.dataverse.Dataset)1 Dataverse (edu.harvard.iq.dataverse.Dataverse)1 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)1 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 OutputStream (java.io.OutputStream)1 Path (java.nio.file.Path)1 DigestInputStream (java.security.DigestInputStream)1 ZipArchiveOutputStream (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)1 MCRException (org.mycore.common.MCRException)1