Search in sources :

Example 66 with FilterHolder

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

the class WebSocketUpgradeFilter method configureContext.

public static WebSocketUpgradeFilter configureContext(ServletContextHandler context) throws ServletException {
    // Prevent double configure
    WebSocketUpgradeFilter filter = (WebSocketUpgradeFilter) context.getAttribute(WebSocketUpgradeFilter.class.getName());
    if (filter != null) {
        return filter;
    }
    // Dynamically add filter
    NativeWebSocketConfiguration configuration = NativeWebSocketServletContainerInitializer.getDefaultFrom(context.getServletContext());
    filter = new WebSocketUpgradeFilter(configuration);
    filter.setToAttribute(context, WebSocketUpgradeFilter.class.getName());
    String name = "Jetty_WebSocketUpgradeFilter";
    String pathSpec = "/*";
    EnumSet<DispatcherType> dispatcherTypes = EnumSet.of(DispatcherType.REQUEST);
    FilterHolder fholder = new FilterHolder(filter);
    fholder.setName(name);
    fholder.setAsyncSupported(true);
    fholder.setInitParameter(CONTEXT_ATTRIBUTE_KEY, WebSocketUpgradeFilter.class.getName());
    context.addFilter(fholder, pathSpec, dispatcherTypes);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Adding [{}] {} mapped to {} to {}", name, filter, pathSpec, context);
    }
    return filter;
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) DispatcherType(javax.servlet.DispatcherType)

Example 67 with FilterHolder

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

the class QoSFilterTest method testBlockingQosFilter.

