Search in sources :

Example 1 with HtmlHeaderContainer

use of org.apache.wicket.markup.html.internal.HtmlHeaderContainer 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);
        }
    }
}
Also used : IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) StringResponse(org.apache.wicket.response.StringResponse) WebResponse(org.apache.wicket.request.http.WebResponse) Response(org.apache.wicket.request.Response) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) StringResponse(org.apache.wicket.response.StringResponse) HtmlHeaderContainer(org.apache.wicket.markup.html.internal.HtmlHeaderContainer) Component(org.apache.wicket.Component)

Aggregations

Component (org.apache.wicket.Component)1 IHeaderResponse (org.apache.wicket.markup.head.IHeaderResponse)1 HtmlHeaderContainer (org.apache.wicket.markup.html.internal.HtmlHeaderContainer)1 Response (org.apache.wicket.request.Response)1 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)1 WebResponse (org.apache.wicket.request.http.WebResponse)1 StringResponse (org.apache.wicket.response.StringResponse)1