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);
}
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();
}
}
Aggregations