Search in sources :

Example 6 with LockedDocument

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

the class XMLDBSetMimeType method getMimeTypeStoredResource.

/**
 * Determine mimetype of currently stored resource. Copied from
 * get-mime-type.
 */
private MimeType getMimeTypeStoredResource(XmldbURI pathUri) throws XPathException {
    MimeType returnValue = null;
    try {
        // relative collection Path: add the current base URI
        pathUri = context.getBaseURI().toXmldbURI().resolveCollectionPath(pathUri);
    } catch (final XPathException ex) {
        logger.debug("Unable to convert path {}", pathUri);
        return returnValue;
    }
    try (final LockedDocument lockedDocument = context.getBroker().getXMLResource(pathUri, LockMode.READ_LOCK)) {
        // try to open the document and acquire a lock
        final DocumentImpl doc = lockedDocument == null ? null : lockedDocument.getDocument();
        if (doc == null) {
            throw new XPathException("Resource '" + pathUri + "' does not exist.");
        } else {
            final String mimetype = doc.getMimeType();
            returnValue = MimeTable.getInstance().getContentType(mimetype);
        }
    } catch (final PermissionDeniedException ex) {
        logger.debug(ex.getMessage());
    }
    return returnValue;
}
Also used : XPathException(org.exist.xquery.XPathException) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) MimeType(org.exist.util.MimeType)

Example 7 with LockedDocument

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

the class SecurityManagerTest method deleteAccount.

