use of password.pwm.http.ContextManager in project pwm by pwm-project.
the class RequestInitializationFilter method checkAndInitSessionState.
private void checkAndInitSessionState(final HttpServletRequest request) throws PwmUnrecoverableException {
final ContextManager contextManager = ContextManager.getContextManager(request.getSession());
final PwmApplication pwmApplication = contextManager.getPwmApplication();
{
// destroy any outdated sessions
final HttpSession httpSession = request.getSession(false);
if (httpSession != null) {
final String sessionPwmAppNonce = (String) httpSession.getAttribute(PwmConstants.SESSION_ATTR_PWM_APP_NONCE);
if (sessionPwmAppNonce == null || !sessionPwmAppNonce.equals(pwmApplication.getRuntimeNonce())) {
LOGGER.debug("invalidating http session created with non-current servlet context");
httpSession.invalidate();
}
}
}
{
// handle pwmSession init and assignment.
final HttpSession httpSession = request.getSession();
if (httpSession.getAttribute(PwmConstants.SESSION_ATTR_PWM_SESSION) == null) {
final PwmSession pwmSession = PwmSession.createPwmSession(pwmApplication);
PwmSessionWrapper.sessionMerge(pwmApplication, pwmSession, httpSession);
}
}
}
use of password.pwm.http.ContextManager in project pwm by pwm-project.
the class ConfigGuideServlet method restSkipGuide.
@ActionHandler(action = "skipGuide")
private ProcessStatus restSkipGuide(final PwmRequest pwmRequest) throws PwmUnrecoverableException, IOException {
final Map<String, String> inputJson = pwmRequest.readBodyAsJsonStringMap(PwmHttpRequestWrapper.Flag.BypassValidation);
final String password = inputJson.get("password");
final ContextManager contextManager = ContextManager.getContextManager(pwmRequest);
try {
final StoredConfigurationImpl storedConfiguration = new StoredConfigurationImpl();
storedConfiguration.writeConfigProperty(ConfigurationProperty.CONFIG_IS_EDITABLE, "true");
storedConfiguration.setPassword(password);
ConfigGuideUtils.writeConfig(contextManager, storedConfiguration);
pwmRequest.outputJsonResult(RestResultBean.forSuccessMessage(pwmRequest, Message.Success_Unknown));
pwmRequest.invalidateSession();
} catch (PwmOperationalException e) {
LOGGER.error("error during skip config guide: " + e.getMessage(), e);
}
return ProcessStatus.Halt;
}
use of password.pwm.http.ContextManager in project pwm by pwm-project.
the class RequestInitializationFilter method respondWithUnavailableError.
private void respondWithUnavailableError(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_APP_UNAVAILABLE);
try {
final ContextManager contextManager = ContextManager.getContextManager(req.getServletContext());
if (contextManager != null && contextManager.getStartupErrorInformation() != null) {
errorInformation = contextManager.getStartupErrorInformation();
}
} catch (PwmUnrecoverableException e2) {
LOGGER.error("error reading session context from servlet container: " + e2.getMessage());
}
req.setAttribute(PwmRequestAttribute.PwmErrorInfo.toString(), errorInformation);
final String url = JspUrl.APP_UNAVAILABLE.getPath();
req.getServletContext().getRequestDispatcher(url).forward(req, resp);
}
use of password.pwm.http.ContextManager in project pwm by pwm-project.
the class RequestInitializationFilter method doFilter.
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 PwmURL pwmURL = new PwmURL(req);
PwmApplication testPwmApplicationLoad = null;
try {
testPwmApplicationLoad = ContextManager.getPwmApplication(req);
} catch (PwmException e) {
}
if (testPwmApplicationLoad != null && mode == PwmApplicationMode.RUNNING) {
if (testPwmApplicationLoad.getStatisticsManager() != null) {
testPwmApplicationLoad.getStatisticsManager().updateEps(EpsStatistic.REQUESTS, 1);
}
}
if (testPwmApplicationLoad == null && pwmURL.isResourceURL()) {
filterChain.doFilter(req, resp);
} else if (pwmURL.isRestService()) {
filterChain.doFilter(req, resp);
} else {
if (mode == PwmApplicationMode.ERROR) {
try {
final ContextManager contextManager = ContextManager.getContextManager(req.getServletContext());
if (contextManager != null) {
final ErrorInformation startupError = contextManager.getStartupErrorInformation();
servletRequest.setAttribute(PwmRequestAttribute.PwmErrorInfo.toString(), startupError);
}
} catch (Exception e) {
if (pwmURL.isResourceURL()) {
filterChain.doFilter(servletRequest, servletResponse);
return;
}
LOGGER.error("error while trying to detect application status: " + e.getMessage());
}
LOGGER.error("unable to satisfy incoming request, application is not available");
resp.setStatus(500);
final String url = JspUrl.APP_UNAVAILABLE.getPath();
servletRequest.getServletContext().getRequestDispatcher(url).forward(servletRequest, servletResponse);
} else {
initializeServletRequest(req, resp, filterChain);
}
}
}
use of password.pwm.http.ContextManager in project pwm by pwm-project.
the class ConfigGuideServlet method restGotoStep.
@ActionHandler(action = "gotoStep")
private ProcessStatus restGotoStep(final PwmRequest pwmRequest) throws PwmUnrecoverableException, IOException, ServletException {
final ConfigGuideBean configGuideBean = getBean(pwmRequest);
final String requestedStep = pwmRequest.readParameterAsString("step");
GuideStep step = GuideStep.START;
if (requestedStep != null && requestedStep.length() > 0) {
try {
step = GuideStep.valueOf(requestedStep);
} catch (IllegalArgumentException e) {
final String errorMsg = "unknown goto step request: " + requestedStep;
LOGGER.error(pwmRequest, errorMsg);
}
}
if (step == GuideStep.START) {
configGuideBean.getFormData().clear();
configGuideBean.getFormData().putAll(ConfigGuideForm.defaultForm());
} else if (step == GuideStep.NEXT) {
step = configGuideBean.getStep().next();
while (step != GuideStep.FINISH && !step.visible(configGuideBean)) {
step = step.next();
}
} else if (step == GuideStep.PREVIOUS) {
step = configGuideBean.getStep().previous();
while (step != GuideStep.START && !step.visible(configGuideBean)) {
step = step.previous();
}
}
if (step == GuideStep.FINISH) {
final ContextManager contextManager = ContextManager.getContextManager(pwmRequest);
try {
ConfigGuideUtils.writeConfig(contextManager, configGuideBean);
pwmRequest.getPwmSession().getSessionStateBean().setTheme(null);
} catch (PwmException e) {
final RestResultBean restResultBean = RestResultBean.fromError(e.getErrorInformation(), pwmRequest);
pwmRequest.outputJsonResult(restResultBean);
return ProcessStatus.Halt;
} catch (Exception e) {
final RestResultBean restResultBean = RestResultBean.fromError(new ErrorInformation(PwmError.ERROR_UNKNOWN, "error during save: " + e.getMessage()), pwmRequest);
pwmRequest.outputJsonResult(restResultBean);
return ProcessStatus.Halt;
}
final HashMap<String, String> resultData = new HashMap<>();
resultData.put("serverRestart", "true");
pwmRequest.outputJsonResult(RestResultBean.withData(resultData));
pwmRequest.invalidateSession();
} else {
configGuideBean.setStep(step);
pwmRequest.outputJsonResult(RestResultBean.forSuccessMessage(pwmRequest, Message.Success_Unknown));
LOGGER.trace("setting current step to: " + step);
}
return ProcessStatus.Continue;
}
Aggregations