use of org.exist.backup.restore.listener.RestoreListener in project exist by eXist-db.
the class SystemExportFiltersTest method exportImport.
@Test
public void exportImport() throws Exception {
Path file;
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
List<String> filters = new ArrayList<>();
filters.add(FilterForBackup.class.getName());
broker.getConfiguration().setProperty(SystemExport.CONFIG_FILTERS, filters);
final Collection test = broker.getCollection(TEST_COLLECTION_URI);
assertNotNull(test);
boolean direct = true;
final SystemExport sysexport = new SystemExport(broker, transaction, null, null, direct);
final Path backupDir = tempFolder.newFolder().toPath();
file = sysexport.export(backupDir.toAbsolutePath().toString(), false, false, null);
transaction.commit();
}
TestUtils.cleanupDB();
final SystemImport restore = new SystemImport(pool);
final RestoreListener listener = new LogRestoreListener();
restore.restore(TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD, null, file, listener);
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
final Collection test = broker.getCollection(TEST_COLLECTION_URI);
assertNotNull(test);
DocumentImpl doc = getDoc(broker, test, doc01uri.lastSegment());
assertEquals(XML1_BACKUP, serializer(broker, doc));
doc = getDoc(broker, test, doc02uri.lastSegment());
assertEquals(XML2_PROPER, serializer(broker, doc));
doc = getDoc(broker, test, doc03uri.lastSegment());
assertEquals(XML3_PROPER, serializer(broker, doc));
doc = getDoc(broker, test, doc11uri.lastSegment());
assertTrue(doc instanceof BinaryDocument);
try (final InputStream is = broker.getBinaryResource(transaction, ((BinaryDocument) doc))) {
assertEquals(BINARY, InputStreamUtil.readString(is, UTF_8));
}
transaction.commit();
}
}
use of org.exist.backup.restore.listener.RestoreListener in project exist by eXist-db.
the class SystemExportImportTest method exportImport.
@Test
public void exportImport() throws EXistException, IOException, PermissionDeniedException, SAXException, ParserConfigurationException, AuthenticationException, URISyntaxException, XMLDBException {
Path file;
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
final Collection test = broker.getCollection(TEST_COLLECTION_URI);
assertNotNull(test);
final SystemExport sysexport = new SystemExport(broker, transaction, null, null, direct);
final String backupDir = temporaryFolder.newFolder().getAbsolutePath();
file = sysexport.export(backupDir, false, zip, null);
transaction.commit();
}
clean();
final SystemImport restore = new SystemImport(pool);
final RestoreListener listener = new LogRestoreListener();
restore.restore(TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD, null, file, listener);
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
final Collection test = broker.getCollection(TEST_COLLECTION_URI);
assertNotNull(test);
DocumentImpl doc = getDoc(broker, test, doc01uri.lastSegment());
assertEquals(XML1, serializer(broker, doc));
doc = getDoc(broker, test, doc02uri.lastSegment());
assertEquals(XML2_PROPER, serializer(broker, doc));
doc = getDoc(broker, test, doc03uri.lastSegment());
assertEquals(XML3_PROPER, serializer(broker, doc));
doc = getDoc(broker, test, doc11uri.lastSegment());
assertTrue(doc instanceof BinaryDocument);
try (final InputStream is = broker.getBinaryResource(transaction, ((BinaryDocument) doc))) {
assertEquals(BINARY, InputStreamUtil.readString(is, UTF_8));
}
transaction.commit();
}
}
use of org.exist.backup.restore.listener.RestoreListener in project exist by eXist-db.
the class FnImport method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
if (!context.getSubject().hasDbaRole()) {
throw (new XPathException(this, "Permission denied, calling user '" + context.getSubject().getName() + "' must be a DBA to kill a running xquery"));
}
final String dirOrFile = args[0].getStringValue();
String adminPass = null;
if (args[1].hasOne()) {
adminPass = args[1].getStringValue();
}
String adminPassAfter = null;
if (args[2].hasOne()) {
adminPassAfter = args[2].getStringValue();
}
MemTreeBuilder builder = null;
context.pushDocumentContext();
try {
if (NAME.equals(getName())) {
builder = context.getDocumentBuilder();
builder.startDocument();
builder.startElement(IMPORT_ELEMENT, null);
}
try {
final SystemImport restore = new SystemImport(context.getDatabase());
final RestoreListener listener = new XMLRestoreListener(builder);
restore.restore(org.exist.security.SecurityManager.DBA_USER, adminPass, adminPassAfter, Paths.get(dirOrFile), listener);
} catch (final Exception e) {
throw new XPathException(this, "restore failed with exception: " + e.getMessage(), e);
}
if (builder == null) {
return Sequence.EMPTY_SEQUENCE;
} else {
builder.endElement();
builder.endDocument();
return (NodeValue) builder.getDocument().getDocumentElement();
}
} finally {
if (builder != null) {
context.popDocumentContext();
}
}
}
use of org.exist.backup.restore.listener.RestoreListener in project exist by eXist-db.
the class Restore method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
final String dirOrFile = args[0].getStringValue();
String adminPass = null;
if (args[1].hasOne()) {
adminPass = args[1].getStringValue();
}
String adminPassAfter = null;
if (args[2].hasOne()) {
adminPassAfter = args[2].getStringValue();
}
final boolean overwriteApps = args.length == 4 && args[3].effectiveBooleanValue();
context.pushDocumentContext();
try {
final MemTreeBuilder builder = context.getDocumentBuilder();
builder.startDocument();
builder.startElement(RESTORE_ELEMENT, null);
final BrokerPool pool = context.getBroker().getBrokerPool();
try {
final Subject admin = pool.getSecurityManager().authenticate(SecurityManager.DBA_USER, adminPass);
try (final DBBroker broker = pool.get(Optional.of(admin));
final Txn transaction = broker.continueOrBeginTransaction()) {
final RestoreListener listener = new XMLRestoreListener(builder);
final org.exist.backup.Restore restore = new org.exist.backup.Restore();
restore.restore(broker, transaction, adminPassAfter, Paths.get(dirOrFile), listener, overwriteApps);
transaction.commit();
}
} catch (final Exception e) {
throw new XPathException(this, "restore failed with exception: " + e.getMessage(), e);
}
builder.endElement();
builder.endDocument();
return (NodeValue) builder.getDocument().getDocumentElement();
} finally {
context.popDocumentContext();
}
}
Aggregations