Search in sources :

Example 1 with VirtualTempPath

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

the class RemoteResourceSet method getMembersAsResource.

@Override
public Resource getMembersAsResource() throws XMLDBException {
    final List<Object> params = new ArrayList<>();
    params.add(handle);
    params.add(outputProperties);
    VirtualTempPath tempFile = new VirtualTempPath(getInMemorySize(outputProperties), TemporaryFileManager.getInstance());
    try (final OutputStream os = tempFile.newOutputStream()) {
        Map<?, ?> table = (Map<?, ?>) collection.execute("retrieveAllFirstChunk", params);
        long offset = (Integer) table.get("offset");
        byte[] data = (byte[]) table.get("data");
        final boolean isCompressed = "yes".equals(outputProperties.getProperty(EXistOutputKeys.COMPRESS_OUTPUT, "no"));
        // One for the local cached file
        Inflater dec = null;
        byte[] decResult = null;
        int decLength = 0;
        if (isCompressed) {
            dec = new Inflater();
            decResult = new byte[65536];
            dec.setInput(data);
            do {
                decLength = dec.inflate(decResult);
                os.write(decResult, 0, decLength);
            } while (decLength == decResult.length || !dec.needsInput());
        } else {
            os.write(data);
        }
        while (offset > 0) {
            params.clear();
            params.add(table.get("handle"));
            params.add(Long.toString(offset));
            table = (Map<?, ?>) collection.execute("getNextExtendedChunk", params);
            offset = Long.parseLong((String) table.get("offset"));
            data = (byte[]) table.get("data");
            // One for the local cached file
            if (isCompressed) {
                dec.setInput(data);
                do {
                    decLength = dec.inflate(decResult);
                    os.write(decResult, 0, decLength);
                } while (decLength == decResult.length || !dec.needsInput());
            } else {
                os.write(data);
            }
        }
        if (dec != null) {
            dec.end();
        }
        final RemoteXMLResource res = new RemoteXMLResource(collection, handle, 0, XmldbURI.EMPTY_URI, Optional.empty());
        res.setContent(tempFile);
        res.setProperties(outputProperties);
        return res;
    } catch (final XMLDBException xre) {
        final byte[] data = (byte[]) collection.execute("retrieveAll", params);
        String content;
        try {
            content = new String(data, outputProperties.getProperty(OutputKeys.ENCODING, "UTF-8"));
        } catch (final UnsupportedEncodingException ue) {
            LOG.warn(ue);
            content = new String(data);
        }
        final RemoteXMLResource res = new RemoteXMLResource(collection, handle, 0, XmldbURI.EMPTY_URI, Optional.empty());
        res.setContent(content);
        res.setProperties(outputProperties);
        return res;
    } catch (final IOException | DataFormatException ioe) {
        throw new XMLDBException(ErrorCodes.VENDOR_ERROR, ioe.getMessage(), ioe);
    }
}
Also used : VirtualTempPath(org.exist.util.io.VirtualTempPath) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) XMLDBException(org.xmldb.api.base.XMLDBException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) DataFormatException(java.util.zip.DataFormatException) Inflater(java.util.zip.Inflater) Map(java.util.Map)

Example 2 with VirtualTempPath

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

the class RemoteXMLResource method setContentAsDOM.

@Override
public void setContentAsDOM(final Node root) throws XMLDBException {
    Properties properties = getProperties();
    try {
        VirtualTempPath tempFile = new VirtualTempPath(getInMemorySize(properties), TemporaryFileManager.getInstance());
        try (OutputStream out = tempFile.newOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(out, UTF_8)) {
            final DOMSerializer xmlout = new DOMSerializer(osw, properties);
            final short type = root.getNodeType();
            if (type == Node.ELEMENT_NODE || type == Node.DOCUMENT_FRAGMENT_NODE || type == Node.DOCUMENT_NODE) {
                xmlout.serialize(root);
            } else {
                throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "invalid node type");
            }
        }
        setContent(tempFile);
    } catch (final TransformerException | IOException ioe) {
        freeResources();
        throw new XMLDBException(ErrorCodes.VENDOR_ERROR, ioe.getMessage(), ioe);
    }
}
Also used : VirtualTempPath(org.exist.util.io.VirtualTempPath) DOMSerializer(org.exist.util.serializer.DOMSerializer) OutputStream(java.io.OutputStream) XMLDBException(org.xmldb.api.base.XMLDBException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) Properties(java.util.Properties) TransformerException(javax.xml.transform.TransformerException)

