@POST
  @Consumes("application/json")
  public Response post(StaticAnnouncement announcement, @Context UriInfo uriInfo) {
    if (!nodeInfo.getEnvironment().equals(announcement.getEnvironment())) {
      return Response.status(BAD_REQUEST)
          .entity(
              format(
                  "Environment mismatch. Expected: %s, Provided: %s",
                  nodeInfo.getEnvironment(), announcement.getEnvironment()))
          .build();
    }

    Id<Service> id = Id.random();
    String location = Objects.firstNonNull(announcement.getLocation(), "/somewhere/" + id);

    Service service = Service.copyOf(announcement).setId(id).setLocation(location).build();

    store.put(service);

    URI uri =
        UriBuilder.fromUri(uriInfo.getBaseUri())
            .path(StaticAnnouncementResource.class)
            .path("{id}")
            .build(id);
    return Response.created(uri).entity(service).build();
  }
Example #2
0
 @Inject
 public TaskSystemTable(TaskManager taskManager, NodeInfo nodeInfo) {
   this.taskManager = taskManager;
   this.nodeId = nodeInfo.getNodeId();
 }
 @GET
 @Produces("application/json")
 public Services get() {
   return new Services(nodeInfo.getEnvironment(), store.getAll());
 }
Example #4
0
  @SuppressWarnings({"deprecation"})
  public HttpServer(
      HttpServerInfo httpServerInfo,
      NodeInfo nodeInfo,
      HttpServerConfig config,
      Servlet theServlet,
      Map<String, String> parameters,
      Set<Filter> filters,
      Set<HttpResourceBinding> resources,
      Servlet theAdminServlet,
      Map<String, String> adminParameters,
      Set<Filter> adminFilters,
      MBeanServer mbeanServer,
      LoginService loginService,
      TraceTokenManager tokenManager,
      RequestStats stats,
      EventClient eventClient)
      throws IOException {
    Preconditions.checkNotNull(httpServerInfo, "httpServerInfo is null");
    Preconditions.checkNotNull(nodeInfo, "nodeInfo is null");
    Preconditions.checkNotNull(config, "config is null");
    Preconditions.checkNotNull(theServlet, "theServlet is null");

    QueuedThreadPool threadPool = new QueuedThreadPool(config.getMaxThreads());
    threadPool.setMinThreads(config.getMinThreads());
    threadPool.setIdleTimeout(Ints.checkedCast(config.getThreadMaxIdleTime().toMillis()));
    threadPool.setName("http-worker");
    server = new Server(threadPool);

    if (config.isShowStackTrace()) {
      server.addBean(new ErrorHandler());
    }

    if (mbeanServer != null) {
      // export jmx mbeans if a server was provided
      MBeanContainer mbeanContainer = new MBeanContainer(mbeanServer);
      server.addBean(mbeanContainer);
    }

    // set up HTTP connector
    if (config.isHttpEnabled()) {
      HttpConfiguration httpConfiguration = new HttpConfiguration();
      httpConfiguration.setSendServerVersion(false);
      httpConfiguration.setSendXPoweredBy(false);
      if (config.getMaxRequestHeaderSize() != null) {
        httpConfiguration.setRequestHeaderSize(
            Ints.checkedCast(config.getMaxRequestHeaderSize().toBytes()));
      }

      // if https is enabled, set the CONFIDENTIAL and INTEGRAL redirection information
      if (config.isHttpsEnabled()) {
        httpConfiguration.setSecureScheme("https");
        httpConfiguration.setSecurePort(httpServerInfo.getHttpsUri().getPort());
      }

      Integer acceptors = config.getHttpAcceptorThreads();
      Integer selectors = config.getHttpSelectorThreads();
      httpConnector =
          new ServerConnector(
              server,
              null,
              null,
              null,
              acceptors == null ? -1 : acceptors,
              selectors == null ? -1 : selectors,
              new HttpConnectionFactory(httpConfiguration));
      httpConnector.setName("http");
      httpConnector.setPort(httpServerInfo.getHttpUri().getPort());
      httpConnector.setIdleTimeout(config.getNetworkMaxIdleTime().toMillis());
      httpConnector.setHost(nodeInfo.getBindIp().getHostAddress());
      httpConnector.setAcceptQueueSize(config.getHttpAcceptQueueSize());

      server.addConnector(httpConnector);
    } else {
      httpConnector = null;
    }

    // set up NIO-based HTTPS connector
    if (config.isHttpsEnabled()) {
      HttpConfiguration httpsConfiguration = new HttpConfiguration();
      httpsConfiguration.setSendServerVersion(false);
      httpsConfiguration.setSendXPoweredBy(false);
      if (config.getMaxRequestHeaderSize() != null) {
        httpsConfiguration.setRequestHeaderSize(
            Ints.checkedCast(config.getMaxRequestHeaderSize().toBytes()));
      }
      httpsConfiguration.addCustomizer(new SecureRequestCustomizer());

      SslContextFactory sslContextFactory = new SslContextFactory(config.getKeystorePath());
      sslContextFactory.setKeyStorePassword(config.getKeystorePassword());
      SslConnectionFactory sslConnectionFactory =
          new SslConnectionFactory(sslContextFactory, "http/1.1");

      Integer acceptors = config.getHttpsAcceptorThreads();
      Integer selectors = config.getHttpsSelectorThreads();
      httpsConnector =
          new ServerConnector(
              server,
              null,
              null,
              null,
              acceptors == null ? -1 : acceptors,
              selectors == null ? -1 : selectors,
              sslConnectionFactory,
              new HttpConnectionFactory(httpsConfiguration));
      httpsConnector.setName("https");
      httpsConnector.setPort(httpServerInfo.getHttpsUri().getPort());
      httpsConnector.setIdleTimeout(config.getNetworkMaxIdleTime().toMillis());
      httpsConnector.setHost(nodeInfo.getBindIp().getHostAddress());
      httpsConnector.setAcceptQueueSize(config.getHttpAcceptQueueSize());

      server.addConnector(httpsConnector);
    } else {
      httpsConnector = null;
    }

    // set up NIO-based Admin connector
    if (theAdminServlet != null && config.isAdminEnabled()) {
      HttpConfiguration adminConfiguration = new HttpConfiguration();
      adminConfiguration.setSendServerVersion(false);
      adminConfiguration.setSendXPoweredBy(false);
      if (config.getMaxRequestHeaderSize() != null) {
        adminConfiguration.setRequestHeaderSize(
            Ints.checkedCast(config.getMaxRequestHeaderSize().toBytes()));
      }

      QueuedThreadPool adminThreadPool = new QueuedThreadPool(config.getAdminMaxThreads());
      adminThreadPool.setName("http-admin-worker");
      adminThreadPool.setMinThreads(config.getAdminMinThreads());
      adminThreadPool.setIdleTimeout(Ints.checkedCast(config.getThreadMaxIdleTime().toMillis()));

      if (config.isHttpsEnabled()) {
        adminConfiguration.addCustomizer(new SecureRequestCustomizer());

        SslContextFactory sslContextFactory = new SslContextFactory(config.getKeystorePath());
        sslContextFactory.setKeyStorePassword(config.getKeystorePassword());
        SslConnectionFactory sslConnectionFactory =
            new SslConnectionFactory(sslContextFactory, "http/1.1");
        adminConnector =
            new ServerConnector(
                server,
                adminThreadPool,
                null,
                null,
                0,
                -1,
                sslConnectionFactory,
                new HttpConnectionFactory(adminConfiguration));
      } else {
        adminConnector =
            new ServerConnector(
                server,
                adminThreadPool,
                null,
                null,
                0,
                -1,
                new HttpConnectionFactory(adminConfiguration));
      }

      adminConnector.setName("admin");
      adminConnector.setPort(httpServerInfo.getAdminUri().getPort());
      adminConnector.setIdleTimeout(config.getNetworkMaxIdleTime().toMillis());
      adminConnector.setHost(nodeInfo.getBindIp().getHostAddress());
      adminConnector.setAcceptQueueSize(config.getHttpAcceptQueueSize());

      server.addConnector(adminConnector);
    } else {
      adminConnector = null;
    }

    /**
     * structure is:
     *
     * <p>server |--- statistics handler |--- context handler | |--- trace token filter | |--- gzip
     * response filter | |--- gzip request filter | |--- security handler | |--- user provided
     * filters | |--- the servlet (normally GuiceContainer) | |--- resource handlers |--- log
     * handler |-- admin context handler \ --- the admin servlet
     */
    HandlerCollection handlers = new HandlerCollection();

    for (HttpResourceBinding resource : resources) {
      handlers.addHandler(
          new ClassPathResourceHandler(
              resource.getBaseUri(),
              resource.getClassPathResourceBase(),
              resource.getWelcomeFiles()));
    }

    handlers.addHandler(
        createServletContext(
            theServlet, parameters, filters, tokenManager, loginService, "http", "https"));
    if (config.isLogEnabled()) {
      handlers.addHandler(createLogHandler(config, tokenManager, eventClient));
    }

    RequestLogHandler statsRecorder = new RequestLogHandler();
    statsRecorder.setRequestLog(new StatsRecordingHandler(stats));
    handlers.addHandler(statsRecorder);

    // add handlers to Jetty
    StatisticsHandler statsHandler = new StatisticsHandler();
    statsHandler.setHandler(handlers);

    HandlerList rootHandlers = new HandlerList();
    if (theAdminServlet != null && config.isAdminEnabled()) {
      rootHandlers.addHandler(
          createServletContext(
              theAdminServlet, adminParameters, adminFilters, tokenManager, loginService, "admin"));
    }
    rootHandlers.addHandler(statsHandler);
    server.setHandler(rootHandlers);
  }