use of org.apache.wicket.response.StringResponse 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.response.StringResponse in project wicket by apache.
the class RequestCycleUrlForTest method before.
@Before
public void before() {
Request request = mock(Request.class);
Url baseUrl = Url.parse("");
when(request.getClientUrl()).thenReturn(baseUrl);
Response response = new StringResponse() {
@Override
public String encodeURL(CharSequence url) {
return url + JSESSIONID;
}
};
IRequestMapper mapper = mock(IRequestMapper.class);
Url bookmarkablePageUrl = Url.parse(BOOKMARKABLE_PAGE_URL);
when(mapper.mapHandler(argThat(new ExactClassMatcher<BookmarkablePageRequestHandler>(BookmarkablePageRequestHandler.class)))).thenReturn(bookmarkablePageUrl);
Url resourceUrl = Url.parse(RESOURCE_URL);
when(mapper.mapHandler(argThat(new ExactClassMatcher<ResourceRequestHandler>(ResourceRequestHandler.class)))).thenReturn(resourceUrl);
Url resourceReferenceUrl = Url.parse(RES_REF_URL);
when(mapper.mapHandler(argThat(new ExactClassMatcher<ResourceReferenceRequestHandler>(ResourceReferenceRequestHandler.class)))).thenReturn(resourceReferenceUrl);
IExceptionMapper exceptionMapper = mock(IExceptionMapper.class);
RequestCycleContext context = new RequestCycleContext(request, response, mapper, exceptionMapper);
requestCycle = new RequestCycle(context);
requestCycle.getUrlRenderer().setBaseUrl(baseUrl);
}
use of org.apache.wicket.response.StringResponse 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.response.StringResponse 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.response.StringResponse 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