Search in sources :

Example 1 with UIContext

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

the class AbstractContainerHelper method renderErrorPageToHTML.

/**
 * Render the error page component to HTML.
 *
 * @param errorPage the error page component
 * @return the error page as HTML
 */
protected String renderErrorPageToHTML(final WComponent errorPage) {
    // Check if using the default error page
    boolean defaultErrorPage = errorPage instanceof FatalErrorPage;
    String html = null;
    // If not default implementation of error page, Transform error page to HTML
    if (!defaultErrorPage) {
        // Set UIC and Environment (Needed for Theme Paths)
        UIContext uic = new UIContextImpl();
        uic.setEnvironment(createEnvironment());
        UIContextHolder.pushContext(uic);
        try {
            html = WebUtilities.renderWithTransformToHTML(errorPage);
        } catch (Exception e) {
            LOG.warn("Could not transform error page.", e);
        } finally {
            UIContextHolder.popContext();
        }
    }
    // Not transformed. So just render.
    if (html == null) {
        UIContextHolder.pushContext(new UIContextImpl());
        try {
            html = WebUtilities.render(errorPage);
        } catch (Exception e) {
            LOG.warn("Could not render error page.", e);
            html = "System error occurred but could not render error page.";
        } finally {
            UIContextHolder.popContext();
        }
    }
    return html;
}
Also used : FatalErrorPage(com.github.bordertech.wcomponents.FatalErrorPage) UIContext(com.github.bordertech.wcomponents.UIContext) UIContextImpl(com.github.bordertech.wcomponents.UIContextImpl) IOException(java.io.IOException) SystemException(com.github.bordertech.wcomponents.util.SystemException)

Example 2 with UIContext

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

the class AbstractContainerHelper method processAction.

/**
 * Support standard processing of the action phase of a request.
 *
 * @throws IOException if there is an IO error on writing a response.
 */
public void processAction() throws IOException {
    if (isDisposed()) {
        LOG.error("Skipping action phase. Attempt to reuse disposed ContainerHelper instance");
        return;
    }
    try {
        // Check user context has been prepared
        if (getNewConversation() == null) {
            throw new IllegalStateException("User context has not been prepared before the action phase");
        }
        prepareAction();
        UIContext uic = getUIContext();
        if (uic == null) {
            throw new IllegalStateException("No user context set for the action phase.");
        }
        UIContextHolder.pushContext(uic);
        // Make sure maps are cleared up
        uic.clearScratchMap();
        uic.clearRequestScratchMap();
        prepareRequest();
        Request req = getRequest();
        getInterceptor().attachResponse(getResponse());
        getInterceptor().serviceRequest(req);
        if (req.isLogout()) {
            handleLogout();
            dispose();
        }
    } catch (ActionEscape esc) {
        LOG.debug("ActionEscape performed.");
        // Action escapes must be handled in the action phase and then
        // do nothing if they reach the render phase (which they will in
        // the servlet implementation)
        handleEscape(esc);
        dispose();
    } catch (Escape esc) {
        LOG.debug("Escape performed during action phase.");
    // We can't handle the escape until the render phase.
    } catch (Throwable t) {
        // We try not to let any exception propagate to container.
        String message = "Caught exception during action phase.";
        LOG.error(message, t);
        // We can't handle the error until the render phase.
        propogateError(t);
    } finally {
        UIContextHolder.reset();
    }
}
Also used : Escape(com.github.bordertech.wcomponents.Escape) ActionEscape(com.github.bordertech.wcomponents.ActionEscape) ActionEscape(com.github.bordertech.wcomponents.ActionEscape) UIContext(com.github.bordertech.wcomponents.UIContext) Request(com.github.bordertech.wcomponents.Request)

Example 3 with UIContext

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

the class AbstractContainerHelper method render.

/**
 * Support standard processing of the render phase of a request.
 *
 * @throws IOException IO Exception
 */
