private static void setupOpenListener(
      HttpOpenListener listener,
      ModelController modelController,
      ConsoleMode consoleMode,
      String consoleSlot,
      ControlledProcessStateService controlledProcessStateService,
      int securePort,
      SecurityRealm securityRealm,
      final ChannelUpgradeHandler upgradeHandler) {

    CanonicalPathHandler canonicalPathHandler = new CanonicalPathHandler();
    listener.setRootHandler(canonicalPathHandler);

    PathHandler pathHandler = new PathHandler();
    HttpHandler current = pathHandler;
    if (upgradeHandler != null) {
      upgradeHandler.setNonUpgradeHandler(current);
      current = upgradeHandler;
    }

    if (securePort > 0) {
      current = new SinglePortConfidentialityHandler(current, securePort);
    }
    // caching handler, used for static resources
    current =
        new CacheHandler(
            new DirectBufferCache(
                1024, 1024 * 10, 1024 * 1000, BufferAllocator.BYTE_BUFFER_ALLOCATOR),
            current);
    current = new SimpleErrorPageHandler(current);

    canonicalPathHandler.setNext(current);

    ResourceHandlerDefinition consoleHandler = null;
    try {
      consoleHandler = consoleMode.createConsoleHandler(consoleSlot);
    } catch (ModuleLoadException e) {
      ROOT_LOGGER.consoleModuleNotFound(consoleSlot == null ? "main" : consoleSlot);
    }

    try {
      pathHandler.addPrefixPath(
          ErrorContextHandler.ERROR_CONTEXT, ErrorContextHandler.createErrorContext(consoleSlot));
    } catch (ModuleLoadException e) {
      ROOT_LOGGER.errorContextModuleNotFound(consoleSlot == null ? "main" : consoleSlot);
    }

    ManagementRootConsoleRedirectHandler rootConsoleRedirectHandler =
        new ManagementRootConsoleRedirectHandler(consoleHandler);
    DomainApiCheckHandler domainApiHandler =
        new DomainApiCheckHandler(modelController, controlledProcessStateService);
    pathHandler.addPrefixPath("/", rootConsoleRedirectHandler);
    if (consoleHandler != null) {
      HttpHandler readinessHandler =
          new RedirectReadinessHandler(
              securityRealm, consoleHandler.getHandler(), ErrorContextHandler.ERROR_CONTEXT);
      pathHandler.addPrefixPath(consoleHandler.getContext(), readinessHandler);
    }

    HttpHandler readinessHandler =
        new DmrFailureReadinessHandler(
            securityRealm,
            secureDomainAccess(domainApiHandler, securityRealm),
            ErrorContextHandler.ERROR_CONTEXT);
    pathHandler.addPrefixPath(DomainApiCheckHandler.PATH, readinessHandler);
    pathHandler.addExactPath("management-upload", readinessHandler);

    if (securityRealm != null) {
      pathHandler.addPrefixPath(LogoutHandler.PATH, new LogoutHandler(securityRealm.getName()));
    }
  }
