Search in sources :

Example 46 with ServletRequest

use of javax.servlet.ServletRequest in project gocd by gocd.

the class LocaleResolverTest method shouldSetLocaleStringToCurrentThread.

@Test
public void shouldSetLocaleStringToCurrentThread() throws IOException, ServletException {
    when(req.getLocale()).thenReturn(new Locale("ja"));
    CurrentLocale.setLocaleString("en");
    localeResolver.doFilter(req, res, new FilterChain() {

        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            localeInside = CurrentLocale.getLocaleString();
        }
    });
    assertThat(CurrentLocale.getLocaleString(), is("en"));
    assertThat(localeInside, is("ja"));
}
Also used : CurrentLocale(com.thoughtworks.go.i18n.CurrentLocale) Locale(java.util.Locale) ServletException(javax.servlet.ServletException) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) FilterChain(javax.servlet.FilterChain) IOException(java.io.IOException) Test(org.junit.Test)

Example 47 with ServletRequest

use of javax.servlet.ServletRequest in project gocd by gocd.

the class LocaleResolverTest method shouldFixThreadLocaleEvenIfFilterFails.

@Test
public void shouldFixThreadLocaleEvenIfFilterFails() throws IOException, ServletException {
    when(req.getLocale()).thenReturn(new Locale("ja"));
    CurrentLocale.setLocaleString("en");
    final RuntimeException exception = new RuntimeException("Oh no!");
    try {
        localeResolver.doFilter(req, res, new FilterChain() {

            public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
                throw exception;
            }
        });
        fail("exception should have been bubbled up");
    } catch (RuntimeException e) {
        assertThat(e, sameInstance(exception));
    }
    assertThat(CurrentLocale.getLocaleString(), is("en"));
}
Also used : CurrentLocale(com.thoughtworks.go.i18n.CurrentLocale) Locale(java.util.Locale) ServletException(javax.servlet.ServletException) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) FilterChain(javax.servlet.FilterChain) IOException(java.io.IOException) Test(org.junit.Test)

Example 48 with ServletRequest

use of javax.servlet.ServletRequest in project guice by google.

the class ServletTest method testRequestAndResponseBindings_matchesPassedParameters.

public void testRequestAndResponseBindings_matchesPassedParameters() throws Exception {
    final int[] filterInvoked = new int[1];
    final boolean[] servletInvoked = new boolean[1];
    createInjector(new ServletModule() {

        @Override
        protected void configureServlets() {
            final HttpServletRequest[] previousReq = new HttpServletRequest[1];
            final HttpServletResponse[] previousResp = new HttpServletResponse[1];
            final Provider<ServletRequest> servletReqProvider = getProvider(ServletRequest.class);
            final Provider<HttpServletRequest> reqProvider = getProvider(HttpServletRequest.class);
            final Provider<ServletResponse> servletRespProvider = getProvider(ServletResponse.class);
            final Provider<HttpServletResponse> respProvider = getProvider(HttpServletResponse.class);
            Filter filter = new Filter() {

                @Override
                public void init(FilterConfig filterConfig) {
                }

                @Override
                public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
                    filterInvoked[0]++;
                    assertSame(req, servletReqProvider.get());
                    assertSame(req, reqProvider.get());
                    if (previousReq[0] != null) {
                        assertEquals(req, previousReq[0]);
                    }
                    assertSame(resp, servletRespProvider.get());
                    assertSame(resp, respProvider.get());
                    if (previousResp[0] != null) {
                        assertEquals(resp, previousResp[0]);
                    }
                    chain.doFilter(previousReq[0] = new HttpServletRequestWrapper((HttpServletRequest) req), previousResp[0] = new HttpServletResponseWrapper((HttpServletResponse) resp));
                    assertSame(req, reqProvider.get());
                    assertSame(resp, respProvider.get());
                }

                @Override
                public void destroy() {
                }
            };
            filter("/*").through(filter);
            // filter twice to test wrapping in filters
            filter("/*").through(filter);
            serve("/*").with(new HttpServlet() {

                @Override
                protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
                    servletInvoked[0] = true;
                    assertSame(req, servletReqProvider.get());
                    assertSame(req, reqProvider.get());
                    assertSame(resp, servletRespProvider.get());
                    assertSame(resp, respProvider.get());
                }
            });
        }
    });
    GuiceFilter filter = new GuiceFilter();
    filter.doFilter(newFakeHttpServletRequest(), newFakeHttpServletResponse(), new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            throw new IllegalStateException("Shouldn't get here");
        }
    });
    assertEquals(2, filterInvoked[0]);
    assertTrue(servletInvoked[0]);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ServletTestUtils.newFakeHttpServletRequest(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest) ServletTestUtils.newFakeHttpServletResponse(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) HttpServlet(javax.servlet.http.HttpServlet) FilterChain(javax.servlet.FilterChain) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) ServletTestUtils.newFakeHttpServletResponse(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Provider(com.google.inject.Provider) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletTestUtils.newFakeHttpServletRequest(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest) ServletException(javax.servlet.ServletException) Filter(javax.servlet.Filter) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) FilterConfig(javax.servlet.FilterConfig)

