Search in sources :

Example 6 with LocalDB

use of password.pwm.util.localdb.LocalDB 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 7 with LocalDB

use of password.pwm.util.localdb.LocalDB in project pwm by pwm-project.

the class LocalDBInfoCommand method doCommand.

public void doCommand() throws Exception {
    final Instant startTime = Instant.now();
    final LocalDB localDB = cliEnvironment.getLocalDB();
    final long localDBdiskSpace = FileSystemUtility.getFileDirectorySize(localDB.getFileLocation());
    out("beginning LocalDBInfo");
    out("LocalDB total disk space = " + PwmNumberFormat.forDefaultLocale().format(localDBdiskSpace) + " (" + StringUtil.formatDiskSize(localDBdiskSpace) + ")");
    out("examining LocalDB, this may take a while.... ");
    for (final LocalDB.DB db : LocalDB.DB.values()) {
        out("---" + db.toString() + "---");
        final Map<LocalDBUtility.StatsKey, Object> stats = LocalDBUtility.dbStats(localDB, db);
        out(JsonUtil.serializeMap(stats, JsonUtil.Flag.PrettyPrint));
    }
    out("completed LocalDBInfo in " + TimeDuration.fromCurrent(startTime).asCompactString());
}
Also used : Instant(java.time.Instant) LocalDB(password.pwm.util.localdb.LocalDB)

Example 8 with LocalDB

use of password.pwm.util.localdb.LocalDB in project pwm by pwm-project.

the class ClearResponsesCommand method doCommand.

@Override
void doCommand() throws Exception {
    final String msg = "Proceeding with this operation will clear all stored responses from the LocalDB." + "\n" + "Please consider exporting the responses before proceeding. " + "\n" + "\n" + "The application must be stopped for this operation to succeed." + "\n";
    if (!promptForContinue(msg)) {
        return;
    }
    final LocalDB localDB = cliEnvironment.getLocalDB();
    if (localDB.size(LocalDB.DB.RESPONSE_STORAGE) == 0) {
        out("The LocalDB response database is already empty");
        return;
    }
    out("clearing " + localDB.size(LocalDB.DB.RESPONSE_STORAGE) + " responses");
    localDB.truncate(LocalDB.DB.RESPONSE_STORAGE);
    out("all saved responses are now removed from LocalDB");
}
Also used : LocalDB(password.pwm.util.localdb.LocalDB)

Example 9 with LocalDB

use of password.pwm.util.localdb.LocalDB 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 10 with LocalDB

use of password.pwm.util.localdb.LocalDB in project pwm by pwm-project.

the class AppDashboardData method makeLocalDbTableSizes.

private static Map<LocalDB.DB, String> makeLocalDbTableSizes(final PwmApplication pwmApplication, final Locale locale) {
    final Map<LocalDB.DB, String> returnData = new LinkedHashMap<>();
    final LocalDB localDB = pwmApplication.getLocalDB();
    final PwmNumberFormat numberFormat = PwmNumberFormat.forLocale(locale);
    try {
        for (final LocalDB.DB db : LocalDB.DB.values()) {
            returnData.put(db, numberFormat.format(localDB.size(db)));
        }
    } catch (LocalDBException e) {
        LOGGER.error("error making localDB size bean: " + e.getMessage());
    }
    return Collections.unmodifiableMap(returnData);
}
Also used : PwmNumberFormat(password.pwm.util.java.PwmNumberFormat) LocalDBException(password.pwm.util.localdb.LocalDBException) LocalDB(password.pwm.util.localdb.LocalDB) LocalDB(password.pwm.util.localdb.LocalDB) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

LocalDB (password.pwm.util.localdb.LocalDB)10 File (java.io.File)5 PwmApplication (password.pwm.PwmApplication)3 LocalDBUtility (password.pwm.util.localdb.LocalDBUtility)3 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Writer (java.io.Writer)2 Configuration (password.pwm.config.Configuration)2 PwmOperationalException (password.pwm.error.PwmOperationalException)2 LocalDBException (password.pwm.util.localdb.LocalDBException)2 ChaiUnavailableException (com.novell.ldapchai.exception.ChaiUnavailableException)1 InputStream (java.io.InputStream)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Logger (org.apache.log4j.Logger)1 ConfigurationReader (password.pwm.config.stored.ConfigurationReader)1