Search in sources :

Example 1 with Response

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

the class AbstractContainerHelper method handleError.

/**
 * Last resort error handling.
 *
 * @param error the error to handle.
 * @throws IOException if there is an error writing the error HTML.
 */
public void handleError(final Throwable error) throws IOException {
    LOG.debug("Start handleError...");
    // Should the session be removed upon error?
    boolean terminate = ConfigurationProperties.getTerminateSessionOnError();
    // If we are unfriendly, terminate the session
    if (terminate) {
        invalidateSession();
    }
    // Are we in developer friendly error mode?
    boolean friendly = ConfigurationProperties.getDeveloperErrorHandling();
    FatalErrorPageFactory factory = Factory.newInstance(FatalErrorPageFactory.class);
    WComponent errorPage = factory.createErrorPage(friendly, error);
    String html = renderErrorPageToHTML(errorPage);
    // Setup the response
    Response response = getResponse();
    response.setContentType(WebUtilities.CONTENT_TYPE_HTML);
    // Make sure not cached
    getResponse().setHeader("Cache-Control", CacheType.NO_CACHE.getSettings());
    getResponse().setHeader("Pragma", "no-cache");
    getResponse().setHeader("Expires", "-1");
    getPrintWriter().println(html);
    LOG.debug("End handleError");
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) Response(com.github.bordertech.wcomponents.Response) FatalErrorPageFactory(com.github.bordertech.wcomponents.FatalErrorPageFactory)

Example 2 with Response

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

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

the class DataListInterceptor method paint.

/**
 * {@inheritDoc}
 */
@Override
public void paint(final RenderContext renderContext) {
    if (key == null) {
        super.paint(renderContext);
        return;
    }
    Response response = getResponse();
    Object table = LOOKUP_TABLE.getTableForCacheKey(key);
    List<?> data = LOOKUP_TABLE.getTable(table);
    response.setContentType(WebUtilities.CONTENT_TYPE_XML);
    response.setHeader("Cache-Control", CacheType.DATALIST_CACHE.getSettings());
    XmlStringBuilder xml = new XmlStringBuilder(((WebXmlRenderContext) renderContext).getWriter());
    xml.write(XMLUtil.XML_DECLARATION);
    if (data != null) {
        xml.appendTagOpen("ui:datalist");
        xml.append(XMLUtil.UI_NAMESPACE);
        xml.appendAttribute("id", key);
        xml.appendClose();
        for (Object item : data) {
            // Check for null option (ie null or empty). Match isEmpty() logic.
            boolean isNull = item == null ? true : (item.toString().length() == 0);
            xml.appendTagOpen("ui:option");
            xml.appendAttribute("value", LOOKUP_TABLE.getCode(table, item));
            xml.appendOptionalAttribute("isNull", isNull, "true");
            xml.appendClose();
            xml.append(WebUtilities.encode(LOOKUP_TABLE.getDescription(table, item)));
            xml.appendEndTag("ui:option");
        }
        xml.appendEndTag("ui:datalist");
    }
}
Also used : Response(com.github.bordertech.wcomponents.Response) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder)

Example 4 with Response

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

the class TransformXMLInterceptor method paint.

/**
 * Override paint to perform XML to HTML transformation.
 *
 * @param renderContext the renderContext to send the output to.
 */
