Search in sources :

Example 1 with UrlRenderer

use of org.apache.wicket.request.UrlRenderer in project wicket by apache.

the class ServletWebResponseTest method encodeRedirectAbsoluteUrl.

/**
 * WICKET-5582 absolute URLs stay absolute after encoding
 */
@Test
public void encodeRedirectAbsoluteUrl() {
    final String url = "http://localhost:8080/path";
    ServletWebRequest webRequest = mock(ServletWebRequest.class);
    when(webRequest.isAjax()).thenReturn(Boolean.FALSE);
    Url baseUrl = Url.parse("./baseUrl");
    baseUrl.setProtocol("http");
    baseUrl.setHost("someHost");
    baseUrl.setPort(80);
    when(webRequest.getClientUrl()).thenReturn(baseUrl);
    UrlRenderer renderer = new UrlRenderer(webRequest);
    RequestCycle requestCycle = mock(RequestCycle.class);
    ThreadContext.setRequestCycle(requestCycle);
    when(requestCycle.getUrlRenderer()).thenReturn(renderer);
    HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
    when(httpServletResponse.encodeRedirectURL(Matchers.eq(url))).thenReturn(url + ";foo");
    ServletWebResponse webResponse = new ServletWebResponse(webRequest, httpServletResponse);
    assertEquals(url + ";foo", webResponse.encodeRedirectURL(url));
}
Also used : RequestCycle(org.apache.wicket.request.cycle.RequestCycle) MockHttpServletResponse(org.apache.wicket.protocol.http.mock.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) UrlRenderer(org.apache.wicket.request.UrlRenderer) Url(org.apache.wicket.request.Url) Test(org.junit.Test)

Example 2 with UrlRenderer

use of org.apache.wicket.request.UrlRenderer in project wicket by apache.

the class ServletWebResponse method encodeURL.

@Override
public String encodeURL(CharSequence url) {
    Args.notNull(url, "url");
    UrlRenderer urlRenderer = getUrlRenderer();
    Url originalUrl = Url.parse(url);
    /*
		  WICKET-4645 - always pass absolute url to the web container for encoding
		  because when REDIRECT_TO_BUFFER is in use Wicket may render PageB when
		  PageA is actually the requested one and the web container cannot resolve
		  the base url properly
		 */
    String fullUrl = urlRenderer.renderFullUrl(originalUrl);
    String encodedFullUrl = httpServletResponse.encodeURL(fullUrl);
    final String encodedUrl;
    if (originalUrl.isFull()) {
        encodedUrl = encodedFullUrl;
    } else {
        if (fullUrl.equals(encodedFullUrl)) {
            // no encoding happened so just reuse the original url
            encodedUrl = url.toString();
        } else {
            // get the relative url with the jsessionid encoded in it
            Url _encoded = Url.parse(encodedFullUrl);
            encodedUrl = urlRenderer.renderRelativeUrl(_encoded);
        }
    }
    return encodedUrl;
}
Also used : UrlRenderer(org.apache.wicket.request.UrlRenderer) Url(org.apache.wicket.request.Url)

Example 3 with UrlRenderer

use of org.apache.wicket.request.UrlRenderer in project wicket by apache.

the class ServletWebResponse method encodeRedirectURL.

@Override
public String encodeRedirectURL(CharSequence url) {
    Args.notNull(url, "url");
    UrlRenderer urlRenderer = getUrlRenderer();
    Url originalUrl = Url.parse(url);
    /*
		 * WICKET-4645 - always pass absolute url to the web container for encoding because when
		 * REDIRECT_TO_BUFFER is in use Wicket may render PageB when PageA is actually the requested
		 * one and the web container cannot resolve the base url properly
		 */
    String fullUrl = urlRenderer.renderFullUrl(originalUrl);
    String encodedFullUrl = httpServletResponse.encodeRedirectURL(fullUrl);
    final String encodedUrl;
    if (originalUrl.isFull()) {
        encodedUrl = encodedFullUrl;
    } else {
        if (fullUrl.equals(encodedFullUrl)) {
            // no encoding happened so just reuse the original url
            encodedUrl = url.toString();
        } else {
            // get the relative url with the jsessionid encoded in it
            Url _encoded = Url.parse(encodedFullUrl);
            encodedUrl = urlRenderer.renderRelativeUrl(_encoded);
        }
    }
    return encodedUrl;
}
Also used : UrlRenderer(org.apache.wicket.request.UrlRenderer) Url(org.apache.wicket.request.Url)

Example 4 with UrlRenderer

use of org.apache.wicket.request.UrlRenderer 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 5 with UrlRenderer

use of org.apache.wicket.request.UrlRenderer in project wicket by apache.

the class UrlRendererTest method test6.

/**
 */
@Test
public void test6() {
    UrlRenderer r1 = new UrlRenderer(new MockWebRequest(Url.parse("url/")));
    assertEquals("./x?1", r1.renderUrl(Url.parse("url/x?1")));
}
Also used : MockWebRequest(org.apache.wicket.mock.MockWebRequest) UrlRenderer(org.apache.wicket.request.UrlRenderer) Test(org.junit.Test)

Aggregations

UrlRenderer (org.apache.wicket.request.UrlRenderer)53 Test (org.junit.Test)49 MockWebRequest (org.apache.wicket.mock.MockWebRequest)47 Url (org.apache.wicket.request.Url)30 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 MockHttpServletResponse (org.apache.wicket.protocol.http.mock.MockHttpServletResponse)2 Before (org.junit.Before)2 IPageProvider (org.apache.wicket.core.request.handler.IPageProvider)1 RenderPageRequestHandler (org.apache.wicket.core.request.handler.RenderPageRequestHandler)1 IHeaderResponse (org.apache.wicket.markup.head.IHeaderResponse)1 HeaderResponse (org.apache.wicket.markup.head.internal.HeaderResponse)1 BufferedWebResponse (org.apache.wicket.protocol.http.BufferedWebResponse)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 WebRequest (org.apache.wicket.request.http.WebRequest)1 WebResponse (org.apache.wicket.request.http.WebResponse)1 IResource (org.apache.wicket.request.resource.IResource)1