Search in sources :

Example 21 with ErrorHandler

use of org.eclipse.jetty.server.handler.ErrorHandler in project drill by apache.

the class WebServer method createServletContextHandler.

private ServletContextHandler createServletContextHandler(final boolean authEnabled) throws DrillbitStartupException {
    // Add resources
    final ErrorHandler errorHandler = new DrillErrorHandler();
    errorHandler.setShowStacks(true);
    errorHandler.setShowMessageInTitle(true);
    final ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
    servletContextHandler.setErrorHandler(errorHandler);
    servletContextHandler.setContextPath("/");
    final ServletHolder servletHolder = new ServletHolder(new ServletContainer(new DrillRestServer(workManager, servletContextHandler.getServletContext(), drillbit)));
    servletHolder.setInitOrder(1);
    servletContextHandler.addServlet(servletHolder, "/*");
    servletContextHandler.addServlet(new ServletHolder(new MetricsServlet(metrics)), STATUS_METRICS_PATH);
    servletContextHandler.addServlet(new ServletHolder(new ThreadDumpServlet()), STATUS_THREADS_PATH);
    final ServletHolder staticHolder = new ServletHolder("static", DefaultServlet.class);
    // Get resource URL for Drill static assets, based on where Drill icon is located
    String drillIconResourcePath = Resource.newClassPathResource(BASE_STATIC_PATH + DRILL_ICON_RESOURCE_RELATIVE_PATH).getURI().toString();
    staticHolder.setInitParameter("resourceBase", drillIconResourcePath.substring(0, drillIconResourcePath.length() - DRILL_ICON_RESOURCE_RELATIVE_PATH.length()));
    staticHolder.setInitParameter("dirAllowed", "false");
    staticHolder.setInitParameter("pathInfoOnly", "true");
    servletContextHandler.addServlet(staticHolder, "/static/*");
    // Add Local path resource (This will allow access to dynamically created files like JavaScript)
    final ServletHolder dynamicHolder = new ServletHolder("dynamic", DefaultServlet.class);
    // Skip if unable to get a temp directory (e.g. during Unit tests)
    if (getOrCreateTmpJavaScriptDir() != null) {
        dynamicHolder.setInitParameter("resourceBase", getOrCreateTmpJavaScriptDir().getAbsolutePath());
        dynamicHolder.setInitParameter("dirAllowed", "true");
        dynamicHolder.setInitParameter("pathInfoOnly", "true");
        servletContextHandler.addServlet(dynamicHolder, "/dynamic/*");
    }
    if (authEnabled) {
        // DrillSecurityHandler is used to support SPNEGO and FORM authentication together
        servletContextHandler.setSecurityHandler(new DrillHttpSecurityHandlerProvider(config, workManager.getContext()));
        servletContextHandler.setSessionHandler(createSessionHandler(servletContextHandler.getSecurityHandler()));
    }
    // Applying filters for CSRF protection.
    servletContextHandler.addFilter(CsrfTokenInjectFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    for (String path : new String[] { "/query", "/storage/create_update", "/option/*" }) {
        servletContextHandler.addFilter(CsrfTokenValidateFilter.class, path, EnumSet.of(DispatcherType.REQUEST));
    }
    if (config.getBoolean(ExecConstants.HTTP_CORS_ENABLED)) {
        FilterHolder holder = new FilterHolder(CrossOriginFilter.class);
        holder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, StringUtils.join(config.getStringList(ExecConstants.HTTP_CORS_ALLOWED_ORIGINS), ","));
        holder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, StringUtils.join(config.getStringList(ExecConstants.HTTP_CORS_ALLOWED_METHODS), ","));
        holder.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, StringUtils.join(config.getStringList(ExecConstants.HTTP_CORS_ALLOWED_HEADERS), ","));
        holder.setInitParameter(CrossOriginFilter.ALLOW_CREDENTIALS_PARAM, String.valueOf(config.getBoolean(ExecConstants.HTTP_CORS_CREDENTIALS)));
        for (String path : new String[] { "*.json", "/storage/*/enable/*", "/status*" }) {
            servletContextHandler.addFilter(holder, path, EnumSet.of(DispatcherType.REQUEST));
        }
    }
    // Allow for Other Drillbits to make REST calls
    FilterHolder filterHolder = new FilterHolder(CrossOriginFilter.class);
    filterHolder.setInitParameter("allowedOrigins", "*");
    // Allowing CORS for metrics only
    servletContextHandler.addFilter(filterHolder, STATUS_METRICS_PATH, null);
    FilterHolder responseHeadersSettingFilter = new FilterHolder(ResponseHeadersSettingFilter.class);
    responseHeadersSettingFilter.setInitParameters(ResponseHeadersSettingFilter.retrieveResponseHeaders(config));
    servletContextHandler.addFilter(responseHeadersSettingFilter, "/*", EnumSet.of(DispatcherType.REQUEST));
    return servletContextHandler;
}
Also used : DrillErrorHandler(org.apache.drill.exec.server.rest.auth.DrillErrorHandler) ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) DrillErrorHandler(org.apache.drill.exec.server.rest.auth.DrillErrorHandler) DrillHttpSecurityHandlerProvider(org.apache.drill.exec.server.rest.auth.DrillHttpSecurityHandlerProvider) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) MetricsServlet(com.codahale.metrics.servlets.MetricsServlet) ThreadDumpServlet(com.codahale.metrics.servlets.ThreadDumpServlet) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 22 with ErrorHandler

use of org.eclipse.jetty.server.handler.ErrorHandler in project drill by apache.

the class WebServer method createErrorHandler.

private ErrorHandler createErrorHandler() {
    // Error handler to show detailed errors.
    // Should probably be turned off in production.
    final ErrorHandler errorHandler = new ErrorHandler();
    errorHandler.setShowStacks(true);
    errorHandler.setShowMessageInTitle(true);
    return errorHandler;
}
Also used : ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler)

Aggregations

ErrorHandler (org.eclipse.jetty.server.handler.ErrorHandler)22 IOException (java.io.IOException)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 Server (org.eclipse.jetty.server.Server)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 Writer (java.io.Writer)5 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)5 ServerConnector (org.eclipse.jetty.server.ServerConnector)4 Request (org.eclipse.jetty.server.Request)3 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)3 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)3 Test (org.junit.Test)3 MetricsServlet (com.codahale.metrics.servlets.MetricsServlet)2 ThreadDumpServlet (com.codahale.metrics.servlets.ThreadDumpServlet)2 ServletException (javax.servlet.ServletException)2 DrillErrorHandler (org.apache.drill.exec.server.rest.auth.DrillErrorHandler)2 DrillHttpSecurityHandlerProvider (org.apache.drill.exec.server.rest.auth.DrillHttpSecurityHandlerProvider)2 RuntimeIOException (org.eclipse.jetty.io.RuntimeIOException)2 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2