Search in sources :

Example 86 with UIContext

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

the class UIContextDumpInterceptor method paint.

/**
 * Paints the component.
 *
 * @param renderContext the renderContext to send the output to.
 */
@Override
public void paint(final RenderContext renderContext) {
    boolean enabled = ConfigurationProperties.getDeveloperDumpUIContext();
    super.paint(renderContext);
    if (enabled) {
        UIContext uic = UIContextHolder.getCurrent();
        // We want to dump the shared WComponent tree (UIC) first, so that all the
        // references from the UI context are shown as such
        Object[] treeAndSession = new Object[] { uic.getUI(), uic };
        try {
            ObjectGraphNode root = ObjectGraphDump.dump(treeAndSession);
            // We only want to dump the tree once
            boolean treeDumped = "true".equalsIgnoreCase((String) uic.getFwkAttribute(UICONTEXT_TREE_DUMPED_KEY));
            if (!treeDumped) {
                uic.setFwkAttribute(UICONTEXT_TREE_DUMPED_KEY, "true");
                // The UI will be the 1st child
                LOG.info("UI dump follows:");
                LOG.info(((ObjectGraphNode) root.getChildAt(0)).toXml());
            }
            // The UI context will be the 2nd child
            LOG.info("UI Context dump follows:");
            LOG.info(((ObjectGraphNode) root.getChildAt(1)).toXml());
        } catch (Exception e) {
            LOG.error("Failed to dump context", e);
        }
    }
}
Also used : UIContext(com.github.bordertech.wcomponents.UIContext) ObjectGraphNode(com.github.bordertech.wcomponents.util.ObjectGraphNode)

Example 87 with UIContext

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

the class WWindowInterceptor method serviceRequest.

/**
 * Temporarily replaces the environment while the request is being handled.
 *
 * @param request the request being responded to.
 */
@Override
public void serviceRequest(final Request request) {
    // Get window id off the request
    windowId = request.getParameter(WWindow.WWINDOW_REQUEST_PARAM_KEY);
    if (windowId == null) {
        super.serviceRequest(request);
    } else {
        // Get the window component
        ComponentWithContext target = WebUtilities.getComponentById(windowId, true);
        if (target == null) {
            throw new SystemException("No window component for id " + windowId);
        }
        // Setup the Environment on the context
        UIContext uic = UIContextHolder.getCurrentPrimaryUIContext();
        Environment originalEnvironment = uic.getEnvironment();
        uic.setEnvironment(new EnvironmentDelegate(originalEnvironment, windowId, target));
        if (attachWindow) {
            attachUI(target.getComponent());
        }
        UIContextHolder.pushContext(target.getContext());
        try {
            super.serviceRequest(request);
        } finally {
            uic.setEnvironment(originalEnvironment);
            UIContextHolder.popContext();
        }
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) UIContext(com.github.bordertech.wcomponents.UIContext) Environment(com.github.bordertech.wcomponents.Environment) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

Example 88 with UIContext

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

the class WrongStepAjaxInterceptor 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 trigger id
    triggerId = request.getParameter(WServlet.AJAX_TRIGGER_PARAM_NAME);
    if (triggerId == null) {
        throw new SystemException("No AJAX trigger id to check step count");
    }
    // Get trigger and its context
    ComponentWithContext trigger = AjaxHelper.getCurrentTriggerAndContext();
    if (trigger == null) {
        throw new IllegalStateException("No component/context available for AJAX trigger " + triggerId + ".");
    }
    // Get expected step count
    UIContext uic = UIContextHolder.getCurrent();
    int expected = uic.getEnvironment().getStep();
    // Step should already be set on the session
    if (expected == 0) {
        throw new SystemException("Step count should already be set on the session before AJAX request.");
    }
    // Get step count on the request
    int got = StepCountUtil.getRequestStep(request);
    // Check we are on the current step
    if (expected == got) {
        // Process Service Request
        getBackingComponent().serviceRequest(request);
    } else {
        // Invalid step
        LOG.warn("AJAX: Wrong step detected. Expected step " + expected + " but got step " + got);
        // "GET" Ajax requests are just ignored and return an error code
        if ("GET".equals(request.getMethod())) {
            LOG.warn("Error code will be sent in the response for AJAX GET Request.");
            handleErrorCode();
            // Make sure the render phase is not processed
            throw new ActionEscape();
        } else if (StepCountUtil.isErrorRedirect()) {
            // Redirect to error page
            LOG.warn("User will be redirected to an error page.");
            redirectUrl = StepCountUtil.getErrorUrl();
        } else {
            // Warp to the future by refreshing the page
            LOG.warn("Warp the user back to the future by refreshing the page.");
            handleWarpToTheFuture(uic);
            redirectUrl = buildApplicationUrl(uic);
        }
    }
}
Also used : ActionEscape(com.github.bordertech.wcomponents.ActionEscape) SystemException(com.github.bordertech.wcomponents.util.SystemException) UIContext(com.github.bordertech.wcomponents.UIContext) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

Example 89 with UIContext

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

the class WrongStepContentInterceptor 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();
    // Step should already be set on the session
    if (expected == 0) {
        throw new SystemException("Step count should already be set on the session before content request.");
    }
    // Get step count on the request
    int got = StepCountUtil.getRequestStep(request);
    // Check tokens match (both must be provided)
    if (expected == got) {
        // Process Service Request
        getBackingComponent().serviceRequest(request);
    } else if (!StepCountUtil.isStepOnRequest(request) && StepCountUtil.isCachedContentRequest(request)) {
        // cached content (no step on request)
        // Process Service Request
        getBackingComponent().serviceRequest(request);
    } else {
        // Invalid token
        // Set an error code
        LOG.warn("Wrong step detected for content request. Expected step [" + expected + "] but got step [" + got + "].");
        handleError();
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) UIContext(com.github.bordertech.wcomponents.UIContext)

Example 90 with UIContext

use of com.github.bordertech.wcomponents.UIContext 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)

Aggregations

UIContext (com.github.bordertech.wcomponents.UIContext)114 Test (org.junit.Test)47 WComponent (com.github.bordertech.wcomponents.WComponent)18 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)15 SystemException (com.github.bordertech.wcomponents.util.SystemException)14 WApplication (com.github.bordertech.wcomponents.WApplication)13 UIContextImpl (com.github.bordertech.wcomponents.UIContextImpl)11 WText (com.github.bordertech.wcomponents.WText)11 PrintWriter (java.io.PrintWriter)11 ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)10 SeleniumWComponentsWebDriver (com.github.bordertech.wcomponents.test.selenium.driver.SeleniumWComponentsWebDriver)9 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)7 DefaultWComponent (com.github.bordertech.wcomponents.DefaultWComponent)7 WDropdown (com.github.bordertech.wcomponents.WDropdown)7 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)7 StringWriter (java.io.StringWriter)7 AjaxOperation (com.github.bordertech.wcomponents.AjaxOperation)6 Environment (com.github.bordertech.wcomponents.Environment)6 MockWEnvironment (com.github.bordertech.wcomponents.MockWEnvironment)6 WButton (com.github.bordertech.wcomponents.WButton)6