Search in sources :

Example 6 with BufferedWebResponse

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

the class WebPageRendererTest method testRedirectToBufferStatefulPageAndSameUrls.

/**
 * Tests that when the page is stateful and the urls are the same then the response is written
 * directly
 */
@Test
public void testRedirectToBufferStatefulPageAndSameUrls() {
    final AtomicBoolean stored = new AtomicBoolean(false);
    PageRenderer renderer = new TestPageRenderer(handler) {

        @Override
        protected boolean isRedirectToBuffer() {
            return true;
        }

        @Override
        protected void storeBufferedResponse(Url url, BufferedWebResponse response) {
            stored.set(true);
        }
    };
    when(provider.isNewPageInstance()).thenReturn(true);
    Url sameUrl = Url.parse("same");
    // needed for earlier checks
    when(urlRenderer.getBaseUrl()).thenReturn(sameUrl);
    when(requestCycle.mapUrlFor(eq(handler))).thenReturn(sameUrl);
    renderer.respond(requestCycle);
    verify(response).write(any(byte[].class));
    verify(response, never()).sendRedirect(anyString());
    Assert.assertFalse(stored.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) Url(org.apache.wicket.request.Url) Test(org.junit.Test)

Example 7 with BufferedWebResponse

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

the class WebPageRendererTest method testRedirectToBufferStatelessPageAndRedirectIsDisabled.

/**
 * Tests that when the page is stateless and redirect for stateless pages is disabled then the
 * page should be written without redirect
 */
@Test
public void testRedirectToBufferStatelessPageAndRedirectIsDisabled() {
    final AtomicBoolean stored = new AtomicBoolean(false);
    TestPageRenderer renderer = new TestPageRenderer(handler) {

        @Override
        protected boolean enableRedirectForStatelessPage() {
            return false;
        }

        @Override
        protected void storeBufferedResponse(Url url, BufferedWebResponse response) {
            stored.set(true);
        }
    };
    renderer.redirectToBuffer = true;
    renderer.pageStateless = true;
    // needed for earlier checks
    when(urlRenderer.getBaseUrl()).thenReturn(Url.parse("a"));
    when(requestCycle.mapUrlFor(eq(handler))).thenReturn(Url.parse("b"));
    renderer.respond(requestCycle);
    verify(response).write(any(byte[].class));
    verify(response, never()).sendRedirect(anyString());
    Assert.assertFalse(stored.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) Url(org.apache.wicket.request.Url) Test(org.junit.Test)

Example 8 with BufferedWebResponse

use of org.apache.wicket.protocol.http.BufferedWebResponse 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 9 with BufferedWebResponse

use of org.apache.wicket.protocol.http.BufferedWebResponse 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)

Example 10 with BufferedWebResponse

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

the class BufferedResponseMapper method mapRequest.

/**
 * @see org.apache.wicket.request.IRequestMapper#mapRequest(org.apache.wicket.request.Request)
 */
@Override
public IRequestHandler mapRequest(Request request) {
    request = getRequest(request);
    BufferedWebResponse response = getAndRemoveBufferedResponse(request.getUrl());
    if (response != null) {
        return new BufferedResponseRequestHandler(response);
    } else {
        return null;
    }
}
Also used : BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) BufferedResponseRequestHandler(org.apache.wicket.core.request.handler.BufferedResponseRequestHandler)

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