@Override
public void paint(final RenderContext renderContext) {
    if (!doTransform) {
        super.paint(renderContext);
        return;
    }
    if (!(renderContext instanceof WebXmlRenderContext)) {
        LOG.warn("Unable to transform a " + renderContext);
        super.paint(renderContext);
        return;
    }
    LOG.debug("Transform XML Interceptor: Start");
    UIContext uic = UIContextHolder.getCurrent();
    // Set up a render context to buffer the XML payload.
    StringWriter xmlBuffer = new StringWriter();
    PrintWriter xmlWriter = new PrintWriter(xmlBuffer);
    WebXmlRenderContext xmlContext = new WebXmlRenderContext(xmlWriter, uic.getLocale());
    // write the XML to the buffer
    super.paint(xmlContext);
    // Get a handle to the true PrintWriter.
    WebXmlRenderContext webRenderContext = (WebXmlRenderContext) renderContext;
    PrintWriter writer = webRenderContext.getWriter();
    /*
		 * Switch the response content-type to HTML.
		 * In theory the transformation could be to ANYTHING (not just HTML) so perhaps it would make more sense to
		 *    write a new interceptor "ContentTypeInterceptor" which attempts to sniff payloads and choose the correct
		 *    content-type. This is exactly the kind of thing IE6 loved to do, so perhaps it's a bad idea.
		 */
    Response response = getResponse();
    response.setContentType(WebUtilities.CONTENT_TYPE_HTML);
    String xml = xmlBuffer.toString();
    if (isAllowCorruptCharacters() && !Util.empty(xml)) {
        // Remove illegal HTML characters from the content before transforming it.
        xml = removeCorruptCharacters(xml);
    }
    // Double encode template tokens in the XML
    xml = WebUtilities.doubleEncodeBrackets(xml);
    // Setup temp writer
    StringWriter tempBuffer = new StringWriter();
    PrintWriter tempWriter = new PrintWriter(tempBuffer);
    // Perform the transformation and write the result.
    transform(xml, uic, tempWriter);
    // Decode Double Encoded Brackets
    String tempResp = tempBuffer.toString();
    tempResp = WebUtilities.doubleDecodeBrackets(tempResp);
    // Write response
    writer.write(tempResp);
    LOG.debug("Transform XML Interceptor: Finished");
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) Response(com.github.bordertech.wcomponents.Response) StringWriter(java.io.StringWriter) UIContext(com.github.bordertech.wcomponents.UIContext) PrintWriter(java.io.PrintWriter)

Example 5 with Response

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

the class WebXmlRenderingPerformance_Test method sendRequest.

/**
 * Invokes WComponent request processing, so that this test case can more closely match a production scenario.
 *
 * @param comp the component to invoke request processing on.
 * @param uic the user context to use.
 */
private void sendRequest(final WComponent comp, final UIContext uic) {
    PrintWriter writer = new PrintWriter(new NullWriter());
    uic.setEnvironment(new WServlet.WServletEnvironment("", "http://localhost", ""));
    uic.setUI(comp);
    InterceptorComponent root = ServletUtil.createInterceptorChain(new MockHttpServletRequest());
    root.attachUI(comp);
    Response response = new MockResponse();
    root.attachResponse(response);
    setActiveContext(uic);
    MockRequest request = new MockRequest();
    try {
        root.serviceRequest(request);
        root.preparePaint(request);
        root.paint(new WebXmlRenderContext(writer));
    } finally {
        resetContext();
    }
}
Also used : Response(com.github.bordertech.wcomponents.Response) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) InterceptorComponent(com.github.bordertech.wcomponents.container.InterceptorComponent) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) WServlet(com.github.bordertech.wcomponents.servlet.WServlet) NullWriter(com.github.bordertech.wcomponents.util.NullWriter) PrintWriter(java.io.PrintWriter)

Aggregations

Response (com.github.bordertech.wcomponents.Response)5 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)3 PrintWriter (java.io.PrintWriter)3 UIContext (com.github.bordertech.wcomponents.UIContext)2 WComponent (com.github.bordertech.wcomponents.WComponent)2 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)1 Environment (com.github.bordertech.wcomponents.Environment)1 Escape (com.github.bordertech.wcomponents.Escape)1 FatalErrorPageFactory (com.github.bordertech.wcomponents.FatalErrorPageFactory)1 WApplication (com.github.bordertech.wcomponents.WApplication)1 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)1 InterceptorComponent (com.github.bordertech.wcomponents.container.InterceptorComponent)1 WServlet (com.github.bordertech.wcomponents.servlet.WServlet)1 NullWriter (com.github.bordertech.wcomponents.util.NullWriter)1 SystemException (com.github.bordertech.wcomponents.util.SystemException)1 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)1 MockResponse (com.github.bordertech.wcomponents.util.mock.MockResponse)1 MockHttpServletRequest (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest)1 StringWriter (java.io.StringWriter)1