Search in sources :

Example 16 with DocumentImpl

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

the class SystemExportFiltersTest method exportImport.

@Test
public void exportImport() throws Exception {
    Path file;
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        List<String> filters = new ArrayList<>();
        filters.add(FilterForBackup.class.getName());
        broker.getConfiguration().setProperty(SystemExport.CONFIG_FILTERS, filters);
        final Collection test = broker.getCollection(TEST_COLLECTION_URI);
        assertNotNull(test);
        boolean direct = true;
        final SystemExport sysexport = new SystemExport(broker, transaction, null, null, direct);
        final Path backupDir = tempFolder.newFolder().toPath();
        file = sysexport.export(backupDir.toAbsolutePath().toString(), false, false, null);
        transaction.commit();
    }
    TestUtils.cleanupDB();
    final SystemImport restore = new SystemImport(pool);
    final RestoreListener listener = new LogRestoreListener();
    restore.restore(TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD, null, file, listener);
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        final Collection test = broker.getCollection(TEST_COLLECTION_URI);
        assertNotNull(test);
        DocumentImpl doc = getDoc(broker, test, doc01uri.lastSegment());
        assertEquals(XML1_BACKUP, serializer(broker, doc));
        doc = getDoc(broker, test, doc02uri.lastSegment());
        assertEquals(XML2_PROPER, serializer(broker, doc));
        doc = getDoc(broker, test, doc03uri.lastSegment());
        assertEquals(XML3_PROPER, serializer(broker, doc));
        doc = getDoc(broker, test, doc11uri.lastSegment());
        assertTrue(doc instanceof BinaryDocument);
        try (final InputStream is = broker.getBinaryResource(transaction, ((BinaryDocument) doc))) {
            assertEquals(BINARY, InputStreamUtil.readString(is, UTF_8));
        }
        transaction.commit();
    }
}
Also used : Path(java.nio.file.Path) LogRestoreListener(org.exist.backup.restore.listener.LogRestoreListener) InputStream(java.io.InputStream) Txn(org.exist.storage.txn.Txn) DocumentImpl(org.exist.dom.persistent.DocumentImpl) BinaryDocument(org.exist.dom.persistent.BinaryDocument) DBBroker(org.exist.storage.DBBroker) Collection(org.exist.collections.Collection) LogRestoreListener(org.exist.backup.restore.listener.LogRestoreListener) RestoreListener(org.exist.backup.restore.listener.RestoreListener) BrokerPool(org.exist.storage.BrokerPool)

Example 17 with DocumentImpl

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

the class Append method process.

@Override
public long process(Txn transaction) throws PermissionDeniedException, LockException, EXistException, XPathException, TriggerException {
    final NodeList children = content;
    if (children.getLength() == 0) {
        return 0;
    }
    try {
        final StoredNode[] ql = selectAndLock(transaction);
        final NotificationService notifier = broker.getBrokerPool().getNotificationService();
        for (final StoredNode node : ql) {
            final DocumentImpl doc = node.getOwnerDocument();
            if (!doc.getPermissions().validate(broker.getCurrentSubject(), Permission.WRITE)) {
                throw new PermissionDeniedException("User '" + broker.getCurrentSubject().getName() + "' does not have permission to write to the document '" + doc.getDocumentURI() + "'!");
            }
            node.appendChildren(transaction, children, child);
            doc.setLastModified(System.currentTimeMillis());
            modifiedDocuments.add(doc);
            broker.storeXMLResource(transaction, doc);
            notifier.notifyUpdate(doc, UpdateListener.UPDATE);
        }
        checkFragmentation(transaction, modifiedDocuments);
        return ql.length;
    } finally {
        // release all acquired locks
        unlockDocuments(transaction);
    }
}
Also used : NodeList(org.w3c.dom.NodeList) NotificationService(org.exist.storage.NotificationService) PermissionDeniedException(org.exist.security.PermissionDeniedException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) StoredNode(org.exist.dom.persistent.StoredNode)

Example 18 with DocumentImpl

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

the class Insert method process.

@Override
public long process(Txn transaction) throws PermissionDeniedException, LockException, EXistException, XPathException, TriggerException {
    final NodeList children = content;
    if (children.getLength() == 0) {
        return 0;
    }
    try {
        final StoredNode[] ql = selectAndLock(transaction);
        final NotificationService notifier = broker.getBrokerPool().getNotificationService();
        final int len = children.getLength();
        if (LOG.isDebugEnabled()) {
            LOG.debug("found {} nodes to insert", len);
        }
        for (final StoredNode node : ql) {
            final DocumentImpl doc = node.getOwnerDocument();
            if (!doc.getPermissions().validate(broker.getCurrentSubject(), Permission.WRITE)) {
                throw new PermissionDeniedException("permission to update document denied");
            }
            final NodeImpl parent = (NodeImpl) getParent(node);
            switch(mode) {
                case INSERT_BEFORE:
                    parent.insertBefore(transaction, children, node);
                    break;
                case INSERT_AFTER:
                    parent.insertAfter(transaction, children, node);
                    break;
            }
            doc.setLastModified(System.currentTimeMillis());
            modifiedDocuments.add(doc);
            broker.storeXMLResource(transaction, doc);
            notifier.notifyUpdate(doc, UpdateListener.UPDATE);
        }
        checkFragmentation(transaction, modifiedDocuments);
        return ql.length;
    } finally {
        unlockDocuments(transaction);
    }
}
Also used : NodeImpl(org.exist.dom.persistent.NodeImpl) NodeList(org.w3c.dom.NodeList) NotificationService(org.exist.storage.NotificationService) PermissionDeniedException(org.exist.security.PermissionDeniedException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) StoredNode(org.exist.dom.persistent.StoredNode)

