Search in sources :

Example 66 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRDerivateCommands method transformXMLMatchingPatternWithStylesheet.

@MCRCommand(syntax = "transform xml matching file name pattern {0} in derivate {1} with stylesheet {2}", help = "Finds all files in Derivate {1} which match the pattern {0} (the complete path with regex: or glob:*.xml syntax) and transforms them with stylesheet {2}")
public static void transformXMLMatchingPatternWithStylesheet(String pattern, String derivate, String stylesheet) throws IOException {
    MCRXSLTransformer transformer = new MCRXSLTransformer(stylesheet);
    MCRPath derivateRoot = MCRPath.getPath(derivate, "/");
    PathMatcher matcher = derivateRoot.getFileSystem().getPathMatcher(pattern);
    Files.walkFileTree(derivateRoot, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (matcher.matches(file)) {
                LOGGER.info("The file {} matches the pattern {}", file, pattern);
                MCRContent sourceContent = new MCRPathContent(file);
                MCRContent resultContent = transformer.transform(sourceContent);
                try {
                    Document source = sourceContent.asXML();
                    Document result = resultContent.asXML();
                    LOGGER.info("Transforming complete!");
                    if (!MCRXMLHelper.deepEqual(source, result)) {
                        LOGGER.info("Writing result..");
                        resultContent.sendTo(file, StandardCopyOption.REPLACE_EXISTING);
                    } else {
                        LOGGER.info("Result and Source is the same..");
                    }
                } catch (JDOMException | SAXException e) {
                    throw new IOException("Error while processing file : " + file, e);
                }
            }
            return FileVisitResult.CONTINUE;
        }
    });
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) PathMatcher(java.nio.file.PathMatcher) MCRPathContent(org.mycore.common.content.MCRPathContent) MCRXSLTransformer(org.mycore.common.content.transformer.MCRXSLTransformer) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 67 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRDerivateCommands method setMainFile.

@MCRCommand(syntax = "set main file of {0} to {1}", help = "Sets the main file of the derivate with the id {0} to " + "the file with the path {1}")
public static void setMainFile(final String derivateIDString, final String filePath) {
    if (!MCRObjectID.isValid(derivateIDString)) {
        LOGGER.error("{} is not valid. ", derivateIDString);
        return;
    }
    // check for derivate exist
    final MCRObjectID derivateID = MCRObjectID.getInstance(derivateIDString);
    if (!MCRMetadataManager.exists(derivateID)) {
        LOGGER.error("{} does not exist!", derivateIDString);
        return;
    }
    // remove leading slash
    String cleanPath = filePath;
    if (filePath.startsWith(String.valueOf(MCRAbstractFileSystem.SEPARATOR))) {
        cleanPath = filePath.substring(1, filePath.length());
    }
    // check for file exist
    final MCRPath path = MCRPath.getPath(derivateID.toString(), cleanPath);
    if (!Files.exists(path)) {
        LOGGER.error("File {} does not exist!", cleanPath);
        return;
    }
    final MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(derivateID);
    derivate.getDerivate().getInternals().setMainDoc(cleanPath);
    MCRMetadataManager.updateMCRDerivateXML(derivate);
    LOGGER.info("The main file of {} is now '{}'!", derivateIDString, cleanPath);
}
Also used : MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 68 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRObjectCommands method mergeDerivatesOfObject.

@MCRCommand(syntax = "merge derivates of object {0}", help = "Retrieves the MCRObject with the MCRObjectID {0} and if it has more then one MCRDerivate, then all" + " Files will be copied to the first Derivate and all other will be deleted.", order = 190)
public static void mergeDerivatesOfObject(String id) {
    MCRObjectID objectID = MCRObjectID.getInstance(id);
    if (!MCRMetadataManager.exists(objectID)) {
        LOGGER.error("The object with the id {} does not exist!", id);
        return;
    }
    MCRObject object = MCRMetadataManager.retrieveMCRObject(objectID);
    List<MCRMetaLinkID> derivateLinkIDs = object.getStructure().getDerivates();
    List<MCRObjectID> derivateIDs = derivateLinkIDs.stream().map(MCRMetaLinkID::getXLinkHrefID).collect(Collectors.toList());
    if (derivateIDs.size() <= 1) {
        LOGGER.error("The object with the id {} has no Derivates to merge!", id);
        return;
    }
    String mainID = derivateIDs.get(0).toString();
    MCRPath mainDerivateRootPath = MCRPath.getPath(mainID, "/");
    derivateIDs.stream().skip(1).forEach(derivateID -> {
        LOGGER.info("Merge {} into {}...", derivateID, mainID);
        MCRPath copyRootPath = MCRPath.getPath(derivateID.toString(), "/");
        try {
            MCRTreeCopier treeCopier = new MCRTreeCopier(copyRootPath, mainDerivateRootPath);
            Files.walkFileTree(copyRootPath, treeCopier);
            Files.walkFileTree(copyRootPath, MCRRecursiveDeleter.instance());
            MCRMetadataManager.deleteMCRDerivate(derivateID);
        } catch (IOException | MCRAccessException e) {
            throw new MCRException(e);
        }
    });
}
Also used : MCRTreeCopier(org.mycore.datamodel.niofs.utils.MCRTreeCopier) MCRException(org.mycore.common.MCRException) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRAccessException(org.mycore.access.MCRAccessException) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 69 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.