Example 3 with VirtualTempPath

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

the class AbstractRemoteResource method getRemoteContentIntoLocalFile.

protected void getRemoteContentIntoLocalFile(final OutputStream os, final boolean isRetrieve, final int handle, final int pos) throws XMLDBException {
    final String command;
    final List<Object> params = new ArrayList<>();
    if (isRetrieve) {
        command = "retrieveFirstChunk";
        params.add(handle);
        params.add(pos);
    } else {
        command = "getDocumentData";
        params.add(path.toString());
    }
    Properties properties = getProperties();
    params.add(properties);
    try {
        final TemporaryFileManager tempFileManager = TemporaryFileManager.getInstance();
        final VirtualTempPath tempFile = new VirtualTempPath(getInMemorySize(properties), tempFileManager);
        Map<?, ?> table = (Map<?, ?>) collection.execute(command, params);
        final String method;
        final boolean useLongOffset;
        if (table.containsKey("supports-long-offset") && (Boolean) table.get("supports-long-offset")) {
            useLongOffset = true;
            method = "getNextExtendedChunk";
        } else {
            useLongOffset = false;
            method = "getNextChunk";
        }
        long offset = (Integer) table.get("offset");
        byte[] data = (byte[]) table.get("data");
        final boolean isCompressed = "yes".equals(properties.getProperty(EXistOutputKeys.COMPRESS_OUTPUT, "no"));
        try (final OutputStream osTempFile = tempFile.newOutputStream()) {
            // One for the local cached file
            Inflater dec = null;
            byte[] decResult = null;
            int decLength;
            if (isCompressed) {
                dec = new Inflater();
                decResult = new byte[65536];
                dec.setInput(data);
                do {
                    decLength = dec.inflate(decResult);
                    osTempFile.write(decResult, 0, decLength);
                    // And other for the stream where we want to save it!
                    if (os != null) {
                        os.write(decResult, 0, decLength);
                    }
                } while (decLength == decResult.length || !dec.needsInput());
            } else {
                osTempFile.write(data);
                // And other for the stream where we want to save it!
                if (os != null) {
                    os.write(data);
                }
            }
            while (offset > 0) {
                params.clear();
                params.add(table.get("handle"));
                params.add(useLongOffset ? Long.toString(offset) : Integer.valueOf((int) offset));
                table = (Map<?, ?>) collection.execute(method, params);
                offset = useLongOffset ? Long.parseLong((String) table.get("offset")) : ((Integer) table.get("offset"));
                data = (byte[]) table.get("data");
                // One for the local cached file
                if (isCompressed) {
                    dec.setInput(data);
                    do {
                        decLength = dec.inflate(decResult);
                        osTempFile.write(decResult, 0, decLength);
                        // And other for the stream where we want to save it!
                        if (os != null) {
                            os.write(decResult, 0, decLength);
                        }
                    } while (decLength == decResult.length || !dec.needsInput());
                } else {
                    osTempFile.write(data);
                    // And other for the stream where we want to save it!
                    if (os != null) {
                        os.write(data);
                    }
                }
            }
            if (dec != null) {
                dec.end();
            }
        }
        contentFile = tempFile;
    } catch (final IOException | DataFormatException e) {
        throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
    }
}
Also used : VirtualTempPath(org.exist.util.io.VirtualTempPath) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) XMLDBException(org.xmldb.api.base.XMLDBException) TemporaryFileManager(org.exist.util.io.TemporaryFileManager) DataFormatException(java.util.zip.DataFormatException) Inflater(java.util.zip.Inflater)

Aggregations

VirtualTempPath (org.exist.util.io.VirtualTempPath)3 XMLDBException (org.xmldb.api.base.XMLDBException)3 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 DataFormatException (java.util.zip.DataFormatException)2 Inflater (java.util.zip.Inflater)2 OutputStreamWriter (java.io.OutputStreamWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Properties (java.util.Properties)1 TransformerException (javax.xml.transform.TransformerException)1 UnsynchronizedByteArrayOutputStream (org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)1 TemporaryFileManager (org.exist.util.io.TemporaryFileManager)1 DOMSerializer (org.exist.util.serializer.DOMSerializer)1