示例#2
0
  public synchronized void start() {
    xnio = Xnio.getInstance(Undertow.class.getClassLoader());
    channels = new ArrayList<>();
    try {
      worker =
          xnio.createWorker(
              OptionMap.builder()
                  .set(Options.WORKER_IO_THREADS, ioThreads)
                  .set(Options.CONNECTION_HIGH_WATER, 1000000)
                  .set(Options.CONNECTION_LOW_WATER, 1000000)
                  .set(Options.WORKER_TASK_CORE_THREADS, workerThreads)
                  .set(Options.WORKER_TASK_MAX_THREADS, workerThreads)
                  .set(Options.TCP_NODELAY, true)
                  .set(Options.CORK, true)
                  .addAll(workerOptions)
                  .getMap());

      OptionMap socketOptions =
          OptionMap.builder()
              .set(Options.WORKER_IO_THREADS, ioThreads)
              .set(Options.TCP_NODELAY, true)
              .set(Options.REUSE_ADDRESSES, true)
              .set(Options.BALANCING_TOKENS, 1)
              .set(Options.BALANCING_CONNECTIONS, 2)
              .set(Options.BACKLOG, 1000)
              .addAll(this.socketOptions)
              .getMap();

      Pool<ByteBuffer> buffers =
          new ByteBufferSlicePool(
              directBuffers
                  ? BufferAllocator.DIRECT_BYTE_BUFFER_ALLOCATOR
                  : BufferAllocator.BYTE_BUFFER_ALLOCATOR,
              bufferSize,
              bufferSize * buffersPerRegion);

      for (ListenerConfig listener : listeners) {
        final HttpHandler rootHandler =
            listener.rootHandler != null ? listener.rootHandler : this.rootHandler;
        if (listener.type == ListenerType.AJP) {
          AjpOpenListener openListener = new AjpOpenListener(buffers, serverOptions);
          openListener.setRootHandler(rootHandler);
          ChannelListener<AcceptingChannel<StreamConnection>> acceptListener =
              ChannelListeners.openListenerAdapter(openListener);
          AcceptingChannel<? extends StreamConnection> server =
              worker.createStreamConnectionServer(
                  new InetSocketAddress(Inet4Address.getByName(listener.host), listener.port),
                  acceptListener,
                  socketOptions);
          server.resumeAccepts();
          channels.add(server);
        } else {
          OptionMap undertowOptions =
              OptionMap.builder()
                  .set(UndertowOptions.BUFFER_PIPELINED_DATA, true)
                  .addAll(serverOptions)
                  .getMap();
          if (listener.type == ListenerType.HTTP) {
            HttpOpenListener openListener = new HttpOpenListener(buffers, undertowOptions);
            openListener.setRootHandler(rootHandler);
            ChannelListener<AcceptingChannel<StreamConnection>> acceptListener =
                ChannelListeners.openListenerAdapter(openListener);
            AcceptingChannel<? extends StreamConnection> server =
                worker.createStreamConnectionServer(
                    new InetSocketAddress(Inet4Address.getByName(listener.host), listener.port),
                    acceptListener,
                    socketOptions);
            server.resumeAccepts();
            channels.add(server);
          } else if (listener.type == ListenerType.HTTPS) {
            ChannelListener<StreamConnection> openListener;

            HttpOpenListener httpOpenListener = new HttpOpenListener(buffers, undertowOptions);
            httpOpenListener.setRootHandler(rootHandler);

            boolean spdy = serverOptions.get(UndertowOptions.ENABLE_SPDY, false);
            boolean http2 = serverOptions.get(UndertowOptions.ENABLE_HTTP2, false);
            if (spdy || http2) {
              AlpnOpenListener alpn =
                  new AlpnOpenListener(buffers, undertowOptions, httpOpenListener);
              if (spdy) {
                SpdyOpenListener spdyListener =
                    new SpdyOpenListener(
                        buffers,
                        new ByteBufferSlicePool(BufferAllocator.BYTE_BUFFER_ALLOCATOR, 1024, 1024),
                        undertowOptions);
                spdyListener.setRootHandler(rootHandler);
                alpn.addProtocol(SpdyOpenListener.SPDY_3_1, spdyListener, 5);
              }
              if (http2) {
                Http2OpenListener http2Listener = new Http2OpenListener(buffers, undertowOptions);
                http2Listener.setRootHandler(rootHandler);
                alpn.addProtocol(Http2OpenListener.HTTP2, http2Listener, 10);
                alpn.addProtocol(Http2OpenListener.HTTP2_14, http2Listener, 7);
              }
              openListener = alpn;
            } else {
              openListener = httpOpenListener;
            }
            ChannelListener<AcceptingChannel<StreamConnection>> acceptListener =
                ChannelListeners.openListenerAdapter(openListener);
            XnioSsl xnioSsl;
            if (listener.sslContext != null) {
              xnioSsl =
                  new UndertowXnioSsl(
                      xnio,
                      OptionMap.create(Options.USE_DIRECT_BUFFERS, true),
                      listener.sslContext);
            } else {
              xnioSsl =
                  xnio.getSslProvider(
                      listener.keyManagers,
                      listener.trustManagers,
                      OptionMap.create(Options.USE_DIRECT_BUFFERS, true));
            }
            AcceptingChannel<SslConnection> sslServer =
                xnioSsl.createSslConnectionServer(
                    worker,
                    new InetSocketAddress(Inet4Address.getByName(listener.host), listener.port),
                    (ChannelListener) acceptListener,
                    socketOptions);
            sslServer.resumeAccepts();
            channels.add(sslServer);
          }
        }
      }

    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }