use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class TestTrigger method configure.
public void configure(DBBroker broker, Txn transaction, org.exist.collections.Collection parent, Map<String, List<?>> parameters) throws TriggerException {
super.configure(broker, transaction, parent, parameters);
XmldbURI docPath = XmldbURI.create("messages.xml");
final boolean triggersEnabled = broker.isTriggersEnabled();
try {
this.doc = parent.getDocument(broker, docPath);
if (this.doc == null) {
LOG.debug("creating new file for collection contents");
// IMPORTANT: temporarily disable triggers on the collection.
// We would end up in infinite recursion if we don't do that
broker.setTriggersEnabled(false);
broker.storeDocument(transaction, docPath, new StringInputSource(TEMPLATE), MimeType.XML_TYPE, parent);
this.doc = parent.getDocument(broker, docPath);
}
} catch (Exception e) {
throw new TriggerException(e.getMessage(), e);
} finally {
// restore triggers enabled setting
broker.setTriggersEnabled(triggersEnabled);
}
}
use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class XQueryTriggerTest method collectionMove.
/**
* test a trigger fired by a Collection manipulations
*/
@Test
public void collectionMove() throws XMLDBException, URISyntaxException {
final IndexQueryService idxConf = (IndexQueryService) testCollection.getService("IndexQueryService", "1.0");
idxConf.configureCollection(COLLECTION_CONFIG);
final XmldbURI srcURI = XmldbURI.xmldbUriFor("/db/testXQueryTrigger/test");
final XmldbURI dstURI = XmldbURI.xmldbUriFor("/db/testXQueryTrigger/test-dst");
final EXistCollectionManagementService service = (EXistCollectionManagementService) testCollection.getService("CollectionManagementService", "1.0");
final Collection src = service.createCollection("test");
assertNotNull(src);
final Collection dst = service.createCollection("test-dst");
assertNotNull(dst);
service.move(srcURI, dstURI, null);
// remove the trigger for the Collection under test
idxConf.configureCollection(EMPTY_COLLECTION_CONFIG);
ResourceSet result = existEmbeddedServer.executeQuery(BEFORE + CREATE + COLLECTION + testCollectionURI);
assertEquals(1, result.getSize());
result = existEmbeddedServer.executeQuery(AFTER + CREATE + COLLECTION + testCollectionURI);
assertEquals(1, result.getSize());
result = existEmbeddedServer.executeQuery(BEFORE + CREATE + COLLECTION + testDstCollectionURI);
assertEquals(1, result.getSize());
result = existEmbeddedServer.executeQuery(AFTER + CREATE + COLLECTION + testDstCollectionURI);
assertEquals(1, result.getSize());
result = existEmbeddedServer.executeQuery(BEFORE + MOVE + COLLECTION + testCollectionURI);
assertEquals(1, result.getSize());
result = existEmbeddedServer.executeQuery(AFTER + MOVE + COLLECTION + testDstTestCollectionURI);
assertEquals(1, result.getSize());
result = existEmbeddedServer.executeQuery(EVENTS);
assertEquals(6, result.getSize());
}
use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class CollectionRemovalTest method initDB.
@Before
public void initDB() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final TransactionManager transact = pool.getTransactionManager();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = transact.beginTransaction()) {
final int worldReadable = 0744;
final int worldForbidden = 0700;
/*
* Creates 3 collections: /db/test, /db/test/test2, /db/test/test2/test3 and /db/test/test2/test4,
* and stores one document into each.
* Collection /db/test/test2/test3 is only readable by the owner (i.e. admin user).
*/
final List<Tuple2<XmldbURI, Integer>> collectionUriAndModes = Arrays.asList(Tuple(TestConstants.TEST_COLLECTION_URI2, worldReadable), Tuple(TestConstants.TEST_COLLECTION_URI3, worldForbidden), Tuple(TestConstants.TEST_COLLECTION_URI2.append("test4"), worldReadable));
// creat collections
for (final Tuple2<XmldbURI, Integer> collectionUriAndMode : collectionUriAndModes) {
final XmldbURI collectionUri = collectionUriAndMode._1;
final int mode = collectionUriAndMode._2;
// create collection
final Collection collection = broker.getOrCreateCollection(transaction, collectionUri);
assertNotNull(collection);
final Permission perms = collection.getPermissions();
perms.setMode(mode);
broker.saveCollection(transaction, collection);
// store document
broker.storeDocument(transaction, XmldbURI.create("document.xml"), new StringInputSource(DATA), MimeType.XML_TYPE, collection);
}
transact.commit(transaction);
}
}
use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class XMLReaderSecurityTest method cannotExpandExternalEntitiesWhenDisabled.
@Test
public void cannotExpandExternalEntitiesWhenDisabled() throws EXistException, IOException, PermissionDeniedException, LockException, SAXException, TransformerException {
final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
// create a temporary file on disk that contains secret info
final Tuple2<String, Path> secret = createTempSecretFile();
final XmldbURI docName = XmldbURI.create("expand-secret.xml");
// attempt to store a document with an external entity which would be expanded to the content of the secret file
try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
try (final Collection testCollection = broker.openCollection(TEST_COLLECTION, Lock.LockMode.WRITE_LOCK)) {
// debugReader("cannotExpandExternalEntitiesWhenDisabled", broker, testCollection);
final String docContent = EXPANSION_DOC.replace(EXTERNAL_FILE_PLACEHOLDER, secret._2.toUri().toString());
broker.storeDocument(transaction, docName, new StringInputSource(docContent), MimeType.XML_TYPE, testCollection);
}
transaction.commit();
}
// read back the document, to confirm that it does not contain the secret
try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
try (final Collection testCollection = broker.openCollection(TEST_COLLECTION, Lock.LockMode.READ_LOCK)) {
try (final LockedDocument testDoc = testCollection.getDocumentWithLock(broker, docName, Lock.LockMode.READ_LOCK)) {
// release the collection lock early inline with asymmetrical locking
testCollection.close();
assertNotNull(testDoc);
final String expected = EXPECTED_EXPANSION_DISABLED_DOC;
final String actual = serialize(testDoc.getDocument());
assertEquals(expected, actual);
}
}
transaction.commit();
}
}
use of org.exist.xmldb.XmldbURI in project exist by eXist-db.
the class XMLReaderExpansionTest method expandExternalEntities.
@Test
public void expandExternalEntities() throws EXistException, IOException, PermissionDeniedException, LockException, SAXException, TransformerException {
final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
final Map<String, Boolean> parserConfig = new HashMap<>();
parserConfig.put(FEATURE_EXTERNAL_GENERAL_ENTITIES, true);
brokerPool.getConfiguration().setProperty(XMLReaderPool.XmlParser.XML_PARSER_FEATURES_PROPERTY, parserConfig);
// create a temporary file on disk that contains secret info
final Tuple2<String, Path> secret = createTempSecretFile();
final XmldbURI docName = XmldbURI.create("expand-secret.xml");
// attempt to store a document with an external entity which would be expanded to the content of the secret file
try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
try (final Collection testCollection = broker.openCollection(TEST_COLLECTION, Lock.LockMode.WRITE_LOCK)) {
// debugReader("expandExternalEntities", broker, testCollection);
final String docContent = EXPANSION_DOC.replace(EXTERNAL_FILE_PLACEHOLDER, secret._2.toUri().toString());
broker.storeDocument(transaction, docName, new StringInputSource(docContent), MimeType.XML_TYPE, testCollection);
}
transaction.commit();
}
// read back the document, to confirm that it does contain the secret
try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
try (final Collection testCollection = broker.openCollection(TEST_COLLECTION, Lock.LockMode.READ_LOCK)) {
try (final LockedDocument testDoc = testCollection.getDocumentWithLock(broker, docName, Lock.LockMode.READ_LOCK)) {
// release the collection lock early inline with asymmetrical locking
testCollection.close();
assertNotNull(testDoc);
final String expected = EXPECTED_EXPANDED_DOC.replace(EXTERNAL_FILE_PLACEHOLDER, secret._1);
final String actual = serialize(testDoc.getDocument());
assertEquals(expected, actual);
}
}
transaction.commit();
}
}
Aggregations