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);
}
}
}
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();
}
}
Aggregations