Search in sources :

Example 1 with ContextManager

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);
        }
    }
}
Also used : PwmApplication(password.pwm.PwmApplication) HttpSession(javax.servlet.http.HttpSession) ContextManager(password.pwm.http.ContextManager) PwmSession(password.pwm.http.PwmSession)

Example 2 with ContextManager

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;
}
Also used : StoredConfigurationImpl(password.pwm.config.stored.StoredConfigurationImpl) ContextManager(password.pwm.http.ContextManager) PwmOperationalException(password.pwm.error.PwmOperationalException)

Example 3 with ContextManager

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);
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) ContextManager(password.pwm.http.ContextManager) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException)

Example 4 with ContextManager

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);
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) PwmException(password.pwm.error.PwmException) ErrorInformation(password.pwm.error.ErrorInformation) PwmApplication(password.pwm.PwmApplication) ContextManager(password.pwm.http.ContextManager) HttpServletResponse(javax.servlet.http.HttpServletResponse) PwmURL(password.pwm.http.PwmURL) PwmApplicationMode(password.pwm.PwmApplicationMode) ServletException(javax.servlet.ServletException) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmException(password.pwm.error.PwmException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 5 with ContextManager

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;
}
Also used : PwmException(password.pwm.error.PwmException) ConfigGuideBean(password.pwm.http.bean.ConfigGuideBean) ErrorInformation(password.pwm.error.ErrorInformation) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ContextManager(password.pwm.http.ContextManager) 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)

Aggregations

ContextManager (password.pwm.http.ContextManager)8 ErrorInformation (password.pwm.error.ErrorInformation)5 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)5 IOException (java.io.IOException)4 ServletException (javax.servlet.ServletException)4 PwmApplication (password.pwm.PwmApplication)4 PwmException (password.pwm.error.PwmException)4 ChaiUnavailableException (com.novell.ldapchai.exception.ChaiUnavailableException)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 StoredConfigurationImpl (password.pwm.config.stored.StoredConfigurationImpl)2 PwmOperationalException (password.pwm.error.PwmOperationalException)2 File (java.io.File)1 InputStream (java.io.InputStream)1 UnknownHostException (java.net.UnknownHostException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1 PwmApplicationMode (password.pwm.PwmApplicationMode)1