Search in sources :

Example 81 with ServletResponse

use of javax.servlet.ServletResponse in project jetty.project by eclipse.

the class ServletHandler method doHandle.

@Override
public void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    ServletHolder servlet_holder = (ServletHolder) baseRequest.getUserIdentityScope();
    FilterChain chain = null;
    // find the servlet
    if (target.startsWith("/")) {
        if (servlet_holder != null && _filterMappings != null && _filterMappings.length > 0)
            chain = getFilterChain(baseRequest, target, servlet_holder);
    } else {
        if (servlet_holder != null) {
            if (_filterMappings != null && _filterMappings.length > 0) {
                chain = getFilterChain(baseRequest, null, servlet_holder);
            }
        }
    }
    if (LOG.isDebugEnabled())
        LOG.debug("chain={}", chain);
    try {
        if (servlet_holder == null)
            notFound(baseRequest, request, response);
        else {
            // unwrap any tunnelling of base Servlet request/responses
            ServletRequest req = request;
            if (req instanceof ServletRequestHttpWrapper)
                req = ((ServletRequestHttpWrapper) req).getRequest();
            ServletResponse res = response;
            if (res instanceof ServletResponseHttpWrapper)
                res = ((ServletResponseHttpWrapper) res).getResponse();
            // Do the filter/handling thang
            servlet_holder.prepare(baseRequest, req, res);
            if (chain != null)
                chain.doFilter(req, res);
            else
                servlet_holder.handle(baseRequest, req, res);
        }
    } finally {
        if (servlet_holder != null)
            baseRequest.setHandled(true);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponseHttpWrapper(org.eclipse.jetty.server.ServletResponseHttpWrapper) FilterChain(javax.servlet.FilterChain) ServletRequestHttpWrapper(org.eclipse.jetty.server.ServletRequestHttpWrapper)

Example 82 with ServletResponse

use of javax.servlet.ServletResponse in project gitblit by gitblit.

the class ProxyFilter method doFilter.

@Override
public void doFilter(ServletRequest req, ServletResponse res, final FilterChain last) throws IOException, ServletException {
    final Iterator<HttpRequestFilter> itr = filters.iterator();
    new FilterChain() {

        @Override
        public void doFilter(ServletRequest req, ServletResponse res) throws IOException, ServletException {
            if (itr.hasNext()) {
                itr.next().doFilter(req, res, this);
            } else {
                last.doFilter(req, res);
            }
        }
    }.doFilter(req, res);
}
Also used : ServletException(javax.servlet.ServletException) ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) HttpRequestFilter(com.gitblit.extensions.HttpRequestFilter) FilterChain(javax.servlet.FilterChain) IOException(java.io.IOException)

Example 83 with ServletResponse

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

the class FlashLoadingFilterIntegrationTest method shouldClearThreadContextInCaseOfExceptionAsWell.

@Test
public void shouldClearThreadContextInCaseOfExceptionAsWell() throws IOException, ServletException {
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();
    FilterChain filterChain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) {
            messageKey = service.add(new FlashMessageModel("my message", "error"));
            flash = service.get(messageKey);
            throw new RuntimeException("exception here");
        }
    };
    try {
        filter.doFilter(req, res, filterChain);
        fail("exception gobbled");
    } catch (Exception e) {
        assertThat(e.getMessage(), is("exception here"));
    }
    assertThat(flash.toString(), is("my message"));
    try {
        service.get(messageKey);
        fail("attempt to load flash message should fail, as no thread local is cleared out");
    } catch (RuntimeException e) {
        assertThat(e.getMessage(), is("No flash context found, this call should only be made within a request."));
    }
}
Also used : ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockFilterChain(org.springframework.mock.web.MockFilterChain) FlashMessageModel(com.thoughtworks.go.presentation.FlashMessageModel) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Test(org.junit.Test)

Example 84 with ServletResponse

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

the class FlashLoadingFilterIntegrationTest method shouldInitializeFlashIfNotPresent.

@Test
public void shouldInitializeFlashIfNotPresent() throws IOException, ServletException {
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();
    FilterChain filterChain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) {
            messageKey = service.add(new FlashMessageModel("my message", "error"));
            flash = service.get(messageKey);
        }
    };
    assertThat(messageKey, is(nullValue()));
    filter.doFilter(req, res, filterChain);
    assertThat(messageKey, not(nullValue()));
    assertThat(flash.toString(), is("my message"));
    assertThat(flash.getFlashClass(), is("error"));
}
Also used : ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockFilterChain(org.springframework.mock.web.MockFilterChain) FlashMessageModel(com.thoughtworks.go.presentation.FlashMessageModel) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 85 with ServletResponse

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

the class FilterDefinitionTest method testFilterCreateDispatchDestroy.

public final void testFilterCreateDispatchDestroy() throws ServletException, IOException {
    Injector injector = createMock(Injector.class);
    Binding binding = createMock(Binding.class);
    HttpServletRequest request = createMock(HttpServletRequest.class);
    final MockFilter mockFilter = new MockFilter();
    expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
    expect(injector.getBinding(Key.get(Filter.class))).andReturn(binding);
    expect(injector.getInstance(Key.get(Filter.class))).andReturn(mockFilter).anyTimes();
    expect(request.getRequestURI()).andReturn("/index.html");
    expect(request.getContextPath()).andReturn("").anyTimes();
    replay(injector, binding, request);
    String pattern = "/*";
    final FilterDefinition filterDef = new FilterDefinition(Key.get(Filter.class), UriPatternType.get(UriPatternType.SERVLET, pattern), new HashMap<String, String>(), null);
    //should fire on mockfilter now
    filterDef.init(createMock(ServletContext.class), injector, Sets.<Filter>newIdentityHashSet());
    assertTrue(filterDef.getFilter() instanceof MockFilter);
    assertTrue("Init did not fire", mockFilter.isInit());
    Filter matchingFilter = filterDef.getFilterIfMatching(request);
    assertSame(mockFilter, matchingFilter);
    final boolean[] proceed = new boolean[1];
    matchingFilter.doFilter(request, null, new FilterChainInvocation(null, null, null) {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            proceed[0] = true;
        }
    });
    assertTrue("Filter did not proceed down chain", proceed[0]);
    filterDef.destroy(Sets.<Filter>newIdentityHashSet());
    assertTrue("Destroy did not fire", mockFilter.isDestroy());
    verify(injector, request);
}
Also used : Binding(com.google.inject.Binding) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletResponse(javax.servlet.ServletResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) Filter(javax.servlet.Filter) Injector(com.google.inject.Injector) ServletContext(javax.servlet.ServletContext)

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