protected Handler createAppServlet(
     Server server,
     JerseyEnvironment jersey,
     ObjectMapper objectMapper,
     Validator validator,
     MutableServletContextHandler handler,
     @Nullable Servlet jerseyContainer,
     MetricRegistry metricRegistry) {
   configureSessionsAndSecurity(handler, server);
   handler
       .addFilter(AllowedMethodsFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST))
       .setInitParameter(
           AllowedMethodsFilter.ALLOWED_METHODS_PARAM, Joiner.on(',').join(allowedMethods));
   handler.addFilter(ThreadNameFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
   serverPush.addFilter(handler);
   if (jerseyContainer != null) {
     if (jerseyRootPath.isPresent()) {
       jersey.setUrlPattern(jerseyRootPath.get());
     }
     jersey.register(new JacksonMessageBodyProvider(objectMapper));
     jersey.register(new HibernateValidationFeature(validator));
     if (registerDefaultExceptionMappers == null || registerDefaultExceptionMappers) {
       jersey.register(new LoggingExceptionMapper<Throwable>() {});
       jersey.register(new JerseyViolationExceptionMapper());
       jersey.register(new JsonProcessingExceptionMapper());
       jersey.register(new EarlyEofExceptionMapper());
     }
     handler.addServlet(new NonblockingServletHolder(jerseyContainer), jersey.getUrlPattern());
   }
   final InstrumentedHandler instrumented = new InstrumentedHandler(metricRegistry);
   instrumented.setServer(server);
   instrumented.setHandler(handler);
   return instrumented;
 }
Beispiel #2
0
  private void run() throws Exception {
    InetSocketAddress address;

    if (this.host != null) {
      address = new InetSocketAddress(this.host, this.port);
    } else {
      address = new InetSocketAddress(this.port);
    }

    Server server = new Server(address);

    ContextHandlerCollection handlerCollection = new ContextHandlerCollection();

    final ModelRegistry modelRegistry = new ModelRegistry();

    final MetricRegistry metricRegistry = new MetricRegistry();

    Binder binder =
        new AbstractBinder() {

          @Override
          protected void configure() {
            bind(modelRegistry).to(ModelRegistry.class);
            bind(metricRegistry).to(MetricRegistry.class);
          }
        };

    ResourceConfig config = new ResourceConfig(ModelResource.class);
    config.register(binder);
    config.register(JacksonFeature.class);
    config.register(MultiPartFeature.class);
    config.register(ObjectMapperProvider.class);
    config.register(RolesAllowedDynamicFeature.class);

    // Naive implementation that grants the "admin" role to all local network users
    config.register(NetworkSecurityContextFilter.class);

    ServletContextHandler servletHandler = new ServletContextHandler();
    servletHandler.setContextPath(this.contextPath);

    ServletContainer jerseyServlet = new ServletContainer(config);

    servletHandler.addServlet(new ServletHolder(jerseyServlet), "/*");

    InstrumentedHandler instrumentedHandler = new InstrumentedHandler(metricRegistry);
    instrumentedHandler.setHandler(servletHandler);

    handlerCollection.addHandler(instrumentedHandler);

    if (this.consoleWar != null) {
      WebAppContext consoleHandler = new WebAppContext();
      consoleHandler.setContextPath(this.contextPath + "/console"); // XXX
      consoleHandler.setWar(this.consoleWar.getAbsolutePath());

      handlerCollection.addHandler(consoleHandler);
    }

    server.setHandler(handlerCollection);

    DirectoryDeployer deployer = null;

    if (this.modelDir != null) {

      if (!this.modelDir.isDirectory()) {
        throw new IOException(this.modelDir.getAbsolutePath() + " is not a directory");
      }

      deployer = new DirectoryDeployer(modelRegistry, this.modelDir.toPath());
    }

    server.start();

    if (deployer != null) {
      deployer.start();
    }

    server.join();

    if (deployer != null) {
      deployer.interrupt();

      deployer.join();
    }
  }