Search in sources :

Example 11 with TemporaryFileManager

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

the class RepoBackup method backup.

public static Path backup(final DBBroker broker) throws IOException {
    final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
    final Path tempFile = temporaryFileManager.getTemporaryFile();
    try (final ZipOutputStream os = new ZipOutputStream(Files.newOutputStream(tempFile))) {
        final Path directory = ExistRepository.getRepositoryDir(broker.getConfiguration());
        zipDir(directory.toAbsolutePath(), os, "");
    }
    return tempFile;
}
Also used : Path(java.nio.file.Path) ZipOutputStream(java.util.zip.ZipOutputStream) TemporaryFileManager(org.exist.util.io.TemporaryFileManager)

Example 12 with TemporaryFileManager

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

Example 13 with TemporaryFileManager

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

the class XMLDBStore method loadFromURI.

private Resource loadFromURI(final Collection collection, final URI uri, final String docName, final MimeType mimeType) throws XPathException {
    Resource resource;
    if ("file".equals(uri.getScheme())) {
        final String path = uri.getPath();
        if (path == null) {
            throw new XPathException(this, "Cannot read from URI: " + uri.toASCIIString());
        }
        final Path file = Paths.get(path);
        if (!Files.isReadable(file)) {
            throw new XPathException(this, "Cannot read path: " + path);
        }
        resource = loadFromFile(collection, file, docName, mimeType);
    } else {
        final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
        Path temp = null;
        try {
            temp = temporaryFileManager.getTemporaryFile();
            try (final InputStream is = uri.toURL().openStream()) {
                Files.copy(is, temp);
                resource = loadFromFile(collection, temp, docName, mimeType);
            } finally {
                if (temp != null) {
                    temporaryFileManager.returnTemporaryFile(temp);
                }
            }
        } catch (final MalformedURLException e) {
            throw new XPathException(this, "Malformed URL: " + uri.toString(), e);
        } catch (final IOException e) {
            throw new XPathException(this, "IOException while reading from URL: " + uri.toString(), e);
        }
    }
    return resource;
}
Also used : Path(java.nio.file.Path) MalformedURLException(java.net.MalformedURLException) XPathException(org.exist.xquery.XPathException) InputStream(java.io.InputStream) BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) IOException(java.io.IOException) TemporaryFileManager(org.exist.util.io.TemporaryFileManager)

Example 14 with TemporaryFileManager

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

the class RestXqServiceRegistryPersistence method getRegistryFile.

private Optional<Path> getRegistryFile(final boolean temp) {
    try (final DBBroker broker = getBrokerPool().getBroker()) {
        final Configuration configuration = broker.getConfiguration();
        final Path dataDir = (Path) configuration.getProperty(BrokerPool.PROPERTY_DATA_DIR);
        final Path registryFile;
        if (temp) {
            final TemporaryFileManager temporaryFileManager = TemporaryFileManager.getInstance();
            registryFile = temporaryFileManager.getTemporaryFile();
        } else {
            registryFile = dataDir.resolve(REGISTRY_FILENAME);
        }
        return Optional.of(registryFile);
    } catch (final EXistException | IOException e) {
        log.error(e.getMessage(), e);
        return Optional.empty();
    }
}
Also used : Path(java.nio.file.Path) DBBroker(org.exist.storage.DBBroker) Configuration(org.exist.util.Configuration) EXistException(org.exist.EXistException) IOException(java.io.IOException) 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