public void render() throws IOException {
    if (isDisposed()) {
        LOG.debug("Skipping render phase.");
        return;
    }
    try {
        // Check user context has been prepared
        if (getNewConversation() == null) {
            throw new IllegalStateException("User context has not been prepared before the render phase");
        }
        prepareRender();
        UIContext uic = getUIContext();
        if (uic == null) {
            throw new IllegalStateException("No user context set for the render phase.");
        }
        UIContextHolder.pushContext(uic);
        prepareRequest();
        // Handle errors from the action phase now.
        if (havePropogatedError()) {
            handleError(getPropogatedError());
            return;
        }
        WComponent uiComponent = getUI();
        if (uiComponent == null) {
            throw new SystemException("No UI Component exists.");
        }
        Environment environment = uiComponent.getEnvironment();
        if (environment == null) {
            throw new SystemException("No WEnvironment exists.");
        }
        getInterceptor().attachResponse(getResponse());
        getInterceptor().preparePaint(getRequest());
        String contentType = getUI().getHeaders().getContentType();
        Response response = getResponse();
        response.setContentType(contentType);
        addGenericHeaders(uic, getUI());
        PrintWriter writer = getPrintWriter();
        getInterceptor().paint(new WebXmlRenderContext(writer, uic.getLocale()));
        // The following only matters for a Portal context
        String title = uiComponent instanceof WApplication ? ((WApplication) uiComponent).getTitle() : null;
        if (title != null) {
            setTitle(title);
        }
    } catch (Escape esc) {
        LOG.debug("Escape performed during render phase.");
        handleEscape(esc);
    } catch (Throwable t) {
        // We try not to let any exception propagate to container.
        String message = "Caught exception during render phase.";
        LOG.error(message, t);
        handleError(t);
    } finally {
        UIContextHolder.reset();
        dispose();
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) Response(com.github.bordertech.wcomponents.Response) WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) Escape(com.github.bordertech.wcomponents.Escape) ActionEscape(com.github.bordertech.wcomponents.ActionEscape) SystemException(com.github.bordertech.wcomponents.util.SystemException) UIContext(com.github.bordertech.wcomponents.UIContext) WApplication(com.github.bordertech.wcomponents.WApplication) Environment(com.github.bordertech.wcomponents.Environment) PrintWriter(java.io.PrintWriter)

Example 4 with UIContext

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

the class AbstractContainerHelper method prepareRequest.

/**
 * Prepare the session for the current request.
 */
protected void prepareRequest() {
    LOG.debug("Preparing for request by adding headers and environment to top wcomponent");
    // Configure the UIContext to handle this request.
    UIContext uiContext = getUIContext();
    // Add WEnvironment if not already done.
    // If the component is new, then it will not have a WEnvironment yet.
    Environment env;
    if (uiContext.isDummyEnvironment()) {
        env = createEnvironment();
        uiContext.setEnvironment(env);
    } else {
        env = uiContext.getEnvironment();
    }
    // Update the environment for the current phase of the request
    // processing.
    updateEnvironment(env);
    // container we are running in.
    if (getRequest() == null) {
        setRequest(createRequest());
    }
    // Update the wcomponent Request for the current phase of the request
    // processing.
    updateRequest(getRequest());
}
Also used : UIContext(com.github.bordertech.wcomponents.UIContext) Environment(com.github.bordertech.wcomponents.Environment)

Example 5 with UIContext

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

the class DevToolkit method paint.

/**
 * Paints the DevToolkit content.
 *
 * @param templateName the resource name of the Velocity template to use.
 * @param writer the writer to send the content to.
 */
private void paint(final String templateName, final PrintWriter writer) {
    if (!isEnabled()) {
        return;
    }
    try {
        Template template = VelocityEngineFactory.getVelocityEngine().getTemplate(templateName);
        VelocityContext context = new VelocityContext();
        context.put("this", this);
        UIContext uic = UIContextHolder.getCurrentPrimaryUIContext();
        context.put("uic", uic);
        context.put("ui", uic.getUI());
        template.merge(context, writer);
    } catch (Exception e) {
        LOG.error("Unable to render dev toolkit", e);
    }
}
Also used : UIContext(com.github.bordertech.wcomponents.UIContext) VelocityContext(org.apache.velocity.VelocityContext) Template(org.apache.velocity.Template)

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