Search in sources :

Example 21 with Serializer

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);
    }
}
Also used : Path(java.nio.file.Path) CloseNotifyingInputStream(org.exist.util.io.CloseNotifyingInputStream) EXistException(org.exist.EXistException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) TemporaryFileManager(org.exist.util.io.TemporaryFileManager) SAXException(org.xml.sax.SAXException) DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) XmldbURI(org.exist.xmldb.XmldbURI) Serializer(org.exist.storage.serializers.Serializer)

Example 22 with Serializer

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);
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) DocumentType(org.w3c.dom.DocumentType) BrokerPool(org.exist.storage.BrokerPool) Serializer(org.exist.storage.serializers.Serializer)

Example 23 with 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);
        }
    }
}
Also used : XQuery(org.exist.xquery.XQuery) InputStream(java.io.InputStream) Sequence(org.exist.xquery.value.Sequence) DOMFile(org.exist.storage.dom.DOMFile) Txn(org.exist.storage.txn.Txn) BinaryDocument(org.exist.dom.persistent.BinaryDocument) Item(org.exist.xquery.value.Item) SequenceIterator(org.exist.xquery.value.SequenceIterator) StringWriter(java.io.StringWriter) TransactionManager(org.exist.storage.txn.TransactionManager) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Serializer(org.exist.storage.serializers.Serializer)

Example 24 with Serializer

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;
        // }
        }
    }
}
Also used : Path(java.nio.file.Path) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) Writer(java.io.Writer) Serializer(org.exist.storage.serializers.Serializer)

Example 25 with Serializer

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());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Serializer(org.exist.storage.serializers.Serializer)

Aggregations

Serializer (org.exist.storage.serializers.Serializer)64 SAXException (org.xml.sax.SAXException)21 DBBroker (org.exist.storage.DBBroker)16 LockedDocument (org.exist.dom.persistent.LockedDocument)15 SAXSerializer (org.exist.util.serializer.SAXSerializer)14 BrokerPool (org.exist.storage.BrokerPool)11 Properties (java.util.Properties)10 Sequence (org.exist.xquery.value.Sequence)10 Txn (org.exist.storage.txn.Txn)8 XPathException (org.exist.xquery.XPathException)8 StringWriter (java.io.StringWriter)7 Collection (org.exist.collections.Collection)7 IOException (java.io.IOException)6 Item (org.exist.xquery.value.Item)6 Path (java.nio.file.Path)5 EXistException (org.exist.EXistException)5 SequenceIterator (org.exist.xquery.value.SequenceIterator)5 NodeProxy (org.exist.dom.persistent.NodeProxy)4 TransactionManager (org.exist.storage.txn.TransactionManager)4 NodeValue (org.exist.xquery.value.NodeValue)4