Search in sources :

Example 1 with Deposit

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

the class MCRSwordMediaHandler method addResource.

public void addResource(String derivateId, String requestFilePath, Deposit deposit) throws SwordError, SwordServerException {
    MCRPath ifsRootPath = MCRPath.getPath(derivateId, requestFilePath);
    final boolean pathIsDirectory = Files.isDirectory(ifsRootPath);
    final String depositFilename = deposit.getFilename();
    final String packaging = deposit.getPackaging();
    if (!MCRAccessManager.checkPermission(derivateId, MCRAccessManager.PERMISSION_WRITE)) {
        throw new SwordError(UriRegistry.ERROR_METHOD_NOT_ALLOWED, "You dont have the right to write to the derivate!");
    }
    Path tempFile = null;
    try {
        try {
            tempFile = MCRSwordUtil.createTempFileFromStream(deposit.getFilename(), deposit.getInputStream(), deposit.getMd5());
        } catch (IOException e) {
            throw new SwordServerException("Could not store deposit to temp files", e);
        }
        if (packaging != null && packaging.equals(UriRegistry.PACKAGE_SIMPLE_ZIP)) {
            if (pathIsDirectory && deposit.getMimeType().equals(MCRSwordConstants.MIME_TYPE_APPLICATION_ZIP)) {
                ifsRootPath = MCRPath.getPath(derivateId, requestFilePath);
                try {
                    List<MCRSwordUtil.MCRValidationResult> invalidResults = MCRSwordUtil.validateZipFile(this, tempFile).stream().filter(validationResult -> !validationResult.isValid()).collect(Collectors.toList());
                    if (invalidResults.size() > 0) {
                        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, HttpServletResponse.SC_BAD_REQUEST, invalidResults.stream().map(MCRSwordUtil.MCRValidationResult::getMessage).filter(Optional::isPresent).map(Optional::get).collect(Collectors.joining(System.lineSeparator())));
                    }
                    MCRSwordUtil.extractZipToPath(tempFile, ifsRootPath);
                } catch (IOException | NoSuchAlgorithmException | URISyntaxException e) {
                    throw new SwordServerException("Error while extracting ZIP.", e);
                }
            } else {
                throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, HttpServletResponse.SC_BAD_REQUEST, "The Request makes no sense. (mime type must be " + MCRSwordConstants.MIME_TYPE_APPLICATION_ZIP + " and path must be a directory)");
            }
        } else if (packaging != null && packaging.equals(UriRegistry.PACKAGE_BINARY)) {
            try {
                MCRSwordUtil.MCRValidationResult validationResult = validate(tempFile);
                if (!validationResult.isValid()) {
                    throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, HttpServletResponse.SC_BAD_REQUEST, validationResult.getMessage().get());
                }
                ifsRootPath = MCRPath.getPath(derivateId, requestFilePath + depositFilename);
                try (InputStream is = Files.newInputStream(tempFile)) {
                    Files.copy(is, ifsRootPath, StandardCopyOption.REPLACE_EXISTING);
                }
            } catch (IOException e) {
                throw new SwordServerException("Error while adding file " + ifsRootPath, e);
            }
        }
    } finally {
        if (tempFile != null) {
            try {
                LOGGER.info("Delete temp file: {}", tempFile);
                Files.delete(tempFile);
            } catch (IOException e) {
                LOGGER.error("Could not delete temp file: {}", tempFile, e);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) SwordError(org.swordapp.server.SwordError) URISyntaxException(java.net.URISyntaxException) ValidationException(org.mycore.mets.validator.validators.ValidationException) Deposit(org.swordapp.server.Deposit) UriRegistry(org.swordapp.server.UriRegistry) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) METSValidator(org.mycore.mets.validator.METSValidator) StandardCopyOption(java.nio.file.StandardCopyOption) MCRAccessManager(org.mycore.access.MCRAccessManager) MCRSwordUtil(org.mycore.sword.MCRSwordUtil) JDOMException(org.jdom2.JDOMException) Map(java.util.Map) SwordServerException(org.swordapp.server.SwordServerException) MCRAccessException(org.mycore.access.MCRAccessException) Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) Files(java.nio.file.Files) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRSwordConstants(org.mycore.sword.MCRSwordConstants) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) MediaResource(org.swordapp.server.MediaResource) Collectors(java.util.stream.Collectors) FileVisitResult(java.nio.file.FileVisitResult) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MCRSessionMgr(org.mycore.common.MCRSessionMgr) Optional(java.util.Optional) LogManager(org.apache.logging.log4j.LogManager) InputStream(java.io.InputStream) Optional(java.util.Optional) SwordError(org.swordapp.server.SwordError) InputStream(java.io.InputStream) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) URISyntaxException(java.net.URISyntaxException) SwordServerException(org.swordapp.server.SwordServerException) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 FileVisitResult (java.nio.file.FileVisitResult)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 SimpleFileVisitor (java.nio.file.SimpleFileVisitor)1 StandardCopyOption (java.nio.file.StandardCopyOption)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 JDOMException (org.jdom2.JDOMException)1 MCRAccessException (org.mycore.access.MCRAccessException)1 MCRAccessManager (org.mycore.access.MCRAccessManager)1