Search in sources :

Example 61 with RequestCycle

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

the class ServletWebResponseTest method encodeAbsoluteUrl.

/**
 * WICKET-5582 absolute URLs stay absolute after encoding
 */
@Test
public void encodeAbsoluteUrl() {
    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.encodeURL(Matchers.eq(url))).thenReturn(url + ";foo");
    ServletWebResponse webResponse = new ServletWebResponse(webRequest, httpServletResponse);
    assertEquals(url + ";foo", webResponse.encodeURL(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 62 with RequestCycle

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

the class QueryStringWithVersionResourceCachingStrategyTest method testDecorateResponse.

@Test
public void testDecorateResponse() throws Exception {
    Duration defaultDuration = Duration.minutes(60);
    // setup RequestCycle
    BaseWicketTester tester = new BaseWicketTester();
    RequestCycle requestCycle = ThreadContext.getRequestCycle();
    Application.get().getResourceSettings().setDefaultCacheDuration(defaultDuration);
    try {
        // version match
        requestCycle.setMetaData(IResourceCachingStrategy.URL_VERSION, TEST_RESOURCE_VERSION);
        AbstractResource.ResourceResponse response = new AbstractResource.ResourceResponse();
        strategy.decorateResponse(response, new TestResource());
        assertEquals(WebResponse.MAX_CACHE_DURATION, response.getCacheDuration());
        assertEquals(WebResponse.CacheScope.PUBLIC, response.getCacheScope());
        // version mismatch
        requestCycle.setMetaData(IResourceCachingStrategy.URL_VERSION, "foo");
        response = new AbstractResource.ResourceResponse();
        strategy.decorateResponse(response, new TestResource());
        assertEquals(defaultDuration, response.getCacheDuration());
        assertEquals(WebResponse.CacheScope.PRIVATE, response.getCacheScope());
    } finally {
        tester.destroy();
    }
}
Also used : RequestCycle(org.apache.wicket.request.cycle.RequestCycle) Duration(org.apache.wicket.util.time.Duration) AbstractResource(org.apache.wicket.request.resource.AbstractResource) BaseWicketTester(org.apache.wicket.util.tester.BaseWicketTester) Test(org.junit.Test)

Example 63 with RequestCycle

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

the class PushHeaderItem method push.

/**
 * Creates a URL and pushes the resource to the client - this is only supported if http2 is
 * enabled
 *
 * @param pushItems
 *            a list of items to be pushed to the client
 * @return the current push header item
 */
@SuppressWarnings("unchecked")
public PushHeaderItem push(List<PushItem> pushItems) {
    RequestCycle requestCycle = RequestCycle.get();
    if (isHttp2(getContainerRequest(requestCycle.getRequest())))
        for (PushItem pushItem : pushItems) {
            Object object = pushItem.getObject();
            PageParameters parameters = pushItem.getPageParameters();
            if (object == null) {
                throw new WicketRuntimeException("Please provide an object to the items to be pushed, so that the url can be created for the given resource.");
            }
            CharSequence url = null;
            if (object instanceof ResourceReference) {
                url = requestCycle.urlFor((ResourceReference) object, parameters);
            } else if (Page.class.isAssignableFrom(object.getClass())) {
                url = requestCycle.urlFor((Class<? extends Page>) object, parameters);
            } else if (object instanceof IRequestHandler) {
                url = requestCycle.urlFor((IRequestHandler) object);
            } else if (pushItem.getUrl() != null) {
                url = pushItem.getUrl();
            } else {
                Url encoded = new PageParametersEncoder().encodePageParameters(parameters);
                String queryString = encoded.getQueryString();
                url = object.toString() + (queryString != null ? "?" + queryString : "");
            }
            if (url.toString().equals(".")) {
                url = "/";
            } else if (url.toString().startsWith(".")) {
                url = url.toString().substring(1);
            }
            // The context path and the filter have to be applied to the URL, because otherwise
            // the resource is not pushed correctly
            StringBuilder partialUrl = new StringBuilder();
            String contextPath = WebApplication.get().getServletContext().getContextPath();
            partialUrl.append(contextPath);
            if (!"/".equals(contextPath)) {
                partialUrl.append('/');
            }
            String filterPath = WebApplication.get().getWicketFilter().getFilterPath();
            if ("/".equals(filterPath)) {
                filterPath = "";
            } else if (filterPath.endsWith("/")) {
                filterPath = filterPath.substring(0, filterPath.length() - 1);
            }
            partialUrl.append(filterPath);
            partialUrl.append(url.toString());
            // Set the url the resource is going to be pushed with
            pushItem.setUrl(partialUrl.toString());
            // Apply the push item to be used during the push process
            this.pushItems.add(pushItem);
        }
    return this;
}
Also used : IRequestHandler(org.apache.wicket.request.IRequestHandler) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) Page(org.apache.wicket.Page) WebPage(org.apache.wicket.markup.html.WebPage) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) Url(org.apache.wicket.request.Url) ResourceReference(org.apache.wicket.request.resource.ResourceReference) PageParametersEncoder(org.apache.wicket.request.mapper.parameter.PageParametersEncoder)

