Search in sources :

Example 36 with UIContext

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);
}
Also used : UIContext(com.github.bordertech.wcomponents.UIContext)

Example 37 with UIContext

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);
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) UIContext(com.github.bordertech.wcomponents.UIContext)

Example 38 with UIContext

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);
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) StringWriter(java.io.StringWriter) UIContext(com.github.bordertech.wcomponents.UIContext) DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory) StringReader(java.io.StringReader) Mustache(com.github.mustachejava.Mustache) PrintWriter(java.io.PrintWriter) DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory) MustacheFactory(com.github.mustachejava.MustacheFactory)

Example 39 with UIContext

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();
        }
    }
}
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 40 with UIContext

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

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