Search in sources :

Example 36 with FilterHolder

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

the class JettyServer method start.

public void start(final Injector injector) throws Exception {
    final long startTime = System.currentTimeMillis();
    server = new Server();
    // Setup JMX
    final MBeanContainer mbContainer = new MBeanContainer(mbeanServer);
    server.getContainer().addEventListener(mbContainer);
    server.addBean(mbContainer);
    mbContainer.addBean(Log.getLog());
    final Connector connector = new SelectChannelConnector();
    connector.setStatsOn(config.isJettyStatsOn());
    connector.setHost(config.getLocalIp());
    connector.setPort(config.getLocalPort());
    server.addConnector(connector);
    server.setStopAtShutdown(true);
    final ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.addEventListener(new GuiceServletContextListener() {

        @Override
        protected Injector getInjector() {
            return injector;
        }
    });
    // Jersey insists on using java.util.logging (JUL)
    final EventListener listener = new SetupJULBridge();
    context.addEventListener(listener);
    // Make sure Guice filter all requests
    final FilterHolder filterHolder = new FilterHolder(GuiceFilter.class);
    context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    // Backend servlet for Guice - never used
    final ServletHolder sh = new ServletHolder(DefaultServlet.class);
    context.addServlet(sh, "/*");
    server.start();
    final long secondsToStart = (System.currentTimeMillis() - startTime) / 1000;
    log.info(String.format("Jetty server started in %d:%02d", secondsToStart / 60, secondsToStart % 60));
}
Also used : Connector(org.eclipse.jetty.server.Connector) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) MBeanServer(javax.management.MBeanServer) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) Injector(com.google.inject.Injector) MBeanContainer(org.eclipse.jetty.jmx.MBeanContainer) EventListener(java.util.EventListener) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) GuiceServletContextListener(com.google.inject.servlet.GuiceServletContextListener)

Example 37 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project spring-framework by spring-projects.

the class JettyWebSocketTestServer method deployConfig.

@Override
public void deployConfig(WebApplicationContext wac, Filter... filters) {
    ServletHolder servletHolder = new ServletHolder(new DispatcherServlet(wac));
    this.contextHandler = new ServletContextHandler();
    this.contextHandler.addServlet(servletHolder, "/");
    for (Filter filter : filters) {
        this.contextHandler.addFilter(new FilterHolder(filter), "/*", getDispatcherTypes());
    }
    this.jettyServer.setHandler(this.contextHandler);
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) Filter(javax.servlet.Filter) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 38 with FilterHolder

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

the class WebsocketComponent method applyCrossOriginFiltering.

private void applyCrossOriginFiltering(WebsocketEndpoint endpoint, ServletContextHandler context) {
    if (endpoint.isCrossOriginFilterOn()) {
        FilterHolder filterHolder = new FilterHolder();
        CrossOriginFilter filter = new CrossOriginFilter();
        filterHolder.setFilter(filter);
        filterHolder.setInitParameter("allowedOrigins", endpoint.getAllowedOrigins());
        context.addFilter(filterHolder, endpoint.getFilterPath(), EnumSet.allOf(DispatcherType.class));
    }
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) CrossOriginFilter(org.eclipse.jetty.servlets.CrossOriginFilter) DispatcherType(javax.servlet.DispatcherType)

Example 39 with FilterHolder

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

the class GHServer method start.

public void start(Injector injector) throws Exception {
    ResourceHandler resHandler = new ResourceHandler();
    resHandler.setDirectoriesListed(false);
    resHandler.setWelcomeFiles(new String[] { "index.html" });
    resHandler.setResourceBase(args.get("jetty.resourcebase", "./web/src/main/webapp"));
    server = new Server();
    // getSessionHandler and getSecurityHandler should always return null
    ServletContextHandler servHandler = new ServletContextHandler(ServletContextHandler.NO_SECURITY | ServletContextHandler.NO_SESSIONS);
    servHandler.setErrorHandler(new GHErrorHandler());
    servHandler.setContextPath("/");
    servHandler.addServlet(new ServletHolder(new InvalidRequestServlet()), "/*");
    FilterHolder guiceFilter = new FilterHolder(injector.getInstance(GuiceFilter.class));
    servHandler.addFilter(guiceFilter, "/*", EnumSet.allOf(DispatcherType.class));
    ServerConnector connector0 = new ServerConnector(server);
    int httpPort = args.getInt("jetty.port", 8989);
    String host = args.get("jetty.host", "");
    connector0.setPort(httpPort);
    int requestHeaderSize = args.getInt("jetty.request_header_size", -1);
    if (requestHeaderSize > 0)
        connector0.getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration().setRequestHeaderSize(requestHeaderSize);
    if (!host.isEmpty())
        connector0.setHost(host);
    server.addConnector(connector0);
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resHandler, servHandler });
    GzipHandler gzipHandler = new GzipHandler();
    gzipHandler.setIncludedMethods("GET", "POST");
    // Note: gzip only affects the response body like our previous 'GHGZIPHook' behaviour: http://stackoverflow.com/a/31565805/194609
    // If no mimeTypes are defined the content-type is "not 'application/gzip'", See also https://github.com/graphhopper/directions-api/issues/28 for pitfalls
    // gzipHandler.setIncludedMimeTypes();
    gzipHandler.setHandler(handlers);
    server.setHandler(gzipHandler);
    server.start();
    logger.info("Started server at HTTP " + host + ":" + httpPort);
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) GuiceFilter(com.google.inject.servlet.GuiceFilter) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) DispatcherType(javax.servlet.DispatcherType)

Example 40 with FilterHolder

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

the class WebServer method start.

/**
   * Start the web server including setup.
   * @throws Exception
   */
public void start() throws Exception {
    if (embeddedJetty == null) {
        return;
    }
    final boolean authEnabled = config.getBoolean(ExecConstants.USER_AUTHENTICATION_ENABLED);
    if (authEnabled && !context.getAuthProvider().containsFactory(PlainFactory.SIMPLE_NAME)) {
        logger.warn("Not starting web server. Currently Drill supports web authentication only through " + "username/password. But PLAIN mechanism is not configured.");
        return;
    }
    final ServerConnector serverConnector;
    if (config.getBoolean(ExecConstants.HTTP_ENABLE_SSL)) {
        serverConnector = createHttpsConnector();
    } else {
        serverConnector = createHttpConnector();
    }
    embeddedJetty.addConnector(serverConnector);
    // Add resources
    final ErrorHandler errorHandler = new ErrorHandler();
    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)));
    servletHolder.setInitOrder(1);
    servletContextHandler.addServlet(servletHolder, "/*");
    servletContextHandler.addServlet(new ServletHolder(new MetricsServlet(metrics)), "/status/metrics");
    servletContextHandler.addServlet(new ServletHolder(new ThreadDumpServlet()), "/status/threads");
    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).getURL().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/*");
    if (authEnabled) {
        servletContextHandler.setSecurityHandler(createSecurityHandler());
        servletContextHandler.setSessionHandler(createSessionHandler(servletContextHandler.getSecurityHandler()));
    }
    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));
        }
    }
    embeddedJetty.setHandler(servletContextHandler);
    embeddedJetty.start();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) 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)

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