Search in sources :

Example 41 with WebXmlRenderContext

use of com.github.bordertech.wcomponents.servlet.WebXmlRenderContext in project wcomponents by BorderTech.

the class SafetyContainer method paintComponent.

/**
 * <p>
 * Override paintComponent to provide some protection against bad code when examples are being developed, resulting
 * in invalid XML.</p>
 *
 * <p>
 * Real applications should not emit HTML directly.</p>
 *
 * @param renderContext the RenderContext to send the output to.
 */
@Override
protected void paintComponent(final RenderContext renderContext) {
    final Throwable error = (Throwable) getAttribute(ERROR_KEY);
    if (error == null) {
        if (renderContext instanceof WebXmlRenderContext) {
            // For a WebXmlContext, we can output partial XML.
            WebXmlRenderContext webRenderContext = (WebXmlRenderContext) renderContext;
            final StringWriter buf = new StringWriter();
            WebXmlRenderContext bufferedContext = new WebXmlRenderContext(new PrintWriter(buf));
            try {
                for (int i = 0; i < shim.getChildCount(); i++) {
                    shim.getChildAt(i).paint(bufferedContext);
                }
                webRenderContext.getWriter().write(buf.toString());
            } catch (final Exception e) {
                new ErrorComponent("Error during rendering", e).paint(renderContext);
                new WHorizontalRule().paint(renderContext);
                PrintWriter writer = webRenderContext.getWriter();
                writer.println("\n<br/>Partial XML:<br/>\n<pre>\n");
                writer.println(WebUtilities.encode(buf.toString()));
                writer.println("\n</pre>");
            }
        } else {
            try {
                for (int i = 0; i < shim.getChildCount(); i++) {
                    shim.getChildAt(i).paint(renderContext);
                }
            } catch (final Exception e) {
                new ErrorComponent("Error during rendering", e).paint(renderContext);
                new WHorizontalRule().paint(renderContext);
            }
        }
    } else {
        new ErrorComponent("Error during action phase", error).paint(renderContext);
    }
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) StringWriter(java.io.StringWriter) SystemException(com.github.bordertech.wcomponents.util.SystemException) WHorizontalRule(com.github.bordertech.wcomponents.WHorizontalRule) PrintWriter(java.io.PrintWriter)

Example 42 with WebXmlRenderContext

use of com.github.bordertech.wcomponents.servlet.WebXmlRenderContext in project wcomponents by BorderTech.

the class AjaxInterceptor method paintContainerResponse.

/**
 * Paint the ajax container response.
 *
 * @param renderContext the render context
 * @param operation the ajax operation
 */
private void paintContainerResponse(final RenderContext renderContext, final AjaxOperation operation) {
    WebXmlRenderContext webRenderContext = (WebXmlRenderContext) renderContext;
    XmlStringBuilder xml = webRenderContext.getWriter();
    // Get trigger's context
    ComponentWithContext trigger = AjaxHelper.getCurrentTriggerAndContext();
    if (trigger == null) {
        throw new SystemException("No context available for trigger " + operation.getTriggerId());
    }
    xml.appendTagOpen("ui:ajaxtarget");
    xml.appendAttribute("id", operation.getTargetContainerId());
    xml.appendAttribute("action", AjaxOperation.AjaxAction.REPLACE_CONTENT.getDesc());
    xml.appendClose();
    // Paint targets - Assume targets are in the same context as the trigger
    UIContextHolder.pushContext(trigger.getContext());
    try {
        for (String targetId : operation.getTargets()) {
            ComponentWithContext target;
            if (targetId.equals(operation.getTriggerId())) {
                target = trigger;
            } else {
                target = WebUtilities.getComponentById(targetId, true);
                if (target == null) {
                    LOG.warn("Could not find ajax target to render [" + targetId + "]");
                    continue;
                }
            }
            target.getComponent().paint(renderContext);
        }
    } finally {
        UIContextHolder.popContext();
    }
    xml.appendEndTag("ui:ajaxtarget");
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) SystemException(com.github.bordertech.wcomponents.util.SystemException) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext)

Example 43 with WebXmlRenderContext

use of com.github.bordertech.wcomponents.servlet.WebXmlRenderContext in project wcomponents by BorderTech.

the class AjaxPageShellInterceptor method paint.

/**
 * Paints the targeted ajax regions. The format of the response is an agreement between the server and the client
 * side handling our AJAX response.
 *
 * @param renderContext the renderContext to send the output to.
 */
@Override
public void paint(final RenderContext renderContext) {
    WebXmlRenderContext webRenderContext = (WebXmlRenderContext) renderContext;
    XmlStringBuilder xml = webRenderContext.getWriter();
    AjaxOperation operation = AjaxHelper.getCurrentOperation();
    if (operation == null) {
        // the request attribute that we place in the ui contenxt in the action phase can't be null
        throw new SystemException("Can't paint AJAX response. Couldn't find the expected reference to the AjaxOperation.");
    }
    UIContext uic = UIContextHolder.getCurrent();
    String focusId = uic.getFocussedId();
    xml.append(XMLUtil.getXMLDeclarationWithThemeXslt(uic));
    xml.appendTagOpen("ui:ajaxresponse");
    xml.append(XMLUtil.STANDARD_NAMESPACES);
    xml.appendOptionalAttribute("defaultFocusId", uic.isFocusRequired() && !Util.empty(focusId), focusId);
    xml.appendClose();
    getBackingComponent().paint(renderContext);
    xml.appendEndTag("ui:ajaxresponse");
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) AjaxOperation(com.github.bordertech.wcomponents.AjaxOperation) SystemException(com.github.bordertech.wcomponents.util.SystemException) UIContext(com.github.bordertech.wcomponents.UIContext) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder)

Example 44 with WebXmlRenderContext

use of com.github.bordertech.wcomponents.servlet.WebXmlRenderContext in project wcomponents by BorderTech.

the class PageShellInterceptor method paint.

/**
 * Produce the html output.
 *
 * @param renderContext the renderContext to send the output to.
 */
@Override
public void paint(final RenderContext renderContext) {
    WebXmlRenderContext webRenderContext = (WebXmlRenderContext) renderContext;
    PrintWriter writer = webRenderContext.getWriter();
    beforePaint(writer);
    getBackingComponent().paint(renderContext);
    afterPaint(writer);
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) PrintWriter(java.io.PrintWriter)

Example 45 with WebXmlRenderContext

use of com.github.bordertech.wcomponents.servlet.WebXmlRenderContext 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)

Aggregations

WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)63 PrintWriter (java.io.PrintWriter)48 StringWriter (java.io.StringWriter)27 Test (org.junit.Test)27 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)17 MockResponse (com.github.bordertech.wcomponents.util.mock.MockResponse)17 UIContext (com.github.bordertech.wcomponents.UIContext)15 NullWriter (com.github.bordertech.wcomponents.util.NullWriter)7 SystemException (com.github.bordertech.wcomponents.util.SystemException)7 WText (com.github.bordertech.wcomponents.WText)6 RenderContext (com.github.bordertech.wcomponents.RenderContext)5 WComponent (com.github.bordertech.wcomponents.WComponent)5 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)5 WServlet (com.github.bordertech.wcomponents.servlet.WServlet)5 MockHttpServletRequest (com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest)4 Configuration (org.apache.commons.configuration.Configuration)4 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)3 AllComponents (com.github.bordertech.wcomponents.AllComponents)3 ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)3 Response (com.github.bordertech.wcomponents.Response)3