Search in sources :

Example 1 with TemporaryFileManager

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

the class Jaxp method preparseDTD.

// No-go ...processor is in validating mode
private Path preparseDTD(StreamSource instance, String systemId) throws IOException, TransformerConfigurationException, TransformerException {
    // prepare output tmp storage
    final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
    final Path tmp = temporaryFileManager.getTemporaryFile();
    final StreamResult result = new StreamResult(tmp.toFile());
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemId);
    transformer.transform(instance, result);
    return tmp;
}
Also used : Path(java.nio.file.Path) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TemporaryFileManager(org.exist.util.io.TemporaryFileManager)

Example 2 with TemporaryFileManager

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

the class EXistResult method add.

@Override
public void add(final InputStream is) throws HttpClientException {
    try {
        // we have to make a temporary copy of the data stream, as the socket will be closed shortly
        final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
        final Path tempFile = temporaryFileManager.getTemporaryFile();
        Files.copy(is, tempFile, StandardCopyOption.REPLACE_EXISTING);
        result.add(BinaryValueFromFile.getInstance(context, new Base64BinaryValueType(), tempFile, (isClosed, file) -> temporaryFileManager.returnTemporaryFile(file)));
    } catch (final XPathException | IOException xpe) {
        throw new HttpClientException("Unable to add binary value to result:" + xpe.getMessage(), xpe);
    } finally {
        try {
            is.close();
        } catch (final IOException ioe) {
            logger.warn(ioe.getMessage(), ioe);
        }
    }
}
Also used : Path(java.nio.file.Path) TypeTest(org.exist.xquery.TypeTest) Files(java.nio.file.Files) TemporaryFileManager(org.exist.util.io.TemporaryFileManager) Result(org.expath.httpclient.model.Result) org.exist.xquery.value(org.exist.xquery.value) IOException(java.io.IOException) Reader(java.io.Reader) Source(javax.xml.transform.Source) StandardCopyOption(java.nio.file.StandardCopyOption) HttpResponse(org.expath.httpclient.HttpResponse) Logger(org.apache.logging.log4j.Logger) Charset(java.nio.charset.Charset) ModuleUtils(org.exist.xquery.modules.ModuleUtils) HttpClientException(org.expath.httpclient.HttpClientException) SAXException(org.xml.sax.SAXException) NodeTest(org.exist.xquery.NodeTest) DocumentImpl(org.exist.dom.memtree.DocumentImpl) Path(java.nio.file.Path) LogManager(org.apache.logging.log4j.LogManager) XPathException(org.exist.xquery.XPathException) XQueryContext(org.exist.xquery.XQueryContext) InputStream(java.io.InputStream) HttpClientException(org.expath.httpclient.HttpClientException) XPathException(org.exist.xquery.XPathException) IOException(java.io.IOException) TemporaryFileManager(org.exist.util.io.TemporaryFileManager)

Example 3 with TemporaryFileManager

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

the class RpcConnection method retrieveFirstChunk.

@Override
public Map<String, Object> retrieveFirstChunk(final int resultId, final int num, 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: " + resultId);
        }
        qr.touch();
        final Item item = qr.result.itemAt(num);
        if (item == null) {
            throw new EXistException("index out of range");
        }
        final Map<String, Object> result = new HashMap<>();
        final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
        final Path tempFile = temporaryFileManager.getTemporaryFile();
        if (compression && LOG.isDebugEnabled()) {
            LOG.debug("retrieveFirstChunk 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))) {
            if (Type.subTypeOf(item.getType(), Type.NODE)) {
                final NodeValue nodeValue = (NodeValue) item;
                for (final Map.Entry<Object, Object> entry : qr.serialization.entrySet()) {
                    parameters.put(entry.getKey().toString(), entry.getValue().toString());
                }
                serialize(broker, toProperties(parameters), saxSerializer -> saxSerializer.toSAX(nodeValue), writer);
            } else {
                writer.write(item.getStringValue());
            }
        } catch (final XPathException e) {
            throw new EXistException(e);
        }
        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) EXistException(org.exist.EXistException) TemporaryFileManager(org.exist.util.io.TemporaryFileManager) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) LockedDocumentMap(org.exist.storage.lock.LockedDocumentMap)

Example 4 with TemporaryFileManager

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

the class RpcConnection method getDocumentChunk.

@Override
public List<String> getDocumentChunk(final String name, final Map<String, Object> parameters) throws EXistException, PermissionDeniedException, IOException {
    final List<String> result = new ArrayList<>(2);
    final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
    final Path file = temporaryFileManager.getTemporaryFile();
    try (final OutputStream os = new BufferedOutputStream(Files.newOutputStream(file))) {
        os.write(getDocument(name, parameters));
    }
    result.add(FileUtils.fileName(file));
    result.add(Long.toString(Files.size(file)));
    return result;
}
Also used : DeflaterOutputStream(java.util.zip.DeflaterOutputStream) TemporaryFileManager(org.exist.util.io.TemporaryFileManager)

Example 5 with TemporaryFileManager

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

the class RpcConnection method retrieveFirstChunk.

@Override
public Map<String, Object> retrieveFirstChunk(final String docName, final String id, final Map<String, Object> parameters) throws EXistException, PermissionDeniedException {
    final boolean compression = useCompression(parameters);
    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 NodeId nodeId = factory.getBrokerPool().getNodeFactory().createFromString(id);
        final NodeProxy node = new NodeProxy(document, nodeId);
        final Map<String, Object> result = new HashMap<>();
        final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
        final Path tempFile = temporaryFileManager.getTemporaryFile();
        if (compression && LOG.isDebugEnabled()) {
            LOG.debug("retrieveFirstChunk 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))) {
            serialize(broker, toProperties(parameters), saxSerializer -> saxSerializer.toSAX(node), writer);
        }
        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) URISyntaxException(java.net.URISyntaxException) EXistException(org.exist.EXistException) TemporaryFileManager(org.exist.util.io.TemporaryFileManager) NodeId(org.exist.numbering.NodeId) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) XmldbURI(org.exist.xmldb.XmldbURI)

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