Search in sources :

Example 6 with ContextManager

use of password.pwm.http.ContextManager 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 7 with ContextManager

use of password.pwm.http.ContextManager in project pwm by pwm-project.

the class ConfigManagerServlet method saveConfiguration.

public static void saveConfiguration(final PwmRequest pwmRequest, final StoredConfigurationImpl storedConfiguration) throws PwmUnrecoverableException {
    {
        final List<String> errorStrings = storedConfiguration.validateValues();
        if (errorStrings != null && !errorStrings.isEmpty()) {
            final String errorString = errorStrings.get(0);
            throw new PwmUnrecoverableException(new ErrorInformation(PwmError.CONFIG_FORMAT_ERROR, null, new String[] { errorString }));
        }
    }
    try {
        final ContextManager contextManager = ContextManager.getContextManager(pwmRequest.getHttpServletRequest().getSession().getServletContext());
        contextManager.getConfigReader().saveConfiguration(storedConfiguration, contextManager.getPwmApplication(), pwmRequest.getSessionLabel());
        final PwmApplication pwmApplication = pwmRequest.getPwmApplication();
        if (pwmApplication.getAuditManager() != null && pwmApplication.getAuditManager().status() == PwmService.STATUS.OPEN) {
            final String modifyMessage = "Configuration Changes: " + storedConfiguration.changeLogAsDebugString(PwmConstants.DEFAULT_LOCALE, false);
            final AuditRecord auditRecord = new AuditRecordFactory(pwmApplication).createUserAuditRecord(AuditEvent.MODIFY_CONFIGURATION, pwmRequest.getUserInfoIfLoggedIn(), pwmRequest.getSessionLabel(), modifyMessage);
            pwmApplication.getAuditManager().submit(auditRecord);
        }
        contextManager.requestPwmApplicationRestart();
    } catch (Exception e) {
        final String errorString = "error saving file: " + e.getMessage();
        LOGGER.error(pwmRequest, errorString);
        throw new PwmUnrecoverableException(new ErrorInformation(PwmError.CONFIG_FORMAT_ERROR, null, new String[] { errorString }));
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) AuditRecordFactory(password.pwm.svc.event.AuditRecordFactory) PwmApplication(password.pwm.PwmApplication) ContextManager(password.pwm.http.ContextManager) List(java.util.List) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) AuditRecord(password.pwm.svc.event.AuditRecord) ServletException(javax.servlet.ServletException) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) ChaiUnavailableException(com.novell.ldapchai.exception.ChaiUnavailableException) PwmException(password.pwm.error.PwmException) IOException(java.io.IOException)

Example 8 with ContextManager

use of password.pwm.http.ContextManager in project pwm by pwm-project.

the class ConfigManagerServlet method readCurrentConfiguration.

public static StoredConfigurationImpl readCurrentConfiguration(final PwmRequest pwmRequest) throws PwmUnrecoverableException {
    final ContextManager contextManager = ContextManager.getContextManager(pwmRequest.getHttpServletRequest().getSession());
    final ConfigurationReader runningConfigReader = contextManager.getConfigReader();
    final StoredConfigurationImpl runningConfig = runningConfigReader.getStoredConfiguration();
    return StoredConfigurationImpl.copy(runningConfig);
}
Also used : StoredConfigurationImpl(password.pwm.config.stored.StoredConfigurationImpl) ContextManager(password.pwm.http.ContextManager) ConfigurationReader(password.pwm.config.stored.ConfigurationReader)

Aggregations

ContextManager (password.pwm.http.ContextManager)8 ErrorInformation (password.pwm.error.ErrorInformation)5 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)5 IOException (java.io.IOException)4 ServletException (javax.servlet.ServletException)4 PwmApplication (password.pwm.PwmApplication)4 PwmException (password.pwm.error.PwmException)4 ChaiUnavailableException (com.novell.ldapchai.exception.ChaiUnavailableException)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 StoredConfigurationImpl (password.pwm.config.stored.StoredConfigurationImpl)2 PwmOperationalException (password.pwm.error.PwmOperationalException)2 File (java.io.File)1 InputStream (java.io.InputStream)1 UnknownHostException (java.net.UnknownHostException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1 PwmApplicationMode (password.pwm.PwmApplicationMode)1