Search in sources :

Example 1 with HeaderResponse

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);
    }
}
Also used : HeaderResponse(org.apache.wicket.markup.head.internal.HeaderResponse) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) StringResponse(org.apache.wicket.response.StringResponse) Response(org.apache.wicket.request.Response) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) StringResponse(org.apache.wicket.response.StringResponse)

Example 2 with HeaderResponse

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);
}
Also used : HeaderResponse(org.apache.wicket.markup.head.internal.HeaderResponse) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) StringResponse(org.apache.wicket.response.StringResponse) Response(org.apache.wicket.request.Response) HeaderResponse(org.apache.wicket.markup.head.internal.HeaderResponse) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) IRequestHandler(org.apache.wicket.request.IRequestHandler) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) Request(org.apache.wicket.request.Request) StringResponse(org.apache.wicket.response.StringResponse) ResourceReference(org.apache.wicket.request.resource.ResourceReference) UrlRenderer(org.apache.wicket.request.UrlRenderer) IResource(org.apache.wicket.request.resource.IResource) Before(org.junit.Before)

Example 3 with HeaderResponse

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());
    }
}
Also used : StringResponse(org.apache.wicket.response.StringResponse) HeaderResponse(org.apache.wicket.markup.head.internal.HeaderResponse) Response(org.apache.wicket.request.Response) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) HeaderResponse(org.apache.wicket.markup.head.internal.HeaderResponse) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) StringResponse(org.apache.wicket.response.StringResponse) Test(org.junit.Test)

Example 4 with HeaderResponse

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();
}
Also used : HeaderResponse(org.apache.wicket.markup.head.internal.HeaderResponse) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) DecoratingHeaderResponse(org.apache.wicket.markup.html.DecoratingHeaderResponse) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) HeaderItem(org.apache.wicket.markup.head.HeaderItem) StringResponse(org.apache.wicket.response.StringResponse)

Aggregations

IHeaderResponse (org.apache.wicket.markup.head.IHeaderResponse)4 HeaderResponse (org.apache.wicket.markup.head.internal.HeaderResponse)4 StringResponse (org.apache.wicket.response.StringResponse)4 Response (org.apache.wicket.request.Response)3 HeaderItem (org.apache.wicket.markup.head.HeaderItem)1 DecoratingHeaderResponse (org.apache.wicket.markup.html.DecoratingHeaderResponse)1 IRequestHandler (org.apache.wicket.request.IRequestHandler)1 Request (org.apache.wicket.request.Request)1 UrlRenderer (org.apache.wicket.request.UrlRenderer)1 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)1 IResource (org.apache.wicket.request.resource.IResource)1 ResourceReference (org.apache.wicket.request.resource.ResourceReference)1 Before (org.junit.Before)1 Test (org.junit.Test)1