use of password.pwm.http.PwmRequest in project pwm by pwm-project.
the class AbstractPwmFilter method doFilter.
@Override
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException {
final HttpServletRequest req = (HttpServletRequest) servletRequest;
final HttpServletResponse resp = (HttpServletResponse) servletResponse;
final PwmApplicationMode mode = PwmApplicationMode.determineMode(req);
final boolean interested;
try {
final PwmURL pwmURL = new PwmURL(req);
interested = isInterested(mode, pwmURL);
} catch (Exception e) {
LOGGER.error("unexpected error processing filter chain during isInterested(): " + e.getMessage(), e);
resp.sendError(500, "unexpected error processing filter chain during isInterested");
return;
}
if (interested) {
PwmRequest pwmRequest = null;
try {
pwmRequest = PwmRequest.forRequest(req, resp);
} catch (PwmException e) {
final PwmURL pwmURL = new PwmURL(req);
if (pwmURL.isResourceURL()) {
filterChain.doFilter(req, resp);
return;
}
LOGGER.error(pwmRequest, "unexpected error processing filter chain: " + e.getMessage(), e);
}
try {
final PwmFilterChain pwmFilterChain = new PwmFilterChain(servletRequest, servletResponse, filterChain);
processFilter(mode, pwmRequest, pwmFilterChain);
} catch (PwmException e) {
LOGGER.error(pwmRequest, "unexpected error processing filter chain: " + e.getMessage(), e);
} catch (IOException e) {
LOGGER.debug(pwmRequest, "i/o error processing request: " + e.getMessage());
}
} else {
filterChain.doFilter(req, resp);
}
}
use of password.pwm.http.PwmRequest in project pwm by pwm-project.
the class RequestInitializationFilter method initializeServletRequest.
private void initializeServletRequest(final HttpServletRequest req, final HttpServletResponse resp, final FilterChain filterChain) throws IOException, ServletException {
try {
checkAndInitSessionState(req);
PwmRequest.forRequest(req, resp);
} catch (Throwable e) {
LOGGER.error("can't load application: " + e.getMessage(), e);
if (!(new PwmURL(req).isResourceURL())) {
respondWithUnavailableError(req, resp);
return;
}
return;
}
try {
final PwmRequest pwmRequest = PwmRequest.forRequest(req, resp);
checkIfSessionRecycleNeeded(pwmRequest);
handleRequestInitialization(pwmRequest);
addPwmResponseHeaders(pwmRequest);
checkIdleTimeout(pwmRequest);
try {
handleRequestSecurityChecks(pwmRequest);
} catch (PwmUnrecoverableException e) {
LOGGER.error(pwmRequest, e.getErrorInformation());
pwmRequest.respondWithError(e.getErrorInformation());
if (PwmError.ERROR_INTRUDER_SESSION != e.getError()) {
pwmRequest.invalidateSession();
}
return;
}
} catch (Throwable e) {
final String logMsg = "can't init request: " + e.getMessage();
if (e instanceof PwmException && ((PwmException) e).getError() != PwmError.ERROR_UNKNOWN) {
LOGGER.error(logMsg);
} else {
LOGGER.error(logMsg, e);
}
if (!(new PwmURL(req).isResourceURL())) {
respondWithUnavailableError(req, resp);
return;
}
return;
}
filterChain.doFilter(req, resp);
}
use of password.pwm.http.PwmRequest in project pwm by pwm-project.
the class ConfigEditorServlet method restSearchSettings.
@ActionHandler(action = "search")
private ProcessStatus restSearchSettings(final PwmRequest pwmRequest) throws IOException, PwmUnrecoverableException {
final Instant startTime = Instant.now();
final ConfigManagerBean configManagerBean = getBean(pwmRequest);
final String bodyData = pwmRequest.readRequestBodyAsString();
final Map<String, String> valueMap = JsonUtil.deserializeStringMap(bodyData);
final Locale locale = pwmRequest.getLocale();
final RestResultBean restResultBean;
final String searchTerm = valueMap.get("search");
final StoredConfigurationImpl storedConfiguration = configManagerBean.getStoredConfiguration();
if (searchTerm != null && !searchTerm.isEmpty()) {
final ArrayList<StoredConfigurationImpl.ConfigRecordID> searchResults = new ArrayList<>(configManagerBean.getStoredConfiguration().search(searchTerm, locale));
final ConcurrentHashMap<String, Map<String, SearchResultItem>> returnData = new ConcurrentHashMap<>();
searchResults.parallelStream().filter(recordID -> recordID.getRecordType() == StoredConfigurationImpl.ConfigRecordID.RecordType.SETTING).forEach(recordID -> {
final PwmSetting setting = (PwmSetting) recordID.getRecordID();
final SearchResultItem item = new SearchResultItem(setting.getCategory().toString(), storedConfiguration.readSetting(setting, recordID.getProfileID()).toDebugString(locale), setting.getCategory().toMenuLocationDebug(recordID.getProfileID(), locale), storedConfiguration.isDefaultValue(setting, recordID.getProfileID()), recordID.getProfileID());
final String returnCategory = item.getNavigation();
returnData.putIfAbsent(returnCategory, new ConcurrentHashMap<>());
returnData.get(returnCategory).put(setting.getKey(), item);
});
final TreeMap<String, Map<String, SearchResultItem>> outputMap = new TreeMap<>();
for (final String key : returnData.keySet()) {
outputMap.put(key, new TreeMap<>(returnData.get(key)));
}
restResultBean = RestResultBean.withData(outputMap);
LOGGER.trace(pwmRequest, "finished search operation with " + returnData.size() + " results in " + TimeDuration.fromCurrent(startTime).asCompactString());
} else {
restResultBean = RestResultBean.withData(new ArrayList<StoredConfigurationImpl.ConfigRecordID>());
}
pwmRequest.outputJsonResult(restResultBean);
return ProcessStatus.Halt;
}
use of password.pwm.http.PwmRequest 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;
}
Aggregations