Search in sources :

Example 91 with ServletResponse

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

the class ServletTest method testExistingSessionObject.

public void testExistingSessionObject() 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;
            InSession inSession = injector.getInstance(InSession.class);
            assertSame(inSession, injector.getInstance(InSession.class));
            assertNull(injector.getInstance(IN_SESSION_NULL_KEY));
            assertNull(injector.getInstance(IN_SESSION_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)

Example 92 with ServletResponse

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

the class ServletTest method testHttpSessionIsSerializable.

public void testHttpSessionIsSerializable() throws Exception {
    final Injector injector = createInjector();
    final HttpServletRequest request = newFakeHttpServletRequest();
    final HttpSession session = request.getSession();
    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(InSession.class));
            assertNull(injector.getInstance(IN_SESSION_NULL_KEY));
        }
    };
    filter.doFilter(request, null, filterChain);
    assertTrue(invoked[0]);
    HttpSession deserializedSession = reserialize(session);
    String inSessionKey = IN_SESSION_KEY.toString();
    String inSessionNullKey = IN_SESSION_NULL_KEY.toString();
    assertTrue(deserializedSession.getAttribute(inSessionKey) instanceof InSession);
    assertEquals(NullObject.INSTANCE, deserializedSession.getAttribute(inSessionNullKey));
}
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) HttpSession(javax.servlet.http.HttpSession) FilterChain(javax.servlet.FilterChain)

Example 93 with ServletResponse

use of javax.servlet.ServletResponse in project hudson-2.x by hudson.

the class ChainedServletFilter method doFilter.

public void doFilter(ServletRequest request, ServletResponse response, final FilterChain chain) throws IOException, ServletException {
    LOGGER.entering(ChainedServletFilter.class.getName(), "doFilter");
    new FilterChain() {

        private int position = 0;

        // capture the array for thread-safety
        private final Filter[] filters = ChainedServletFilter.this.filters;

        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            if (position == filters.length) {
                // reached to the end
                chain.doFilter(request, response);
            } else {
                // call next
                filters[position++].doFilter(request, response, this);
            }
        }
    }.doFilter(request, response);
}
Also used : ServletException(javax.servlet.ServletException) ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) IOException(java.io.IOException)

Example 94 with ServletResponse

use of javax.servlet.ServletResponse in project hadoop by apache.

the class TestStaticUserWebFilter method testFilter.

@Test
public void testFilter() throws Exception {
    FilterConfig config = mockConfig("myuser");
    StaticUserFilter suf = new StaticUserFilter();
    suf.init(config);
    ArgumentCaptor<HttpServletRequestWrapper> wrapperArg = ArgumentCaptor.forClass(HttpServletRequestWrapper.class);
    FilterChain chain = mock(FilterChain.class);
    suf.doFilter(mock(HttpServletRequest.class), mock(ServletResponse.class), chain);
    Mockito.verify(chain).doFilter(wrapperArg.capture(), Mockito.<ServletResponse>anyObject());
    HttpServletRequestWrapper wrapper = wrapperArg.getValue();
    assertEquals("myuser", wrapper.getUserPrincipal().getName());
    assertEquals("myuser", wrapper.getRemoteUser());
    suf.destroy();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletResponse(javax.servlet.ServletResponse) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) FilterChain(javax.servlet.FilterChain) StaticUserFilter(org.apache.hadoop.http.lib.StaticUserWebFilter.StaticUserFilter) FilterConfig(javax.servlet.FilterConfig) Test(org.junit.Test)

Example 95 with ServletResponse

use of javax.servlet.ServletResponse in project hadoop by apache.

the class TestHostnameFilter method hostname.

@Test
public void hostname() throws Exception {
    ServletRequest request = Mockito.mock(ServletRequest.class);
    Mockito.when(request.getRemoteAddr()).thenReturn("localhost");
    ServletResponse response = Mockito.mock(ServletResponse.class);
    final AtomicBoolean invoked = new AtomicBoolean();
    FilterChain chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            // Hostname was set to "localhost", but may get resolved automatically to
            // "127.0.0.1" depending on OS.
            assertTrue(HostnameFilter.get().contains("localhost") || HostnameFilter.get().contains("127.0.0.1"));
            invoked.set(true);
        }
    };
    Filter filter = new HostnameFilter();
    filter.init(null);
    assertNull(HostnameFilter.get());
    filter.doFilter(request, response, chain);
    assertTrue(invoked.get());
    assertNull(HostnameFilter.get());
    filter.destroy();
}
Also used : ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) Test(org.junit.Test)

Aggregations

ServletResponse (javax.servlet.ServletResponse)155 ServletRequest (javax.servlet.ServletRequest)129 HttpServletResponse (javax.servlet.http.HttpServletResponse)104 HttpServletRequest (javax.servlet.http.HttpServletRequest)90 FilterChain (javax.servlet.FilterChain)81 Test (org.junit.Test)76 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 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)26 Injector (com.google.inject.Injector)25 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 FilterConfig (javax.servlet.FilterConfig)15 ErrorPage (org.springframework.boot.web.server.ErrorPage)15 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)14