Search in sources :

Example 16 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project druid by druid-io.

the class CoordinatorJettyServerInitializer method initialize.

@Override
public void initialize(Server server, Injector injector) {
    final ServletContextHandler root = new ServletContextHandler(ServletContextHandler.SESSIONS);
    root.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
    ServletHolder holderPwd = new ServletHolder("default", DefaultServlet.class);
    root.addServlet(holderPwd, "/");
    if (config.getConsoleStatic() == null) {
        ResourceCollection staticResources;
        if (beOverlord) {
            staticResources = new ResourceCollection(Resource.newClassPathResource("io/druid/console"), Resource.newClassPathResource("static"), Resource.newClassPathResource("indexer_static"));
        } else {
            staticResources = new ResourceCollection(Resource.newClassPathResource("io/druid/console"), Resource.newClassPathResource("static"));
        }
        root.setBaseResource(staticResources);
    } else {
        // used for console development
        root.setResourceBase(config.getConsoleStatic());
    }
    JettyServerInitUtils.addExtensionFilters(root, injector);
    // /status should not redirect, so add first
    root.addFilter(GuiceFilter.class, "/status/*", null);
    // redirect anything other than status to the current lead
    root.addFilter(new FilterHolder(injector.getInstance(RedirectFilter.class)), "/*", null);
    // The coordinator really needs a standarized api path
    // Can't use '/*' here because of Guice and Jetty static content conflicts
    root.addFilter(GuiceFilter.class, "/info/*", null);
    root.addFilter(GuiceFilter.class, "/druid/coordinator/*", null);
    if (beOverlord) {
        root.addFilter(GuiceFilter.class, "/druid/indexer/*", null);
    }
    // this will be removed in the next major release
    root.addFilter(GuiceFilter.class, "/coordinator/*", null);
    if (!beOverlord) {
        root.addServlet(new ServletHolder(injector.getInstance(OverlordProxyServlet.class)), "/druid/indexer/*");
    }
    HandlerList handlerList = new HandlerList();
    handlerList.setHandlers(new Handler[] { JettyServerInitUtils.getJettyRequestLogHandler(), JettyServerInitUtils.wrapWithDefaultGzipHandler(root) });
    server.setHandler(handlerList);
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ResourceCollection(org.eclipse.jetty.util.resource.ResourceCollection)

Example 17 with FilterHolder

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

the class PutFilterTest method setUp.

@Before
public void setUp() throws Exception {
    _dir = File.createTempFile("testPutFilter", null);
    assertTrue(_dir.delete());
    assertTrue(_dir.mkdir());
    _dir.deleteOnExit();
    assertTrue(_dir.isDirectory());
    tester = new ServletTester("/context");
    tester.setResourceBase(_dir.getCanonicalPath());
    tester.addServlet(org.eclipse.jetty.servlet.DefaultServlet.class, "/");
    FilterHolder holder = tester.addFilter(PutFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    holder.setInitParameter("delAllowed", "true");
    // Bloody Windows does not allow file renaming
    if (!System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows"))
        holder.setInitParameter("putAtomic", "true");
    tester.start();
}
Also used : ServletTester(org.eclipse.jetty.servlet.ServletTester) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) Before(org.junit.Before)

Example 18 with FilterHolder

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

the class QoSFilterTest method testQosFilter.

@Test
public void testQosFilter() throws Exception {
    FilterHolder holder = new FilterHolder(QoSFilter2.class);
    holder.setAsyncSupported(true);
    holder.setInitParameter(QoSFilter.MAX_REQUESTS_INIT_PARAM, String.valueOf(MAX_QOS));
    _tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    List<Worker2> workers = new ArrayList<>();
    for (int i = 0; i < NUM_CONNECTIONS; ++i) {
        workers.add(new Worker2(i));
    }
    ExecutorService executor = Executors.newFixedThreadPool(NUM_CONNECTIONS);
    List<Future<Void>> futures = executor.invokeAll(workers, 20, 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 19 with FilterHolder

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

the class AbstractDoSFilterTest method startServer.

public void startServer(Class<? extends Filter> filter) throws Exception {
    _tester = new ServletTester("/ctx");
    HttpURI uri = new HttpURI(_tester.createConnector(true));
    _host = uri.getHost();
    _port = uri.getPort();
    _tester.getContext().addServlet(TestServlet.class, "/*");
    FilterHolder dosFilter = _tester.getContext().addFilter(filter, "/dos/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    dosFilter.setInitParameter("maxRequestsPerSec", "4");
    dosFilter.setInitParameter("delayMs", "200");
    dosFilter.setInitParameter("throttledRequests", "1");
    dosFilter.setInitParameter("waitMs", "10");
    dosFilter.setInitParameter("throttleMs", "4000");
    dosFilter.setInitParameter("remotePort", "false");
    dosFilter.setInitParameter("insertHeaders", "true");
    FilterHolder timeoutFilter = _tester.getContext().addFilter(filter, "/timeout/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    timeoutFilter.setInitParameter("maxRequestsPerSec", "4");
    timeoutFilter.setInitParameter("delayMs", "200");
    timeoutFilter.setInitParameter("throttledRequests", "1");
    timeoutFilter.setInitParameter("waitMs", "10");
    timeoutFilter.setInitParameter("throttleMs", "4000");
    timeoutFilter.setInitParameter("remotePort", "false");
    timeoutFilter.setInitParameter("insertHeaders", "true");
    timeoutFilter.setInitParameter("maxRequestMs", _requestMaxTime + "");
    _tester.start();
}
Also used : ServletTester(org.eclipse.jetty.servlet.ServletTester) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) HttpURI(org.eclipse.jetty.http.HttpURI)

Example 20 with FilterHolder

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

the class CrossOriginFilterTest method testNonSimpleRequestWithoutPreflight.

@Test
public void testNonSimpleRequestWithoutPreflight() throws Exception {
    // We cannot know if an actual request has performed the preflight before:
    // we'll trust browsers to do it right, so responses to actual requests
    // will contain the CORS response headers.
    FilterHolder filterHolder = new FilterHolder(new CrossOriginFilter());
    tester.getContext().addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
    CountDownLatch latch = new CountDownLatch(1);
    tester.getContext().addServlet(new ServletHolder(new ResourceServlet(latch)), "/*");
    String request = "" + "PUT / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "Origin: http://localhost\r\n" + "\r\n";
    String response = tester.getResponses(request);
    Assert.assertTrue(response.contains("HTTP/1.1 200"));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER));
    Assert.assertTrue(response.contains(CrossOriginFilter.ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER));
    Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

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