Search in sources :

Example 91 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project incubator-pulsar by apache.

the class Filters method addFilterClass.

/**
 * Adds a filter to the servlet context handler which gets instantiated and configured when the server starts.
 *
 * @param context servlet context handler instance
 * @param filter filter class
 * @param initParams initialization parameters used for configuring the filter instance
 */
public static void addFilterClass(ServletContextHandler context, Class<? extends Filter> filter, Map<String, String> initParams) {
    FilterHolder holder = new FilterHolder(filter);
    holder.setInitParameters(initParams);
    addFilterHolder(context, holder);
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder)

Example 92 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project activemq-artemis by apache.

the class WebServerComponent method createWebAppContext.

protected WebAppContext createWebAppContext(String url, String warFile, Path warDirectory, String virtualHost) {
    WebAppContext webapp = new WebAppContext();
    if (url.startsWith("/")) {
        webapp.setContextPath(url);
    } else {
        webapp.setContextPath("/" + url);
    }
    // add the filters needed for audit logging
    webapp.addFilter(new FilterHolder(JolokiaFilter.class), "/*", EnumSet.of(DispatcherType.INCLUDE, DispatcherType.REQUEST));
    webapp.addFilter(new FilterHolder(AuthenticationFilter.class), "/auth/login/*", EnumSet.of(DispatcherType.REQUEST));
    webapp.setWar(warDirectory.resolve(warFile).toString());
    webapp.setAttribute("org.eclipse.jetty.webapp.basetempdir", temporaryWarDir.toFile().getAbsolutePath());
    // Set the default authenticator factory to avoid NPE due to the following commit:
    // https://github.com/eclipse/jetty.project/commit/7e91d34177a880ecbe70009e8f200d02e3a0c5dd
    webapp.getSecurityHandler().setAuthenticatorFactory(new DefaultAuthenticatorFactory());
    webapp.setVirtualHosts(new String[] { virtualHost });
    return webapp;
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) DefaultAuthenticatorFactory(org.eclipse.jetty.security.DefaultAuthenticatorFactory)

Example 93 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project knox by apache.

the class ServiceRegistryFunctionsTest method testSetup.

private void testSetup(String username, Map<String, String> initParams) throws Exception {
    ServiceRegistry mockServiceRegistry = EasyMock.createNiceMock(ServiceRegistry.class);
    EasyMock.expect(mockServiceRegistry.lookupServiceURL("test-cluster", "NAMENODE")).andReturn("test-nn-scheme://test-nn-host:411").anyTimes();
    EasyMock.expect(mockServiceRegistry.lookupServiceURL("test-cluster", "JOBTRACKER")).andReturn("test-jt-scheme://test-jt-host:511").anyTimes();
    GatewayServices mockGatewayServices = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(mockGatewayServices.getService(ServiceType.SERVICE_REGISTRY_SERVICE)).andReturn(mockServiceRegistry).anyTimes();
    EasyMock.replay(mockServiceRegistry, mockGatewayServices);
    String descriptorUrl = getTestResource("rewrite.xml").toExternalForm();
    Log.setLog(new NoOpLogger());
    server = new ServletTester();
    server.setContextPath("/");
    server.getContext().addEventListener(new UrlRewriteServletContextListener());
    server.getContext().setInitParameter(UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME, descriptorUrl);
    server.getContext().setAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE, "test-cluster");
    server.getContext().setAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE, mockGatewayServices);
    FilterHolder setupFilter = server.addFilter(SetupFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    setupFilter.setFilter(new SetupFilter(username));
    FilterHolder rewriteFilter = server.addFilter(UrlRewriteServletFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    if (initParams != null) {
        for (Map.Entry<String, String> entry : initParams.entrySet()) {
            rewriteFilter.setInitParameter(entry.getKey(), entry.getValue());
        }
    }
    rewriteFilter.setFilter(new UrlRewriteServletFilter());
    interactions = new ArrayDeque<>();
    ServletHolder servlet = server.addServlet(MockServlet.class, "/");
    servlet.setServlet(new MockServlet("mock-servlet", interactions));
    server.start();
    interaction = new MockInteraction();
    request = HttpTester.newRequest();
    response = null;
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) MockInteraction(org.apache.knox.test.mock.MockInteraction) ServletTester(org.eclipse.jetty.servlet.ServletTester) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) UrlRewriteServletContextListener(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletContextListener) MockServlet(org.apache.knox.test.mock.MockServlet) UrlRewriteServletFilter(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletFilter) NoOpLogger(org.apache.knox.test.log.NoOpLogger) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) HashMap(java.util.HashMap) Map(java.util.Map)

