Search in sources :

Example 51 with MCRPath

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

the class MCRUpdateMetsOnDerivateChangeEventHandler method checkUpdateMets.

/**
 * Checks if the mets.xml should be updated.
 *
 * @param evt the mcr event
 * @param file the file which was changed
 * @param attrs the file attributes
 * @return true if the mets shoud be updated, otherwise false
 */
protected boolean checkUpdateMets(MCREvent evt, Path file, BasicFileAttributes attrs) {
    // don't update if no MCRPath
    if (!(file instanceof MCRPath)) {
        return false;
    }
    // don't update if mets.xml is deleted
    Path fileName = file.getFileName();
    if (fileName != null && fileName.toString().equals(mets)) {
        return false;
    }
    MCRPath mcrPath = MCRPath.toMCRPath(file);
    String derivateId = mcrPath.getOwner();
    // don't update if mets.xml does not exist
    if (Files.notExists(MCRPath.getPath(derivateId, '/' + mets))) {
        return false;
    }
    // don't update if derivate or mycore object is marked for deletion
    MCRObjectID mcrDerivateId = MCRObjectID.getInstance(derivateId);
    MCRDerivate mcrDerivate = MCRMetadataManager.retrieveMCRDerivate(mcrDerivateId);
    return !MCRMarkManager.instance().isMarkedForDeletion(mcrDerivate);
}
Also used : MCRPath(org.mycore.datamodel.niofs.MCRPath) Path(java.nio.file.Path) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 52 with MCRPath

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

the class MCRMetsCommands method validateSelectedMets.

@MCRCommand(syntax = "validate selected mets", help = "validates all mets.xml of selected derivates", order = 10)
public static void validateSelectedMets() {
    List<String> selectedObjectIDs = MCRObjectCommands.getSelectedObjectIDs();
    for (String objectID : selectedObjectIDs) {
        LOGGER.info("Validate mets.xml of {}", objectID);
        MCRPath metsFile = MCRPath.getPath(objectID, "/mets.xml");
        if (Files.exists(metsFile)) {
            try {
                MCRContent content = new MCRPathContent(metsFile);
                InputStream metsIS = content.getInputStream();
                METSValidator mv = new METSValidator(metsIS);
                List<ValidationException> validationExceptionList = mv.validate();
                if (validationExceptionList.size() > 0) {
                    invalidMetsQueue.add(objectID);
                }
                for (ValidationException validationException : validationExceptionList) {
                    LOGGER.error(validationException.getMessage());
                }
            } catch (IOException e) {
                LOGGER.error("Error while reading mets.xml of {}", objectID, e);
            } catch (JDOMException e) {
                LOGGER.error("Error while parsing mets.xml of {}", objectID, e);
            }
        }
    }
}
Also used : ValidationException(org.mycore.mets.validator.validators.ValidationException) InputStream(java.io.InputStream) MCRPathContent(org.mycore.common.content.MCRPathContent) METSValidator(org.mycore.mets.validator.METSValidator) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) JDOMException(org.jdom2.JDOMException) MCRContent(org.mycore.common.content.MCRContent) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 53 with MCRPath

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

the class MCRMetsCommands method fixInvalidMets.

@MCRCommand(syntax = "try fix invalid mets", help = "This Command can be used to fix invalid mets files that was found in any validate selected mets runs.", order = 15)
public static void fixInvalidMets() {
    String selectedObjectID;
    while ((selectedObjectID = invalidMetsQueue.poll()) != null) {
        LOGGER.info("Try to fix METS of {}", selectedObjectID);
        MCRPath metsFile = MCRPath.getPath(selectedObjectID, "/mets.xml");
        SAXBuilder saxBuilder = new SAXBuilder();
        Document metsDocument;
        try (InputStream metsInputStream = Files.newInputStream(metsFile)) {
            metsDocument = saxBuilder.build(metsInputStream);
        } catch (IOException | JDOMException e) {
            LOGGER.error(MessageFormat.format("Cannot fix METS of {0}. Can not parse mets.xml!", selectedObjectID), e);
            return;
        }
        MCRMetsSimpleModel mcrMetsSimpleModel;
        try {
            mcrMetsSimpleModel = MCRXMLSimpleModelConverter.fromXML(metsDocument);
        } catch (Exception e) {
            LOGGER.error(MessageFormat.format("Cannot fix METS of {0}. Can not convert to SimpleModel!", selectedObjectID), e);
            return;
        }
        Document newMets = MCRSimpleModelXMLConverter.toXML(mcrMetsSimpleModel);
        XMLOutputter xmlOutputter = new XMLOutputter();
        try (OutputStream os = Files.newOutputStream(metsFile)) {
            xmlOutputter.output(newMets, os);
        } catch (IOException e) {
            LOGGER.error(MessageFormat.format("Cannot fix METS of {0}. Can not write mets to derivate.", selectedObjectID));
        }
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) SAXBuilder(org.jdom2.input.SAXBuilder) InputStream(java.io.InputStream) MCRMetsSimpleModel(org.mycore.mets.model.simple.MCRMetsSimpleModel) OutputStream(java.io.OutputStream) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) ValidationException(org.mycore.mets.validator.validators.ValidationException) JDOMException(org.jdom2.JDOMException) IOException(java.io.IOException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 54 with MCRPath

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

the class MCRSwordMediaHandler method replaceMediaResource.

public void replaceMediaResource(String derivateId, String requestFilePath, Deposit deposit) throws SwordError, SwordServerException {
    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!");
    }
    MCRPath path = MCRPath.getPath(derivateId, requestFilePath);
    if (!Files.exists(path)) {
        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, HttpServletResponse.SC_NOT_FOUND, "Cannot replace a not existing file.");
    }
    final boolean pathIsDirectory = Files.isDirectory(path);
    if (pathIsDirectory) {
        throw new SwordError(UriRegistry.ERROR_BAD_REQUEST, HttpServletResponse.SC_METHOD_NOT_ALLOWED, "replaceMediaResource is not supported with directories");
    }
// TODO: replace file
}
Also used : SwordError(org.swordapp.server.SwordError) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 55 with MCRPath

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

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