@Test
public void testBlockingQosFilter() throws Exception {
    FilterHolder holder = new FilterHolder(QoSFilter2.class);
    holder.setAsyncSupported(true);
    holder.setInitParameter(QoSFilter.MAX_REQUESTS_INIT_PARAM, "" + MAX_QOS);
    _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    List<Worker> workers = new ArrayList<>();
    for (int i = 0; i < NUM_CONNECTIONS; ++i) {
        workers.add(new Worker(i));
    }
    ExecutorService executor = Executors.newFixedThreadPool(NUM_CONNECTIONS);
    List<Future<Void>> futures = executor.invokeAll(workers, 10, TimeUnit.SECONDS);
    rethrowExceptions(futures);
    if (TestServlet.__maxSleepers < MAX_QOS)
        LOG.warn("TEST WAS NOT PARALLEL ENOUGH!");
    else
        Assert.assertEquals(TestServlet.__maxSleepers, MAX_QOS);
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ArrayList(java.util.ArrayList) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Test(org.junit.Test)

Example 68 with FilterHolder

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

the class GzipFilterLayeredTest method testGzipDos.

@Test
public void testGzipDos() throws Exception {
    GzipTester tester = new GzipTester(testingdir, GzipHandler.GZIP);
    // Add Gzip Filter first
    FilterHolder gzipHolder = new FilterHolder(gzipFilterClass);
    gzipHolder.setAsyncSupported(true);
    tester.addFilter(gzipHolder, "*.txt", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    tester.addFilter(gzipHolder, "*.mp3", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    gzipHolder.setInitParameter("mimeTypes", "text/plain");
    // Add (DoSFilter-like) manip filter (in chain of Gzip)
    FilterHolder manipHolder = new FilterHolder(AsyncManipFilter.class);
    manipHolder.setAsyncSupported(true);
    tester.addFilter(manipHolder, "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    // Add content servlet
    tester.setContentServlet(contentServletClass);
    try {
        String testFilename = String.format("GzipDos-%s-%s", contentServletClass.getSimpleName(), fileName);
        File testFile = tester.prepareServerFile(testFilename, fileSize);
        tester.start();
        HttpTester.Response response = tester.executeRequest("GET", "/context/" + testFile.getName(), 5, TimeUnit.SECONDS);
        assertThat("Response status", response.getStatus(), is(HttpStatus.OK_200));
        if (expectCompressed) {
            // Must be gzip compressed
            assertThat("Content-Encoding", response.get("Content-Encoding"), containsString(GzipHandler.GZIP));
        }
        // Uncompressed content Size
        ContentMetadata content = tester.getResponseMetadata(response);
        assertThat("(Uncompressed) Content Length", content.size, is((long) fileSize));
    } finally {
        tester.stop();
    }
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) GzipTester(org.eclipse.jetty.server.handler.gzip.GzipTester) Matchers.containsString(org.hamcrest.Matchers.containsString) File(java.io.File) ContentMetadata(org.eclipse.jetty.server.handler.gzip.GzipTester.ContentMetadata) HttpTester(org.eclipse.jetty.http.HttpTester) Test(org.junit.Test)

Example 69 with FilterHolder

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

the class GzipFilterLayeredTest method testDosGzip.

@Test
public void testDosGzip() throws Exception {
    GzipTester tester = new GzipTester(testingdir, GzipHandler.GZIP);
    // Add (DoSFilter-like) manip filter
    FilterHolder manipHolder = new FilterHolder(AsyncManipFilter.class);
    manipHolder.setAsyncSupported(true);
    tester.addFilter(manipHolder, "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    // Add Gzip Filter first (in chain of DosFilter)
    FilterHolder gzipHolder = new FilterHolder(gzipFilterClass);
    gzipHolder.setAsyncSupported(true);
    tester.addFilter(gzipHolder, "*.txt", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    tester.addFilter(gzipHolder, "*.mp3", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    gzipHolder.setInitParameter("mimeTypes", "text/plain");
    // Add content servlet
    tester.setContentServlet(contentServletClass);
    try {
        String testFilename = String.format("DosGzip-%s-%s", contentServletClass.getSimpleName(), fileName);
        File testFile = tester.prepareServerFile(testFilename, fileSize);
        tester.start();
        HttpTester.Response response = tester.executeRequest("GET", "/context/" + testFile.getName(), 5, TimeUnit.SECONDS);
        assertThat("Response status", response.getStatus(), is(HttpStatus.OK_200));
        if (expectCompressed) {
            // Must be gzip compressed
            assertThat("Content-Encoding", response.get("Content-Encoding"), containsString(GzipHandler.GZIP));
        } else {
            assertThat("Content-Encoding", response.get("Content-Encoding"), not(containsString(GzipHandler.GZIP)));
        }
        // Uncompressed content Size
        ContentMetadata content = tester.getResponseMetadata(response);
        assertThat("(Uncompressed) Content Length", content.size, is((long) fileSize));
    } finally {
        tester.stop();
    }
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) GzipTester(org.eclipse.jetty.server.handler.gzip.GzipTester) Matchers.containsString(org.hamcrest.Matchers.containsString) File(java.io.File) ContentMetadata(org.eclipse.jetty.server.handler.gzip.GzipTester.ContentMetadata) HttpTester(org.eclipse.jetty.http.HttpTester) Test(org.junit.Test)

Example 70 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project Openfire by igniterealtime.

the class HttpBindManager method createBoshHandler.

private void createBoshHandler(ContextHandlerCollection contexts, String boshPath) {
    ServletContextHandler context = new ServletContextHandler(contexts, boshPath, ServletContextHandler.SESSIONS);
    // Ensure the JSP engine is initialized correctly (in order to be able to cope with Tomcat/Jasper precompiled JSPs).
    final List<ContainerInitializer> initializers = new ArrayList<>();
    initializers.add(new ContainerInitializer(new JasperInitializer(), null));
    context.setAttribute("org.eclipse.jetty.containerInitializers", initializers);
    context.setAttribute(InstanceManager.class.getName(), new SimpleInstanceManager());
    context.setAllowNullPathInfo(true);
    context.addServlet(new ServletHolder(new HttpBindServlet()), "/*");
    if (isHttpCompressionEnabled()) {
        Filter gzipFilter = new AsyncGzipFilter() {

            @Override
            public void init(FilterConfig config) throws ServletException {
                super.init(config);
                _methods.add(HttpMethod.POST.asString());
                Log.info("Installed response compression filter");
            }
        };
        FilterHolder filterHolder = new FilterHolder();
        filterHolder.setFilter(gzipFilter);
        context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
    }
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) AsyncGzipFilter(org.eclipse.jetty.servlets.AsyncGzipFilter) SimpleInstanceManager(org.apache.tomcat.SimpleInstanceManager) InstanceManager(org.apache.tomcat.InstanceManager) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) SimpleInstanceManager(org.apache.tomcat.SimpleInstanceManager) Filter(javax.servlet.Filter) AsyncGzipFilter(org.eclipse.jetty.servlets.AsyncGzipFilter) ContainerInitializer(org.eclipse.jetty.plus.annotation.ContainerInitializer) FilterConfig(javax.servlet.FilterConfig) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) JasperInitializer(org.apache.jasper.servlet.JasperInitializer)

Aggregations

FilterHolder (org.eclipse.jetty.servlet.FilterHolder)84 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)44 Test (org.junit.Test)35 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)32 Server (org.eclipse.jetty.server.Server)21 CountDownLatch (java.util.concurrent.CountDownLatch)18 Filter (javax.servlet.Filter)11 IOException (java.io.IOException)10 URL (java.net.URL)10 DispatcherType (javax.servlet.DispatcherType)9 ServletException (javax.servlet.ServletException)8 AuthenticationToken (org.apache.hadoop.security.authentication.server.AuthenticationToken)8 ServerConnector (org.eclipse.jetty.server.ServerConnector)8 File (java.io.File)7 HttpURLConnection (java.net.HttpURLConnection)7 PrivilegedActionException (java.security.PrivilegedActionException)7 AuthenticationException (org.apache.hadoop.security.authentication.client.AuthenticationException)7 FilterMapping (org.eclipse.jetty.servlet.FilterMapping)7 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)7 List (java.util.List)6