Class NettyHttpServer

java.lang.Object
eu.cloudnetservice.driver.network.netty.NettySslServer
eu.cloudnetservice.driver.network.netty.http.NettyHttpServer
All Implemented Interfaces:
HttpComponent<HttpServer>, HttpServer, AutoCloseable

@Singleton public class NettyHttpServer extends NettySslServer implements HttpServer
The default implementation of the web server, using netty as its backing mechanism.
Since:
4.0
  • Field Details

    • LOGGER

      private static final eu.cloudnetservice.common.log.Logger LOGGER
    • channelFutures

      protected final Map<HostAndPort,io.netty5.util.concurrent.Future<Void>> channelFutures
    • registeredHandlers

      protected final Collection<NettyHttpServer.HttpHandlerEntry> registeredHandlers
    • bossGroup

      protected final io.netty5.channel.EventLoopGroup bossGroup
    • workerGroup

      protected final io.netty5.channel.EventLoopGroup workerGroup
    • annoParser

      protected final HttpAnnotationParser<HttpServer> annoParser
  • Constructor Details

    • NettyHttpServer

      public NettyHttpServer()
      Constructs a new instance of a netty http server instance. Equivalent to new NettyHttpServer(null).
    • NettyHttpServer

      public NettyHttpServer(@Nullable @Nullable SSLConfiguration sslConfiguration)
      Constructs a new netty http server instance with the given ssl configuration.
      Parameters:
      sslConfiguration - the ssl configuration to use, null for no ssl.
  • Method Details

    • sslEnabled

      public boolean sslEnabled()
      Gets whether this component has ssl enabled or not.
      Specified by:
      sslEnabled in interface HttpComponent<HttpServer>
      Returns:
      whether this component has ssl enabled or not.
    • annotationParser

      @NonNull public @NonNull HttpAnnotationParser<HttpServer> annotationParser()
      Get a http annotation parser which is associated with this component and can therefore be used to register annotated handlers to this component.
      Specified by:
      annotationParser in interface HttpComponent<HttpServer>
      Returns:
      the associated http annotation parser instance.
    • addListener

      @NonNull public @NonNull eu.cloudnetservice.common.concurrent.Task<Void> addListener(int port)
      Binds this http server to the given port on any local address (ipv6 form: ::/0) if no other listener is already listening to that port.
      Specified by:
      addListener in interface HttpServer
      Parameters:
      port - the port to which the listener should get bound.
      Returns:
      a future completed exceptionally if the bind fails, normally if the bind succeeded.
    • addListener

      @NonNull public @NonNull eu.cloudnetservice.common.concurrent.Task<Void> addListener(@NonNull @NonNull HostAndPort hostAndPort)
      Binds this network server to the given host and port if no listener is already listening on the given address.
      Specified by:
      addListener in interface HttpServer
      Parameters:
      hostAndPort - the address to which a listener should get bound.
      Returns:
      a future completed exceptionally if the bind fails, normally if the bind succeeded.
    • registerHandler

      @NonNull public @NonNull HttpServer registerHandler(@NonNull @NonNull String path, HttpHandler... handlers)
      Registers the given handlers to this component. The given handlers will get called when a request matches the given path. Equivalent to component.registerHandler(path, HttpHandler.PRIORITY_NORMAL, handlers).
      Specified by:
      registerHandler in interface HttpComponent<HttpServer>
      Parameters:
      path - the path to register the handler to.
      handlers - the handlers to register.
      Returns:
      the same component instance as used to call the method, for chaining.
    • registerHandler

      @NonNull public @NonNull HttpServer registerHandler(@NonNull @NonNull String path, int priority, HttpHandler... handlers)
      Registers the given handlers to this component. The given handlers will get called when a request matches the given path. Equivalent to component.registerHandler(path, null, priority, handlers).
      Specified by:
      registerHandler in interface HttpComponent<HttpServer>
      Parameters:
      path - the path to register the handler to.
      priority - the priority of the given handlers.
      handlers - the handlers to register.
      Returns:
      the same component instance as used to call the method, for chaining.
    • registerHandler

      @NonNull public @NonNull HttpServer registerHandler(@NonNull @NonNull String path, @Nullable @Nullable Integer port, int priority, @NonNull @NonNull HttpHandler... handlers)
      Registers the given handlers to this component. The given handlers will get called when a request matches the given path and (if provided) gets called on the same port as set here. The path may contain the following placeholders:
      • *: Represents a wildcard character which matches anything provided in the path.
      • {name}: Represents a path parameter you want to retrieve when handling the request.
      Specified by:
      registerHandler in interface HttpComponent<HttpServer>
      Parameters:
      path - the path to register the handler to.
      port - the port on which the handlers should get called, null represents all ports.
      priority - the priority of the given handlers.
      handlers - the handlers to register.
      Returns:
      the same component instance as used to call the method, for chaining.
    • removeHandler

      @NonNull public @NonNull HttpServer removeHandler(@NonNull @NonNull HttpHandler handler)
      Unregisters the given handler from this component if it was registered previously.
      Specified by:
      removeHandler in interface HttpComponent<HttpServer>
      Parameters:
      handler - the handler to unregister.
      Returns:
      the same component instance as used to call the method, for chaining.
    • removeHandler

      @NonNull public @NonNull HttpServer removeHandler(@NonNull @NonNull ClassLoader classLoader)
      Removes all registered handlers from this component whose classes were loaded by the given class loader.
      Specified by:
      removeHandler in interface HttpComponent<HttpServer>
      Parameters:
      classLoader - the loader of the handler classes to unregister.
      Returns:
      the same component instance as used to call the method, for chaining.
    • httpHandlers

      @NonNull public @NonNull Collection<HttpHandler> httpHandlers()
      Get all handlers which were registered to this component.
      Specified by:
      httpHandlers in interface HttpComponent<HttpServer>
      Returns:
      all handlers which were registered to this component.
    • clearHandlers

      @NonNull public @NonNull HttpServer clearHandlers()
      Removes all handlers which were previously registered to this component.
      Specified by:
      clearHandlers in interface HttpComponent<HttpServer>
      Returns:
      the same component instance as used to call the method, for chaining.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable