Search in sources :

Example 61 with FilterChain

use of javax.servlet.FilterChain in project felix by apache.

the class ServletResponseWrapper method sendError.

@Override
public void sendError(final int code, final String message) throws IOException {
    resetBuffer();
    setStatus(code);
    boolean invokeSuper = true;
    if (invocationCount.incrementAndGet() == 1) {
        // If we are allowed to have a body
        if (code != SC_NO_CONTENT && code != SC_NOT_MODIFIED && code != SC_PARTIAL_CONTENT && code >= SC_OK) {
            final Throwable exception = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
            final ServletHandler errorResolution = (errorRegistry == null ? null : errorRegistry.getErrorHandler(code, exception));
            if (errorResolution != null) {
                try {
                    request.setAttribute(RequestDispatcher.ERROR_STATUS_CODE, new Integer(code));
                    if (message != null) {
                        request.setAttribute(RequestDispatcher.ERROR_MESSAGE, message);
                    }
                    request.setAttribute(RequestDispatcher.ERROR_REQUEST_URI, request.getRequestURI());
                    if (this.servletName != null) {
                        request.setAttribute(RequestDispatcher.ERROR_SERVLET_NAME, this.servletName);
                    }
                    final String servletPath = null;
                    final String pathInfo = request.getRequestURI();
                    // XXX
                    final String queryString = null;
                    final RequestInfo requestInfo = new RequestInfo(servletPath, pathInfo, queryString, pathInfo);
                    final FilterHandler[] filterHandlers = errorRegistry.getFilterHandlers(errorResolution, DispatcherType.ERROR, request.getRequestURI());
                    final ServletRequestWrapper reqWrapper = new ServletRequestWrapper(request, errorResolution.getContext(), requestInfo, null, false, null, null);
                    final FilterChain filterChain = new InvocationChain(errorResolution, filterHandlers);
                    filterChain.doFilter(reqWrapper, this);
                    invokeSuper = false;
                } catch (final ServletException e) {
                // ignore
                } finally {
                    request.removeAttribute(RequestDispatcher.ERROR_STATUS_CODE);
                    request.removeAttribute(RequestDispatcher.ERROR_MESSAGE);
                    request.removeAttribute(RequestDispatcher.ERROR_REQUEST_URI);
                    request.removeAttribute(RequestDispatcher.ERROR_SERVLET_NAME);
                    request.removeAttribute(RequestDispatcher.ERROR_EXCEPTION);
                    request.removeAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE);
                }
            }
        }
    }
    if (invokeSuper) {
        super.sendError(code, message);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServletException(javax.servlet.ServletException) ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) FilterChain(javax.servlet.FilterChain) FilterHandler(org.apache.felix.http.base.internal.handler.FilterHandler)

Example 62 with FilterChain

use of javax.servlet.FilterChain in project felix by apache.

the class WhiteboardManager method invokePreprocessors.

/**
 * Invoke all preprocessors
 *
 * @param req The request
 * @param res The response
 * @return {@code true} to continue with dispatching, {@code false} to terminate the request.
 * @throws IOException
 * @throws ServletException
 */