Example 19 with DocumentImpl

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

the class Remove method process.

@Override
public long process(Txn transaction) throws PermissionDeniedException, LockException, EXistException, XPathException, TriggerException {
    try {
        final StoredNode[] ql = selectAndLock(transaction);
        final NotificationService notifier = broker.getBrokerPool().getNotificationService();
        for (final StoredNode node : ql) {
            final DocumentImpl doc = node.getOwnerDocument();
            if (!doc.getPermissions().validate(broker.getCurrentSubject(), Permission.WRITE)) {
                throw new PermissionDeniedException("User '" + broker.getCurrentSubject().getName() + "' does not have permission to write to the document '" + doc.getDocumentURI() + "'!");
            }
            final NodeImpl parent = (NodeImpl) getParent(node);
            if (parent == null || parent.getNodeType() != Node.ELEMENT_NODE) {
                throw new EXistException("you cannot remove the document element. Use update " + "instead");
            } else {
                parent.removeChild(transaction, node);
            }
            doc.setLastModified(System.currentTimeMillis());
            modifiedDocuments.add(doc);
            broker.storeXMLResource(transaction, doc);
            notifier.notifyUpdate(doc, UpdateListener.UPDATE);
        }
        checkFragmentation(transaction, modifiedDocuments);
        return ql.length;
    } finally {
        unlockDocuments(transaction);
    }
}
Also used : NodeImpl(org.exist.dom.persistent.NodeImpl) NotificationService(org.exist.storage.NotificationService) PermissionDeniedException(org.exist.security.PermissionDeniedException) EXistException(org.exist.EXistException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) StoredNode(org.exist.dom.persistent.StoredNode)

Example 20 with DocumentImpl

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

the class Update method process.

@Override
public long process(Txn transaction) throws PermissionDeniedException, LockException, EXistException, XPathException, TriggerException {
    final NodeList children = content;
    if (children.getLength() == 0) {
        return 0;
    }
    int modifications = children.getLength();
    try {
        final StoredNode[] ql = selectAndLock(transaction);
        final NotificationService notifier = broker.getBrokerPool().getNotificationService();
        for (final StoredNode node : ql) {
            if (node == null) {
                LOG.warn("select {} returned empty node", selectStmt);
                continue;
            }
            final DocumentImpl doc = node.getOwnerDocument();
            if (!doc.getPermissions().validate(broker.getCurrentSubject(), Permission.WRITE)) {
                throw new PermissionDeniedException("User '" + broker.getCurrentSubject().getName() + "' does not have permission to write to the document '" + doc.getDocumentURI() + "'!");
            }
            switch(node.getNodeType()) {
                case Node.ELEMENT_NODE:
                    if (modifications == 0) {
                        modifications = 1;
                    }
                    ((ElementImpl) node).update(transaction, children);
                    break;
                case Node.TEXT_NODE:
                    final ElementImpl textParent = (ElementImpl) node.getParentNode();
                    final Node textTemp = children.item(0);
                    final TextImpl text = new TextImpl(textTemp.getNodeValue());
                    modifications = 1;
                    text.setOwnerDocument(doc);
                    textParent.updateChild(transaction, node, text);
                    break;
                case Node.ATTRIBUTE_NODE:
                    final ElementImpl attrParent = (ElementImpl) ((Attr) node).getOwnerElement();
                    if (attrParent == null) {
                        LOG.warn("parent node not found for {}", node.getNodeId());
                        break;
                    }
                    final AttrImpl attr = (AttrImpl) node;
                    final Node attrTemp = children.item(0);
                    final AttrImpl attribute = new AttrImpl(attr.getQName(), attrTemp.getNodeValue(), broker.getBrokerPool().getSymbols());
                    attribute.setOwnerDocument(doc);
                    attrParent.updateChild(transaction, node, attribute);
                    break;
                default:
                    throw new EXistException("unsupported node-type");
            }
            doc.setLastModified(System.currentTimeMillis());
            modifiedDocuments.add(doc);
            broker.storeXMLResource(transaction, doc);
            notifier.notifyUpdate(doc, UpdateListener.UPDATE);
        }
        checkFragmentation(transaction, modifiedDocuments);
    } finally {
        unlockDocuments(transaction);
    }
    return modifications;
}
Also used : ElementImpl(org.exist.dom.persistent.ElementImpl) NodeList(org.w3c.dom.NodeList) StoredNode(org.exist.dom.persistent.StoredNode) Node(org.w3c.dom.Node) NotificationService(org.exist.storage.NotificationService) PermissionDeniedException(org.exist.security.PermissionDeniedException) AttrImpl(org.exist.dom.persistent.AttrImpl) EXistException(org.exist.EXistException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) TextImpl(org.exist.dom.persistent.TextImpl) StoredNode(org.exist.dom.persistent.StoredNode)

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