Search in sources :

Example 6 with IRequestCycle

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

the class RedirectRequestHandlerTest method permenanentlyMovedShouldSetLocationHeader.

/**
 * permenanentlyMovedShouldSetLocationHeader()
 */
@Test
public void permenanentlyMovedShouldSetLocationHeader() {
    RedirectRequestHandler handler = new RedirectRequestHandler(REDIRECT_URL, HttpServletResponse.SC_MOVED_PERMANENTLY);
    IRequestCycle requestCycle = Mockito.mock(IRequestCycle.class);
    WebResponse webResponse = Mockito.mock(WebResponse.class);
    Mockito.when(requestCycle.getResponse()).thenReturn(webResponse);
    handler.respond(requestCycle);
    Mockito.verify(webResponse).setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
    Mockito.verify(webResponse).setHeader("Location", REDIRECT_URL);
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse) IRequestCycle(org.apache.wicket.request.IRequestCycle) Test(org.junit.Test)

Example 7 with IRequestCycle

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

the class RequestCycleListenerTest method basicOperations.

/**
 * @throws Exception
 */
@Test
public void basicOperations() throws Exception {
    IncrementingListener incrementingListener = new IncrementingListener();
    Application.get().getRequestCycleListeners().add(incrementingListener);
    RequestCycle cycle = newRequestCycle((RuntimeException) null);
    incrementingListener.assertValues(0, 0, 0, 0, 0, 0);
    assertValues(0, 0, 0);
    cycle.processRequestAndDetach();
    // 0 exceptions mapped
    incrementingListener.assertValues(1, 1, 1, 1, 0, 1);
    // 0 exceptions mapped
    assertValues(0, 1, 1);
    // TEST WITH TWO LISTENERS
    cycle = newRequestCycle((RuntimeException) null);
    cycle.getListeners().add(incrementingListener);
    cycle.processRequestAndDetach();
    // 0 exceptions mapped, all other 2 due to two listeners
    incrementingListener.assertValues(2, 2, 2, 2, 0, 2);
    // 0 exceptions mapped
    assertValues(0, 1, 1);
    // TEST WITH TWO LISTENERS AND AN EXCEPTION DURING RESPONSE
    cycle = newRequestCycle(new RuntimeException("testing purposes only"));
    cycle.getListeners().add(incrementingListener);
    cycle.processRequestAndDetach();
    // 0 executed
    incrementingListener.assertValues(2, 2, 2, 0, 2, 2);
    // 1 exception mapped, 0 responded
    assertValues(1, 0, 1);
    // TEST A REPLACE EXCEPTION DURING RESPONSE
    IRequestHandler replacement = new IRequestHandler() {

        @Override
        public void respond(IRequestCycle requestCycle) {
            responses++;
        }

        @Override
        public void detach(IRequestCycle requestCycle) {
            detaches++;
        }
    };
    cycle = newRequestCycle(new ReplaceHandlerException(replacement, true));
    cycle.scheduleRequestHandlerAfterCurrent(new IRequestHandler() {

        @Override
        public void respond(IRequestCycle requestCycle) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void detach(IRequestCycle requestCycle) {
            throw new UnsupportedOperationException();
        }
    });
    cycle.processRequestAndDetach();
    // 2 resolved, 1 executed, 0 exception mapped
    incrementingListener.assertValues(1, 1, 2, 1, 0, 1);
    // 0 exception mapped, 1 responded, 2 detached
    assertValues(0, 1, 2);
    // TEST A REPLACE EXCEPTION DURING RESPONSE
    cycle = newRequestCycle(new ReplaceHandlerException(replacement, false));
    cycle.scheduleRequestHandlerAfterCurrent(new IRequestHandler() {

        @Override
        public void respond(IRequestCycle requestCycle) {
            responses++;
        }

        @Override
        public void detach(IRequestCycle requestCycle) {
            detaches++;
        }
    });
    cycle.processRequestAndDetach();
    // 2 resolved, 2 executed, 0 exception mapped
    incrementingListener.assertValues(1, 1, 3, 2, 0, 1);
    // 0 exception mapped, 2 responded, 3 detached
    assertValues(0, 2, 3);
}
Also used : IRequestHandler(org.apache.wicket.request.IRequestHandler) ReplaceHandlerException(org.apache.wicket.request.RequestHandlerExecutor.ReplaceHandlerException) IRequestCycle(org.apache.wicket.request.IRequestCycle) IRequestCycle(org.apache.wicket.request.IRequestCycle) Test(org.junit.Test)

