use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRFileNodeServlet method getContent.
/* (non-Javadoc)
* @see org.mycore.frontend.servlets.MCRContentServlet#getContent(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
public MCRContent getContent(HttpServletRequest request, HttpServletResponse response) throws IOException {
if (!isParametersValid(request, response)) {
return null;
}
String ownerID = getOwnerID(request);
if (!MCRAccessManager.checkPermissionForReadingDerivate(ownerID)) {
LOGGER.info("AccessForbidden to {}", request.getPathInfo());
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return null;
}
String path = getPath(request);
MCRPath mcrPath = MCRPath.getPath(ownerID, path);
BasicFileAttributes attr;
try {
attr = Files.readAttributes(mcrPath, BasicFileAttributes.class);
} catch (NoSuchFileException e) {
String msg = e.getMessage();
if (msg == null) {
msg = "File or directory not found: " + mcrPath;
}
response.sendError(HttpServletResponse.SC_NOT_FOUND, msg);
return null;
}
if (attr.isDirectory()) {
try {
return sendDirectory(request, response, mcrPath);
} catch (TransformerException | SAXException e) {
throw new IOException(e);
}
}
if (attr.isRegularFile()) {
return sendFile(request, response, mcrPath);
}
response.sendError(HttpServletResponse.SC_NOT_FOUND, "Not a file or directory: " + mcrPath);
return null;
}
use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRMetadataManager method create.
/**
* Stores the derivate.
*
* @param mcrDerivate
* derivate instance to store
* @throws MCRPersistenceException
* if a persistence problem is occurred
* @throws MCRAccessException
* if write permission to object is missing
*/
public static void create(final MCRDerivate mcrDerivate) throws MCRPersistenceException, MCRAccessException {
if (exists(mcrDerivate.getId())) {
throw new MCRPersistenceException("The derivate " + mcrDerivate.getId() + " already exists, nothing done.");
}
try {
mcrDerivate.validate();
} catch (MCRException exc) {
throw new MCRPersistenceException("The derivate " + mcrDerivate.getId() + " is not valid.", exc);
}
final MCRObjectID objid = mcrDerivate.getDerivate().getMetaLink().getXLinkHrefID();
if (!MCRAccessManager.checkPermission(objid, PERMISSION_WRITE)) {
throw MCRAccessException.missingPermission("Add derivate " + mcrDerivate.getId() + " to object.", objid.toString(), PERMISSION_WRITE);
}
byte[] objectBackup;
try {
objectBackup = MCRXMLMetadataManager.instance().retrieveBLOB(objid);
} catch (IOException ioExc) {
throw new MCRPersistenceException("Unable to retrieve xml blob of " + objid);
}
if (objectBackup == null) {
throw new MCRPersistenceException("Cannot find " + objid + " to attach derivate " + mcrDerivate.getId() + " to it.");
}
// prepare the derivate metadata and store under the XML table
if (mcrDerivate.getService().getDate("createdate") == null || !mcrDerivate.isImportMode()) {
mcrDerivate.getService().setDate("createdate");
}
if (mcrDerivate.getService().getDate("modifydate") == null || !mcrDerivate.isImportMode()) {
mcrDerivate.getService().setDate("modifydate");
}
// handle events
fireEvent(mcrDerivate, null, MCREvent.CREATE_EVENT);
// add the link to metadata
final MCRMetaLinkID der = new MCRMetaLinkID();
der.setReference(mcrDerivate.getId().toString(), null, mcrDerivate.getLabel());
der.setSubTag("derobject");
try {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("adding Derivate in data store");
}
MCRMetadataManager.addOrUpdateDerivateToObject(objid, der);
} catch (final Exception e) {
MCRMetadataManager.restore(mcrDerivate, objid, objectBackup);
// throw final exception
throw new MCRPersistenceException("Error while creatlink to MCRObject " + objid + ".", e);
}
// create data in IFS
if (mcrDerivate.getDerivate().getInternals() != null) {
MCRObjectID derId = mcrDerivate.getId();
MCRPath rootPath = MCRPath.getPath(derId.toString(), "/");
if (mcrDerivate.getDerivate().getInternals().getSourcePath() == null) {
try {
rootPath.getFileSystem().createRoot(rootPath.getOwner());
BasicFileAttributes attrs = Files.readAttributes(rootPath, BasicFileAttributes.class);
if (!(attrs.fileKey() instanceof String)) {
throw new MCRPersistenceException("Cannot get ID from newely created directory, as it is not a String." + rootPath);
}
mcrDerivate.getDerivate().getInternals().setIFSID(attrs.fileKey().toString());
} catch (IOException ioExc) {
throw new MCRPersistenceException("Cannot create root of '" + rootPath.getOwner() + "' or read the file attributes.", ioExc);
}
} else {
final String sourcepath = mcrDerivate.getDerivate().getInternals().getSourcePath();
final File f = new File(sourcepath);
if (f.exists()) {
try {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Starting File-Import");
}
importDerivate(derId.toString(), f.toPath());
BasicFileAttributes attrs = Files.readAttributes(rootPath, BasicFileAttributes.class);
if (!(attrs.fileKey() instanceof String)) {
throw new MCRPersistenceException("Cannot get ID from newely created directory, as it is not a String." + rootPath);
}
mcrDerivate.getDerivate().getInternals().setIFSID(attrs.fileKey().toString());
} catch (final Exception e) {
if (Files.exists(rootPath)) {
deleteDerivate(derId.toString());
}
MCRMetadataManager.restore(mcrDerivate, objid, objectBackup);
throw new MCRPersistenceException("Can't add derivate to the IFS", e);
}
} else {
LOGGER.warn("Empty derivate, the File or Directory -->{}<-- was not found.", sourcepath);
}
}
}
}
use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRFileMetaEventHandler method handlePathDeleted.
@Override
protected void handlePathDeleted(MCREvent evt, Path path, BasicFileAttributes attrs) {
if (attrs.isDirectory()) {
return;
}
MCRPath mcrPath = MCRPath.toMCRPath(path);
MCRObjectID derivateID = MCRObjectID.getInstance(mcrPath.getOwner());
if (!MCRMetadataManager.exists(derivateID)) {
LOGGER.warn("Derivate {} from file '{}' does not exist.", derivateID, path);
return;
}
MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(derivateID);
MCRObjectDerivate objectDerivate = derivate.getDerivate();
if (objectDerivate.deleteFileMetaData('/' + path.subpath(0, path.getNameCount()).toString())) {
try {
MCRMetadataManager.update(derivate);
} catch (MCRPersistenceException | MCRAccessException e) {
throw new MCRPersistenceException("Could not update derivate: " + derivateID, e);
}
}
}
use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRFileMetaEventHandler method handleDerivateUpdated.
@Override
protected void handleDerivateUpdated(MCREvent evt, MCRDerivate der) {
HashSet<MCRCategLinkReference> before = new HashSet<>();
before.addAll(CATEGLINK_SERVICE.getReferences(der.getId().toString()));
handleDerivateDeleted(evt, der);
handleDerivateCreated(evt, der);
HashSet<MCRCategLinkReference> after = new HashSet<>();
after.addAll(CATEGLINK_SERVICE.getReferences(der.getId().toString()));
HashSet<MCRCategLinkReference> combined = new HashSet<>(before);
combined.addAll(after);
for (MCRCategLinkReference ref : combined) {
MCRObjectID derId = der.getId();
String path = ref.getObjectID();
MCRPath file = MCRPath.getPath(derId.toString(), path);
BasicFileAttributes attrs;
try {
attrs = Files.readAttributes(file, BasicFileAttributes.class);
} catch (IOException e) {
LOGGER.warn("File is linked to category but cannot be read:{}{}", der.getId(), ref.getObjectID(), e);
continue;
}
MCREvent fileEvent = new MCREvent(MCREvent.PATH_TYPE, MCREvent.INDEX_EVENT);
fileEvent.put(MCREvent.PATH_KEY, file);
fileEvent.put(MCREvent.FILEATTR_KEY, attrs);
MCREventManager.instance().handleEvent(fileEvent);
}
}
use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRMigrationCommands method fixDerivateLinks.
@MCRCommand(syntax = "fix invalid derivate links {0} for {1}", help = "Fixes the paths of all derivate links " + "({0} -> xpath -> e.g. /mycoreobject/metadata/derivateLinks/derivateLink) for object {1}. (MCR-1267)", order = 15)
public static void fixDerivateLinks(String xpath, String id) throws IOException, JDOMException, SAXException {
// get mcr object
MCRObjectID objectID = MCRObjectID.getInstance(id);
// find derivate links
Document xml = MCRXMLMetadataManager.instance().retrieveXML(objectID);
Element mcrObjectXML = xml.getRootElement();
XPathExpression<Element> expression = XPathFactory.instance().compile(xpath, Filters.element());
List<Element> derivateLinkElements = expression.evaluate(mcrObjectXML);
// check them
boolean changedObject = false;
for (Element derivateLinkElement : derivateLinkElements) {
String href = derivateLinkElement.getAttributeValue("href", MCRConstants.XLINK_NAMESPACE);
MCRMetaDerivateLink link = new MCRMetaDerivateLink();
link.setReference(href, null, null);
String owner = link.getOwner();
try {
String path = link.getPath();
MCRPath mcrPath = MCRPath.getPath(owner, path);
if (!Files.exists(mcrPath)) {
// -> e.g. a?c.tif -> path (a), query (c.tif) which is obvious wrong
if (tryRawPath(objectID, derivateLinkElement, href, link, owner)) {
changedObject = true;
} else {
LOGGER.warn("{} of {}cannot be found on file system. This is most likly a dead link.", href, objectID);
}
}
} catch (URISyntaxException uriExc) {
// not encoded properly
if (tryRawPath(objectID, derivateLinkElement, href, link, owner)) {
changedObject = true;
} else {
LOGGER.warn("{} of {} isn't URI encoded and cannot be found on file system. This is most likly a dead link.", href, objectID);
}
}
}
// store the mcr object if its changed
if (changedObject) {
// we use MCRXMLMetadataMananger because we don't want to validate the old mcr object
MCRXMLMetadataManager.instance().update(objectID, xml, new Date());
// manually fire update event
MCRObject newObject = MCRMetadataManager.retrieveMCRObject(objectID);
newObject.setImportMode(true);
MCRMetadataManager.fireUpdateEvent(newObject);
}
}
Aggregations