Search in sources :

Example 21 with ComponentWithContext

use of com.github.bordertech.wcomponents.ComponentWithContext 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 22 with ComponentWithContext

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

the class StepCountUtil method isCachedContentRequest.

/**
 * Check if the request is for cached content.
 *
 * @param request the request being processed
 * @return true if content is cached, otherwise false
 */
public static boolean isCachedContentRequest(final Request request) {
    // Get target id on request
    String targetId = request.getParameter(Environment.TARGET_ID);
    if (targetId == null) {
        return false;
    }
    // Get target
    ComponentWithContext targetWithContext = WebUtilities.getComponentById(targetId, true);
    if (targetWithContext == null) {
        return false;
    }
    // Check for caching key
    WComponent target = targetWithContext.getComponent();
    UIContextHolder.pushContext(targetWithContext.getContext());
    try {
        // TODO Look at implementing CacheableTarget interface
        String key = null;
        if (target instanceof WContent) {
            key = ((WContent) target).getCacheKey();
        } else if (target instanceof WImage) {
            key = ((WImage) target).getCacheKey();
        } else if (target instanceof WVideo) {
            key = ((WVideo) target).getCacheKey();
        } else if (target instanceof WAudio) {
            key = ((WAudio) target).getCacheKey();
        }
        return !Util.empty(key);
    } finally {
        UIContextHolder.popContext();
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) WContent(com.github.bordertech.wcomponents.WContent) WVideo(com.github.bordertech.wcomponents.WVideo) WImage(com.github.bordertech.wcomponents.WImage) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext) WAudio(com.github.bordertech.wcomponents.WAudio)

Aggregations

ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)22 UIContext (com.github.bordertech.wcomponents.UIContext)10 SystemException (com.github.bordertech.wcomponents.util.SystemException)10 WComponent (com.github.bordertech.wcomponents.WComponent)8 AjaxOperation (com.github.bordertech.wcomponents.AjaxOperation)3 Environment (com.github.bordertech.wcomponents.Environment)3 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)3 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)3 ArrayList (java.util.ArrayList)3 VisitorResult (com.github.bordertech.wcomponents.util.WComponentTreeVisitor.VisitorResult)2 FindComponentByIdVisitor (com.github.bordertech.wcomponents.util.visitor.FindComponentByIdVisitor)2 Test (org.junit.Test)2 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)1 AjaxInternalTrigger (com.github.bordertech.wcomponents.AjaxInternalTrigger)1 WApplication (com.github.bordertech.wcomponents.WApplication)1 WAudio (com.github.bordertech.wcomponents.WAudio)1 WContent (com.github.bordertech.wcomponents.WContent)1 WImage (com.github.bordertech.wcomponents.WImage)1 WVideo (com.github.bordertech.wcomponents.WVideo)1 WWindow (com.github.bordertech.wcomponents.WWindow)1