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);
}
}
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");
}
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");
}
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);
}
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");
}
Aggregations