use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class RpcConnection method compile.
/**
* @deprecated Use compileQuery lambda instead!
* @param broker the broker to use
* @param source the xquery to compile
* @param parameters context for the compilation of the query
* @return the compiled query
* @throws XPathException If the query contains errors
* @throws IOException If an error occurs reading of writing to the disk
* @throws PermissionDeniedException If the current user is not allowed to perform the action
*/
@Deprecated
private CompiledXQuery compile(final DBBroker broker, final Source source, final Map<String, Object> parameters) throws XPathException, IOException, PermissionDeniedException {
final XQuery xquery = broker.getBrokerPool().getXQueryService();
final XQueryPool pool = broker.getBrokerPool().getXQueryPool();
CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
XQueryContext context;
if (compiled == null) {
context = new XQueryContext(broker.getBrokerPool());
} else {
context = compiled.getContext();
context.prepareForReuse();
}
final String base = (String) parameters.get(RpcAPI.BASE_URI);
if (base != null) {
context.setBaseURI(new AnyURIValue(base));
}
final String moduleLoadPath = (String) parameters.get(RpcAPI.MODULE_LOAD_PATH);
if (moduleLoadPath != null) {
context.setModuleLoadPath(moduleLoadPath);
}
final Map<String, String> namespaces = (Map<String, String>) parameters.get(RpcAPI.NAMESPACES);
if (namespaces != null && namespaces.size() > 0) {
context.declareNamespaces(namespaces);
}
// declare static variables
final Map<String, Object> variableDecls = (Map<String, Object>) parameters.get(RpcAPI.VARIABLES);
if (variableDecls != null) {
for (final Map.Entry<String, Object> entry : variableDecls.entrySet()) {
if (LOG.isDebugEnabled()) {
LOG.debug("declaring {} = {}", entry.getKey(), entry.getValue());
}
context.declareVariable(entry.getKey(), entry.getValue());
}
}
final Object[] staticDocuments = (Object[]) parameters.get(RpcAPI.STATIC_DOCUMENTS);
if (staticDocuments != null) {
try {
final XmldbURI[] d = new XmldbURI[staticDocuments.length];
for (int i = 0; i < staticDocuments.length; i++) {
XmldbURI next = XmldbURI.xmldbUriFor((String) staticDocuments[i]);
d[i] = next;
}
context.setStaticallyKnownDocuments(d);
} catch (final URISyntaxException e) {
throw new XPathException(e);
}
} else if (context.isBaseURIDeclared()) {
context.setStaticallyKnownDocuments(new XmldbURI[] { context.getBaseURI().toXmldbURI() });
}
if (compiled == null) {
compiled = xquery.compile(context, source);
}
return compiled;
}
use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class RpcConnection method setPermissions.
@Override
public boolean setPermissions(final String resource, final String owner, final String group, final int mode) throws EXistException, PermissionDeniedException, URISyntaxException {
final XmldbURI uri = XmldbURI.xmldbUriFor(resource);
return withDb((broker, transaction) -> {
PermissionFactory.chown(broker, transaction, uri, Optional.ofNullable(owner), Optional.ofNullable(group));
PermissionFactory.chmod(broker, transaction, uri, Optional.of(mode), Optional.empty());
return true;
});
}
use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class RpcConnection method describeCollection.
/**
* The method <code>describeCollection</code>
*
* Returns details of a collection - collections (list of sub-collections) -
* name - created - owner - group - permissions - acl
*
* If you do not have read access on the collection, the list of
* sub-collections will be empty, an exception will not be thrown!
*
* @param collUri a <code>XmldbURI</code> value
* @return a <code>Map</code> value
* @throws EXistException if an internal error occurs
* @throws PermissionDeniedException If the current user is not allowed to perform this action
*/
private Map<String, Object> describeCollection(final XmldbURI collUri) throws EXistException, PermissionDeniedException {
return this.<Map<String, Object>>readCollection(collUri).apply((collection, broker, transaction) -> {
final Map<String, Object> desc = new HashMap<>();
final List<String> collections = new ArrayList<>();
if (collection.getPermissionsNoLock().validate(user, Permission.READ)) {
for (final Iterator<XmldbURI> i = collection.collectionIterator(broker); i.hasNext(); ) {
collections.add(i.next().toString());
}
}
final Permission perms = collection.getPermissionsNoLock();
desc.put("collections", collections);
desc.put("name", collection.getURI().toString());
desc.put("created", Long.toString(collection.getCreated()));
desc.put("owner", perms.getOwner().getName());
desc.put("group", perms.getGroup().getName());
desc.put("permissions", perms.getMode());
if (perms instanceof ACLPermission) {
desc.put("acl", getACEs(perms));
}
return desc;
});
}
use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class RpcConnection method getDocumentListing.
@Override
public List<String> getDocumentListing() throws EXistException, PermissionDeniedException {
return withDb((broker, transaction) -> {
final DocumentSet docs = broker.getAllXMLResources(new DefaultDocumentSet());
final XmldbURI[] names = docs.getNames();
final List<String> list = new ArrayList<>();
for (final XmldbURI name : names) {
list.add(name.toString());
}
return list;
});
}
use of org.exist.xmldb.XmldbURI 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