Search in sources :

Example 21 with DocumentImpl

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

the class HistoryTriggerTest method checkHistoryOfOriginal.

private void checkHistoryOfOriginal(final BrokerPool brokerPool, final XmldbURI originalDocName, final String orginalDocContent) throws EXistException, PermissionDeniedException, LockException {
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
        final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
        try (final Collection historyCollection = broker.openCollection(HistoryTrigger.DEFAULT_ROOT_PATH.append(TEST_COLLECTION_URI).append(originalDocName), Lock.LockMode.READ_LOCK)) {
            assertNotNull(historyCollection);
            final DocumentSet documentSet = historyCollection.getDocuments(broker, new DefaultDocumentSet());
            assertEquals(1, documentSet.getDocumentCount());
            final Iterator<DocumentImpl> it = documentSet.getDocumentIterator();
            assertTrue(it.hasNext());
            final DocumentImpl doc = it.next();
            final Diff diff = DiffBuilder.compare(Input.from(orginalDocContent)).withTest(Input.from(doc)).build();
            assertFalse(diff.toString(), diff.hasDifferences());
            assertFalse(it.hasNext());
        }
        transaction.commit();
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) Diff(org.xmlunit.diff.Diff) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentSet(org.exist.dom.persistent.DocumentSet) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 22 with DocumentImpl

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

the class NativeStructuralIndexWorkerTest method documentIdSet.

private DocumentSet documentIdSet(final List<Integer> documentIds) {
    final DocumentSet mockDocumentSet = createMock(DocumentSet.class);
    final List<DocumentImpl> docs = documentIds.stream().map(id -> {
        final DocumentImpl mockDocument = createMock(DocumentImpl.class);
        expect(mockDocument.getDocId()).andReturn(id).anyTimes();
        return mockDocument;
    }).collect(Collectors.toList());
    expect(mockDocumentSet.getDocumentIterator()).andReturn(docs.iterator());
    replay(mockDocumentSet);
    docs.forEach(EasyMock::replay);
    return mockDocumentSet;
}
Also used : Arrays(java.util.Arrays) List(java.util.List) RunWith(org.junit.runner.RunWith) DocumentImpl(org.exist.dom.persistent.DocumentImpl) Test(org.junit.Test) EasyMock(org.easymock.EasyMock) DocumentSet(org.exist.dom.persistent.DocumentSet) ParallelRunner(com.googlecode.junittoolbox.ParallelRunner) Collectors(java.util.stream.Collectors) Assert.assertEquals(org.junit.Assert.assertEquals) EasyMock(org.easymock.EasyMock) DocumentSet(org.exist.dom.persistent.DocumentSet) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 23 with DocumentImpl

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

the class IntersectTest method memtree_intersect_persistent.

/**
 * Tests the XQuery `intersect` operator against an
 * in-memory node on the left and a persistent node on the right
 */
@Test
public void memtree_intersect_persistent() throws XPathException, NoSuchMethodException {
    final XQueryContext mockContext = createMock(XQueryContext.class);
    final PathExpr mockLeft = createMock(PathExpr.class);
    final PathExpr mockRight = createMock(PathExpr.class);
    final Sequence mockContextSequence = createMock(Sequence.class);
    final Item mockContextItem = createMock(Item.class);
    final Profiler mockProfiler = createMock(Profiler.class);
    final DocumentImpl mockPersistentDoc = createMock(DocumentImpl.class);
    final NodeProxy mockPersistentNode = createMockBuilder(NodeProxy.class).withConstructor(DocumentImpl.class, NodeId.class).withArgs(mockPersistentDoc, new DLN(1)).addMockedMethods(NodeProxy.class.getMethod("isEmpty", new Class[] {}), NodeProxy.class.getMethod("getItemType", new Class[] {}), NodeProxy.class.getMethod("equals", new Class[] { Object.class })).createMock();
    expect(mockContext.nextExpressionId()).andReturn(1);
    expect(mockContext.getProfiler()).andReturn(mockProfiler);
    // memtree node
    expect(mockLeft.eval(mockContextSequence, mockContextItem)).andReturn((org.exist.dom.memtree.ElementImpl) createInMemoryDocument().getDocumentElement());
    // persistent node
    expect(mockRight.eval(mockContextSequence, mockContextItem)).andReturn(mockPersistentNode);
    expect(mockPersistentNode.isEmpty()).andReturn(false);
    expect(mockPersistentNode.getItemType()).andReturn(Type.NODE);
    expect(mockContext.getProfiler()).andReturn(mockProfiler);
    replay(mockPersistentDoc, mockPersistentNode, mockRight, mockLeft, mockContext);
    // test
    final Intersect intersect = new Intersect(mockContext, mockLeft, mockRight);
    final Sequence result = intersect.eval(mockContextSequence, mockContextItem);
    assertEquals(0, ((ValueSequence) result).size());
    verify(mockPersistentDoc, mockPersistentNode, mockRight, mockLeft, mockContext);
}
Also used : Item(org.exist.xquery.value.Item) DLN(org.exist.numbering.DLN) NodeId(org.exist.numbering.NodeId) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy) Test(org.junit.Test)

