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);
}
}
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());
}
}
}
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);
}
}
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);
}
}
});
}
}
}
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);
}
}
}
}
});
}
}
}
Aggregations