Example 64 with RequestCycle

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

the class OrientDefaultExceptionsHandlingListener method extractCurrentPage.

private Page extractCurrentPage(boolean fullSearch) {
    final RequestCycle requestCycle = RequestCycle.get();
    IRequestHandler handler = requestCycle.getActiveRequestHandler();
    if (handler == null) {
        handler = requestCycle.getRequestHandlerScheduledAfterCurrent();
        if (handler == null && fullSearch) {
            handler = OrientDbWebApplication.get().getRootRequestMapper().mapRequest(requestCycle.getRequest());
        }
    }
    if (handler instanceof IPageRequestHandler) {
        IPageRequestHandler pageRequestHandler = (IPageRequestHandler) handler;
        return (Page) pageRequestHandler.getPage();
    }
    return null;
}
Also used : IRequestHandler(org.apache.wicket.request.IRequestHandler) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) IRequestCycle(org.apache.wicket.request.IRequestCycle) IPageRequestHandler(org.apache.wicket.core.request.handler.IPageRequestHandler) Page(org.apache.wicket.Page)

Example 65 with RequestCycle

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

the class ApplicationHelper method ensureApplication.

public static IApplication ensureApplication(Long langId) {
    IApplication a = ensureApplication();
    if (ThreadContext.getRequestCycle() == null) {
        ServletWebRequest req = new ServletWebRequest(new MockHttpServletRequest((Application) a, new MockHttpSession(a.getServletContext()), a.getServletContext()), "");
        RequestCycleContext rctx = new RequestCycleContext(req, new MockWebResponse(), a.getRootRequestMapper(), a.getExceptionMapperProvider().get());
        ThreadContext.setRequestCycle(new RequestCycle(rctx));
    }
    if (ThreadContext.getSession() == null) {
        WebSession s = WebSession.get();
        if (langId > 0) {
            ((IWebSession) s).setLanguage(langId);
        }
    }
    return a;
}
Also used : MockWebResponse(org.apache.wicket.mock.MockWebResponse) IApplication(org.apache.openmeetings.IApplication) RequestCycleContext(org.apache.wicket.request.cycle.RequestCycleContext) IWebSession(org.apache.openmeetings.IWebSession) WebSession(org.apache.wicket.protocol.http.WebSession) IWebSession(org.apache.openmeetings.IWebSession) MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) MockHttpSession(org.apache.wicket.protocol.http.mock.MockHttpSession) ServletWebRequest(org.apache.wicket.protocol.http.servlet.ServletWebRequest) IApplication(org.apache.openmeetings.IApplication) Application(org.apache.wicket.Application) WebApplication(org.apache.wicket.protocol.http.WebApplication)

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