use of org.apache.wicket.request.Response in project wicket by apache.
the class WebPage method validateHeaders.
/**
* Validate that each component which wanted to contribute to the header section actually was
* able to do so.
*/
private void validateHeaders() {
// search for HtmlHeaderContainer in the first level of children or deeper
// if there are transparent resolvers used
HtmlHeaderContainer header = visitChildren(new IVisitor<Component, HtmlHeaderContainer>() {
@Override
public void component(final Component component, final IVisit<HtmlHeaderContainer> visit) {
if (component instanceof HtmlHeaderContainer) {
visit.stop((HtmlHeaderContainer) component);
} else if (component instanceof TransparentWebMarkupContainer == false) {
visit.dontGoDeeper();
}
}
});
if (header == null) {
// the markup must at least contain a <body> tag for wicket to automatically
// create a HtmlHeaderContainer. Log an error if no header container
// was created but any of the components or behaviors want to contribute
// something to the header.
header = new HtmlHeaderContainer(HtmlHeaderSectionHandler.HEADER_ID);
add(header);
RequestCycle requestCycle = getRequestCycle();
Response orgResponse = requestCycle.getResponse();
try {
StringResponse tempResponse = new StringResponse();
requestCycle.setResponse(tempResponse);
// Render all header sections of all components on the page
AbstractHeaderRenderStrategy.get().renderHeader(header, null, getPage());
IHeaderResponse headerResponse = header.getHeaderResponse();
headerResponse.close();
CharSequence collectedHeaderOutput = tempResponse.getBuffer();
if (collectedHeaderOutput.length() > 0) {
reportMissingHead(collectedHeaderOutput);
}
} catch (Exception e) {
// just swallow this exception, there isn't much we can do about.
log.error("header/body check throws exception", e);
} finally {
this.remove(header);
requestCycle.setResponse(orgResponse);
}
}
}
use of org.apache.wicket.request.Response in project wicket by apache.
the class BorderBehavior method beforeRender.
@Override
public void beforeRender(final Component component) {
final MarkupStream stream = getMarkupStream(component);
final Response response = component.getResponse();
stream.setCurrentIndex(0);
boolean insideBorderMarkup = false;
while (stream.isCurrentIndexInsideTheStream()) {
MarkupElement elem = stream.get();
stream.next();
if (elem instanceof WicketTag) {
WicketTag wTag = (WicketTag) elem;
if (!insideBorderMarkup) {
if (wTag.isBorderTag() && wTag.isOpen()) {
insideBorderMarkup = true;
continue;
} else {
throw new WicketRuntimeException("Unexpected tag encountered in markup of component border " + getClass().getName() + ". Tag: " + wTag.toString() + ", expected tag: <wicket:border>");
}
} else {
if (wTag.isBodyTag()) {
break;
} else {
throw new WicketRuntimeException("Unexpected tag encountered in markup of component border " + getClass().getName() + ". Tag: " + wTag.toString() + ", expected tag: <wicket:body> or </wicket:body>");
}
}
}
if (insideBorderMarkup) {
response.write(elem.toCharSequence());
}
}
if (!stream.isCurrentIndexInsideTheStream()) {
throw new WicketRuntimeException("Markup for component border " + getClass().getName() + " ended prematurely, was expecting </wicket:border>");
}
}
use of org.apache.wicket.request.Response in project wicket by apache.
the class PartialPageUpdate method writeComponents.
/**
* Processes components added to the target. This involves attaching components, rendering
* markup into a client side xml envelope, and detaching them
*
* @param response
* the response to write to
* @param encoding
* the encoding for the response
*/
private void writeComponents(Response response, String encoding) {
componentsFrozen = true;
List<Component> toBeWritten = new ArrayList<>(markupIdToComponent.size());
// delay preparation of feedbacks after all other components
try (FeedbackDelay delay = new FeedbackDelay(RequestCycle.get())) {
for (Component component : markupIdToComponent.values()) {
if (!containsAncestorFor(component)) {
if (prepareComponent(component)) {
toBeWritten.add(component);
}
}
}
// .. now prepare all postponed feedbacks
delay.beforeRender();
}
// write components
for (Component component : toBeWritten) {
writeComponent(response, component.getAjaxRegionMarkupId(), component, encoding);
}
if (header != null) {
RequestCycle cycle = RequestCycle.get();
// some header responses buffer all calls to render*** until close is called.
// when they are closed, they do something (i.e. aggregate all JS resource urls to a
// single url), and then "flush" (by writing to the real response) before closing.
// to support this, we need to allow header contributions to be written in the close
// tag, which we do here:
headerRendering = true;
// save old response, set new
Response oldResponse = cycle.setResponse(headerBuffer);
headerBuffer.reset();
// now, close the response (which may render things)
header.getHeaderResponse().close();
// revert to old response
cycle.setResponse(oldResponse);
// write the XML tags and we're done
writeHeaderContribution(response);
headerRendering = false;
}
}
use of org.apache.wicket.request.Response in project wicket by apache.
the class XmlPartialPageUpdate method writeComponent.
@Override
protected void writeComponent(Response response, String markupId, Component component, String encoding) {
// substitute our encoding response for the old one so we can capture
// component's markup in a manner safe for transport inside CDATA block
Response oldResponse = RequestCycle.get().setResponse(bodyBuffer);
try {
// render any associated headers of the component
writeHeaderContribution(response, component);
bodyBuffer.reset();
try {
component.renderPart();
} catch (RuntimeException e) {
bodyBuffer.reset();
throw e;
}
} finally {
// Restore original response
RequestCycle.get().setResponse(oldResponse);
}
response.write("<component id=\"");
response.write(markupId);
response.write("\" ><![CDATA[");
response.write(encode(bodyBuffer.getContents()));
response.write("]]></component>");
bodyBuffer.reset();
}
use of org.apache.wicket.request.Response in project wicket by apache.
the class AbstractOutputTransformerContainer method execute.
/**
* @param code
*/
private void execute(final Runnable code) {
// Temporarily replace the web response with a String response
final Response webResponse = getResponse();
try {
// Create a new response object
final Response response = newResponse();
if (response == null) {
throw new IllegalStateException("newResponse() must not return null");
}
// and make it the current one
getRequestCycle().setResponse(response);
// Invoke default execution
code.run();
try {
// Tranform the data
CharSequence output = transform(this, response.toString());
webResponse.write(output);
} catch (Exception ex) {
throw new WicketRuntimeException("Error while transforming the output: " + this, ex);
}
} finally {
// Restore the original response
getRequestCycle().setResponse(webResponse);
}
}
Aggregations