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();
}
}
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);
}
}
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);
}
}
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);
}
}
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;
}
Aggregations