use of com.evolvedbinary.j8fu.Try.Try in project exist by eXist-db.
the class DocUtils method getDocumentByPathFromDB.
private static Sequence getDocumentByPathFromDB(final XQueryContext context, final String path) throws XPathException, PermissionDeniedException {
// check if the loaded documents should remain locked
final LockMode lockType = context.lockDocumentsOnLoad() ? LockMode.WRITE_LOCK : LockMode.READ_LOCK;
try {
final XmldbURI baseURI = context.getBaseURI().toXmldbURI();
final XmldbURI pathUri;
if (baseURI != null && !(baseURI.equals("") || baseURI.equals("/db"))) {
// relative collection Path: add the current base URI
pathUri = baseURI.resolveCollectionPath(XmldbURI.xmldbUriFor(path, false));
} else {
pathUri = XmldbURI.xmldbUriFor(path, false);
}
// relative collection Path: add the current module call URI if applicable
final XmldbURI resourceUri = Optional.ofNullable(context.getModuleLoadPath()).filter(moduleLoadPath -> !moduleLoadPath.isEmpty()).flatMap(moduleLoadPath -> Try(() -> XmldbURI.xmldbUriFor(moduleLoadPath)).toOption()).map(moduleLoadPath -> moduleLoadPath.resolveCollectionPath(pathUri)).orElse(pathUri);
// try to open the document and acquire a lock
try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(resourceUri, lockType)) {
if (lockedDoc == null) {
return Sequence.EMPTY_SEQUENCE;
} else {
final DocumentImpl doc = lockedDoc.getDocument();
if (!doc.getPermissions().validate(context.getSubject(), Permission.READ)) {
throw new PermissionDeniedException("Insufficient privileges to read resource " + path);
}
if (doc.getResourceType() == DocumentImpl.BINARY_FILE) {
throw new XPathException("Document " + path + " is a binary resource, not an XML document. Please consider using the function util:binary-doc() to retrieve a reference to it.");
}
return new NodeProxy(doc);
}
}
} catch (final URISyntaxException e) {
throw new XPathException(e);
}
}
Aggregations