Search in sources :

Example 61 with PwmException

use of password.pwm.error.PwmException in project pwm by pwm-project.

the class ConfigEditorServlet method restExecuteSettingFunction.

@ActionHandler(action = "executeSettingFunction")
private ProcessStatus restExecuteSettingFunction(final PwmRequest pwmRequest) throws IOException, PwmUnrecoverableException {
    final ConfigManagerBean configManagerBean = getBean(pwmRequest);
    final String bodyString = pwmRequest.readRequestBodyAsString();
    final Map<String, String> requestMap = JsonUtil.deserializeStringMap(bodyString);
    final PwmSetting pwmSetting = PwmSetting.forKey(requestMap.get("setting"));
    final String functionName = requestMap.get("function");
    final String profileID = pwmSetting.getCategory().hasProfiles() ? pwmRequest.readParameterAsString("profile") : null;
    final String extraData = requestMap.get("extraData");
    try {
        final Class implementingClass = Class.forName(functionName);
        final SettingUIFunction function = (SettingUIFunction) implementingClass.newInstance();
        final Serializable result = function.provideFunction(pwmRequest, configManagerBean.getStoredConfiguration(), pwmSetting, profileID, extraData);
        final RestResultBean restResultBean = RestResultBean.forSuccessMessage(result, pwmRequest, Message.Success_Unknown);
        pwmRequest.outputJsonResult(restResultBean);
    } catch (Exception e) {
        final RestResultBean restResultBean;
        if (e instanceof PwmException) {
            restResultBean = RestResultBean.fromError(((PwmException) e).getErrorInformation(), pwmRequest, true);
        } else {
            final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_UNKNOWN, "error performing user search: " + e.getMessage());
            restResultBean = RestResultBean.fromError(errorInformation, pwmRequest);
        }
        pwmRequest.outputJsonResult(restResultBean);
    }
    return ProcessStatus.Halt;
}
Also used : PwmSetting(password.pwm.config.PwmSetting) PwmException(password.pwm.error.PwmException) ConfigManagerBean(password.pwm.http.bean.ConfigManagerBean) ErrorInformation(password.pwm.error.ErrorInformation) Serializable(java.io.Serializable) SettingUIFunction(password.pwm.config.SettingUIFunction) ServletException(javax.servlet.ServletException) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmOperationalException(password.pwm.error.PwmOperationalException) ChaiUnavailableException(com.novell.ldapchai.exception.ChaiUnavailableException) PwmException(password.pwm.error.PwmException) IOException(java.io.IOException) RestResultBean(password.pwm.ws.server.RestResultBean)

Example 62 with PwmException

use of password.pwm.error.PwmException in project pwm by pwm-project.

the class ConfigEditorServletUtils method readFileUploadToSettingValue.

public static FileValue readFileUploadToSettingValue(final PwmRequest pwmRequest, final int maxFileSize) throws PwmUnrecoverableException, IOException, ServletException {
    final Map<String, PwmRequest.FileUploadItem> fileUploads;
    try {
        fileUploads = pwmRequest.readFileUploads(maxFileSize, 1);
    } catch (PwmException e) {
        pwmRequest.outputJsonResult(RestResultBean.fromError(e.getErrorInformation(), pwmRequest));
        LOGGER.error(pwmRequest, "error during file upload: " + e.getErrorInformation().toDebugStr());
        return null;
    } catch (Throwable e) {
        final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_UNKNOWN, "error during file upload: " + e.getMessage());
        pwmRequest.outputJsonResult(RestResultBean.fromError(errorInformation, pwmRequest));
        LOGGER.error(pwmRequest, errorInformation);
        return null;
    }
    if (fileUploads.containsKey(PwmConstants.PARAM_FILE_UPLOAD)) {
        final PwmRequest.FileUploadItem uploadItem = fileUploads.get(PwmConstants.PARAM_FILE_UPLOAD);
        final Map<FileValue.FileInformation, FileValue.FileContent> newFileValueMap = new LinkedHashMap<>();
        newFileValueMap.put(new FileValue.FileInformation(uploadItem.getName(), uploadItem.getType()), new FileValue.FileContent(uploadItem.getContent()));
        return new FileValue(newFileValueMap);
    }
    final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_UNKNOWN, "no file found in upload");
    pwmRequest.outputJsonResult(RestResultBean.fromError(errorInformation, pwmRequest));
    LOGGER.error(pwmRequest, "error during file upload: " + errorInformation.toDebugStr());
    return null;
}
Also used : FileValue(password.pwm.config.value.FileValue) PwmRequest(password.pwm.http.PwmRequest) LinkedHashMap(java.util.LinkedHashMap) PwmException(password.pwm.error.PwmException) ErrorInformation(password.pwm.error.ErrorInformation)