public void invokePreprocessors(final HttpServletRequest req, final HttpServletResponse res, final Preprocessor dispatcher) throws ServletException, IOException {
    final List<PreprocessorHandler> localHandlers = this.preprocessorHandlers;
    if (localHandlers.isEmpty()) {
        // no preprocessors, we can directly execute
        dispatcher.doFilter(req, res, null);
    } else {
        final FilterChain chain = new FilterChain() {

            private int index = 0;

            @Override
            public void doFilter(final ServletRequest request, final ServletResponse response) throws IOException, ServletException {
                if (index == localHandlers.size()) {
                    dispatcher.doFilter(request, response, null);
                } else {
                    final PreprocessorHandler handler = localHandlers.get(index);
                    index++;
                    handler.handle(request, response, this);
                }
            }
        };
        chain.doFilter(req, res);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) PreprocessorHandler(org.apache.felix.http.base.internal.handler.PreprocessorHandler) FilterChain(javax.servlet.FilterChain)

Example 63 with FilterChain

use of javax.servlet.FilterChain in project felix by apache.

the class FilterHandlerTest method testHandleFoundContextRoot.

@Test
public void testHandleFoundContextRoot() throws Exception {
    FilterHandler h1 = createHandler(0, "/");
    HttpServletRequest req = createServletRequest();
    HttpServletResponse res = createServletResponse();
    FilterChain chain = mock(FilterChain.class);
    when(this.context.handleSecurity(req, res)).thenReturn(true);
    when(req.getRequestURI()).thenReturn(null);
    h1.handle(req, res, chain);
    verify(this.filter).doFilter(req, res, chain);
    verify(chain, never()).doFilter(req, res);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 64 with FilterChain

use of javax.servlet.FilterChain in project felix by apache.

the class HttpServiceTest method setupOldWhiteboardFilter.

public void setupOldWhiteboardFilter(final String pattern) throws Exception {
    Dictionary<String, Object> servletProps = new Hashtable<String, Object>();
    servletProps.put("pattern", pattern);
    final Filter f = new Filter() {

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            initLatch.countDown();
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            response.getWriter().print("FILTER-");
            response.flushBuffer();
            chain.doFilter(request, response);
        }

        @Override
        public void destroy() {
            destroyLatch.countDown();
        }
    };
    registrations.add(m_context.registerService(Filter.class.getName(), f, servletProps));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) Filter(javax.servlet.Filter) Hashtable(java.util.Hashtable) FilterChain(javax.servlet.FilterChain) FilterConfig(javax.servlet.FilterConfig)

Example 65 with FilterChain

use of javax.servlet.FilterChain in project felix by apache.

the class JettyServiceTest method testInitBundleContextDeployIT.

/**
 * Tests to ensure the osgi-bundlecontext is available for init methods.
 *
 * @throws MalformedURLException
 * @throws InterruptedException
 */
@Test
public void testInitBundleContextDeployIT() throws Exception {
    // Setup mocks
    Deployment mockDeployment = mock(Deployment.class);
    Bundle mockBundle = mock(Bundle.class);
    BundleContext mockBundleContext = mock(BundleContext.class);
    // Setup behaviors
    when(mockDeployment.getBundle()).thenReturn(mockBundle);
    final org.osgi.framework.Filter f = mock(org.osgi.framework.Filter.class);
    when(f.toString()).thenReturn("(prop=*)");
    when(mockBundleContext.createFilter(anyString())).thenReturn(f);
    when(mockBundle.getBundleContext()).thenReturn(mockBundleContext);
    when(mockBundle.getSymbolicName()).thenReturn("test");
    when(mockBundle.getVersion()).thenReturn(new Version("0.0.1"));
    Dictionary<String, String> headerProperties = new Hashtable<String, String>();
    headerProperties.put("Web-ContextPath", "test");
    when(mockBundle.getHeaders()).thenReturn(headerProperties);
    when(mockDeployment.getContextPath()).thenReturn("test");
    when(mockBundle.getEntry("/")).thenReturn(new URL("http://www.apache.com"));
    when(mockBundle.getState()).thenReturn(Bundle.ACTIVE);
    EnumSet<DispatcherType> dispatcherSet = EnumSet.allOf(DispatcherType.class);
    dispatcherSet.add(DispatcherType.REQUEST);
    WebAppBundleContext webAppBundleContext = new WebAppBundleContext("/", mockBundle, this.getClass().getClassLoader());
    final CountDownLatch testLatch = new CountDownLatch(2);
    // Add a Filter to test whether the osgi-bundlecontext is available at init
    webAppBundleContext.addServlet(new ServletHolder(new Servlet() {

        @Override
        public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        // Do Nothing
        }

        @Override
        public void init(ServletConfig config) throws ServletException {
            ServletContext context = config.getServletContext();
            assertNotNull(context.getAttribute(OSGI_BUNDLECONTEXT));
            testLatch.countDown();
        }

        @Override
        public String getServletInfo() {
            return null;
        }

        @Override
        public ServletConfig getServletConfig() {
            return null;
        }

        @Override
        public void destroy() {
        // Do Nothing
        }
    }), "/test1");
    webAppBundleContext.addFilter(new FilterHolder(new Filter() {

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            ServletContext context = filterConfig.getServletContext();
            assertNotNull(context.getAttribute(OSGI_BUNDLECONTEXT));
            testLatch.countDown();
        }

        @Override
        public void doFilter(ServletRequest arg0, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // Do Nothing
        }

        @Override
        public void destroy() {
        // Do Nothing
        }
    }), "/test2", dispatcherSet);
    jettyService.deploy(mockDeployment, webAppBundleContext);
    // Fail if takes too long.
    if (!testLatch.await(10, TimeUnit.SECONDS)) {
        fail("Test Was not asserted");
    }
}
Also used : ServletRequest(javax.servlet.ServletRequest) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) FilterChain(javax.servlet.FilterChain) Deployment(org.apache.felix.http.jetty.internal.JettyService.Deployment) Matchers.anyString(org.mockito.Matchers.anyString) URL(java.net.URL) Version(org.osgi.framework.Version) Servlet(javax.servlet.Servlet) ServletContext(javax.servlet.ServletContext) FilterConfig(javax.servlet.FilterConfig) DispatcherType(javax.servlet.DispatcherType) ServletResponse(javax.servlet.ServletResponse) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) ServletConfig(javax.servlet.ServletConfig) CountDownLatch(java.util.concurrent.CountDownLatch) Filter(javax.servlet.Filter) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Aggregations

FilterChain (javax.servlet.FilterChain)418 HttpServletRequest (javax.servlet.http.HttpServletRequest)317 HttpServletResponse (javax.servlet.http.HttpServletResponse)269 Test (org.junit.Test)246 ServletResponse (javax.servlet.ServletResponse)135 ServletRequest (javax.servlet.ServletRequest)118 FilterConfig (javax.servlet.FilterConfig)80 Filter (javax.servlet.Filter)68 ServletException (javax.servlet.ServletException)54 IOException (java.io.IOException)48 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)46 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)46 Injector (com.google.inject.Injector)32 ServletTestUtils.newFakeHttpServletRequest (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest)25 ServletContext (javax.servlet.ServletContext)25 Test (org.testng.annotations.Test)25 HttpSession (javax.servlet.http.HttpSession)24 MockFilterChain (org.springframework.mock.web.MockFilterChain)24 InvocationOnMock (org.mockito.invocation.InvocationOnMock)22 Properties (java.util.Properties)19