Example 94 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project knox by apache.

the class HttpServer2 method addGlobalFilter.

@Override
public void addGlobalFilter(String name, String classname, Map<String, String> parameters) {
    final String[] ALL_URLS = { "/*" };
    FilterHolder filterHolder = getFilterHolder(name, classname, parameters);
    FilterMapping fmap = getFilterMapping(name, ALL_URLS);
    defineFilter(webAppContext, filterHolder, fmap);
    for (ServletContextHandler ctx : defaultContexts.keySet()) {
        defineFilter(ctx, filterHolder, fmap);
    }
    LOG.info("Added global filter '" + name + "' (class=" + classname + ")");
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) FilterMapping(org.eclipse.jetty.servlet.FilterMapping) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 95 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project knox by apache.

the class FrontendFunctionProcessorTest method testSetup.

private void testSetup(String username, Map<String, String> initParams, Attributes attributes) throws Exception {
    ServiceRegistry mockServiceRegistry = EasyMock.createNiceMock(ServiceRegistry.class);
    EasyMock.expect(mockServiceRegistry.lookupServiceURL("test-cluster", "NAMENODE")).andReturn("test-nn-scheme://test-nn-host:411").anyTimes();
    EasyMock.expect(mockServiceRegistry.lookupServiceURL("test-cluster", "JOBTRACKER")).andReturn("test-jt-scheme://test-jt-host:511").anyTimes();
    GatewayServices mockGatewayServices = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(mockGatewayServices.getService(ServiceType.SERVICE_REGISTRY_SERVICE)).andReturn(mockServiceRegistry).anyTimes();
    EasyMock.replay(mockServiceRegistry, mockGatewayServices);
    String descriptorUrl = TestUtils.getResourceUrl(FrontendFunctionProcessorTest.class, "rewrite.xml").toExternalForm();
    Log.setLog(new NoOpLogger());
    server = new ServletTester();
    server.setContextPath("/");
    server.getContext().addEventListener(new UrlRewriteServletContextListener());
    server.getContext().setInitParameter(UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME, descriptorUrl);
    if (attributes != null) {
        server.getContext().setAttributes(attributes);
    }
    server.getContext().setAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE, "test-cluster");
    server.getContext().setAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE, mockGatewayServices);
    FilterHolder setupFilter = server.addFilter(SetupFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    setupFilter.setFilter(new SetupFilter(username));
    FilterHolder rewriteFilter = server.addFilter(UrlRewriteServletFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    if (initParams != null) {
        for (Map.Entry<String, String> entry : initParams.entrySet()) {
            rewriteFilter.setInitParameter(entry.getKey(), entry.getValue());
        }
    }
    rewriteFilter.setFilter(new UrlRewriteServletFilter());
    interactions = new ArrayDeque<>();
    ServletHolder servlet = server.addServlet(MockServlet.class, "/");
    servlet.setServlet(new MockServlet("mock-servlet", interactions));
    server.start();
    interaction = new MockInteraction();
    request = HttpTester.newRequest();
    response = null;
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) MockInteraction(org.apache.knox.test.mock.MockInteraction) ServletTester(org.eclipse.jetty.servlet.ServletTester) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) UrlRewriteServletContextListener(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletContextListener) MockServlet(org.apache.knox.test.mock.MockServlet) UrlRewriteServletFilter(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletFilter) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) NoOpLogger(org.apache.knox.test.log.NoOpLogger) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) Map(java.util.Map) AttributesMap(org.eclipse.jetty.util.AttributesMap) HashMap(java.util.HashMap)

Aggregations

FilterHolder (org.eclipse.jetty.servlet.FilterHolder)233 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)105 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)86 DispatcherType (javax.servlet.DispatcherType)45 Server (org.eclipse.jetty.server.Server)40 Test (org.junit.Test)31 Filter (javax.servlet.Filter)27 CountDownLatch (java.util.concurrent.CountDownLatch)24 HashMap (java.util.HashMap)22 IOException (java.io.IOException)20 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)20 Map (java.util.Map)19 ServerConnector (org.eclipse.jetty.server.ServerConnector)19 File (java.io.File)17 FilterMapping (org.eclipse.jetty.servlet.FilterMapping)17 CrossOriginFilter (org.eclipse.jetty.servlets.CrossOriginFilter)16 ServletContainer (org.glassfish.jersey.servlet.ServletContainer)16 ServletHandler (org.eclipse.jetty.servlet.ServletHandler)15 ServletException (javax.servlet.ServletException)12 ArrayList (java.util.ArrayList)11