Search in sources :

Example 1 with ActionEscape

use of com.github.bordertech.wcomponents.ActionEscape in project wcomponents by BorderTech.

the class AbstractContainerHelper method processAction.

/**
 * Support standard processing of the action phase of a request.
 *
 * @throws IOException if there is an IO error on writing a response.
 */
public void processAction() throws IOException {
    if (isDisposed()) {
        LOG.error("Skipping action phase. Attempt to reuse disposed ContainerHelper instance");
        return;
    }
    try {
        // Check user context has been prepared
        if (getNewConversation() == null) {
            throw new IllegalStateException("User context has not been prepared before the action phase");
        }
        prepareAction();
        UIContext uic = getUIContext();
        if (uic == null) {
            throw new IllegalStateException("No user context set for the action phase.");
        }
        UIContextHolder.pushContext(uic);
        // Make sure maps are cleared up
        uic.clearScratchMap();
        uic.clearRequestScratchMap();
        prepareRequest();
        Request req = getRequest();
        getInterceptor().attachResponse(getResponse());
        getInterceptor().serviceRequest(req);
        if (req.isLogout()) {
            handleLogout();
            dispose();
        }
    } catch (ActionEscape esc) {
        LOG.debug("ActionEscape performed.");
        // Action escapes must be handled in the action phase and then
        // do nothing if they reach the render phase (which they will in
        // the servlet implementation)
        handleEscape(esc);
        dispose();
    } catch (Escape esc) {
        LOG.debug("Escape performed during action phase.");
    // We can't handle the escape until the render phase.
    } catch (SessionTokenException e) {
        // This session token exception can occur on a page submit action phase.
        // Do a warn level to create less noise in the error logs.
        LOG.warn(e.getMessage());
        propogateError(e);
    } catch (Throwable t) {
        // We try not to let any exception propagate to container.
        String message = "Caught exception during action phase.";
        LOG.error(message, t);
        // We can't handle the error until the render phase.
        propogateError(t);
    } finally {
        UIContextHolder.reset();
    }
}
Also used : Escape(com.github.bordertech.wcomponents.Escape) ActionEscape(com.github.bordertech.wcomponents.ActionEscape) ActionEscape(com.github.bordertech.wcomponents.ActionEscape) UIContext(com.github.bordertech.wcomponents.UIContext) Request(com.github.bordertech.wcomponents.Request)

Example 2 with ActionEscape

use of com.github.bordertech.wcomponents.ActionEscape in project wcomponents by BorderTech.

the class WrongStepContentInterceptor_Test method sendContentRequest.

/**
 * Utility method to send a mock request to the application.
 *
 * @param target the target content.
 * @param clientStep the client-side step count
 * @param serverStep the server-side step count
 * @return the response.
 */
private MockResponse sendContentRequest(final WComponent target, final int clientStep, final int serverStep) {
    UIContext uic = createUIContext();
    uic.setUI(WebUtilities.getTop(target));
    WServlet.WServletEnvironment env = new WServlet.WServletEnvironment("/app", "http://localhost", "");
    env.setStep(serverStep);
    uic.setEnvironment(env);
    setActiveContext(uic);
    MockRequest request = new MockRequest();
    request.setParameter(Environment.TARGET_ID, target.getId());
    request.setParameter(Environment.STEP_VARIABLE, String.valueOf(clientStep));
    MockResponse response = new MockResponse();
    InterceptorComponent interceptor = new WrongStepContentInterceptor();
    interceptor.attachUI(uic.getUI());
    interceptor.attachResponse(response);
    // Handle the request. This will either return the content or a step error
    try {
        interceptor.serviceRequest(request);
        interceptor.preparePaint(request);
        interceptor.paint(new WebXmlRenderContext(response.getWriter()));
    } catch (ContentEscape escape) {
        try {
            // Content has been returned
            escape.setRequest(request);
            escape.setResponse(response);
            escape.escape();
        } catch (IOException e) {
            Assert.fail("Failed to write content");
        }
    } catch (ErrorCodeEscape escape) {
        try {
            escape.setRequest(request);
            escape.setResponse(response);
            escape.escape();
        } catch (IOException e) {
            Assert.fail("Failed to write error content");
        }
    } catch (ActionEscape ignored) {
    // don't care
    }
    // Step error
    return response;
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) ActionEscape(com.github.bordertech.wcomponents.ActionEscape) UIContext(com.github.bordertech.wcomponents.UIContext) ContentEscape(com.github.bordertech.wcomponents.ContentEscape) IOException(java.io.IOException) WServlet(com.github.bordertech.wcomponents.servlet.WServlet) ErrorCodeEscape(com.github.bordertech.wcomponents.ErrorCodeEscape) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest)

