Search in sources :

Example 86 with ServletRequest

use of javax.servlet.ServletRequest in project ddf by codice.

the class DelegateServletFilterTest method testDoFilterWithFilters.

/**
     * Tests the main logic of performing the filter with adding filters.
     *
     * @throws ServletException
     * @throws IOException
     * @throws InvalidSyntaxException
     */
@Test
public void testDoFilterWithFilters() throws IOException, ServletException, InvalidSyntaxException {
    ServletRequest request = mock(HttpServletRequest.class);
    ServletResponse response = mock(HttpServletResponse.class);
    final BundleContext context = createMockContext(true);
    DelegateServletFilter filter = new DelegateServletFilter() {

        @Override
        protected BundleContext getContext() {
            return context;
        }
    };
    filter.doFilter(request, response, initialChain);
    verifyFiltersCalled(request, response, initialChain);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 87 with ServletRequest

use of javax.servlet.ServletRequest in project ddf by codice.

the class ProxyFilterChainTest method testDoFilter.

/**
     * Tests that all of the filters are properly called.
     *
     * @throws ServletException
     * @throws IOException
     */
@Test
public void testDoFilter() throws IOException, ServletException {
    FilterChain initialChain = mock(FilterChain.class);
    ProxyFilterChain proxyChain = new ProxyFilterChain(initialChain);
    Filter filter1 = createMockFilter("filter1");
    Filter filter2 = createMockFilter("filter2");
    Filter filter3 = createMockFilter("filter3");
    ServletRequest request = mock(ServletRequest.class);
    ServletResponse response = mock(ServletResponse.class);
    proxyChain.addFilter(filter1);
    proxyChain.addFilters(Arrays.asList(filter2, filter3));
    proxyChain.doFilter(request, response);
    // Verify that all of the filters were called once.
    verify(filter1).doFilter(request, response, proxyChain);
    verify(filter2).doFilter(request, response, proxyChain);
    verify(filter3).doFilter(request, response, proxyChain);
    // the initial chain should have also been called once (at the end).
    verify(initialChain).doFilter(request, response);
}
Also used : ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) Test(org.junit.Test)

Example 88 with ServletRequest

use of javax.servlet.ServletRequest in project ddf by codice.

the class ProxyFilterChainTest method createMockFilter.

private Filter createMockFilter(final String name) throws IOException, ServletException {
    Filter mockFilter = mock(Filter.class);
    Mockito.when(mockFilter.toString()).thenReturn(name);
    Mockito.doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            LOGGER.debug("{} was called.", name);
            ((FilterChain) args[2]).doFilter(((ServletRequest) args[0]), ((ServletResponse) args[1]));
            return null;
        }
    }).when(mockFilter).doFilter(any(ServletRequest.class), any(ServletResponse.class), any(FilterChain.class));
    return mockFilter;
}
Also used : ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) Filter(javax.servlet.Filter) InvocationOnMock(org.mockito.invocation.InvocationOnMock) FilterChain(javax.servlet.FilterChain)

Example 89 with ServletRequest

use of javax.servlet.ServletRequest in project processdash by dtuma.

the class DashboardUriPrefixFilter method doFilter.

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    String prefix = getPrefix(request);
    if (prefix == null) {
        chain.doFilter(request, response);
    } else {
        Request req = Request.getRequest((HttpServletRequest) request);
        String origRequestURI = req.getRequestURI();
        String origContextPath = req.getContextPath();
        String fullRequestURI = prefix + origRequestURI;
        String fullContextPath = prefix + origContextPath;
        try {
            req.setRequestURI(fullRequestURI);
            req.setContextPath(fullContextPath);
            chain.doFilter(request, response);
        } finally {
            req.setRequestURI(origRequestURI);
            req.setContextPath(origContextPath);
        }
    }
}
Also used : Request(org.eclipse.jetty.server.Request) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest)

Example 90 with ServletRequest

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

the class AmIpFilter method doFilter.

@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
    ProxyUtils.rejectNonHttpRequests(req);
    HttpServletRequest httpReq = (HttpServletRequest) req;
    HttpServletResponse httpResp = (HttpServletResponse) resp;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Remote address for request is: {}", httpReq.getRemoteAddr());
    }
    if (!getProxyAddresses().contains(httpReq.getRemoteAddr())) {
        StringBuilder redirect = new StringBuilder(findRedirectUrl());
        redirect.append(httpReq.getRequestURI());
        int insertPoint = redirect.indexOf(PROXY_PATH);
        if (insertPoint >= 0) {
            // Add /redirect as the second component of the path so that the RM web
            // proxy knows that this request was a redirect.
            insertPoint += PROXY_PATH.length();
            redirect.insert(insertPoint, "/redirect");
        }
        ProxyUtils.sendRedirect(httpReq, httpResp, redirect.toString());
    } else {
        String user = null;
        if (httpReq.getCookies() != null) {
            for (Cookie c : httpReq.getCookies()) {
                if (WebAppProxyServlet.PROXY_USER_COOKIE_NAME.equals(c.getName())) {
                    user = c.getValue();
                    break;
                }
            }
        }
        if (user == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Could not find " + WebAppProxyServlet.PROXY_USER_COOKIE_NAME + " cookie, so user will not be set");
            }
            chain.doFilter(req, resp);
        } else {
            AmIpPrincipal principal = new AmIpPrincipal(user);
            ServletRequest requestWrapper = new AmIpServletRequestWrapper(httpReq, principal);
            chain.doFilter(requestWrapper, resp);
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Cookie(javax.servlet.http.Cookie) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse)

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