Example 24 with DocumentImpl

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

the class Index method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    try {
        // Retrieve Lucene
        LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
        if (isCalledAs("index")) {
            // Get first parameter, this is the document
            String path = args[0].itemAt(0).getStringValue();
            // Retrieve document from database
            try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(XmldbURI.xmldbUriFor(path), LockMode.READ_LOCK)) {
                // Verify the document actually exists
                final DocumentImpl doc = lockedDoc == null ? null : lockedDoc.getDocument();
                if (doc == null) {
                    throw new XPathException(this, "Document " + path + " does not exist.");
                }
                boolean flush = args.length == 2 || args[2].effectiveBooleanValue();
                // Note: code order is important here,
                index.setDocument(doc, ReindexMode.STORE);
                index.setMode(ReindexMode.STORE);
                // Get 'solr' node from second parameter
                NodeValue descriptor = (NodeValue) args[1].itemAt(0);
                // Pas document and index instructions to indexer
                index.indexNonXML(descriptor);
                if (flush) {
                    // Make sure things are written
                    index.writeNonXML();
                }
            }
        } else {
            // "close"
            index.writeNonXML();
        }
    } catch (Exception ex) {
        // PermissionDeniedException
        logger.error(ex.getMessage(), ex);
        throw new XPathException(this, ex);
    }
    // Return nothing [status would be nice]
    return Sequence.EMPTY_SEQUENCE;
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) LockedDocument(org.exist.dom.persistent.LockedDocument) DocumentImpl(org.exist.dom.persistent.DocumentImpl) LuceneIndexWorker(org.exist.indexing.lucene.LuceneIndexWorker) XPathException(org.exist.xquery.XPathException)

Example 25 with DocumentImpl

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

the class ExistDocument method unlock.

/**
 * Unlock document in database.
 */
void unlock() throws PermissionDeniedException, DocumentNotLockedException, EXistException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("unlock {}", xmldbUri);
    }
    final TransactionManager txnManager = brokerPool.getTransactionManager();
    // Try to get document
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
        final Txn txn = txnManager.beginTransaction();
        final LockedDocument lockedDocument = broker.getXMLResource(xmldbUri, LockMode.WRITE_LOCK)) {
        final DocumentImpl document = lockedDocument.getDocument();
        if (document == null) {
            final String msg = String.format("No resource found for path: %s", xmldbUri);
            LOG.debug(msg);
            throw new EXistException(msg);
        }
        // Get current userlock
        Account lock = document.getUserLock();
        // Check if Resource is already locked.
        if (lock == null) {
            LOG.debug("Resource {} is not locked.", xmldbUri);
            throw new DocumentNotLockedException("" + xmldbUri);
        }
        // Check if Resource is from subject
        if (!lock.getName().equals(subject.getName()) && !subject.hasDbaRole()) {
            LOG.debug("Resource lock is from user {}", lock.getName());
            throw new PermissionDeniedException(lock.getName());
        }
        // Update document
        document.setUserLock(null);
        document.setLockToken(null);
        // Make it persistant
        broker.storeMetadata(txn, document);
        txnManager.commit(txn);
    } catch (EXistException | PermissionDeniedException e) {
        LOG.error(e);
        throw e;
    } catch (TriggerException e) {
        LOG.error(e);
        throw new EXistException(e);
    } finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finished create lock");
        }
    }
}
Also used : Account(org.exist.security.Account) DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) DocumentNotLockedException(org.exist.webdav.exceptions.DocumentNotLockedException) TriggerException(org.exist.collections.triggers.TriggerException) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Aggregations

DocumentImpl (org.exist.dom.persistent.DocumentImpl)128 PermissionDeniedException (org.exist.security.PermissionDeniedException)51 Collection (org.exist.collections.Collection)40 LockedDocument (org.exist.dom.persistent.LockedDocument)39 Txn (org.exist.storage.txn.Txn)35 XmldbURI (org.exist.xmldb.XmldbURI)34 EXistException (org.exist.EXistException)27 LockException (org.exist.util.LockException)26 DBBroker (org.exist.storage.DBBroker)25 IOException (java.io.IOException)19 NodeProxy (org.exist.dom.persistent.NodeProxy)18 URISyntaxException (java.net.URISyntaxException)17 BrokerPool (org.exist.storage.BrokerPool)17 TransactionManager (org.exist.storage.txn.TransactionManager)17 Test (org.junit.Test)17 XPathException (org.exist.xquery.XPathException)16 NodeId (org.exist.numbering.NodeId)14 SAXException (org.xml.sax.SAXException)13 StoredNode (org.exist.dom.persistent.StoredNode)12 BinaryDocument (org.exist.dom.persistent.BinaryDocument)11