Search in sources :

Example 66 with LockedDocument

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

the class SourceFactoryTest method getSourceFromDb_noContext.

@Test
public void getSourceFromDb_noContext() throws IOException, PermissionDeniedException {
    final String contextPath = null;
    final String location = "/db/library.xqm";
    final DBBroker mockBroker = createMock(DBBroker.class);
    final LockedDocument mockLockedDoc = createMock(LockedDocument.class);
    final BinaryDocument mockBinDoc = createMock(BinaryDocument.class);
    expect(mockBroker.getXMLResource(anyObject(), anyObject())).andReturn(mockLockedDoc);
    expect(mockLockedDoc.getDocument()).andReturn(mockBinDoc);
    expect(mockBinDoc.getResourceType()).andReturn(BinaryDocument.BINARY_FILE);
    expect(mockBinDoc.getURI()).andReturn(XmldbURI.create(location)).times(2);
    expect(mockBinDoc.getLastModified()).andReturn(123456789l);
    /*expect*/
    mockLockedDoc.close();
    replay(mockBroker, mockLockedDoc, mockBinDoc);
    final Source libSource = SourceFactory.getSource(mockBroker, contextPath, location, false);
    assertTrue(libSource instanceof DBSource);
    assertEquals(XmldbURI.create(location), ((DBSource) libSource).getDocumentPath());
    verify(mockBroker, mockLockedDoc, mockBinDoc);
}
Also used : BinaryDocument(org.exist.dom.persistent.BinaryDocument) DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) Test(org.junit.Test)

Example 67 with LockedDocument

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

the class Scan method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    Source source = null;
    String name;
    if (getArgumentCount() == 2) {
        byte[] data;
        try {
            data = binaryValueToByteArray((BinaryValue) args[0].itemAt(0));
        } catch (IOException ioe) {
            throw new XPathException(ioe.getMessage(), ioe);
        }
        name = args[1].getStringValue();
        source = new BinarySource(data, true);
    } else {
        String uri = args[0].getStringValue();
        if (uri.startsWith(XmldbURI.XMLDB_URI_PREFIX)) {
            try {
                XmldbURI resourceURI = XmldbURI.xmldbUriFor(uri);
                try (final Collection collection = context.getBroker().openCollection(resourceURI.removeLastSegment(), LockMode.READ_LOCK)) {
                    if (collection == null) {
                        LOG.warn("collection not found: {}", resourceURI.getCollectionPath());
                        return Sequence.EMPTY_SEQUENCE;
                    }
                    try (final LockedDocument lockedDoc = collection.getDocumentWithLock(context.getBroker(), resourceURI.lastSegment(), LockMode.READ_LOCK)) {
                        // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
                        collection.close();
                        final DocumentImpl doc = lockedDoc == null ? null : lockedDoc.getDocument();
                        if (doc == null) {
                            return Sequence.EMPTY_SEQUENCE;
                        }
                        if (doc.getResourceType() != DocumentImpl.BINARY_FILE || !doc.getMimeType().equals("application/xquery")) {
                            throw new XPathException(this, "XQuery resource: " + uri + " is not an XQuery or " + "declares a wrong mime-type");
                        }
                        source = new DBSource(context.getBroker(), (BinaryDocument) doc, false);
                        name = doc.getFileURI().toString();
                    }
                } catch (LockException e) {
                    throw new XPathException(this, "internal lock error: " + e.getMessage());
                } catch (PermissionDeniedException pde) {
                    throw new XPathException(this, pde.getMessage(), pde);
                }
            } catch (URISyntaxException e) {
                throw new XPathException(this, "invalid module uri: " + uri + ": " + e.getMessage(), e);
            }
        } else {
            // first check if the URI points to a registered module
            String location = context.getModuleLocation(uri);
            if (location != null)
                uri = location;
            try {
                source = SourceFactory.getSource(context.getBroker(), context.getModuleLoadPath(), uri, false);
                if (source == null) {
                    throw new XPathException(this, "failed to read module " + uri);
                }
                name = extractName(uri);
            } catch (IOException e) {
                throw new XPathException(this, "failed to read module " + uri, e);
            } catch (PermissionDeniedException e) {
                throw new XPathException(this, "permission denied to read module " + uri, e);
            }
        }
    }
    try {
        XQDocHelper helper = new XQDocHelper();
        String xml = helper.scan(source, name);
        NodeValue root = ModuleUtils.stringToXML(context, xml);
        if (root == null)
            return Sequence.EMPTY_SEQUENCE;
        return normalize((NodeValue) ((Document) root).getDocumentElement());
    } catch (XQDocException | SAXException e) {
        throw new XPathException(this, "error while scanning module: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new XPathException(this, "IO error while scanning module: " + e.getMessage(), e);
    }
}
Also used : XQDocException(org.xqdoc.conversion.XQDocException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) LockedDocument(org.exist.dom.persistent.LockedDocument) Document(org.w3c.dom.Document) BinaryDocument(org.exist.dom.persistent.BinaryDocument) DocumentImpl(org.exist.dom.persistent.DocumentImpl) SAXException(org.xml.sax.SAXException) BinaryDocument(org.exist.dom.persistent.BinaryDocument) LockException(org.exist.util.LockException) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) XmldbURI(org.exist.xmldb.XmldbURI) XQDocHelper(org.exist.xqdoc.XQDocHelper)