@Test
public void deleteAccount() throws EXistException, PermissionDeniedException, XPathException, LockException {
    final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
    final SecurityManager securityManager = brokerPool.getSecurityManager();
    try (final DBBroker broker = brokerPool.get(Optional.of(securityManager.getSystemSubject()))) {
        // 1. pre-check - assert the account exists
        assertTrue(securityManager.hasAccount(TEST_USER_NAME));
        // 2. pre-check - assert the XML document for the account and group exists
        try (final LockedDocument document = broker.getXMLResource(ACCOUNTS_URI.append(TEST_USER_NAME + ".xml"), Lock.LockMode.READ_LOCK)) {
            assertNotNull(document);
        }
        try (final LockedDocument document = broker.getXMLResource(GROUPS_URI.append(TEST_GROUP_NAME + ".xml"), Lock.LockMode.READ_LOCK)) {
            assertNotNull(document);
        }
        // 3. pre-check - check that both the accounts collection and groups collection have sub-collections for removed accounts and groups
        try (final Collection collection = broker.openCollection(ACCOUNTS_URI, Lock.LockMode.READ_LOCK)) {
            assertNotNull(collection);
            assertEquals(1, collection.getChildCollectionCount(broker));
            assertTrue(collection.hasChildCollection(broker, XmldbURI.create(REMOVED_COLLECTION_NAME)));
        }
        try (final Collection collection = broker.openCollection(GROUPS_URI, Lock.LockMode.READ_LOCK)) {
            assertNotNull(collection);
            assertEquals(1, collection.getChildCollectionCount(broker));
            assertTrue(collection.hasChildCollection(broker, XmldbURI.create(REMOVED_COLLECTION_NAME)));
        }
        // 4. pre-check - assert that the removed Collections do exist for accounts and groups, but is empty
        try (final Collection collection = broker.openCollection(REMOVED_ACCOUNTS_URI, Lock.LockMode.READ_LOCK)) {
            assertNotNull(collection);
            assertEquals(0, collection.getChildCollectionCount(broker));
            assertEquals(0, collection.getDocumentCount(broker));
        }
        try (final Collection collection = broker.openCollection(REMOVED_GROUPS_URI, Lock.LockMode.READ_LOCK)) {
            assertNotNull(collection);
            assertEquals(0, collection.getChildCollectionCount(broker));
            assertEquals(0, collection.getDocumentCount(broker));
        }
        // 5. pre-check - assert the XML document for any removed account or group does NOT exist
        assertFalse(removedAccountExists(broker, TEST_USER_NAME));
        assertFalse(removedGroupExists(broker, TEST_GROUP_NAME));
        // 6. DELETE THE ACCOUNT
        securityManager.deleteAccount(TEST_USER_NAME);
        // 7. post-check - assert the account does NOT exist
        assertFalse(securityManager.hasAccount(TEST_USER_NAME));
        // 8. post-check - assert the XML document for the account does NOT exist, but that the group still exists
        try (final LockedDocument document = broker.getXMLResource(ACCOUNTS_URI.append(TEST_USER_NAME + ".xml"), Lock.LockMode.READ_LOCK)) {
            assertNull(document);
        }
        try (final LockedDocument document = broker.getXMLResource(GROUPS_URI.append(TEST_GROUP_NAME + ".xml"), Lock.LockMode.READ_LOCK)) {
            assertNotNull(document);
        }
        // 9. post-check - check that both the accounts collection and groups collection still have sub-collections for removed accounts and groups
        try (final Collection collection = broker.openCollection(ACCOUNTS_URI, Lock.LockMode.READ_LOCK)) {
            assertNotNull(collection);
            assertEquals(1, collection.getChildCollectionCount(broker));
            assertTrue(collection.hasChildCollection(broker, XmldbURI.create(REMOVED_COLLECTION_NAME)));
        }
        try (final Collection collection = broker.openCollection(GROUPS_URI, Lock.LockMode.READ_LOCK)) {
            assertNotNull(collection);
            assertEquals(1, collection.getChildCollectionCount(broker));
            assertTrue(collection.hasChildCollection(broker, XmldbURI.create(REMOVED_COLLECTION_NAME)));
        }
        // 10. post-check - assert that the removed Collections do exist for accounts and groups, but contain only 1 document (i.e. for the removed account)
        try (final Collection collection = broker.openCollection(REMOVED_ACCOUNTS_URI, Lock.LockMode.READ_LOCK)) {
            assertNotNull(collection);
            assertEquals(0, collection.getChildCollectionCount(broker));
            assertEquals(1, collection.getDocumentCount(broker));
        }
        try (final Collection collection = broker.openCollection(REMOVED_GROUPS_URI, Lock.LockMode.READ_LOCK)) {
            assertNotNull(collection);
            assertEquals(0, collection.getChildCollectionCount(broker));
            assertEquals(0, collection.getDocumentCount(broker));
        }
        // 11. post-check - assert the XML document for the removed account does exist, but no such document exists for the group
        assertTrue(removedAccountExists(broker, TEST_USER_NAME));
        assertFalse(removedGroupExists(broker, TEST_GROUP_NAME));
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 8 with LockedDocument

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

the class EmbeddedXMLStreamReaderTest method assertNodesIn.

public void assertNodesIn(final NamedEvent[] expected, final Function<Document, NodeHandle> initialNodeFun, final Optional<Function<Document, NodeHandle>> containerFun) throws EXistException, PermissionDeniedException, IOException, XMLStreamException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        try (final LockedDocument lockedDocument = broker.getXMLResource(TEST_MIXED_XML_COLLECTION.append(MIXED_XML_NAME), Lock.LockMode.WRITE_LOCK)) {
            assertNotNull(lockedDocument);
            final Document document = lockedDocument.getDocument();
            assertNotNull(document);
            final NodeHandle initialNode = initialNodeFun.apply(document);
            final Optional<NodeHandle> maybeContainerNode = containerFun.map(f -> f.apply(document));
            final IEmbeddedXMLStreamReader xmlStreamReader = broker.getXMLStreamReader(initialNode, false);
            final NamedEvent[] actual = readAllEvents(maybeContainerNode, xmlStreamReader);
            assertArrayEquals(formatExpectedActual(expected, actual), expected, actual);
        }
        transaction.commit();
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) NodeHandle(org.exist.dom.persistent.NodeHandle) LockedDocument(org.exist.dom.persistent.LockedDocument) Txn(org.exist.storage.txn.Txn) Document(org.w3c.dom.Document) LockedDocument(org.exist.dom.persistent.LockedDocument) NamedEvent(org.exist.stax.EmbeddedXMLStreamReaderTest.NamedEvent) BrokerPool(org.exist.storage.BrokerPool)

Example 9 with LockedDocument

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

the class AuditTrailSessionListenerTest method sessionDestroyed.

/**
 * Ensures that AuditTrailSessionListener releases any locks
 * on the XQuery document when destroying a session
 */
@Test
public void sessionDestroyed() throws EXistException, PermissionDeniedException {
    final HttpSessionEvent httpSessionEvent = createMock(HttpSessionEvent.class);
    final HttpSession httpSession = createMock(HttpSession.class);
    expect(httpSessionEvent.getSession()).andReturn(httpSession);
    expect(httpSession.getId()).andReturn("mock-session");
    replay(httpSessionEvent, httpSession);
    final AuditTrailSessionListener listener = new AuditTrailSessionListener();
    listener.sessionDestroyed(httpSessionEvent);
    verify(httpSessionEvent, httpSession);
    final XmldbURI docUri = XmldbURI.create(DESTROYED_SCRIPT_PATH);
    try (final DBBroker broker = existEmbeddedServer.getBrokerPool().getBroker();
        final LockedDocument lockedResource = broker.getXMLResource(docUri, Lock.LockMode.NO_LOCK)) {
        // ensure that AuditTrailSessionListener released the lock
        final LockManager lockManager = broker.getBrokerPool().getLockManager();
        assertFalse(lockManager.isDocumentLockedForRead(docUri));
        assertFalse(lockManager.isDocumentLockedForWrite(docUri));
    }
}
Also used : LockManager(org.exist.storage.lock.LockManager) DBBroker(org.exist.storage.DBBroker) HttpSession(javax.servlet.http.HttpSession) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) LockedDocument(org.exist.dom.persistent.LockedDocument) XmldbURI(org.exist.xmldb.XmldbURI) Test(org.junit.Test)

