Search in sources :

Example 61 with XmldbURI

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;
}
Also used : URISyntaxException(java.net.URISyntaxException) LockedDocumentMap(org.exist.storage.lock.LockedDocumentMap) XmldbURI(org.exist.xmldb.XmldbURI)

Example 62 with XmldbURI

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;
    });
}
Also used : XmldbURI(org.exist.xmldb.XmldbURI)

Example 63 with XmldbURI

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;
    });
}
Also used : ACLPermission(org.exist.security.ACLPermission) ACLPermission(org.exist.security.ACLPermission) Permission(org.exist.security.Permission) XmldbURI(org.exist.xmldb.XmldbURI)

Example 64 with XmldbURI

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;
    });
}
Also used : XmldbURI(org.exist.xmldb.XmldbURI)

Example 65 with XmldbURI

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);
    }
}
Also used : LockMode(org.exist.storage.lock.Lock.LockMode) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) BrokerPool(org.exist.storage.BrokerPool) XMLReaderPool(org.exist.util.XMLReaderPool) ErrorCodes(org.exist.xquery.ErrorCodes) NodeProxy(org.exist.dom.persistent.NodeProxy) PermissionDeniedException(org.exist.security.PermissionDeniedException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) XMLReader(org.xml.sax.XMLReader) Source(org.exist.source.Source) java.net(java.net) Namespaces(org.exist.Namespaces) Document(org.w3c.dom.Document) XmldbURI(org.exist.xmldb.XmldbURI) DocumentImpl(org.exist.dom.persistent.DocumentImpl) Permission(org.exist.security.Permission) XQueryContext(org.exist.xquery.XQueryContext) Nullable(javax.annotation.Nullable) InputSource(org.xml.sax.InputSource) LockedDocument(org.exist.dom.persistent.LockedDocument) AnyURIValue(org.exist.xquery.value.AnyURIValue) IOException(java.io.IOException) Try(com.evolvedbinary.j8fu.Try.Try) URLSource(org.exist.source.URLSource) SAXException(org.xml.sax.SAXException) SourceFactory(org.exist.source.SourceFactory) Optional(java.util.Optional) Sequence(org.exist.xquery.value.Sequence) Pattern(java.util.regex.Pattern) SAXAdapter(org.exist.dom.memtree.SAXAdapter) XPathException(org.exist.xquery.XPathException) InputStream(java.io.InputStream) XPathException(org.exist.xquery.XPathException) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockMode(org.exist.storage.lock.Lock.LockMode) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

XmldbURI (org.exist.xmldb.XmldbURI)260 Collection (org.exist.collections.Collection)100 PermissionDeniedException (org.exist.security.PermissionDeniedException)69 Test (org.junit.Test)56 Txn (org.exist.storage.txn.Txn)55 EXistException (org.exist.EXistException)42 URISyntaxException (java.net.URISyntaxException)39 LockedDocument (org.exist.dom.persistent.LockedDocument)39 IOException (java.io.IOException)38 DBBroker (org.exist.storage.DBBroker)38 DocumentImpl (org.exist.dom.persistent.DocumentImpl)34 SAXException (org.xml.sax.SAXException)33 Permission (org.exist.security.Permission)30 LockException (org.exist.util.LockException)27 Path (java.nio.file.Path)22 XPathException (org.exist.xquery.XPathException)22 BrokerPool (org.exist.storage.BrokerPool)21 TransactionManager (org.exist.storage.txn.TransactionManager)20 Subject (org.exist.security.Subject)19 StringInputSource (org.exist.util.StringInputSource)17