Example 3 with ActionEscape

use of com.github.bordertech.wcomponents.ActionEscape in project wcomponents by BorderTech.

the class SafetyContainer method handleRequest.

/**
 * Override handleRequest in order to safely process the example component, which has been excluded from normal
 * WComponent processing.
 *
 * @param request the request being responded to.
 */
@Override
public void handleRequest(final Request request) {
    if (!isInitialised()) {
        getOrCreateComponentModel().delegate = new SafetyContainerDelegate(UIContextHolder.getCurrent());
        setInitialised(true);
    }
    try {
        UIContext delegate = getComponentModel().delegate;
        UIContextHolder.pushContext(delegate);
        try {
            for (int i = 0; i < shim.getChildCount(); i++) {
                shim.getChildAt(i).serviceRequest(request);
            }
            delegate.doInvokeLaters();
        } finally {
            UIContextHolder.popContext();
        }
    } catch (final ActionEscape e) {
        // We don't want to catch ActionEscapes (e.g. ForwardExceptions)
        throw e;
    } catch (final Exception e) {
        if (isAjaxOrTargetedRequest(request)) {
            throw new SystemException(e.getMessage(), e);
        } else {
            setAttribute(ERROR_KEY, e);
        }
    }
}
Also used : ActionEscape(com.github.bordertech.wcomponents.ActionEscape) SystemException(com.github.bordertech.wcomponents.util.SystemException) UIContext(com.github.bordertech.wcomponents.UIContext) SystemException(com.github.bordertech.wcomponents.util.SystemException)

Example 4 with ActionEscape

use of com.github.bordertech.wcomponents.ActionEscape in project wcomponents by BorderTech.

the class WrongStepServerInterceptor method serviceRequest.

/**
 * Override to check whether the step variable in the incoming request matches what we expect.
 *
 * @param request the request being serviced.
 */
