use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class SessionTokenInterceptor method preparePaint.
/**
* {@inheritDoc}
*/
@Override
public void preparePaint(final Request request) {
// Set session token
UIContext uic = UIContextHolder.getCurrent();
if (uic.getEnvironment().getSessionToken() == null) {
uic.getEnvironment().setSessionToken(UUID.randomUUID().toString());
}
super.preparePaint(request);
}
use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class SessionTokenInterceptor method serviceRequest.
/**
* Override to check whether the session token variable in the incoming request matches what we expect.
*
* @param request the request being serviced.
*/
@Override
public void serviceRequest(final Request request) {
// Get the expected session token
UIContext uic = UIContextHolder.getCurrent();
String expected = uic.getEnvironment().getSessionToken();
// Get the session token from the request
String got = request.getParameter(Environment.SESSION_TOKEN_VARIABLE);
// or processing a GET and no token
if (Util.equals(expected, got) || (got == null && "GET".equals(request.getMethod()))) {
// Process request
getBackingComponent().serviceRequest(request);
} else {
// Invalid token
String msg;
if (expected == null && got != null) {
msg = "Session for token [" + got + "] is no longer valid or timed out.";
} else {
msg = "Wrong session token detected for servlet request. Expected token [" + expected + "] but got token [" + got + "].";
}
LOG.error(msg);
String message = I18nUtilities.format(uic.getLocale(), InternalMessages.DEFAULT_SESSION_TOKEN_ERROR);
throw new SystemException(message);
}
}
use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class TemplateRenderInterceptor method paint.
/**
* {@inheritDoc}
*/
@Override
public void paint(final RenderContext renderContext) {
UIContext uic = UIContextHolder.getCurrent();
// Generate the HTML
StringWriter outputBuffer = new StringWriter();
PrintWriter outputWriter = new PrintWriter(outputBuffer);
WebXmlRenderContext outputContext = new WebXmlRenderContext(outputWriter, uic.getLocale());
super.paint(outputContext);
String html = outputBuffer.toString();
// Only process TEMPLATE if has I18N brackets
if (html.contains("{{#i18n")) {
// Create a new instance of factory to avoid caching the page.
// https://github.com/spullara/mustache.java/issues/117
final MustacheFactory mf = new DefaultMustacheFactory();
Mustache mustache = mf.compile(new StringReader(html), UUID.randomUUID().toString());
StringWriter templateWriter = new StringWriter();
mustache.execute(templateWriter, new I18NContext(getResourceBundle()));
html = templateWriter.toString();
}
// Get the OUTPUT writer
WebXmlRenderContext webRenderContext = (WebXmlRenderContext) renderContext;
PrintWriter writer = webRenderContext.getWriter();
writer.print(html);
}
use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class WWindowInterceptor method preparePaint.
/**
* Temporarily replaces the environment while the UI prepares to render.
*
* @param request the request being responded to.
*/
@Override
public void preparePaint(final Request request) {
if (windowId == null) {
super.preparePaint(request);
} else {
// Get the window component
ComponentWithContext target = WebUtilities.getComponentById(windowId, true);
if (target == null) {
throw new SystemException("No window component for id " + windowId);
}
UIContext uic = UIContextHolder.getCurrentPrimaryUIContext();
Environment originalEnvironment = uic.getEnvironment();
uic.setEnvironment(new EnvironmentDelegate(originalEnvironment, windowId, target));
UIContextHolder.pushContext(target.getContext());
try {
super.preparePaint(request);
} finally {
uic.setEnvironment(originalEnvironment);
UIContextHolder.popContext();
}
}
}
use of com.github.bordertech.wcomponents.UIContext in project wcomponents by BorderTech.
the class WWindowInterceptor method paint.
/**
* Temporarily replaces the environment while the UI is being rendered.
*
* @param renderContext the context to render to.
*/
@Override
public void paint(final RenderContext renderContext) {
if (windowId == null) {
super.paint(renderContext);
} else {
// Get the window component
ComponentWithContext target = WebUtilities.getComponentById(windowId, true);
if (target == null) {
throw new SystemException("No window component for id " + windowId);
}
UIContext uic = UIContextHolder.getCurrentPrimaryUIContext();
Environment originalEnvironment = uic.getEnvironment();
uic.setEnvironment(new EnvironmentDelegate(originalEnvironment, windowId, target));
UIContextHolder.pushContext(target.getContext());
try {
super.paint(renderContext);
} finally {
uic.setEnvironment(originalEnvironment);
UIContextHolder.popContext();
}
}
}
Aggregations