the class MCRUploadHandlerIFS method receiveFile.

@Override
public synchronized long receiveFile(String path, InputStream in, long length, String checksum) throws IOException, MCRPersistenceException, MCRAccessException {
    LOGGER.debug("incoming receiveFile request: {} {} {} bytes", path, checksum, length);
    this.setProgressText(path);
    List<Path> tempFiles = new LinkedList<>();
    Supplier<Path> tempFileSupplier = () -> {
        try {
            Path tempFile = Files.createTempFile(derivateID + "-" + path.hashCode(), ".upload");
            tempFiles.add(tempFile);
            return tempFile;
        } catch (IOException e) {
            throw new UncheckedIOException("Error while creating temp File!", e);
        }
    };
    try (InputStream fIn = preprocessInputStream(path, in, length, tempFileSupplier)) {
        if (rootDir == null) {
            // MCR-1376: Create derivate only if at least one file was successfully uploaded
            prepareUpload();
        }
        MCRPath file = getFile(path);
        LOGGER.info("Creating file {}.", file);
        Files.copy(fIn, file, StandardCopyOption.REPLACE_EXISTING);
        return tempFiles.isEmpty() ? length : Files.size(tempFiles.stream().reduce((a, b) -> b).get());
    } finally {
        tempFiles.stream().filter(Files::exists).forEach((tempFilePath) -> {
            try {
                Files.delete(tempFilePath);
            } catch (IOException e) {
                LOGGER.error("Could not delete temp file {}", tempFilePath);
            }
        });
        this.filesUploaded++;
        int progress = (int) (((float) this.filesUploaded / (float) getNumFiles()) * 100f);
        this.setProgress(progress);
    }
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRConfiguration(org.mycore.common.config.MCRConfiguration) MCRFileAttributes(org.mycore.datamodel.niofs.MCRFileAttributes) Constructor(java.lang.reflect.Constructor) Supplier(java.util.function.Supplier) MCRConfigurationException(org.mycore.common.config.MCRConfigurationException) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) StandardCopyOption(java.nio.file.StandardCopyOption) MCRAccessManager(org.mycore.access.MCRAccessManager) DirectoryStream(java.nio.file.DirectoryStream) MCRMetaIFS(org.mycore.datamodel.metadata.MCRMetaIFS) LinkedList(java.util.LinkedList) MCRAccessException(org.mycore.access.MCRAccessException) Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) MCRObjectDerivate(org.mycore.datamodel.metadata.MCRObjectDerivate) Files(java.nio.file.Files) MCRPath(org.mycore.datamodel.niofs.MCRPath) Collection(java.util.Collection) MCRPersistenceException(org.mycore.common.MCRPersistenceException) FileSystemException(java.nio.file.FileSystemException) IOException(java.io.IOException) MCRAccessInterface(org.mycore.access.MCRAccessInterface) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) FileVisitResult(java.nio.file.FileVisitResult) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MCRProcessableStatus(org.mycore.common.processing.MCRProcessableStatus) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) InputStream(java.io.InputStream) InputStream(java.io.InputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) LinkedList(java.util.LinkedList)

Example 70 with MCRPath

use of org.mycore.datamodel.niofs.MCRPath 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

MCRPath (org.mycore.datamodel.niofs.MCRPath)96 IOException (java.io.IOException)49 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)26 Path (java.nio.file.Path)25 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)22 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)22 Document (org.jdom2.Document)15 JDOMException (org.jdom2.JDOMException)15 MCRPersistenceException (org.mycore.common.MCRPersistenceException)14 MCRException (org.mycore.common.MCRException)13 MCRDirectory (org.mycore.datamodel.ifs.MCRDirectory)13 MCRAccessException (org.mycore.access.MCRAccessException)12 Files (java.nio.file.Files)11 Collectors (java.util.stream.Collectors)11 LogManager (org.apache.logging.log4j.LogManager)11 Logger (org.apache.logging.log4j.Logger)11 FileVisitResult (java.nio.file.FileVisitResult)10 NoSuchFileException (java.nio.file.NoSuchFileException)10 Date (java.util.Date)10 List (java.util.List)10