use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class EmbeddedInputStream method openStream.
private static Either<IOException, InputStream> openStream(final BrokerPool pool, final XmldbURL url) {
if (LOG.isDebugEnabled()) {
LOG.debug("Begin document download");
}
try {
final XmldbURI path = XmldbURI.create(url.getPath());
try (final DBBroker broker = pool.getBroker()) {
try (final LockedDocument lockedResource = broker.getXMLResource(path, Lock.LockMode.READ_LOCK)) {
if (lockedResource == null) {
// Test for collection
try (final Collection collection = broker.openCollection(path, Lock.LockMode.READ_LOCK)) {
if (collection == null) {
// No collection, no document
return Left(new IOException("Resource " + url.getPath() + " not found."));
} else {
// Collection
return Left(new IOException("Resource " + url.getPath() + " is a collection."));
}
}
} else {
final DocumentImpl resource = lockedResource.getDocument();
if (resource.getResourceType() == DocumentImpl.XML_FILE) {
final Serializer serializer = broker.borrowSerializer();
try {
// Preserve doctype
serializer.setProperty(EXistOutputKeys.OUTPUT_DOCTYPE, "yes");
// serialize the XML to a temporary file
final TemporaryFileManager tempFileManager = TemporaryFileManager.getInstance();
final Path tempFile = tempFileManager.getTemporaryFile();
try (final Writer writer = Files.newBufferedWriter(tempFile, UTF_8, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
serializer.serialize(resource, writer);
}
// NOTE: the temp file will be returned to the manager when the InputStream is closed
return Right(new CloseNotifyingInputStream(Files.newInputStream(tempFile, StandardOpenOption.READ), () -> tempFileManager.returnTemporaryFile(tempFile)));
} finally {
broker.returnSerializer(serializer);
}
} else if (resource.getResourceType() == BinaryDocument.BINARY_FILE) {
return Right(broker.getBinaryResource((BinaryDocument) resource));
} else {
return Left(new IOException("Unknown resource type " + url.getPath() + ": " + resource.getResourceType()));
}
}
} finally {
if (LOG.isDebugEnabled()) {
LOG.debug("End document download");
}
}
}
} catch (final EXistException | PermissionDeniedException | SAXException e) {
LOG.error(e);
return Left(new IOException(e.getMessage(), e));
} catch (final IOException e) {
return Left(e);
}
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class DocTypeTest method docType_usingString.
@Test
public void docType_usingString() throws EXistException, PermissionDeniedException, SAXException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final LockedDocument lockedDoc = broker.getXMLResource(root.getURI().append(XmldbURI.create("test.xml")), LockMode.READ_LOCK)) {
final DocumentImpl doc = lockedDoc.getDocument();
DocumentType docType = doc.getDoctype();
assertNotNull(docType);
assertEquals("-//OASIS//DTD DITA Topic//EN", docType.getPublicId());
final Serializer serializer = broker.borrowSerializer();
try {
serializer.setProperties(OUTPUT_PROPERTIES);
String serialized = serializer.serialize(doc);
assertTrue("Checking for Public Id in output", serialized.contains("-//OASIS//DTD DITA Topic//EN"));
} finally {
broker.returnSerializer(serializer);
}
}
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class RecoveryTest method verify.
private void verify(final BrokerPool pool) throws EXistException, PermissionDeniedException, SAXException, XPathException, IOException, BTreeException, LockException {
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final Serializer serializer = broker.borrowSerializer();
try {
try (final LockedDocument lockedDoc = broker.getXMLResource(XmldbURI.ROOT_COLLECTION_URI.append("test/test2/hamlet.xml"), LockMode.READ_LOCK)) {
assertNotNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/hamlet.xml' should not be null", lockedDoc);
final String data = serializer.serialize(lockedDoc.getDocument());
assertNotNull(data);
}
try (final LockedDocument lockedDoc = broker.getXMLResource(XmldbURI.ROOT_COLLECTION_URI.append("test/test2/test_string.xml"), LockMode.READ_LOCK)) {
assertNotNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/test_string.xml' should not be null", lockedDoc);
final String data = serializer.serialize(lockedDoc.getDocument());
assertNotNull(data);
}
final String lastSampleName = SAMPLES.getShakespeareXmlSampleNames()[SAMPLES.getShakespeareXmlSampleNames().length - 1];
try (final LockedDocument lockedDoc = broker.getXMLResource(TestConstants.TEST_COLLECTION_URI2.append(lastSampleName), LockMode.READ_LOCK)) {
assertNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/'" + lastSampleName + " should not exist anymore", lockedDoc);
}
final XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
final Sequence seq = xquery.execute(broker, "//SPEECH[contains(LINE, 'king')]", null);
assertNotNull(seq);
for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
final Item next = i.nextItem();
final String value = serializer.serialize((NodeValue) next);
}
} finally {
broker.returnSerializer(serializer);
}
try (final LockedDocument lockedBinDoc = broker.getXMLResource(TestConstants.TEST_COLLECTION_URI2.append(TestConstants.TEST_BINARY_URI), LockMode.READ_LOCK)) {
assertNotNull("Binary document is null", lockedBinDoc);
final BinaryDocument binDoc = (BinaryDocument) lockedBinDoc.getDocument();
try (final InputStream is = broker.getBinaryResource(binDoc)) {
final byte[] bdata = new byte[(int) binDoc.getContentLength()];
is.read(bdata);
final String data = new String(bdata);
assertNotNull(data);
}
}
final DOMFile domDb = ((NativeBroker) broker).getDOMFile();
assertNotNull(domDb);
try (final Writer writer = new StringWriter()) {
domDb.dump(writer);
}
final TransactionManager transact = pool.getTransactionManager();
try (final Txn transaction = transact.beginTransaction()) {
try (final Collection root = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.WRITE_LOCK)) {
assertNotNull(root);
transaction.acquireCollectionLock(() -> broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(root.getURI()));
broker.removeCollection(transaction, root);
}
transact.commit(transaction);
}
}
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class LargeValuesTest method restart.
/**
* Just recover.
*/
private void restart() throws EXistException, DatabaseConfigurationException, PermissionDeniedException, IOException, SAXException, LockException {
BrokerPool.FORCE_CORRUPTION = false;
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Collection root = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.READ_LOCK)) {
assertNotNull(root);
try (final LockedDocument lockedDoc = root.getDocumentWithLock(broker, XmldbURI.create("test.xml"), LockMode.READ_LOCK)) {
assertNotNull(lockedDoc);
final Path tempFile = Files.createTempFile("eXist", ".xml");
final Serializer serializer = broker.borrowSerializer();
try (final Writer writer = Files.newBufferedWriter(tempFile, UTF_8)) {
serializer.serialize(lockedDoc.getDocument(), writer);
} finally {
broker.returnSerializer(serializer);
}
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
root.close();
FileUtils.deleteQuietly(tempFile);
// XQuery xquery = broker.getXQueryService();
// DocumentSet docs = broker.getAllXMLResources(new DefaultDocumentSet());
// Sequence result = xquery.execute(broker, "//key/@id/string()", docs.docsToNodeSet(), AccessContext.TEST);
// assertEquals(KEY_COUNT, result.getItemCount());
// for (SequenceIterator i = result.iterate(); i.hasNext();) {
// Item item = i.nextItem();
// String s = item.getStringValue();
// assertTrue(s.length() > 0);
// if (s.length() == 0)
// break;
// }
}
}
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class XQueryUpdateTest method appendCDATA.
@Test
public void appendCDATA() throws EXistException, PermissionDeniedException, XPathException, SAXException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
XQuery xquery = pool.getXQueryService();
String query = " update insert\n" + " <product>\n" + " <description><![CDATA[me & you <>]]></description>\n" + " </product>\n" + " into /products";
XQueryContext context = new XQueryContext(pool);
CompiledXQuery compiled = xquery.compile(context, query);
for (int i = 0; i < ITEMS_TO_APPEND; i++) {
xquery.execute(broker, compiled, null);
}
Sequence seq = xquery.execute(broker, "/products", null);
assertEquals(seq.getItemCount(), 1);
final Serializer serializer = broker.borrowSerializer();
try {
serializer.serialize((NodeValue) seq.itemAt(0));
} finally {
broker.returnSerializer(serializer);
}
seq = xquery.execute(broker, "//product", null);
assertEquals(ITEMS_TO_APPEND, seq.getItemCount());
}
}
Aggregations