Search in sources :

Example 1 with LocalDBUtility

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());
    }
}
Also used : LocalDBUtility(password.pwm.util.localdb.LocalDBUtility) FileOutputStream(java.io.FileOutputStream) LocalDB(password.pwm.util.localdb.LocalDB) File(java.io.File) PwmOperationalException(password.pwm.error.PwmOperationalException)

Example 2 with LocalDBUtility

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());
    }
}
Also used : LocalDBUtility(password.pwm.util.localdb.LocalDBUtility) LocalDB(password.pwm.util.localdb.LocalDB) File(java.io.File) PwmOperationalException(password.pwm.error.PwmOperationalException)

Example 3 with LocalDBUtility

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));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) PwmException(password.pwm.error.PwmException) ErrorInformation(password.pwm.error.ErrorInformation) PwmApplication(password.pwm.PwmApplication) Configuration(password.pwm.config.Configuration) LocalDBUtility(password.pwm.util.localdb.LocalDBUtility) InputStream(java.io.InputStream) ContextManager(password.pwm.http.ContextManager) LocalDB(password.pwm.util.localdb.LocalDB) File(java.io.File) ServletException(javax.servlet.ServletException) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmException(password.pwm.error.PwmException) IOException(java.io.IOException) ChaiUnavailableException(com.novell.ldapchai.exception.ChaiUnavailableException)

Example 4 with LocalDBUtility

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());
    }
}
Also used : LocalDBUtility(password.pwm.util.localdb.LocalDBUtility) PwmResponse(password.pwm.http.PwmResponse) Instant(java.time.Instant) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ServletException(javax.servlet.ServletException) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmException(password.pwm.error.PwmException) IOException(java.io.IOException) ChaiUnavailableException(com.novell.ldapchai.exception.ChaiUnavailableException)

Aggregations

LocalDBUtility (password.pwm.util.localdb.LocalDBUtility)4 File (java.io.File)3 LocalDB (password.pwm.util.localdb.LocalDB)3 ChaiUnavailableException (com.novell.ldapchai.exception.ChaiUnavailableException)2 IOException (java.io.IOException)2 ServletException (javax.servlet.ServletException)2 PwmException (password.pwm.error.PwmException)2 PwmOperationalException (password.pwm.error.PwmOperationalException)2 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)2 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Instant (java.time.Instant)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 PwmApplication (password.pwm.PwmApplication)1 Configuration (password.pwm.config.Configuration)1 ErrorInformation (password.pwm.error.ErrorInformation)1 ContextManager (password.pwm.http.ContextManager)1 PwmResponse (password.pwm.http.PwmResponse)1