@Override
public void serviceRequest(final Request request) {
    // Get expected step count
    UIContext uic = UIContextHolder.getCurrent();
    int expected = uic.getEnvironment().getStep();
    // Get step count from the request
    int got = StepCountUtil.getRequestStep(request);
    // or no Step count and processing a GET
    if (expected == got || (!StepCountUtil.isStepOnRequest(request) && "GET".equals(request.getMethod()))) {
        // Process Service Request
        getBackingComponent().serviceRequest(request);
    } else {
        // Invalid step
        LOG.warn("SERVER: Wrong step detected. Expected step " + expected + " but got step " + got);
        // Redirect to error page
        if (StepCountUtil.isErrorRedirect()) {
            String url = StepCountUtil.getErrorUrl();
            LOG.warn("User will be redirected to an error page. URL: " + url);
            try {
                getResponse().sendRedirect(url);
            } catch (IOException e) {
                LOG.warn("Error trying to redirect for wrong step indicator.");
            }
            // Make sure the render phase is not processed
            throw new ActionEscape();
        } else {
            // Warp to the future
            // Call handle step error
            WComponent application = getUI();
            if (application instanceof WApplication) {
                LOG.warn("The handleStepError method will be called on WApplication.");
                ((WApplication) application).handleStepError();
            }
            LOG.warn("The render phase will warp the user back to the future.");
        }
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) ActionEscape(com.github.bordertech.wcomponents.ActionEscape) UIContext(com.github.bordertech.wcomponents.UIContext) WApplication(com.github.bordertech.wcomponents.WApplication) IOException(java.io.IOException)

Example 5 with ActionEscape

use of com.github.bordertech.wcomponents.ActionEscape in project wcomponents by BorderTech.

the class WrongStepAjaxInterceptor_Test method doAjaxRequest.

/**
 * Does an AJAX request for the app.
 *
 * @param app the MyApp instance to do an AJAX request for.
 * @param clientStep the client-side step counter
 * @param serverStep the server-side step counter
 * @return the response object.
 */
private MockResponse doAjaxRequest(final MyApp app, final int clientStep, final int serverStep) {
    UIContext uic = createUIContext();
    WServlet.WServletEnvironment env = new WServlet.WServletEnvironment(APP_POSTPATH, "http://localhost", "");
    env.setStep(serverStep);
    env.setSessionToken("T");
    uic.setEnvironment(env);
    uic.setUI(app);
    setActiveContext(uic);
    MockRequest request = new MockRequest();
    MockResponse response = new MockResponse();
    // Create interceptors
    AjaxSetupInterceptor ajaxSetupInterceptor = new AjaxSetupInterceptor();
    WrongStepAjaxInterceptor wrongStepInterceptor = new WrongStepAjaxInterceptor();
    AjaxPageShellInterceptor ajaxPageInterceptor = new AjaxPageShellInterceptor();
    AjaxInterceptor ajaxInterceptor = new AjaxInterceptor();
    ajaxPageInterceptor.setBackingComponent(ajaxInterceptor);
    wrongStepInterceptor.setBackingComponent(ajaxPageInterceptor);
    ajaxSetupInterceptor.setBackingComponent(wrongStepInterceptor);
    ajaxSetupInterceptor.attachUI(app);
    ajaxSetupInterceptor.attachResponse(response);
    // Action phase
    try {
        AjaxHelper.registerComponent(app.target.getId(), app.trigger.getId());
        request.setParameter(WServlet.AJAX_TRIGGER_PARAM_NAME, app.trigger.getId());
        request.setParameter(Environment.STEP_VARIABLE, String.valueOf(clientStep));
        ajaxSetupInterceptor.serviceRequest(request);
        ajaxSetupInterceptor.preparePaint(request);
        // Render phase
        ajaxSetupInterceptor.paint(new WebXmlRenderContext(response.getWriter()));
    } catch (ActionEscape ignored) {
    // is thrown to skip render phase
    }
    return response;
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) ActionEscape(com.github.bordertech.wcomponents.ActionEscape) UIContext(com.github.bordertech.wcomponents.UIContext) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) WServlet(com.github.bordertech.wcomponents.servlet.WServlet)

Aggregations

ActionEscape (com.github.bordertech.wcomponents.ActionEscape)6 UIContext (com.github.bordertech.wcomponents.UIContext)6 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)3 MockResponse (com.github.bordertech.wcomponents.util.mock.MockResponse)3 WServlet (com.github.bordertech.wcomponents.servlet.WServlet)2 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)2 IOException (java.io.IOException)2 ContentEscape (com.github.bordertech.wcomponents.ContentEscape)1 ErrorCodeEscape (com.github.bordertech.wcomponents.ErrorCodeEscape)1 Escape (com.github.bordertech.wcomponents.Escape)1 MockWEnvironment (com.github.bordertech.wcomponents.MockWEnvironment)1 Request (com.github.bordertech.wcomponents.Request)1 WApplication (com.github.bordertech.wcomponents.WApplication)1 WComponent (com.github.bordertech.wcomponents.WComponent)1 SystemException (com.github.bordertech.wcomponents.util.SystemException)1 Test (org.junit.Test)1