Search in sources :

Example 46 with XmldbURI

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

Example 47 with XmldbURI

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());
}
Also used : EXistCollectionManagementService(org.exist.xmldb.EXistCollectionManagementService) IndexQueryService(org.exist.xmldb.IndexQueryService) Collection(org.xmldb.api.base.Collection) ResourceSet(org.xmldb.api.base.ResourceSet) XmldbURI(org.exist.xmldb.XmldbURI)

Example 48 with XmldbURI

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);
    }
}
Also used : Txn(org.exist.storage.txn.Txn) DBBroker(org.exist.storage.DBBroker) StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) Permission(org.exist.security.Permission) BrokerPool(org.exist.storage.BrokerPool) XmldbURI(org.exist.xmldb.XmldbURI)

Example 49 with XmldbURI

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();
    }
}
Also used : Path(java.nio.file.Path) DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool) XmldbURI(org.exist.xmldb.XmldbURI) Test(org.junit.Test)

Example 50 with XmldbURI

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();
    }
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) Txn(org.exist.storage.txn.Txn) DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) BrokerPool(org.exist.storage.BrokerPool) XmldbURI(org.exist.xmldb.XmldbURI) Test(org.junit.Test)

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