Search in sources :

Example 96 with DocumentImpl

use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.

the class XQueryUpdateTest method store.

private void store(DBBroker broker, String docName, String data) throws PermissionDeniedException, EXistException, SAXException, LockException, IOException {
    Collection root;
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager mgr = pool.getTransactionManager();
    try (final Txn transaction = mgr.beginTransaction()) {
        root = broker.getOrCreateCollection(transaction, TEST_COLLECTION);
        broker.saveCollection(transaction, root);
        broker.storeDocument(transaction, XmldbURI.create(docName), new StringInputSource(data), MimeType.XML_TYPE, root);
        // TODO : unlock the collection here ?
        mgr.commit(transaction);
    }
    final DocumentImpl doc = root.getDocument(broker, XmldbURI.create(docName));
    final Serializer serializer = broker.borrowSerializer();
    try {
        serializer.serialize(doc);
    } finally {
        broker.returnSerializer(serializer);
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) DocumentImpl(org.exist.dom.persistent.DocumentImpl) BrokerPool(org.exist.storage.BrokerPool) Serializer(org.exist.storage.serializers.Serializer)

Example 97 with DocumentImpl

use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.

the class PermissionsFunctionChownTest method assertDocumentSetUidSetGid.

private static void assertDocumentSetUidSetGid(final Subject execAsUser, final XmldbURI uri, final boolean isSet) throws EXistException, PermissionDeniedException {
    final BrokerPool pool = existWebServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(execAsUser));
        final LockedDocument lockedDoc = broker.getXMLResource(uri, Lock.LockMode.READ_LOCK)) {
        final DocumentImpl doc = lockedDoc.getDocument();
        if (isSet) {
            assertTrue(doc.getPermissions().isSetUid());
            assertTrue(doc.getPermissions().isSetGid());
        } else {
            assertFalse(doc.getPermissions().isSetUid());
            assertFalse(doc.getPermissions().isSetGid());
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) DocumentImpl(org.exist.dom.persistent.DocumentImpl) BrokerPool(org.exist.storage.BrokerPool)

Example 98 with DocumentImpl

use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.

the class TestUtils method cleanupDB.

/**
 * Removes all sub-collections of /db
 * except for /db/system.
 *
 * @throws EXistException if an error occurs with the database.
 * @throws PermissionDeniedException if the user does not have appropriate permissions.
 * @throws LockException if a lock cannot be obtained.
 * @throws IOException if an IO error occurs.
 * @throws TriggerException if a trigger throws an error.
 */
public static void cleanupDB() throws EXistException, PermissionDeniedException, LockException, IOException, TriggerException {
    BrokerPool pool = BrokerPool.getInstance();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        // Remove all collections below the /db root, except /db/system
        try (final Collection root = broker.openCollection(XmldbURI.ROOT_COLLECTION_URI, Lock.LockMode.WRITE_LOCK)) {
            if (root == null) {
                transaction.commit();
                return;
            }
            for (final Iterator<DocumentImpl> i = root.iterator(broker); i.hasNext(); ) {
                final DocumentImpl doc = i.next();
                root.removeXMLResource(transaction, broker, doc.getURI().lastSegment());
            }
            for (final Iterator<XmldbURI> i = root.collectionIterator(broker); i.hasNext(); ) {
                final XmldbURI childName = i.next();
                if (childName.equals("system")) {
                    continue;
                }
                try (final Collection childColl = broker.openCollection(XmldbURI.ROOT_COLLECTION_URI.append(childName), Lock.LockMode.WRITE_LOCK)) {
                    broker.removeCollection(transaction, childColl);
                }
            }
        }
        // Remove /db/system/config/db and all collection configurations with it
        try (final Collection dbConfig = broker.openCollection(XmldbURI.CONFIG_COLLECTION_URI.append("/db"), Lock.LockMode.WRITE_LOCK)) {
            if (dbConfig == null) {
                transaction.commit();
                return;
            }
            broker.removeCollection(transaction, dbConfig);
        }
        pool.getTransactionManager().commit(transaction);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) DocumentImpl(org.exist.dom.persistent.DocumentImpl) BrokerPool(org.exist.storage.BrokerPool) XmldbURI(org.exist.xmldb.XmldbURI)

Example 99 with DocumentImpl

use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.

the class AbstractRealm method loadGroupsFromRealmStorage.

private void loadGroupsFromRealmStorage(final DBBroker broker) throws ConfigurationException, PermissionDeniedException, LockException {
    if (collectionGroups != null && collectionGroups.getDocumentCount(broker) > 0) {
        final AbstractRealm r = this;
        for (final Iterator<DocumentImpl> i = collectionGroups.iterator(broker); i.hasNext(); ) {
            final DocumentImpl doc = i.next();
            final Configuration conf = Configurator.parse(broker.getBrokerPool(), doc);
            final String name = conf.getProperty("name");
            groupsByName.writeE(principalDb -> {
                if (name != null && !principalDb.containsKey(name)) {
                    // Group group = instantiateGroup(this, conf);
                    final GroupImpl group = new GroupImpl(r, conf);
                    getSecurityManager().registerGroup(group);
                    principalDb.put(group.getName(), group);
                    // set collection
                    if (group.getId() > 0) {
                        group.setCollection(broker, collectionGroups);
                    }
                }
            });
        }
    }
}
Also used : Configuration(org.exist.config.Configuration) GroupImpl(org.exist.security.internal.GroupImpl) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 100 with DocumentImpl

use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.

the class AbstractRealm method loadAccountsFromRealmStorage.

private void loadAccountsFromRealmStorage(final DBBroker broker) throws ConfigurationException, PermissionDeniedException, LockException {
    // load accounts information
    if (collectionAccounts != null && collectionAccounts.getDocumentCount(broker) > 0) {
        final AbstractRealm r = this;
        for (final Iterator<DocumentImpl> i = collectionAccounts.iterator(broker); i.hasNext(); ) {
            final DocumentImpl doc = i.next();
            final Configuration conf = Configurator.parse(broker.getBrokerPool(), doc);
            final String name = conf.getProperty("name");
            usersByName.writeE(principalDb -> {
                if (name != null && !principalDb.containsKey(name)) {
                    // A account = instantiateAccount(this, conf);
                    final Account account;
                    try {
                        account = new AccountImpl(r, conf);
                        // ensure that the account has at least a primary group
                        if (account.getGroups().length == 0) {
                            try {
                                account.setPrimaryGroup(getGroup(SecurityManager.UNKNOWN_GROUP));
                                LOG.warn("Account '{}' has no groups, but every account must have at least 1 group. Assigned group: " + SecurityManager.UNKNOWN_GROUP, account.getName());
                            } catch (final PermissionDeniedException e) {
                                throw new ConfigurationException("Account has no group, unable to default to " + SecurityManager.UNKNOWN_GROUP + ": " + e.getMessage(), e);
                            }
                        }
                    } catch (Throwable e) {
                        LOG.error("Account object can't be built from '{}'", doc.getFileURI(), e);
                        return;
                    }
                    getSecurityManager().registerAccount(account);
                    principalDb.put(account.getName(), account);
                    // set collection
                    if (account.getId() > 0) {
                        ((AbstractPrincipal) account).setCollection(broker, collectionAccounts);
                        // ensure that the account has at least a primary group
                        if (account.getGroups().length == 0) {
                            try {
                                account.setPrimaryGroup(getGroup(SecurityManager.UNKNOWN_GROUP));
                                LOG.warn("Account '{}' has no groups, but every account must have at least 1 group. Assigned group: " + SecurityManager.UNKNOWN_GROUP, account.getName());
                            } catch (final PermissionDeniedException e) {
                                throw new ConfigurationException("Account has no group, unable to default to " + SecurityManager.UNKNOWN_GROUP + ": " + e.getMessage(), e);
                            }
                        }
                    }
                }
            });
        }
    }
}
Also used : Configuration(org.exist.config.Configuration) ConfigurationException(org.exist.config.ConfigurationException) AccountImpl(org.exist.security.internal.AccountImpl) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Aggregations

DocumentImpl (org.exist.dom.persistent.DocumentImpl)128 PermissionDeniedException (org.exist.security.PermissionDeniedException)51 Collection (org.exist.collections.Collection)40 LockedDocument (org.exist.dom.persistent.LockedDocument)39 Txn (org.exist.storage.txn.Txn)35 XmldbURI (org.exist.xmldb.XmldbURI)34 EXistException (org.exist.EXistException)27 LockException (org.exist.util.LockException)26 DBBroker (org.exist.storage.DBBroker)25 IOException (java.io.IOException)19 NodeProxy (org.exist.dom.persistent.NodeProxy)18 URISyntaxException (java.net.URISyntaxException)17 BrokerPool (org.exist.storage.BrokerPool)17 TransactionManager (org.exist.storage.txn.TransactionManager)17 Test (org.junit.Test)17 XPathException (org.exist.xquery.XPathException)16 NodeId (org.exist.numbering.NodeId)14 SAXException (org.xml.sax.SAXException)13 StoredNode (org.exist.dom.persistent.StoredNode)12 BinaryDocument (org.exist.dom.persistent.BinaryDocument)11