Search in sources :

Example 1 with LimitRequestsFilter

use of org.apache.druid.server.initialization.jetty.LimitRequestsFilter in project druid by druid-io.

the class QueryJettyServerInitializer method initialize.

@Override
public void initialize(Server server, Injector injector) {
    final ServletContextHandler root = new ServletContextHandler(ServletContextHandler.SESSIONS);
    root.addServlet(new ServletHolder(new DefaultServlet()), "/*");
    // Add LimitRequestsFilter as first in the chain if enabled.
    if (serverConfig.isEnableRequestLimit()) {
        // To reject xth request, limit should be set to x-1 because (x+1)st request wouldn't reach filter
        // but rather wait on jetty queue.
        Preconditions.checkArgument(serverConfig.getNumThreads() > 1, "numThreads must be > 1 to enable Request Limit Filter.");
        log.info("Enabling Request Limit Filter with limit [%d].", serverConfig.getNumThreads() - 1);
        root.addFilter(new FilterHolder(new LimitRequestsFilter(serverConfig.getNumThreads() - 1)), "/*", null);
    }
    final ObjectMapper jsonMapper = injector.getInstance(Key.get(ObjectMapper.class, Json.class));
    final AuthenticatorMapper authenticatorMapper = injector.getInstance(AuthenticatorMapper.class);
    AuthenticationUtils.addSecuritySanityCheckFilter(root, jsonMapper);
    // perform no-op authorization for these resources
    AuthenticationUtils.addNoopAuthenticationAndAuthorizationFilters(root, UNSECURED_PATHS);
    AuthenticationUtils.addNoopAuthenticationAndAuthorizationFilters(root, authConfig.getUnsecuredPaths());
    List<Authenticator> authenticators = authenticatorMapper.getAuthenticatorChain();
    AuthenticationUtils.addAuthenticationFilterChain(root, authenticators);
    AuthenticationUtils.addAllowOptionsFilter(root, authConfig.isAllowUnauthenticatedHttpOptions());
    JettyServerInitUtils.addAllowHttpMethodsFilter(root, serverConfig.getAllowedHttpMethods());
    JettyServerInitUtils.addExtensionFilters(root, injector);
    // Check that requests were authorized before sending responses
    AuthenticationUtils.addPreResponseAuthorizationCheckFilter(root, authenticators, jsonMapper);
    root.addFilter(GuiceFilter.class, "/*", null);
    final HandlerList handlerList = new HandlerList();
    // Do not change the order of the handlers that have already been added
    for (Handler handler : server.getHandlers()) {
        handlerList.addHandler(handler);
    }
    handlerList.addHandler(JettyServerInitUtils.getJettyRequestLogHandler());
    // Add all extension handlers
    for (Handler handler : extensionHandlers) {
        handlerList.addHandler(handler);
    }
    // Add Gzip handler at the very end
    handlerList.addHandler(JettyServerInitUtils.wrapWithDefaultGzipHandler(root, serverConfig.getInflateBufferSize(), serverConfig.getCompressionLevel()));
    final StatisticsHandler statisticsHandler = new StatisticsHandler();
    statisticsHandler.setHandler(handlerList);
    server.setHandler(statisticsHandler);
}
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) Handler(org.eclipse.jetty.server.Handler) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) Json(org.apache.druid.guice.annotations.Json) AuthenticatorMapper(org.apache.druid.server.security.AuthenticatorMapper) LimitRequestsFilter(org.apache.druid.server.initialization.jetty.LimitRequestsFilter) DefaultServlet(org.eclipse.jetty.servlet.DefaultServlet) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Authenticator(org.apache.druid.server.security.Authenticator)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Json (org.apache.druid.guice.annotations.Json)1 LimitRequestsFilter (org.apache.druid.server.initialization.jetty.LimitRequestsFilter)1 Authenticator (org.apache.druid.server.security.Authenticator)1 AuthenticatorMapper (org.apache.druid.server.security.AuthenticatorMapper)1 Handler (org.eclipse.jetty.server.Handler)1 HandlerList (org.eclipse.jetty.server.handler.HandlerList)1 StatisticsHandler (org.eclipse.jetty.server.handler.StatisticsHandler)1 DefaultServlet (org.eclipse.jetty.servlet.DefaultServlet)1 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)1 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)1 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)1