Example 10 with LockedDocument

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

the class AuditTrailSessionListenerTest method sessionCreated.

/**
 * Ensures that AuditTrailSessionListener releases any locks
 * on the XQuery document when creating a session
 */
@Test
public void sessionCreated() throws EXistException, PermissionDeniedException {
    final HttpSessionEvent httpSessionEvent = createMock(HttpSessionEvent.class);
    final HttpSession httpSession = createMock(HttpSession.class);
    expect(httpSessionEvent.getSession()).andReturn(httpSession);
    expect(httpSession.getId()).andReturn("mock-session");
    replay(httpSessionEvent, httpSession);
    final AuditTrailSessionListener listener = new AuditTrailSessionListener();
    listener.sessionCreated(httpSessionEvent);
    verify(httpSessionEvent, httpSession);
    final XmldbURI docUri = XmldbURI.create(CREATE_SCRIPT_PATH);
    try (final DBBroker broker = existEmbeddedServer.getBrokerPool().getBroker();
        final LockedDocument lockedResource = broker.getXMLResource(docUri, Lock.LockMode.NO_LOCK)) {
        // ensure that AuditTrailSessionListener released the lock
        final LockManager lockManager = broker.getBrokerPool().getLockManager();
        assertFalse(lockManager.isDocumentLockedForRead(docUri));
        assertFalse(lockManager.isDocumentLockedForWrite(docUri));
    }
}
Also used : LockManager(org.exist.storage.lock.LockManager) DBBroker(org.exist.storage.DBBroker) HttpSession(javax.servlet.http.HttpSession) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) LockedDocument(org.exist.dom.persistent.LockedDocument) XmldbURI(org.exist.xmldb.XmldbURI) Test(org.junit.Test)

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