use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class CopyResourceTest method copyDoc.
private void copyDoc(final Subject execAsUser, final DBBroker.PreserveType preserve, final XmldbURI srcDocName, final XmldbURI destDocName) throws EXistException, PermissionDeniedException, LockException, IOException, TriggerException {
final XmldbURI src = TEST_COLLECTION_URI.append(srcDocName);
final XmldbURI dest = TEST_COLLECTION_URI.append(destDocName);
final BrokerPool pool = existWebServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(execAsUser));
final Txn transaction = pool.getTransactionManager().beginTransaction();
final LockedDocument lockedSrcDoc = broker.getXMLResource(src, LockMode.READ_LOCK);
final Collection destCol = broker.openCollection(dest.removeLastSegment(), LockMode.WRITE_LOCK)) {
broker.copyResource(transaction, lockedSrcDoc.getDocument(), destCol, dest.lastSegment(), preserve);
transaction.commit();
}
// check the copy of the document is the same as the original
try (final DBBroker broker = pool.get(Optional.of(execAsUser));
final LockedDocument lockedOriginal = broker.getXMLResource(src, LockMode.READ_LOCK);
final LockedDocument lockedCopy = broker.getXMLResource(dest, LockMode.READ_LOCK)) {
final Diff diff = DiffBuilder.compare(Input.fromDocument(lockedOriginal.getDocument())).withTest(Input.fromDocument(lockedCopy.getDocument())).build();
assertFalse(diff.toString(), diff.hasDifferences());
}
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class StoreResourceTest method replaceXmlDoc.
private void replaceXmlDoc(final Subject execAsUser, final DBBroker.PreserveType preserve, final XmldbURI docName, final String content) throws EXistException, PermissionDeniedException, LockException, IOException, SAXException {
final XmldbURI uri = TEST_COLLECTION_URI.append(docName);
final BrokerPool pool = existWebServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(execAsUser));
final Txn transaction = pool.getTransactionManager().beginTransaction();
final Collection col = broker.openCollection(uri.removeLastSegment(), Lock.LockMode.WRITE_LOCK)) {
broker.storeDocument(transaction, uri.lastSegment(), new StringInputSource(content), MimeType.XML_TYPE, col);
transaction.commit();
}
// check the replaced document is correct
try (final DBBroker broker = pool.get(Optional.of(execAsUser));
final LockedDocument lockedDoc = broker.getXMLResource(uri, Lock.LockMode.READ_LOCK)) {
final Serializer serializer = broker.borrowSerializer();
try {
final String docXml = serializer.serialize(lockedDoc.getDocument());
final Diff diff = DiffBuilder.compare(Input.fromString(content)).withTest(Input.fromString(docXml)).build();
assertFalse(diff.toString(), diff.hasDifferences());
} finally {
broker.returnSerializer(serializer);
}
}
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class StoreResourceTest method replaceBinDoc.
private void replaceBinDoc(final Subject execAsUser, final DBBroker.PreserveType preserve, final XmldbURI docName, final String content) throws EXistException, PermissionDeniedException, LockException, IOException, SAXException {
final XmldbURI uri = TEST_COLLECTION_URI.append(docName);
final BrokerPool pool = existWebServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(execAsUser));
final Txn transaction = pool.getTransactionManager().beginTransaction();
final Collection col = broker.openCollection(uri.removeLastSegment(), Lock.LockMode.WRITE_LOCK)) {
broker.storeDocument(transaction, uri.lastSegment(), new StringInputSource(content.getBytes(UTF_8)), MimeType.BINARY_TYPE, col);
transaction.commit();
}
// check the replaced document is correct
try (final DBBroker broker = pool.get(Optional.of(execAsUser));
final LockedDocument lockedDoc = broker.getXMLResource(uri, Lock.LockMode.READ_LOCK);
final InputStream is = broker.getBinaryResource((BinaryDocument) lockedDoc.getDocument());
final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
os.write(is);
assertArrayEquals(content.getBytes(UTF_8), os.toByteArray());
}
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class AbstractRecoverTest method read.
/**
* Read a document from the database.
*
* @param shouldExist true if the document should exist in the database, false if the document should not exist
* @param data The data that was previously stored
* @param dbFilename The name of the file to read from the database
*/
protected void read(final boolean shouldExist, final InputSource data, final String dbFilename) throws EXistException, PermissionDeniedException, IOException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final XmldbURI uri = TestConstants.TEST_COLLECTION_URI.append(dbFilename);
try (final LockedDocument doc = broker.getXMLResource(uri, LockMode.READ_LOCK)) {
if (!shouldExist) {
assertNull("Document should not exist in the database: " + uri, doc);
} else {
assertNotNull("Document does not exist in the database: " + uri, doc);
readAndVerify(broker, doc.getDocument(), data, dbFilename);
}
}
}
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class MetadataFunctions method extractMetadataFromLocalResource.
private Sequence extractMetadataFromLocalResource(final XmldbURI docUri) throws XPathException {
try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(docUri, LockMode.READ_LOCK)) {
if (lockedDoc != null && lockedDoc.getDocument() instanceof BinaryDocument) {
final BinaryDocument binDoc = (BinaryDocument) lockedDoc.getDocument();
final BrokerPool pool = context.getBroker().getBrokerPool();
final BlobStore blobStore = pool.getBlobStore();
try (final Txn transaction = pool.getTransactionManager().beginTransaction()) {
final Sequence result = blobStore.with(transaction, binDoc.getBlobId(), blobFile -> TaggedTryUnchecked(XPathException.class, () -> exifToolExtract(blobFile))).get();
transaction.commit();
return result;
}
} else {
throw new XPathException(this, "The binary document at " + docUri.toString() + " cannot be found.");
}
} catch (PermissionDeniedException | IOException | TransactionException e) {
throw new XPathException(this, "Could not access binary document: " + e.getMessage(), e);
}
}
Aggregations