Search in sources :

Example 21 with StoredValue

use of password.pwm.config.StoredValue in project pwm by pwm-project.

the class ConfigEditorServlet method restWriteSetting.

@ActionHandler(action = "writeSetting")
private ProcessStatus restWriteSetting(final PwmRequest pwmRequest) throws IOException, PwmUnrecoverableException {
    final ConfigManagerBean configManagerBean = getBean(pwmRequest);
    final StoredConfigurationImpl storedConfig = configManagerBean.getStoredConfiguration();
    final String key = pwmRequest.readParameterAsString("key");
    final String bodyString = pwmRequest.readRequestBodyAsString();
    final PwmSetting setting = PwmSetting.forKey(key);
    final LinkedHashMap<String, Object> returnMap = new LinkedHashMap<>();
    final UserIdentity loggedInUser = pwmRequest.getPwmSession().isAuthenticated() ? pwmRequest.getPwmSession().getUserInfo().getUserIdentity() : null;
    if (key.startsWith("localeBundle")) {
        final StringTokenizer st = new StringTokenizer(key, "-");
        st.nextToken();
        final PwmLocaleBundle bundleName = PwmLocaleBundle.valueOf(st.nextToken());
        final String keyName = st.nextToken();
        final Map<String, String> valueMap = JsonUtil.deserializeStringMap(bodyString);
        final Map<String, String> outputMap = new LinkedHashMap<>(valueMap);
        storedConfig.writeLocaleBundleMap(bundleName.getTheClass().getName(), keyName, outputMap);
        returnMap.put("isDefault", outputMap.isEmpty());
        returnMap.put("key", key);
    } else {
        final String profileID = setting.getCategory().hasProfiles() ? pwmRequest.readParameterAsString("profile") : null;
        try {
            final StoredValue storedValue = ValueFactory.fromJson(setting, bodyString);
            final List<String> errorMsgs = storedValue.validateValue(setting);
            if (errorMsgs != null && !errorMsgs.isEmpty()) {
                returnMap.put("errorMessage", setting.getLabel(pwmRequest.getLocale()) + ": " + errorMsgs.get(0));
            }
            storedConfig.writeSetting(setting, profileID, storedValue, loggedInUser);
        } catch (Exception e) {
            final String errorMsg = "error writing default value for setting " + setting.toString() + ", error: " + e.getMessage();
            LOGGER.error(errorMsg, e);
            throw new IllegalStateException(errorMsg, e);
        }
        returnMap.put("key", key);
        returnMap.put("category", setting.getCategory().toString());
        returnMap.put("syntax", setting.getSyntax().toString());
        returnMap.put("isDefault", storedConfig.isDefaultValue(setting, profileID));
    }
    pwmRequest.outputJsonResult(RestResultBean.withData(returnMap));
    return ProcessStatus.Halt;
}
Also used : StoredConfigurationImpl(password.pwm.config.stored.StoredConfigurationImpl) PwmLocaleBundle(password.pwm.i18n.PwmLocaleBundle) UserIdentity(password.pwm.bean.UserIdentity) StoredValue(password.pwm.config.StoredValue) 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) LinkedHashMap(java.util.LinkedHashMap) PwmSetting(password.pwm.config.PwmSetting) ConfigManagerBean(password.pwm.http.bean.ConfigManagerBean) StringTokenizer(java.util.StringTokenizer)

Aggregations

StoredValue (password.pwm.config.StoredValue)21 PwmSetting (password.pwm.config.PwmSetting)11 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)4 LinkedHashMap (java.util.LinkedHashMap)4 PwmSettingCategory (password.pwm.config.PwmSettingCategory)4 StoredConfigurationImpl (password.pwm.config.stored.StoredConfigurationImpl)4 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)4 List (java.util.List)3 TreeMap (java.util.TreeMap)3 StringValue (password.pwm.config.value.StringValue)3 PwmException (password.pwm.error.PwmException)3 PwmOperationalException (password.pwm.error.PwmOperationalException)3 ChaiUnavailableException (com.novell.ldapchai.exception.ChaiUnavailableException)2 X509Certificate (java.security.cert.X509Certificate)2 ServletException (javax.servlet.ServletException)2 StringArrayValue (password.pwm.config.value.StringArrayValue)2 VerificationMethodValue (password.pwm.config.value.VerificationMethodValue)2 Serializable (java.io.Serializable)1 URISyntaxException (java.net.URISyntaxException)1