Search in sources :

Example 1 with ZipArchiveBackupDescriptor

use of org.exist.backup.ZipArchiveBackupDescriptor in project exist by eXist-db.

the class RetrieveBackup method eval.

public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    if (!context.getEffectiveUser().hasDbaRole()) {
        throw new XPathException("You must be a DBA to retrieve a backup");
    }
    final String exportDir = args[0].getStringValue();
    Path dir = Paths.get(exportDir);
    if (!dir.isAbsolute()) {
        dir = ((Path) context.getBroker().getConfiguration().getProperty(BrokerPool.PROPERTY_DATA_DIR)).resolve(exportDir);
    }
    final String name = args[1].getStringValue();
    final Path backupFile = dir.resolve(name);
    if (!Files.isReadable(backupFile)) {
        return (Sequence.EMPTY_SEQUENCE);
    }
    if (!name.endsWith(".zip")) {
        throw (new XPathException(this, "for security reasons, the function only allows " + "reading zipped backup archives"));
    }
    try {
        final ZipArchiveBackupDescriptor descriptor = new ZipArchiveBackupDescriptor(backupFile);
        final Properties properties = descriptor.getProperties();
        if ((properties == null) || (properties.isEmpty())) {
            throw (new XPathException(this, "the file does not see to be a valid backup archive"));
        }
    } catch (final IOException e) {
        throw (new XPathException(this, "the file does not see to be a valid backup archive"));
    }
    // directly stream the backup contents to the HTTP response
    final Optional<ResponseWrapper> maybeResponse = Optional.ofNullable(context.getHttpContext()).map(XQueryContext.HttpContext::getResponse);
    if (!maybeResponse.isPresent()) {
        throw (new XPathException(this, "No response object found in the current XQuery context."));
    }
    final ResponseWrapper response = maybeResponse.get();
    if (!"org.exist.http.servlets.HttpResponseWrapper".equals(response.getClass().getName())) {
        throw new XPathException(this, signature.toString() + " can only be used within the EXistServlet or XQueryServlet");
    }
    response.setContentType("application/zip");
    response.setHeader("Content-Length", String.valueOf(FileUtils.sizeQuietly(backupFile)));
    try {
        try (final OutputStream os = response.getOutputStream()) {
            Files.copy(backupFile, os);
        }
        response.flushBuffer();
    } catch (final IOException e) {
        throw (new XPathException(this, "An IO error occurred while reading the backup archive"));
    }
    return (Sequence.EMPTY_SEQUENCE);
}
Also used : Path(java.nio.file.Path) ZipArchiveBackupDescriptor(org.exist.backup.ZipArchiveBackupDescriptor) OutputStream(java.io.OutputStream) ResponseWrapper(org.exist.http.servlets.ResponseWrapper) IOException(java.io.IOException) Properties(java.util.Properties)

Example 2 with ZipArchiveBackupDescriptor

use of org.exist.backup.ZipArchiveBackupDescriptor in project exist by eXist-db.

the class ListBackups method eval.

public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    if (!context.getEffectiveUser().hasDbaRole()) {
        throw new XPathException("You must be a DBA to list available backups");
    }
    final String exportDir = args[0].getStringValue();
    Path dir = Paths.get(exportDir);
    if (!dir.isAbsolute()) {
        dir = ((Path) context.getBroker().getConfiguration().getProperty(BrokerPool.PROPERTY_DATA_DIR)).resolve(exportDir);
    }
    context.pushDocumentContext();
    try {
        final MemTreeBuilder builder = context.getDocumentBuilder();
        final int nodeNr = builder.startElement(DIRECTORY_ELEMENT, null);
        if (Files.isDirectory(dir) && Files.isReadable(dir)) {
            final Pattern pattern = Pattern.compile(BackupDirectory.FILE_REGEX);
            final Matcher matcher = pattern.matcher("");
            final List<Path> files = FileUtils.list(dir);
            for (final Path file : files) {
                matcher.reset(FileUtils.fileName(file));
                if (matcher.matches()) {
                    final BackupDescriptor descriptor;
                    try {
                        if (FileUtils.fileName(file).endsWith(".zip")) {
                            descriptor = new ZipArchiveBackupDescriptor(file);
                        } else {
                            final Path descriptorFile = file.resolve("db").resolve(BackupDescriptor.COLLECTION_DESCRIPTOR);
                            descriptor = new FileSystemBackupDescriptor(file, descriptorFile);
                        }
                        final Properties properties = descriptor.getProperties();
                        if (properties != null) {
                            final AttributesImpl attrs = new AttributesImpl();
                            attrs.addAttribute("", "file", "file", "CDATA", FileUtils.fileName(file));
                            builder.startElement(BACKUP_ELEMENT, attrs);
                            for (final Object o : properties.keySet()) {
                                final String key = o.toString();
                                builder.startElement(new QName(key, Namespaces.EXIST_NS, ""), null);
                                builder.characters((String) properties.get(key));
                                builder.endElement();
                            }
                            builder.endElement();
                        }
                    } catch (final IOException e) {
                    }
                }
            }
        }
        builder.endElement();
        return (builder.getDocument().getNode(nodeNr));
    } catch (final IOException ioe) {
        throw new XPathException(this, ioe);
    } finally {
        context.popDocumentContext();
    }
}
Also used : Path(java.nio.file.Path) Pattern(java.util.regex.Pattern) ZipArchiveBackupDescriptor(org.exist.backup.ZipArchiveBackupDescriptor) Matcher(java.util.regex.Matcher) QName(org.exist.dom.QName) IOException(java.io.IOException) Properties(java.util.Properties) FileSystemBackupDescriptor(org.exist.backup.FileSystemBackupDescriptor) ZipArchiveBackupDescriptor(org.exist.backup.ZipArchiveBackupDescriptor) BackupDescriptor(org.exist.backup.BackupDescriptor) AttributesImpl(org.xml.sax.helpers.AttributesImpl) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) FileSystemBackupDescriptor(org.exist.backup.FileSystemBackupDescriptor)

Aggregations

IOException (java.io.IOException)2 Path (java.nio.file.Path)2 Properties (java.util.Properties)2 ZipArchiveBackupDescriptor (org.exist.backup.ZipArchiveBackupDescriptor)2 OutputStream (java.io.OutputStream)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 BackupDescriptor (org.exist.backup.BackupDescriptor)1 FileSystemBackupDescriptor (org.exist.backup.FileSystemBackupDescriptor)1 QName (org.exist.dom.QName)1 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)1 ResponseWrapper (org.exist.http.servlets.ResponseWrapper)1 AttributesImpl (org.xml.sax.helpers.AttributesImpl)1