Search in sources :

Example 6 with TemporaryFileManager

use of org.exist.util.io.TemporaryFileManager 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 7 with TemporaryFileManager

use of org.exist.util.io.TemporaryFileManager in project exist by eXist-db.

the class RpcConnection method getDocumentData.

@Override
public Map<String, Object> getDocumentData(final String docName, final Map<String, Object> parameters) throws EXistException, PermissionDeniedException {
    final XmldbURI docUri;
    try {
        docUri = XmldbURI.xmldbUriFor(docName);
    } catch (final URISyntaxException e) {
        throw new EXistException(e);
    }
    return this.<Map<String, Object>>readDocument(docUri).apply((document, broker, transaction) -> {
        final Charset encoding = getEncoding(parameters);
        // A tweak for very large resources, VirtualTempFile
        final Map<String, Object> result = new HashMap<>();
        final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
        final Path tempFile = temporaryFileManager.getTemporaryFile();
        if (document.getResourceType() == DocumentImpl.XML_FILE) {
            try (final Writer writer = Files.newBufferedWriter(tempFile, encoding)) {
                serialize(broker, toProperties(parameters), saxSerializer -> saxSerializer.toSAX(document), writer);
            }
        } else {
            try (final OutputStream os = new BufferedOutputStream(Files.newOutputStream(tempFile))) {
                broker.readBinaryResource(transaction, (BinaryDocument) document, os);
            }
        }
        final byte[] firstChunk = getChunk(tempFile, 0);
        result.put("data", firstChunk);
        int offset = 0;
        if (Files.size(tempFile) > MAX_DOWNLOAD_CHUNK_SIZE) {
            offset = firstChunk.length;
            final int handle = factory.resultSets.add(new SerializedResult(tempFile));
            result.put("handle", Integer.toString(handle));
            result.put("supports-long-offset", Boolean.TRUE);
        } else {
            temporaryFileManager.returnTemporaryFile(tempFile);
        }
        result.put("offset", offset);
        return result;
    });
}
Also used : DeflaterOutputStream(java.util.zip.DeflaterOutputStream) Charset(java.nio.charset.Charset) URISyntaxException(java.net.URISyntaxException) EXistException(org.exist.EXistException) TemporaryFileManager(org.exist.util.io.TemporaryFileManager) XmldbURI(org.exist.xmldb.XmldbURI)

Example 8 with TemporaryFileManager

use of org.exist.util.io.TemporaryFileManager in project exist by eXist-db.

the class RpcConnection method retrieveAllFirstChunk.

@Override
public Map<String, Object> retrieveAllFirstChunk(final int resultId, final Map<String, Object> parameters) throws EXistException, PermissionDeniedException {
    final boolean compression = useCompression(parameters);
    return withDb((broker, transaction) -> {
        final QueryResult qr = factory.resultSets.getResult(resultId);
        if (qr == null) {
            throw new EXistException("result set unknown or timed out");
        }
        qr.touch();
        for (final Map.Entry<Object, Object> entry : qr.serialization.entrySet()) {
            parameters.put(entry.getKey().toString(), entry.getValue().toString());
        }
        final SAXSerializer handler = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
        try {
            final Map<String, Object> result = new HashMap<>();
            final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
            final Path tempFile = temporaryFileManager.getTemporaryFile();
            if (compression && LOG.isDebugEnabled()) {
                LOG.debug("retrieveAllFirstChunk with compression");
            }
            try (final OutputStream os = compression ? new DeflaterOutputStream(new BufferedOutputStream(Files.newOutputStream(tempFile))) : new BufferedOutputStream(Files.newOutputStream(tempFile));
                final Writer writer = new OutputStreamWriter(os, getEncoding(parameters))) {
                handler.setOutput(writer, toProperties(parameters));
                // serialize results
                handler.startDocument();
                handler.startPrefixMapping("exist", Namespaces.EXIST_NS);
                final AttributesImpl attribs = new AttributesImpl();
                attribs.addAttribute("", "hitCount", "hitCount", "CDATA", Integer.toString(qr.result.getItemCount()));
                handler.startElement(Namespaces.EXIST_NS, "result", "exist:result", attribs);
                Item current;
                char[] value;
                try {
                    for (final SequenceIterator i = qr.result.iterate(); i.hasNext(); ) {
                        current = i.nextItem();
                        if (Type.subTypeOf(current.getType(), Type.NODE)) {
                            ((NodeValue) current).toSAX(broker, handler, null);
                        } else {
                            value = current.toString().toCharArray();
                            handler.characters(value, 0, value.length);
                        }
                    }
                } catch (final XPathException e) {
                    throw new EXistException(e);
                }
                handler.endElement(Namespaces.EXIST_NS, "result", "exist:result");
                handler.endPrefixMapping("exist");
                handler.endDocument();
            }
            final byte[] firstChunk = getChunk(tempFile, 0);
            result.put("data", firstChunk);
            int offset = 0;
            if (Files.size(tempFile) > MAX_DOWNLOAD_CHUNK_SIZE) {
                offset = firstChunk.length;
                final int handle = factory.resultSets.add(new SerializedResult(tempFile));
                result.put("handle", Integer.toString(handle));
                result.put("supports-long-offset", Boolean.TRUE);
            } else {
                temporaryFileManager.returnTemporaryFile(tempFile);
            }
            result.put("offset", offset);
            return result;
        } finally {
            SerializerPool.getInstance().returnObject(handler);
        }
    });
}
Also used : DeflaterOutputStream(java.util.zip.DeflaterOutputStream) TemporaryFileManager(org.exist.util.io.TemporaryFileManager) AttributesImpl(org.xml.sax.helpers.AttributesImpl) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) SAXSerializer(org.exist.util.serializer.SAXSerializer) EXistException(org.exist.EXistException) LockedDocumentMap(org.exist.storage.lock.LockedDocumentMap)

