Search in sources :

Example 36 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle 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)

Example 37 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle 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;
    }
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse) Response(org.apache.wicket.request.Response) HeaderResponse(org.apache.wicket.markup.head.internal.HeaderResponse) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) IRequestCycle(org.apache.wicket.request.IRequestCycle) ArrayList(java.util.ArrayList) FeedbackDelay(org.apache.wicket.feedback.FeedbackDelay) Component(org.apache.wicket.Component)

Example 38 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class AbstractRequestLogger method getCurrentRequest.

public RequestData getCurrentRequest() {
    RequestCycle requestCycle = RequestCycle.get();
    RequestData rd = requestCycle.getMetaData(REQUEST_DATA);
    if (rd == null) {
        rd = new RequestData();
        requestCycle.setMetaData(REQUEST_DATA, rd);
        int activeCount = activeRequests.incrementAndGet();
        if (activeCount > peakActiveRequests.get()) {
            peakActiveRequests.set(activeCount);
        }
    }
    return rd;
}
Also used : RequestCycle(org.apache.wicket.request.cycle.RequestCycle)

Example 39 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class AbstractTransformerBehavior method afterRender.

@Override
public void afterRender(final Component component) {
    final RequestCycle requestCycle = RequestCycle.get();
    try {
        BufferedWebResponse tempResponse = (BufferedWebResponse) requestCycle.getResponse();
        if (component instanceof Page && originalResponse instanceof WebResponse) {
            tempResponse.writeMetaData((WebResponse) originalResponse);
        }
        // Transform the data
        CharSequence output = transform(component, tempResponse.getText());
        originalResponse.write(output);
    } catch (Exception ex) {
        throw new WicketRuntimeException("Error while transforming the output of component: " + component, ex);
    } finally {
        // Restore the original response object
        requestCycle.setResponse(originalResponse);
    }
}
Also used : BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) WebResponse(org.apache.wicket.request.http.WebResponse) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) Page(org.apache.wicket.Page) WicketRuntimeException(org.apache.wicket.WicketRuntimeException)

Example 40 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class AbstractTransformerBehavior method beforeRender.

@Override
public void beforeRender(Component component) {
    super.beforeRender(component);
    final RequestCycle requestCycle = RequestCycle.get();
    // Temporarily replace the web response with a String response
    originalResponse = requestCycle.getResponse();
    WebResponse origResponse = (WebResponse) ((originalResponse instanceof WebResponse) ? originalResponse : null);
    BufferedWebResponse tempResponse = newResponse(origResponse);
    // temporarily set StringResponse to collect the transformed output
    requestCycle.setResponse(tempResponse);
}
Also used : BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) WebResponse(org.apache.wicket.request.http.WebResponse) RequestCycle(org.apache.wicket.request.cycle.RequestCycle)

Aggregations

RequestCycle (org.apache.wicket.request.cycle.RequestCycle)69 WebResponse (org.apache.wicket.request.http.WebResponse)14 IRequestHandler (org.apache.wicket.request.IRequestHandler)9 Url (org.apache.wicket.request.Url)9 Response (org.apache.wicket.request.Response)8 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)7 BufferedWebResponse (org.apache.wicket.protocol.http.BufferedWebResponse)7 Page (org.apache.wicket.Page)6 Request (org.apache.wicket.request.Request)6 RenderPageRequestHandler (org.apache.wicket.core.request.handler.RenderPageRequestHandler)5 IRequestCycle (org.apache.wicket.request.IRequestCycle)5 AbstractRequestCycleListener (org.apache.wicket.request.cycle.AbstractRequestCycleListener)5 Test (org.junit.Test)5 Application (org.apache.wicket.Application)4 IHeaderResponse (org.apache.wicket.markup.head.IHeaderResponse)4 WebClientInfo (org.apache.wicket.protocol.http.request.WebClientInfo)4 WebRequest (org.apache.wicket.request.http.WebRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 WebApplication (org.apache.wicket.protocol.http.WebApplication)3 UrlRenderer (org.apache.wicket.request.UrlRenderer)3