Esempio n. 1
0
  // 配置Undertow服务
  public void configServer(Properties configuration, List<FrontController> frontControllers) {
    int ioThread =
        Integer.parseInt(
            configuration.getProperty(
                "io_thread_number",
                String.valueOf(Runtime.getRuntime().availableProcessors() + 1)));
    int workerThread =
        Integer.parseInt(
            configuration.getProperty(
                "work_thread_number",
                String.valueOf(Runtime.getRuntime().availableProcessors() * 200)));
    String httpHost = configuration.getProperty("http.host", "0.0.0.0");
    int httpPort = Integer.parseInt(configuration.getProperty("http.port", "7080"));
    String staticResourcePath = configuration.getProperty("server.context.path.static", "static");

    PathHandler pathHandler = new PathHandler();

    // 静态资源,压缩
    ResourceManager resourceManager = new FileResourceManager(new File(staticResourcePath), 10240);
    ContentEncodedResourceManager encoder =
        new ContentEncodedResourceManager(
            Paths.get(staticResourcePath),
            new CachingResourceManager(100, 10000, null, resourceManager, -1),
            new ContentEncodingRepository()
                .addEncodingHandler("gzip", new GzipEncodingProvider(), 50, null),
            1024,
            1024 * 500,
            null);
    ResourceHandler resourceHandler = new ResourceHandler(resourceManager);

    if (configuration.getProperty("http.gzip", "false").equals("true")) {
      resourceHandler.setContentEncodedResourceManager(encoder);
    }
    pathHandler.addPrefixPath("/", resourceHandler);

    // API
    for (FrontController frontController : frontControllers) {
      logger.info("Setup Undertow server path:" + frontController.getPath());
      pathHandler.addPrefixPath(
          frontController.getPath(), new FrontControllerHandler(frontController));
    }

    undertow =
        Undertow.builder()
            .addHttpListener(httpPort, httpHost)
            .setIoThreads(ioThread)
            .setWorkerThreads(workerThread)
            .setHandler(pathHandler)
            .build();
    logger.info(
        "Setup Undertow server static resources at path:"
            + staticResourcePath
            + " at location:"
            + staticResourcePath);
  }
Esempio n. 2
0
 @Override
 public HttpHandler createHandler(
     HttpHandler next, final OperationContext context, ModelNode model)
     throws OperationFailedException {
   String path = PATH.resolveModelAttribute(context, model).asString();
   UndertowLogger.ROOT_LOGGER.infof("Creating file handler for path %s", path);
   FileResourceManager resourceManager = new FileResourceManager(Paths.get(path));
   ResourceHandler handler = new ResourceHandler();
   handler.setResourceManager(resourceManager);
   handler.setDirectoryListingEnabled(true);
   return handler;
 }