Example 49 with ServletRequest

use of javax.servlet.ServletRequest in project guice by google.

the class ManagedServletPipeline method getRequestDispatcher.

/**
   * @return Returns a request dispatcher wrapped with a servlet mapped to the given path or null if
   *     no mapping was found.
   */
RequestDispatcher getRequestDispatcher(String path) {
    final String newRequestUri = path;
    for (final ServletDefinition servletDefinition : servletDefinitions) {
        if (servletDefinition.shouldServe(path)) {
            return new RequestDispatcher() {

                @Override
                public void forward(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
                    Preconditions.checkState(!servletResponse.isCommitted(), "Response has been committed--you can only call forward before" + " committing the response (hint: don't flush buffers)");
                    // clear buffer before forwarding
                    servletResponse.resetBuffer();
                    ServletRequest requestToProcess;
                    if (servletRequest instanceof HttpServletRequest) {
                        requestToProcess = wrapRequest((HttpServletRequest) servletRequest, newRequestUri);
                    } else {
                        // This should never happen, but instead of throwing an exception
                        // we will allow a happy case pass thru for maximum tolerance to
                        // legacy (and internal) code.
                        requestToProcess = servletRequest;
                    }
                    // now dispatch to the servlet
                    doServiceImpl(servletDefinition, requestToProcess, servletResponse);
                }

                @Override
                public void include(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
                    // route to the target servlet
                    doServiceImpl(servletDefinition, servletRequest, servletResponse);
                }

                private void doServiceImpl(ServletDefinition servletDefinition, ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
                    servletRequest.setAttribute(REQUEST_DISPATCHER_REQUEST, Boolean.TRUE);
                    try {
                        servletDefinition.doService(servletRequest, servletResponse);
                    } finally {
                        servletRequest.removeAttribute(REQUEST_DISPATCHER_REQUEST);
                    }
                }
            };
        }
    }
    //otherwise, can't process
    return null;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletResponse(javax.servlet.ServletResponse) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 50 with ServletRequest

use of javax.servlet.ServletRequest in project guice by google.

the class ServletTest method testNewRequestObject.

public void testNewRequestObject() throws CreationException, IOException, ServletException {
    final Injector injector = createInjector();
    final HttpServletRequest request = newFakeHttpServletRequest();
    GuiceFilter filter = new GuiceFilter();
    final boolean[] invoked = new boolean[1];
    FilterChain filterChain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            invoked[0] = true;
            assertNotNull(injector.getInstance(InRequest.class));
            assertNull(injector.getInstance(IN_REQUEST_NULL_KEY));
        }
    };
    filter.doFilter(request, null, filterChain);
    assertTrue(invoked[0]);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletTestUtils.newFakeHttpServletRequest(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ServletTestUtils.newFakeHttpServletRequest(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest) ServletTestUtils.newFakeHttpServletResponse(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) Injector(com.google.inject.Injector) FilterChain(javax.servlet.FilterChain)

Aggregations

ServletRequest (javax.servlet.ServletRequest)185 ServletResponse (javax.servlet.ServletResponse)129 HttpServletRequest (javax.servlet.http.HttpServletRequest)117 HttpServletResponse (javax.servlet.http.HttpServletResponse)95 FilterChain (javax.servlet.FilterChain)79 Test (org.junit.Test)75 ServletException (javax.servlet.ServletException)59 IOException (java.io.IOException)57 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)35 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)35 MockFilterChain (org.springframework.mock.web.MockFilterChain)32 Filter (javax.servlet.Filter)28 Injector (com.google.inject.Injector)25 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)21 NestedServletException (org.springframework.web.util.NestedServletException)19 ServletTestUtils.newFakeHttpServletRequest (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest)18 ServletTestUtils.newFakeHttpServletResponse (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse)18 HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)15 ErrorPage (org.springframework.boot.web.server.ErrorPage)15 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)14