use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class CollectionRemovalTest method retrieveDoc.
private void retrieveDoc(final XmldbURI uri) throws EXistException, PermissionDeniedException, SAXException, LockException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Collection test = broker.openCollection(uri, LockMode.READ_LOCK)) {
assertNotNull(test);
try (final LockedDocument lockedDoc = test.getDocumentWithLock(broker, XmldbURI.createInternal("document.xml"), LockMode.READ_LOCK)) {
assertNotNull(lockedDoc);
final Serializer serializer = broker.borrowSerializer();
try {
String xml = serializer.serialize(lockedDoc.getDocument());
} finally {
broker.returnSerializer(serializer);
}
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
test.close();
}
}
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class XMLReaderSecurityTest method cannotExpandExternalEntitiesWhenDisabled.
@Test
public void cannotExpandExternalEntitiesWhenDisabled() throws EXistException, IOException, PermissionDeniedException, LockException, SAXException, TransformerException {
final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
// create a temporary file on disk that contains secret info
final Tuple2<String, Path> secret = createTempSecretFile();
final XmldbURI docName = XmldbURI.create("expand-secret.xml");
// attempt to store a document with an external entity which would be expanded to the content of the secret file
try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
try (final Collection testCollection = broker.openCollection(TEST_COLLECTION, Lock.LockMode.WRITE_LOCK)) {
// debugReader("cannotExpandExternalEntitiesWhenDisabled", broker, testCollection);
final String docContent = EXPANSION_DOC.replace(EXTERNAL_FILE_PLACEHOLDER, secret._2.toUri().toString());
broker.storeDocument(transaction, docName, new StringInputSource(docContent), MimeType.XML_TYPE, testCollection);
}
transaction.commit();
}
// read back the document, to confirm that it does not contain the secret
try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
try (final Collection testCollection = broker.openCollection(TEST_COLLECTION, Lock.LockMode.READ_LOCK)) {
try (final LockedDocument testDoc = testCollection.getDocumentWithLock(broker, docName, Lock.LockMode.READ_LOCK)) {
// release the collection lock early inline with asymmetrical locking
testCollection.close();
assertNotNull(testDoc);
final String expected = EXPECTED_EXPANSION_DISABLED_DOC;
final String actual = serialize(testDoc.getDocument());
assertEquals(expected, actual);
}
}
transaction.commit();
}
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class XMLReaderExpansionTest method expandExternalEntities.
@Test
public void expandExternalEntities() throws EXistException, IOException, PermissionDeniedException, LockException, SAXException, TransformerException {
final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
final Map<String, Boolean> parserConfig = new HashMap<>();
parserConfig.put(FEATURE_EXTERNAL_GENERAL_ENTITIES, true);
brokerPool.getConfiguration().setProperty(XMLReaderPool.XmlParser.XML_PARSER_FEATURES_PROPERTY, parserConfig);
// create a temporary file on disk that contains secret info
final Tuple2<String, Path> secret = createTempSecretFile();
final XmldbURI docName = XmldbURI.create("expand-secret.xml");
// attempt to store a document with an external entity which would be expanded to the content of the secret file
try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
try (final Collection testCollection = broker.openCollection(TEST_COLLECTION, Lock.LockMode.WRITE_LOCK)) {
// debugReader("expandExternalEntities", broker, testCollection);
final String docContent = EXPANSION_DOC.replace(EXTERNAL_FILE_PLACEHOLDER, secret._2.toUri().toString());
broker.storeDocument(transaction, docName, new StringInputSource(docContent), MimeType.XML_TYPE, testCollection);
}
transaction.commit();
}
// read back the document, to confirm that it does contain the secret
try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
try (final Collection testCollection = broker.openCollection(TEST_COLLECTION, Lock.LockMode.READ_LOCK)) {
try (final LockedDocument testDoc = testCollection.getDocumentWithLock(broker, docName, Lock.LockMode.READ_LOCK)) {
// release the collection lock early inline with asymmetrical locking
testCollection.close();
assertNotNull(testDoc);
final String expected = EXPECTED_EXPANDED_DOC.replace(EXTERNAL_FILE_PLACEHOLDER, secret._1);
final String actual = serialize(testDoc.getDocument());
assertEquals(expected, actual);
}
}
transaction.commit();
}
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class RemoveIndex method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
// Get first parameter, this is the document
final 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
if (lockedDoc == null) {
throw new XPathException("Document " + path + " does not exist.");
}
// Retrieve Lucene
LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
// Note: code order is important here,
index.setDocument(lockedDoc.getDocument(), ReindexMode.REMOVE_BINARY);
index.flush();
} catch (Exception ex) {
// PermissionDeniedException
throw new XPathException(ex);
}
// Return nothing [status would be nice]
return Sequence.EMPTY_SEQUENCE;
}
use of org.exist.dom.persistent.LockedDocument 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;
}
Aggregations