private static void serve(HttpServlet servlet) throws Exception {
    ServletHandler handler = new ServletHandler();
    handler.addServletWithMapping(new ServletHolder(servlet), "/");

    HttpConfiguration httpConfig = new HttpConfiguration();
    httpConfig.setOutputBufferSize(32 * 1024);
    httpConfig.setRequestHeaderSize(8 * 1024);
    httpConfig.setResponseHeaderSize(8 * 1024);
    httpConfig.setSendDateHeader(true);

    HttpConnectionFactory connFac = new HttpConnectionFactory(httpConfig);

    Server server = new Server(3000);

    ServerConnector connector =
        new ServerConnector(
            server,
            Executors.newFixedThreadPool(4),
            new ScheduledExecutorScheduler(),
            new MappedByteBufferPool(),
            1,
            4,
            connFac);
    connector.setAcceptQueueSize(1000);
    connector.setReuseAddress(true);

    server.setHandler(handler);
    server.start();
  }
  @Override
  public void run() {

    try {
      // Set GAE SystemProperties
      setSystemProperties();

      // Create the server, connector and associated instances
      QueuedThreadPool threadpool = new QueuedThreadPool();
      server = new Server(threadpool);
      HttpConfiguration httpConfig = new HttpConfiguration();
      ServerConnector connector =
          new ServerConnector(server, new HttpConnectionFactory(httpConfig));
      connector.setPort(port);
      server.addConnector(connector);

      MappedByteBufferPool bufferpool = new MappedByteBufferPool();

      // Basic jetty.xml handler setup
      HandlerCollection handlers = new HandlerCollection();
      ContextHandlerCollection contexts =
          new ContextHandlerCollection(); // TODO is a context handler collection needed for a
                                          // single context?
      handlers.setHandlers(new Handler[] {contexts, new DefaultHandler()});
      server.setHandler(handlers);

      // Configuration as done by gae.mod/gae.ini
      httpConfig.setOutputAggregationSize(32768);

      threadpool.setMinThreads(10);
      threadpool.setMaxThreads(500);
      threadpool.setIdleTimeout(60000);

      httpConfig.setOutputBufferSize(32768);
      httpConfig.setRequestHeaderSize(8192);
      httpConfig.setResponseHeaderSize(8192);
      httpConfig.setSendServerVersion(true);
      httpConfig.setSendDateHeader(false);
      httpConfig.setDelayDispatchUntilContent(false);

      // Setup Server as done by gae.xml
      server.addBean(bufferpool);

      httpConfig.setHeaderCacheSize(512);

      RequestLogHandler requestLogHandler = new RequestLogHandler();
      handlers.addHandler(requestLogHandler);
      File logs = File.createTempFile("logs", "logs");
      logs.delete();
      logs.mkdirs();
      NCSARequestLog requestLog =
          new NCSARequestLog(logs.getCanonicalPath() + "/request.yyyy_mm_dd.log");
      requestLogHandler.setRequestLog(requestLog);
      requestLog.setRetainDays(2);
      requestLog.setAppend(true);
      requestLog.setExtended(true);
      requestLog.setLogTimeZone("GMT");
      requestLog.setLogLatency(true);
      requestLog.setPreferProxiedForAddress(true);

      // Ugly hack to delete possible previous run lock file
      new File("target/log.0.lck").delete();
      new File("target/log.0.1.lck").delete();

      // configuration from root.xml
      VmRuntimeWebAppContext context = new VmRuntimeWebAppContext();
      context.setContextPath("/");

      // find the sibling testwebapp target
      File currentDir = new File("").getAbsoluteFile();
      File webAppLocation = new File(currentDir, "target/webapps/testwebapp");
      context.setResourceBase(webAppLocation.getAbsolutePath());
      context.init("WEB-INF/appengine-web.xml");
      context.setParentLoaderPriority(true); // true in tests for easier mocking

      // Hack to find the webdefault.xml
      File webDefault = new File(currentDir.getParentFile(), "docker/etc/webdefault.xml");
      context.setDefaultsDescriptor(webDefault.getAbsolutePath());

      contexts.addHandler(context);

      // start and join
      server.start();

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      started.countDown();
    }

    try {
      if (Log.getLogger(Server.class).isDebugEnabled()) server.dumpStdErr();
      server.join();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  protected AbstractConnector createConnectorJettyInternal(
      Server server, JettyHttpEndpoint endpoint, SslContextFactory sslcf) {
    try {
      String hosto = endpoint.getHttpUri().getHost();
      int porto = endpoint.getPort();
      org.eclipse.jetty.server.HttpConfiguration httpConfig =
          new org.eclipse.jetty.server.HttpConfiguration();
      httpConfig.setSendServerVersion(endpoint.isSendServerVersion());
      httpConfig.setSendDateHeader(endpoint.isSendDateHeader());
      httpConfig.setSendDateHeader(endpoint.isSendDateHeader());

      if (requestBufferSize != null) {
        // Does not work
        // httpConfig.setRequestBufferSize(requestBufferSize);
      }
      if (requestHeaderSize != null) {
        httpConfig.setRequestHeaderSize(requestHeaderSize);
      }
      if (responseBufferSize != null) {
        httpConfig.setOutputBufferSize(responseBufferSize);
      }
      if (responseHeaderSize != null) {
        httpConfig.setResponseHeaderSize(responseHeaderSize);
      }
      if (useXForwardedForHeader) {
        httpConfig.addCustomizer(new ForwardedRequestCustomizer());
      }
      HttpConnectionFactory httpFactory =
          new org.eclipse.jetty.server.HttpConnectionFactory(httpConfig);

      ArrayList<ConnectionFactory> connectionFactories = new ArrayList<ConnectionFactory>();
      ServerConnector result = new org.eclipse.jetty.server.ServerConnector(server);
      if (sslcf != null) {
        httpConfig.addCustomizer(new org.eclipse.jetty.server.SecureRequestCustomizer());
        SslConnectionFactory scf =
            new org.eclipse.jetty.server.SslConnectionFactory(sslcf, "HTTP/1.1");
        connectionFactories.add(scf);
        // The protocol name can be "SSL" or "SSL-HTTP/1.1" depending on the version of Jetty
        result.setDefaultProtocol(scf.getProtocol());
      }
      connectionFactories.add(httpFactory);
      result.setConnectionFactories(connectionFactories);
      result.setPort(porto);
      if (hosto != null) {
        result.setHost(hosto);
      }
      /*
      if (getSocketConnectorProperties() != null && !"https".equals(endpoint.getProtocol())) {
          // must copy the map otherwise it will be deleted
          Map<String, Object> properties = new HashMap<String, Object>(getSocketConnectorProperties());
          IntrospectionSupport.setProperties(httpConfig, properties);
          if (properties.size() > 0) {
              throw new IllegalArgumentException("There are " + properties.size()
                  + " parameters that couldn't be set on the SocketConnector."
                  + " Check the uri if the parameters are spelt correctly and that they are properties of the SelectChannelConnector."
                  + " Unknown parameters=[" + properties + "]");
          }
      } else*/
      if (getSslSocketConnectorProperties() != null && "https".equals(endpoint.getProtocol())) {
        // must copy the map otherwise it will be deleted
        Map<String, Object> properties =
            new HashMap<String, Object>(getSslSocketConnectorProperties());
        IntrospectionSupport.setProperties(sslcf, properties);
        if (properties.size() > 0) {
          throw new IllegalArgumentException(
              "There are "
                  + properties.size()
                  + " parameters that couldn't be set on the SocketConnector."
                  + " Check the uri if the parameters are spelt correctly and that they are properties of the SelectChannelConnector."
                  + " Unknown parameters=["
                  + properties
                  + "]");
        }
      }
      return result;
    } catch (Exception e) {
      throw ObjectHelper.wrapRuntimeCamelException(e);
    }
  }
예제 #4
0
  public void start() throws Exception {
    // Configure Server
    server = new Server();
    if (ssl) {
      // HTTP Configuration
      HttpConfiguration http_config = new HttpConfiguration();
      http_config.setSecureScheme("https");
      http_config.setSecurePort(0);
      http_config.setOutputBufferSize(32768);
      http_config.setRequestHeaderSize(8192);
      http_config.setResponseHeaderSize(8192);
      http_config.setSendServerVersion(true);
      http_config.setSendDateHeader(false);

      sslContextFactory = new SslContextFactory();
      sslContextFactory.setKeyStorePath(
          MavenTestingUtils.getTestResourceFile("keystore").getAbsolutePath());
      sslContextFactory.setKeyStorePassword("storepwd");
      sslContextFactory.setKeyManagerPassword("keypwd");
      sslContextFactory.setExcludeCipherSuites(
          "SSL_RSA_WITH_DES_CBC_SHA",
          "SSL_DHE_RSA_WITH_DES_CBC_SHA",
          "SSL_DHE_DSS_WITH_DES_CBC_SHA",
          "SSL_RSA_EXPORT_WITH_RC4_40_MD5",
          "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA",
          "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA",
          "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA");

      // SSL HTTP Configuration
      HttpConfiguration https_config = new HttpConfiguration(http_config);
      https_config.addCustomizer(new SecureRequestCustomizer());

      // SSL Connector
      connector =
          new ServerConnector(
              server,
              new SslConnectionFactory(sslContextFactory, "http/1.1"),
              new HttpConnectionFactory(https_config));
      connector.setPort(0);
    } else {
      // Basic HTTP connector
      connector = new ServerConnector(server);
      connector.setPort(0);
    }
    server.addConnector(connector);

    ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/");
    server.setHandler(context);

    // Serve capture servlet
    context.addServlet(new ServletHolder(servlet), "/*");

    // Start Server
    server.start();

    // Establish the Server URI
    String host = connector.getHost();
    if (host == null) {
      host = "localhost";
    }
    int port = connector.getLocalPort();
    serverUri = new URI(String.format("%s://%s:%d/", ssl ? "wss" : "ws", host, port));

    // Some debugging
    if (LOG.isDebugEnabled()) {
      LOG.debug(server.dump());
    }
  }