use of password.pwm.util.localdb.LocalDBUtility in project pwm by pwm-project.
the class ExportLocalDBCommand method doCommand.
@Override
void doCommand() throws Exception {
final LocalDB localDB = cliEnvironment.getLocalDB();
final File outputFile = (File) cliEnvironment.getOptions().get(CliParameters.REQUIRED_NEW_OUTPUT_FILE.getName());
if (outputFile.exists()) {
out("outputFile for exportLocalDB cannot already exist");
return;
}
final LocalDBUtility localDBUtility = new LocalDBUtility(localDB);
try (FileOutputStream fileOutputStream = new FileOutputStream(outputFile)) {
localDBUtility.exportLocalDB(fileOutputStream, System.out, true);
} catch (PwmOperationalException e) {
out("error during export: " + e.getMessage());
}
}
use of password.pwm.util.localdb.LocalDBUtility in project pwm by pwm-project.
the class ImportLocalDBCommand method doCommand.
@Override
void doCommand() throws Exception {
final LocalDB localDB = cliEnvironment.getLocalDB();
final String msg = "Proceeding with this operation will clear ALL data from the LocalDB." + "\n" + "Please consider backing up the LocalDB before proceeding. " + "\n" + "\n" + "The application must be stopped for this operation to succeed.";
if (!promptForContinue(msg)) {
out("exiting...");
return;
}
final LocalDBUtility pwmDBUtility = new LocalDBUtility(localDB);
final File inputFile = (File) cliEnvironment.getOptions().get(CliParameters.REQUIRED_EXISTING_INPUT_FILE.getName());
try {
pwmDBUtility.importLocalDB(inputFile, System.out);
} catch (PwmOperationalException e) {
out("error during import: " + e.getMessage());
}
}
use of password.pwm.util.localdb.LocalDBUtility in project pwm by pwm-project.
the class ConfigManagerLocalDBServlet method restUploadLocalDB.
void restUploadLocalDB(final PwmRequest pwmRequest) throws IOException, ServletException, PwmUnrecoverableException {
final PwmApplication pwmApplication = pwmRequest.getPwmApplication();
final HttpServletRequest req = pwmRequest.getHttpServletRequest();
if (pwmApplication.getApplicationMode() == PwmApplicationMode.RUNNING) {
final String errorMsg = "database upload is not permitted when in running mode";
final ErrorInformation errorInformation = new ErrorInformation(PwmError.CONFIG_UPLOAD_FAILURE, errorMsg, new String[] { errorMsg });
pwmRequest.respondWithError(errorInformation, true);
return;
}
if (!ServletFileUpload.isMultipartContent(req)) {
final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_UNKNOWN, "no file found in upload");
pwmRequest.outputJsonResult(RestResultBean.fromError(errorInformation, pwmRequest));
LOGGER.error(pwmRequest, "error during database import: " + errorInformation.toDebugStr());
return;
}
final InputStream inputStream = pwmRequest.readFileUploadStream(PwmConstants.PARAM_FILE_UPLOAD);
final ContextManager contextManager = ContextManager.getContextManager(pwmRequest);
LocalDB localDB = null;
try {
final File localDBLocation = pwmApplication.getLocalDB().getFileLocation();
final Configuration configuration = pwmApplication.getConfig();
contextManager.shutdown();
localDB = LocalDBFactory.getInstance(localDBLocation, false, null, configuration);
final LocalDBUtility localDBUtility = new LocalDBUtility(localDB);
LOGGER.info(pwmRequest, "beginning LocalDB import");
localDBUtility.importLocalDB(inputStream, LOGGER.asAppendable(PwmLogLevel.DEBUG, pwmRequest.getSessionLabel()));
LOGGER.info(pwmRequest, "completed LocalDB import");
} catch (Exception e) {
final ErrorInformation errorInformation = e instanceof PwmException ? ((PwmException) e).getErrorInformation() : new ErrorInformation(PwmError.ERROR_UNKNOWN, e.getMessage());
pwmRequest.outputJsonResult(RestResultBean.fromError(errorInformation, pwmRequest));
LOGGER.error(pwmRequest, "error during LocalDB import: " + errorInformation.toDebugStr());
return;
} finally {
if (localDB != null) {
try {
localDB.close();
} catch (Exception e) {
LOGGER.error(pwmRequest, "error closing LocalDB after import process: " + e.getMessage());
}
}
contextManager.initialize();
}
pwmRequest.outputJsonResult(RestResultBean.forSuccessMessage(pwmRequest, Message.Success_Unknown));
}
use of password.pwm.util.localdb.LocalDBUtility in project pwm by pwm-project.
the class ConfigManagerLocalDBServlet method doExportLocalDB.
private void doExportLocalDB(final PwmRequest pwmRequest) throws IOException, ServletException, PwmUnrecoverableException {
final PwmResponse resp = pwmRequest.getPwmResponse();
final Instant startTime = Instant.now();
resp.setHeader(HttpHeader.ContentDisposition, "attachment;filename=" + PwmConstants.PWM_APP_NAME + "-LocalDB.bak");
resp.setContentType(HttpContentType.octetstream);
resp.setHeader(HttpHeader.ContentTransferEncoding, "binary");
final LocalDBUtility localDBUtility = new LocalDBUtility(pwmRequest.getPwmApplication().getLocalDB());
try {
final int bufferSize = Integer.parseInt(pwmRequest.getConfig().readAppProperty(AppProperty.HTTP_DOWNLOAD_BUFFER_SIZE));
final OutputStream bos = new BufferedOutputStream(resp.getOutputStream(), bufferSize);
localDBUtility.exportLocalDB(bos, LOGGER.asAppendable(PwmLogLevel.DEBUG, pwmRequest.getSessionLabel()), true);
LOGGER.debug(pwmRequest, "completed localDBExport process in " + TimeDuration.fromCurrent(startTime).asCompactString());
} catch (Exception e) {
LOGGER.error(pwmRequest, "error downloading export localdb: " + e.getMessage());
}
}
Aggregations