Example 8 with IRequestCycle

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

the class AutoCompleteBehavior method onRequest.

@Override
protected final void onRequest(final String val, final RequestCycle requestCycle) {
    IRequestHandler target = new IRequestHandler() {

        @Override
        public void respond(final IRequestCycle requestCycle) {
            WebResponse r = (WebResponse) requestCycle.getResponse();
            // Determine encoding
            final String encoding = Application.get().getRequestCycleSettings().getResponseRequestEncoding();
            r.setContentType("text/xml; charset=" + encoding);
            r.disableCaching();
            Iterator<T> comps = getChoices(val);
            int count = 0;
            renderer.renderHeader(r);
            while (comps.hasNext()) {
                final T comp = comps.next();
                renderer.render(comp, r, val);
                count += 1;
            }
            renderer.renderFooter(r, count);
        }
    };
    requestCycle.scheduleRequestHandlerAfterCurrent(target);
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse) IRequestHandler(org.apache.wicket.request.IRequestHandler) IRequestCycle(org.apache.wicket.request.IRequestCycle)

Example 9 with IRequestCycle

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

the class BufferedResponseMapper method getSessionId.

/**
 * @return the current session id for stateful pages and <code>null</code> for stateless pages
 *         and non-http threads
 */
protected String getSessionId() {
    String sessionId = null;
    if (Application.exists() && RequestCycle.get() != null) {
        ISessionStore sessionStore = Application.get().getSessionStore();
        IRequestCycle requestCycle = RequestCycle.get();
        Session session = sessionStore.lookup(requestCycle.getRequest());
        if (session != null) {
            sessionId = session.getId();
        }
    }
    return sessionId;
}
Also used : ISessionStore(org.apache.wicket.session.ISessionStore) IRequestCycle(org.apache.wicket.request.IRequestCycle) Session(org.apache.wicket.Session)

Example 10 with IRequestCycle

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

the class RedirectRequestHandlerTest method seeOtherShouldSetLocationHeader.

/**
 * https://issues.apache.org/jira/browse/WICKET-5131
 */
@Test
public void seeOtherShouldSetLocationHeader() {
    RedirectRequestHandler handler = new RedirectRequestHandler(REDIRECT_URL, HttpServletResponse.SC_SEE_OTHER);
    IRequestCycle requestCycle = Mockito.mock(IRequestCycle.class);
    WebResponse webResponse = Mockito.mock(WebResponse.class);
    Mockito.when(requestCycle.getResponse()).thenReturn(webResponse);
    handler.respond(requestCycle);
    Mockito.verify(webResponse).setStatus(HttpServletResponse.SC_SEE_OTHER);
    Mockito.verify(webResponse).setHeader("Location", REDIRECT_URL);
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse) IRequestCycle(org.apache.wicket.request.IRequestCycle) Test(org.junit.Test)

Aggregations

IRequestCycle (org.apache.wicket.request.IRequestCycle)13 WebResponse (org.apache.wicket.request.http.WebResponse)5 IRequestHandler (org.apache.wicket.request.IRequestHandler)4 ResourceStreamRequestHandler (org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler)4 IResourceStream (org.apache.wicket.util.resource.IResourceStream)4 Test (org.junit.Test)4 Request (org.apache.wicket.request.Request)2 ReplaceHandlerException (org.apache.wicket.request.RequestHandlerExecutor.ReplaceHandlerException)2 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)2 FileResourceStream (org.apache.wicket.util.resource.FileResourceStream)2 File (java.io.File)1 InputStream (java.io.InputStream)1 Application (org.apache.wicket.Application)1 Session (org.apache.wicket.Session)1 PageProvider (org.apache.wicket.core.request.handler.PageProvider)1 RenderPageRequestHandler (org.apache.wicket.core.request.handler.RenderPageRequestHandler)1 MockWebRequest (org.apache.wicket.mock.MockWebRequest)1 IExceptionMapper (org.apache.wicket.request.IExceptionMapper)1 IRequestMapper (org.apache.wicket.request.IRequestMapper)1 Response (org.apache.wicket.request.Response)1