Example 63 with PwmException

use of password.pwm.error.PwmException in project pwm by pwm-project.

the class ConfigEditorServlet method doUploadFile.

@ActionHandler(action = "uploadFile")
private ProcessStatus doUploadFile(final PwmRequest pwmRequest) throws PwmUnrecoverableException, IOException, ServletException {
    final ConfigManagerBean configManagerBean = getBean(pwmRequest);
    final String key = pwmRequest.readParameterAsString("key");
    final PwmSetting setting = PwmSetting.forKey(key);
    final int maxFileSize = Integer.parseInt(pwmRequest.getConfig().readAppProperty(AppProperty.CONFIG_MAX_JDBC_JAR_SIZE));
    if (setting == PwmSetting.HTTPS_CERT) {
        try {
            final PasswordData passwordData = pwmRequest.readParameterAsPassword("password");
            final String alias = pwmRequest.readParameterAsString("alias");
            final HttpsServerCertificateManager.KeyStoreFormat keyStoreFormat;
            try {
                keyStoreFormat = HttpsServerCertificateManager.KeyStoreFormat.valueOf(pwmRequest.readParameterAsString("format"));
            } catch (IllegalArgumentException e) {
                throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_MISSING_PARAMETER, "unknown format type: " + e.getMessage(), new String[] { "format" }));
            }
            final Map<String, PwmRequest.FileUploadItem> fileUploads = pwmRequest.readFileUploads(maxFileSize, 1);
            final ByteArrayInputStream fileIs = new ByteArrayInputStream(fileUploads.get(PwmConstants.PARAM_FILE_UPLOAD).getContent().getBytes());
            HttpsServerCertificateManager.importKey(configManagerBean.getStoredConfiguration(), keyStoreFormat, fileIs, passwordData, alias);
            pwmRequest.outputJsonResult(RestResultBean.forSuccessMessage(pwmRequest, Message.Success_Unknown));
            return ProcessStatus.Halt;
        } catch (PwmException e) {
            LOGGER.error(pwmRequest, "error during https certificate upload: " + e.getMessage());
            pwmRequest.respondWithError(e.getErrorInformation(), false);
            return ProcessStatus.Halt;
        }
    }
    final FileValue fileValue = ConfigEditorServletUtils.readFileUploadToSettingValue(pwmRequest, maxFileSize);
    if (fileValue != null) {
        final UserIdentity userIdentity = pwmRequest.isAuthenticated() ? pwmRequest.getPwmSession().getUserInfo().getUserIdentity() : null;
        configManagerBean.getStoredConfiguration().writeSetting(setting, fileValue, userIdentity);
        pwmRequest.outputJsonResult(RestResultBean.forSuccessMessage(pwmRequest, Message.Success_Unknown));
    }
    return ProcessStatus.Halt;
}
Also used : FileValue(password.pwm.config.value.FileValue) UserIdentity(password.pwm.bean.UserIdentity) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) HttpsServerCertificateManager(password.pwm.util.secure.HttpsServerCertificateManager) PwmSetting(password.pwm.config.PwmSetting) PwmException(password.pwm.error.PwmException) ConfigManagerBean(password.pwm.http.bean.ConfigManagerBean) ErrorInformation(password.pwm.error.ErrorInformation) PasswordData(password.pwm.util.PasswordData) ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

PwmException (password.pwm.error.PwmException)63 ErrorInformation (password.pwm.error.ErrorInformation)42 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)38 IOException (java.io.IOException)19 PwmOperationalException (password.pwm.error.PwmOperationalException)19 PwmApplication (password.pwm.PwmApplication)16 ChaiUnavailableException (com.novell.ldapchai.exception.ChaiUnavailableException)13 UserIdentity (password.pwm.bean.UserIdentity)13 RestResultBean (password.pwm.ws.server.RestResultBean)13 ServletException (javax.servlet.ServletException)12 LinkedHashMap (java.util.LinkedHashMap)9 PwmSession (password.pwm.http.PwmSession)9 Instant (java.time.Instant)8 TimeDuration (password.pwm.util.java.TimeDuration)8 MacroMachine (password.pwm.util.macro.MacroMachine)8 Configuration (password.pwm.config.Configuration)7 PwmRequest (password.pwm.http.PwmRequest)7 UserInfo (password.pwm.ldap.UserInfo)7 PasswordData (password.pwm.util.PasswordData)7 ArrayList (java.util.ArrayList)6