use of org.apache.wicket.markup.head.internal.HeaderResponse in project wicket by apache.
the class HtmlHeaderContainer method onComponentTagBody.
/**
* First render the body of the component. And if it is the header component of a Page (compared
* to a Panel or Border), then get the header sections from all component in the hierarchy and
* render them as well.
*/
@Override
public final void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
// We are able to automatically add <head> to the page if it is
// missing. But we only want to add it, if we have content to be
// written to its body. Thus we first write the output into a
// StringResponse and if not empty, we copy it to the original
// web response.
// Temporarily replace the web response with a String response
final Response webResponse = getResponse();
try {
// Create a (string) response for all headers contributed by any component on the Page.
final StringResponse response = new StringResponse();
getRequestCycle().setResponse(response);
IHeaderResponse headerResponse = getHeaderResponse();
if (!response.equals(headerResponse.getResponse())) {
getRequestCycle().setResponse(headerResponse.getResponse());
}
// Render the header sections of all components on the page
AbstractHeaderRenderStrategy.get().renderHeader(this, new HeaderStreamState(markupStream, openTag), getPage());
// Close the header response before rendering the header container itself
// See https://issues.apache.org/jira/browse/WICKET-3728
headerResponse.close();
// Cleanup extraneous CR and LF from the response
CharSequence output = getCleanResponse(response);
// Automatically add <head> if necessary
if (output.length() > 0) {
if (renderOpenAndCloseTags()) {
webResponse.write("<head>");
}
webResponse.write(output);
if (renderOpenAndCloseTags()) {
webResponse.write("</head>");
}
}
} finally {
// Restore the original response
getRequestCycle().setResponse(webResponse);
}
}
use of org.apache.wicket.markup.head.internal.HeaderResponse in project wicket by apache.
the class HeaderResponseTest method before.
/**
* Prepare
*/
@Before
public void before() {
final Response realResponse = new StringResponse();
headerResponse = new HeaderResponse() {
@Override
protected Response getRealResponse() {
return realResponse;
}
};
reference = new ResourceReference("testReference") {
private static final long serialVersionUID = 1L;
@Override
public IResource getResource() {
return null;
}
};
RequestCycle requestCycle = mock(RequestCycle.class);
when(requestCycle.urlFor(any(IRequestHandler.class))).thenReturn(RESOURCE_NAME);
when(requestCycle.find(any())).thenReturn(Optional.empty());
Request request = mock(Request.class);
when(request.getCharset()).thenReturn(Charset.defaultCharset());
when(requestCycle.getRequest()).thenReturn(request);
UrlRenderer urlRenderer = mock(UrlRenderer.class);
when(urlRenderer.renderContextRelativeUrl((any(String.class)))).thenReturn(RESOURCE_NAME);
when(requestCycle.getUrlRenderer()).thenReturn(urlRenderer);
ThreadContext.setRequestCycle(requestCycle);
}
use of org.apache.wicket.markup.head.internal.HeaderResponse in project wicket by apache.
the class FilteringHeaderResponseTest method createBucketOnTheFlyForFilteredHeaderItem.
/**
* https://issues.apache.org/jira/browse/WICKET-5057
* @throws Exception
*/
@Test
public void createBucketOnTheFlyForFilteredHeaderItem() throws Exception {
try (FilteringHeaderResponse headerResponse = new FilteringHeaderResponse(new HeaderResponse() {
@Override
protected Response getRealResponse() {
return new StringResponse();
}
}, "headerBucketName", Collections.emptyList())) {
String filterName = "filterName";
String headerContent = "content";
FilteredHeaderItem item = new FilteredHeaderItem(StringHeaderItem.forString(headerContent), filterName);
headerResponse.render(item);
CharSequence realContent = headerResponse.getContent(filterName);
assertEquals(headerContent, realContent.toString());
}
}
use of org.apache.wicket.markup.head.internal.HeaderResponse in project wicket by apache.
the class FilteringHeaderResponse method getContent.
/**
* Gets the content that was rendered to this header response and matched the filter with the
* given name.
*
* @param filterName
* the name of the filter to get the bucket for
* @return the content that was accepted by the filter with this name
*/
@SuppressWarnings("resource")
public final CharSequence getContent(String filterName) {
if (filterName == null || !responseFilterMap.containsKey(filterName)) {
return "";
}
List<HeaderItem> resp = responseFilterMap.get(filterName);
final StringResponse strResponse = new StringResponse();
IHeaderResponse headerRenderer = new HeaderResponse() {
@Override
protected Response getRealResponse() {
return strResponse;
}
@Override
public boolean wasRendered(Object object) {
return FilteringHeaderResponse.this.getRealResponse().wasRendered(object);
}
@Override
public void markRendered(Object object) {
FilteringHeaderResponse.this.getRealResponse().markRendered(object);
}
};
headerRenderer = decorate(headerRenderer);
for (HeaderItem curItem : resp) {
headerRenderer.render(curItem);
}
headerRenderer.close();
return strResponse.getBuffer();
}
Aggregations