Example 68 with LockedDocument

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

the class InitCollectionConfigurationTest method deployedInitCollectionConfig.

/**
 * Ensure that etc/collection.xconf.init was deployed at startup
 */
@Test
public void deployedInitCollectionConfig() throws EXistException, PermissionDeniedException, LockException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        try (final Collection collection = broker.openCollection(XmldbURI.CONFIG_COLLECTION_URI.append("db"), Lock.LockMode.READ_LOCK)) {
            final LockedDocument confDoc = collection.getDocumentWithLock(broker, DEFAULT_COLLECTION_CONFIG_FILE_URI, Lock.LockMode.READ_LOCK);
            // asymmetrical - release collection lock
            collection.close();
            assertNotNull(confDoc);
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 69 with LockedDocument

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

the class HistoryTriggerTest method storeAndOverwriteByCopy.

/**
 * Ensure that we can store a document and then overwrite it
 * when the {@link HistoryTrigger} is enabled on the Collection
 *
 * @see <a href="https://github.com/eXist-db/exist/issues/139">History trigger fails #139</a>
 */
@Test
public void storeAndOverwriteByCopy() throws EXistException, PermissionDeniedException, LockException, SAXException, IOException {
    final XmldbURI testDoc1Name = XmldbURI.create("test_store-and-overwrite-by-copy.xml");
    final String testDoc1Content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<hello>12345</hello>";
    final XmldbURI testDoc2Name = XmldbURI.create("other.xml");
    final String testDoc2Content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<other>thing</other>";
    final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
        final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
        // store the first document
        storeInTestCollection(transaction, broker, testDoc1Name, testDoc1Content);
        // store the second document
        storeInTestCollection(transaction, broker, testDoc2Name, testDoc2Content);
        // overwrite the first document by copying the second over it (and make sure we don't get a StackOverflow exception)
        try (final Collection testCollection = broker.openCollection(TEST_COLLECTION_URI, Lock.LockMode.WRITE_LOCK)) {
            assertNotNull(testCollection);
            try (final LockedDocument lockedDoc2 = testCollection.getDocumentWithLock(broker, testDoc2Name, Lock.LockMode.READ_LOCK)) {
                assertNotNull(lockedDoc2);
                // copy doc2 over doc1
                broker.copyResource(transaction, lockedDoc2.getDocument(), testCollection, testDoc1Name);
                // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
                testCollection.close();
            }
        }
        transaction.commit();
    }
    // check that a copy of the original document was made
    checkHistoryOfOriginal(brokerPool, testDoc1Name, testDoc1Content);
}
Also used : DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) XmldbURI(org.exist.xmldb.XmldbURI) BrokerPool(org.exist.storage.BrokerPool)

Example 70 with LockedDocument

use of org.exist.dom.persistent.LockedDocument 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)

Aggregations

LockedDocument (org.exist.dom.persistent.LockedDocument)91 DocumentImpl (org.exist.dom.persistent.DocumentImpl)40 XmldbURI (org.exist.xmldb.XmldbURI)39 DBBroker (org.exist.storage.DBBroker)36 PermissionDeniedException (org.exist.security.PermissionDeniedException)30 Collection (org.exist.collections.Collection)28 Txn (org.exist.storage.txn.Txn)27 BrokerPool (org.exist.storage.BrokerPool)21 EXistException (org.exist.EXistException)20 Test (org.junit.Test)20 IOException (java.io.IOException)18 BinaryDocument (org.exist.dom.persistent.BinaryDocument)18 Serializer (org.exist.storage.serializers.Serializer)15 XPathException (org.exist.xquery.XPathException)14 URISyntaxException (java.net.URISyntaxException)13 InputStream (java.io.InputStream)11 LockException (org.exist.util.LockException)11 Tuple2 (com.evolvedbinary.j8fu.tuple.Tuple2)9 Lock (org.exist.storage.lock.Lock)9 TransactionManager (org.exist.storage.txn.TransactionManager)9