Class DefaultHttpServer

  • All Implemented Interfaces:
    HttpServer

    public class DefaultHttpServer
    extends java.lang.Object
    implements HttpServer
    The default implementation of HttpServer.
    Since:
    1.1
    • Constructor Detail

      • DefaultHttpServer

        public DefaultHttpServer​(java.lang.String name,
                                 int port)
        Constructs a new DefaultHttpServer with the specified name and port.
        Parameters:
        name - the name of the server
        port - the port
      • DefaultHttpServer

        public DefaultHttpServer​(int port)
        Constructs a new DefaultHttpServer with the specified port.
        Parameters:
        port - the port
      • DefaultHttpServer

        public DefaultHttpServer()
        Constructs a new DefaultHttpServer with the default HTTP port (80).
      • DefaultHttpServer

        public DefaultHttpServer​(java.lang.String name)
        Constructs a new DefaultHttpServer with the specified name and default HTTP port (80).
        Parameters:
        name - the name of the server
      • DefaultHttpServer

        public DefaultHttpServer​(com.github.fmjsjx.libnetty.handler.ssl.SslContextProvider sslContextProvider)
        Constructs a new DefaultHttpServer with the specified sslContextProvider and default HTTPs port (443).
        Parameters:
        sslContextProvider - the sslContextProvider
      • DefaultHttpServer

        public DefaultHttpServer​(java.lang.String name,
                                 com.github.fmjsjx.libnetty.handler.ssl.SslContextProvider sslContextProvider)
        Constructs a new DefaultHttpServer with the specified name, sslContextProvider and default HTTPs port (443).
        Parameters:
        name - the name of the server
        sslContextProvider - the sslContextProvider
      • DefaultHttpServer

        public DefaultHttpServer​(java.lang.String name,
                                 com.github.fmjsjx.libnetty.handler.ssl.SslContextProvider sslContextProvider,
                                 int port)
        Constructs a new DefaultHttpServer with the specified name, sslContextProvider and port.
        Parameters:
        name - the name of the server
        sslContextProvider - the sslContextProvider
        port - the port
    • Method Detail

      • name

        public java.lang.String name()
        Description copied from interface: HttpServer
        Returns the display name of this server.
        Specified by:
        name in interface HttpServer
        Returns:
        the display name of this server
      • name

        public DefaultHttpServer name​(java.lang.String name)
        Set the name of this server.

        The default value is "default".

        Parameters:
        name - the name string
        Returns:
        this server
      • port

        public int port()
        Returns the listening port of this server.
        Returns:
        the port
      • port

        public DefaultHttpServer port​(int port)
        Set the listening port of this server.
        Parameters:
        port - the port
        Returns:
        this server
      • host

        public java.lang.String host()
        Returns the host name to which the server should bind.
        Returns:
        the host name
      • host

        public DefaultHttpServer host​(java.lang.String host)
        Set the host name to which the server should bind.

        The default value, null, means any address.

        Parameters:
        host - the host name
        Returns:
        this server
      • address

        public java.net.InetAddress address()
        Returns the network address to which the server should bind.
        Returns:
        the network address to which the server should bind
      • address

        public DefaultHttpServer address​(java.net.InetAddress address)
        Set the network address to which the server should bind

        The default value, null, means any address.

        Parameters:
        address - the network address
        Returns:
        this server
      • ioThreads

        public int ioThreads()
        Returns the number of I/O threads to create for this server. When the value is 0, the default, the number is derived from the number of available processors x 2.
        Returns:
        the number of I/O threads to create for this server
      • ioThreads

        public DefaultHttpServer ioThreads​(int ioThreads)
        Set the number of I/O threads to create for this server.
        Parameters:
        ioThreads - the number of I/O threads to create for this server
        Returns:
        this server
      • transport

        public DefaultHttpServer transport​(io.netty.channel.EventLoopGroup group,
                                           java.lang.Class<? extends io.netty.channel.ServerChannel> channelClass)
        Specify the transport components of this server.
        Parameters:
        group - a EventLoopGroup used for both the parent (acceptor) and the child (client)
        channelClass - a Class which is used to create Channel instances
        Returns:
        this server
      • transport

        public DefaultHttpServer transport​(io.netty.channel.EventLoopGroup parentGroup,
                                           io.netty.channel.EventLoopGroup childGroup,
                                           java.lang.Class<? extends io.netty.channel.ServerChannel> channelClass)
        Specify the transport components of this server.
        Parameters:
        parentGroup - a EventLoopGroup used for the parent (acceptor)
        childGroup - a EventLoopGroup used for the child (client)
        channelClass - a Class which is used to create Channel instances
        Returns:
        this server
      • corsConfig

        public DefaultHttpServer corsConfig​(io.netty.handler.codec.http.cors.CorsConfig corsConfig)
        Set the CorsConfig of this server.

        The default value, null, means this server does not allow cross domain.

        Parameters:
        corsConfig - the CorsConfig to be set
        Returns:
        this server
      • maxContentLength

        public int maxContentLength()
        Returns the maximum length of HTTP content.
        Returns:
        the maximum length of HTTP content
      • maxContentLength

        public DefaultHttpServer maxContentLength​(int maxContentLength)
        Set the maximum length of HTTP content.

        The default value is 2147483647(Integer.MAX_VALUE).

        Parameters:
        maxContentLength - the maximum length of HTTP content
        Returns:
        this server
      • timeoutSeconds

        public int timeoutSeconds()
        Returns the time in seconds that connectors wait for another HTTP request before closing the connection.
        Returns:
        the time in seconds
      • timeout

        public DefaultHttpServer timeout​(java.time.Duration timeout)
        Set the time that connectors wait for another HTTP request before closing the connection.

        The default value is 60 seconds.

        Parameters:
        timeout - time as Duration type
        Returns:
        this server
      • timeoutSeconds

        public DefaultHttpServer timeoutSeconds​(int timeoutSeconds)
        Set the time in seconds that connectors wait for another HTTP request before closing the connection.

        The default value is 60.

        Parameters:
        timeoutSeconds - time in seconds
        Returns:
        this server
      • neverTimeout

        public DefaultHttpServer neverTimeout()
        Let connections never timeout.

        This method is equivalent to:

         
             timeoutSeconds(0);
         
         
         or
         
         
             timeout(Duration.ZERO);
         
         
        Returns:
        this server
      • option

        public <T> DefaultHttpServer option​(io.netty.channel.ChannelOption<T> option,
                                            T value)
        Allow to specify a ChannelOption which is used for the Channel instances once they got created. Use a value of null to remove a previous set ChannelOption.
        Type Parameters:
        T - the type of the value which is valid for the ChannelOption
        Parameters:
        option - a ChannelOption
        value - the value
        Returns:
        this server
        See Also:
        ChannelOption
      • soBackLog

        public DefaultHttpServer soBackLog​(int value)
        Set SO_BACKLOG.
        Parameters:
        value - the value
        Returns:
        this server
      • childOption

        public <T> DefaultHttpServer childOption​(io.netty.channel.ChannelOption<T> childOption,
                                                 T value)
        Allow to specify a ChannelOption which is used for the Channel instances once they get created (after the acceptor accepted the Channel). Use a value of null to remove a previous set ChannelOption.
        Type Parameters:
        T - the type of the value which is valid for the ChannelOption
        Parameters:
        childOption - a ChannelOption
        value - the value
        Returns:
        this server
        See Also:
        ChannelOption
      • tcpNoDelay

        public DefaultHttpServer tcpNoDelay()
        Enable TCP_NODELAY (disable/enable Nagle's algorithm).
        Returns:
        this server
      • isSslEnabled

        public boolean isSslEnabled()
        Description copied from interface: HttpServer
        Returns whether to enable SSL support.
        Specified by:
        isSslEnabled in interface HttpServer
        Returns:
        true if is enabled SSL support
      • enableSsl

        public DefaultHttpServer enableSsl​(com.github.fmjsjx.libnetty.handler.ssl.SslContextProvider sslContextProvider)
        Enable SSL support and set the SslContextProvider.
        Parameters:
        sslContextProvider - a SslContextProvider
        Returns:
        this server
      • disableSsl

        public DefaultHttpServer disableSsl()
        Disable SSL support.
        Returns:
        this server
      • applyCompressionSettings

        public DefaultHttpServer applyCompressionSettings​(java.util.function.Consumer<com.github.fmjsjx.libnetty.http.HttpContentCompressorFactory.Builder> action)
        Enable HTTP content compression feature and apply compression settings.
        Parameters:
        action - the apply action
        Returns:
        this server
      • supportJson

        public DefaultHttpServer supportJson()
        Support JSON features.

        This method is equivalent to: component(JsonLibrary.getInstance()).

        Returns:
        this server
        Since:
        1.3
      • addHeaders

        public DefaultHttpServer addHeaders​(java.util.function.Consumer<io.netty.handler.codec.http.HttpHeaders> addHeaders)
        Set the function to add HTTP response headers. Include default headers.

        The default headers:

         server: libnetty
         
        Parameters:
        addHeaders - the function to add HTTP response headers
        Returns:
        this server
      • addHeaders

        public DefaultHttpServer addHeaders​(java.util.function.Consumer<io.netty.handler.codec.http.HttpHeaders> addHeaders,
                                            boolean force)
        Set the function to add HTTP response headers.

        The default headers:

         server: libnetty
         
        Parameters:
        addHeaders - the function to add HTTP response headers
        force - if true then only invoke given function, if false then will invoke both default function and given function
        Returns:
        this server
      • reset

        public DefaultHttpServer reset()
        Reset all settings of this server.
        Returns:
        this server
      • isRunning

        public boolean isRunning()
        Description copied from interface: HttpServer
        Returns if this server is running or not.
        Specified by:
        isRunning in interface HttpServer
        Returns:
        true if this server is running
      • startup

        public HttpServer startup()
                           throws java.lang.Exception
        Description copied from interface: HttpServer
        Start up this server.
        Specified by:
        startup in interface HttpServer
        Returns:
        this server
        Throws:
        java.lang.Exception - if any error occurs
      • channel

        public io.netty.channel.ServerChannel channel()
        Description copied from interface: HttpServer
        Returns the binding ServerChannel.
        Specified by:
        channel in interface HttpServer
        Returns:
        a ServerChannel
      • shutdown

        public HttpServer shutdown()
                            throws java.lang.Exception
        Description copied from interface: HttpServer
        Shut down this server.
        Specified by:
        shutdown in interface HttpServer
        Returns:
        this server
        Throws:
        java.lang.Exception - if any error occurs
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object