Search in sources :

Example 11 with BufferedWebResponse

use of org.apache.wicket.protocol.http.BufferedWebResponse in project wicket by apache.

the class BufferedResponseMapper method getAndRemoveBufferedResponse.

protected BufferedWebResponse getAndRemoveBufferedResponse(Url url) {
    String sessionId = getSessionId();
    BufferedWebResponse response = null;
    if (Strings.isEmpty(sessionId) == false) {
        response = WebApplication.get().getAndRemoveBufferedResponse(sessionId, url);
    }
    return response;
}
Also used : BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse)

Example 12 with BufferedWebResponse

use of org.apache.wicket.protocol.http.BufferedWebResponse in project wicket by apache.

the class ComponentRenderer method renderPage.

/**
 * Collects the html generated by rendering a page.
 *
 * @param page
 *            supplier of the page
 * @return the html rendered by the panel
 */
public CharSequence renderPage(final Supplier<? extends Page> page) {
    return inThreadContext(() -> {
        Request request = newRequest();
        BufferedWebResponse response = new BufferedWebResponse(null);
        RequestCycle cycle = application.createRequestCycle(request, response);
        ThreadContext.setRequestCycle(cycle);
        page.get().renderPage();
        return response.getText();
    });
}
Also used : BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) MockWebRequest(org.apache.wicket.mock.MockWebRequest) Request(org.apache.wicket.request.Request)

Example 13 with BufferedWebResponse

use of org.apache.wicket.protocol.http.BufferedWebResponse in project wicket by apache.

the class ComponentRenderer method renderPage.

/**
 * Collects the Html generated by the rendering a page.
 * <p>
 * Important note: Must be called on a thread bound to an application's {@link ThreadContext}!
 *
 * @param pageProvider
 *            the provider of the page class/instance and its parameters
 * @return the html rendered by a page
 *
 * @see ThreadContext
 */
public static CharSequence renderPage(final PageProvider pageProvider) {
    Application application = Application.get();
    RequestCycle originalRequestCycle = RequestCycle.get();
    BufferedWebResponse tempResponse = new BufferedWebResponse(null);
    RequestCycle tempRequestCycle = application.createRequestCycle(originalRequestCycle.getRequest(), tempResponse);
    try {
        ThreadContext.setRequestCycle(tempRequestCycle);
        pageProvider.getPageInstance().renderPage();
    } finally {
        ThreadContext.setRequestCycle(originalRequestCycle);
    }
    return tempResponse.getText();
}
Also used : BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) MockApplication(org.apache.wicket.mock.MockApplication) Application(org.apache.wicket.Application) WebApplication(org.apache.wicket.protocol.http.WebApplication)

Example 14 with BufferedWebResponse

use of org.apache.wicket.protocol.http.BufferedWebResponse in project wicket by apache.

the class ComponentRenderer method renderComponent.

/**
 * Collects the Html generated by rendering a component.
 * <p>
 * Important notes:
 * <ul>
 * <li>this method is meant to render fresh component instances that are disposed after the html
 * has been generate. To avoid unwanted side effects do not use it with components that are from
 * an existing hierarchy.</li>
 * <li>does <strong>not</strong> support rendering
 * {@link org.apache.wicket.markup.html.panel.Fragment} instances</li>
 * <li>must be called on a thread bound to an application's {@link ThreadContext}!</li>
 * </ul>
 *
 * @param component
 *            the component to render.
 * @return the html rendered by the component
 *
 * @see ThreadContext
 */
public static CharSequence renderComponent(final Component component) {
    RequestCycle requestCycle = RequestCycle.get();
    final Response originalResponse = requestCycle.getResponse();
    BufferedWebResponse tempResponse = new BufferedWebResponse(null);
    MarkupContainer oldParent = component.getParent();
    if (oldParent != null && LOGGER.isWarnEnabled()) {
        LOGGER.warn("Component '{}' with a parent '{}' is passed for standalone rendering. " + "It is recommended to render only orphan components because they are not cleaned up/detached" + " after the rendering.", component, oldParent);
    }
    try {
        requestCycle.setResponse(tempResponse);
        // add the component to a dummy page just for the rendering
        RenderPage page = new RenderPage(component);
        page.internalInitialize();
        component.beforeRender();
        component.renderPart();
    } finally {
        if (oldParent != null) {
            // re-add the child to its old parent
            oldParent.add(component);
        }
        requestCycle.setResponse(originalResponse);
    }
    return tempResponse.getText();
}
Also used : BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) Response(org.apache.wicket.request.Response) BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) MarkupContainer(org.apache.wicket.MarkupContainer) RequestCycle(org.apache.wicket.request.cycle.RequestCycle)

Example 15 with BufferedWebResponse

use of org.apache.wicket.protocol.http.BufferedWebResponse in project wicket by apache.

the class TestPageRenderer method renderPage.

@Override
protected BufferedWebResponse renderPage(Url targetUrl, RequestCycle requestCycle) {
    BufferedWebResponse webResponse = super.renderPage(targetUrl, requestCycle);
    webResponse.write("some response".getBytes());
    return webResponse;
}
Also used : BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse)

Aggregations

BufferedWebResponse (org.apache.wicket.protocol.http.BufferedWebResponse)17 Url (org.apache.wicket.request.Url)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 Test (org.junit.Test)7 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)6 WebResponse (org.apache.wicket.request.http.WebResponse)4 Application (org.apache.wicket.Application)1 MarkupContainer (org.apache.wicket.MarkupContainer)1 Page (org.apache.wicket.Page)1 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)1 BufferedResponseRequestHandler (org.apache.wicket.core.request.handler.BufferedResponseRequestHandler)1 MockApplication (org.apache.wicket.mock.MockApplication)1 MockWebRequest (org.apache.wicket.mock.MockWebRequest)1 WebApplication (org.apache.wicket.protocol.http.WebApplication)1 IRequestHandler (org.apache.wicket.request.IRequestHandler)1 Request (org.apache.wicket.request.Request)1 Response (org.apache.wicket.request.Response)1 IRequestablePage (org.apache.wicket.request.component.IRequestablePage)1