Example 9 with TemporaryFileManager

use of org.exist.util.io.TemporaryFileManager in project exist by eXist-db.

the class ZipArchiveBackupDescriptor method getRepoBackup.

@Override
public Path getRepoBackup() throws IOException {
    final ZipEntry ze = archive.getEntry(RepoBackup.REPO_ARCHIVE);
    if (ze == null) {
        return null;
    }
    final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
    final Path temp = temporaryFileManager.getTemporaryFile();
    try (final InputStream is = archive.getInputStream(ze)) {
        Files.copy(is, temp, StandardCopyOption.REPLACE_EXISTING);
    }
    return temp;
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) TemporaryFileManager(org.exist.util.io.TemporaryFileManager)

Example 10 with TemporaryFileManager

use of org.exist.util.io.TemporaryFileManager in project exist by eXist-db.

the class EmbeddedOutputStream method openStream.

private static Either<IOException, OutputStream> openStream(final BrokerPool pool, final XmldbURL url) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Begin document download");
    }
    try {
        // get a temporary file
        final TemporaryFileManager tempFileManager = TemporaryFileManager.getInstance();
        final Path tempFile = tempFileManager.getTemporaryFile();
        final OutputStream osTemp = new BufferedOutputStream(Files.newOutputStream(tempFile, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE));
        // upload the content of the temp file to the db when it is closed, then return the temp file
        final RunnableE<IOException> uploadOnClose = () -> {
            uploadToDb(pool, url, tempFile);
            tempFileManager.returnTemporaryFile(tempFile);
        };
        return Right(new CloseNotifyingOutputStream(osTemp, uploadOnClose));
    } catch (final IOException e) {
        return Left(e);
    }
}
Also used : Path(java.nio.file.Path) CloseNotifyingOutputStream(org.exist.util.io.CloseNotifyingOutputStream) CloseNotifyingOutputStream(org.exist.util.io.CloseNotifyingOutputStream) TemporaryFileManager(org.exist.util.io.TemporaryFileManager)

Aggregations

TemporaryFileManager (org.exist.util.io.TemporaryFileManager)14 Path (java.nio.file.Path)8 EXistException (org.exist.EXistException)6 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)5 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 XmldbURI (org.exist.xmldb.XmldbURI)3 URISyntaxException (java.net.URISyntaxException)2 Charset (java.nio.charset.Charset)2 DBBroker (org.exist.storage.DBBroker)2 LockedDocumentMap (org.exist.storage.lock.LockedDocumentMap)2 XPathException (org.exist.xquery.XPathException)2 SAXException (org.xml.sax.SAXException)2 Reader (java.io.Reader)1 MalformedURLException (java.net.MalformedURLException)1 Files (java.nio.file.Files)1 StandardCopyOption (java.nio.file.StandardCopyOption)1 DataFormatException (java.util.zip.DataFormatException)1 Inflater (java.util.zip.Inflater)1 ZipEntry (java.util.zip.ZipEntry)1