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);
}
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);
}
}
}
}
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));
}
}
}
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
}
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;
}
Aggregations