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;
}
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;
}
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;
}
Aggregations