Class DefaultHttpServer
- java.lang.Object
-
- com.github.fmjsjx.libnetty.http.server.DefaultHttpServer
-
- All Implemented Interfaces:
HttpServer
public class DefaultHttpServer extends java.lang.Object implements HttpServer
The default implementation ofHttpServer.- Since:
- 1.1
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.github.fmjsjx.libnetty.http.server.HttpServer
HttpServer.AbstractUser, HttpServer.User
-
-
Constructor Summary
Constructors Constructor Description DefaultHttpServer()Constructs a newDefaultHttpServerwith the default HTTP port (80).DefaultHttpServer(int port)Constructs a newDefaultHttpServerwith the specifiedport.DefaultHttpServer(com.github.fmjsjx.libnetty.handler.ssl.SslContextProvider sslContextProvider)Constructs a newDefaultHttpServerwith the specifiedsslContextProviderand default HTTPs port (443).DefaultHttpServer(java.lang.String name)DefaultHttpServer(java.lang.String name, int port)DefaultHttpServer(java.lang.String name, com.github.fmjsjx.libnetty.handler.ssl.SslContextProvider sslContextProvider)Constructs a newDefaultHttpServerwith the specifiedname,sslContextProviderand default HTTPs port (443).DefaultHttpServer(java.lang.String name, com.github.fmjsjx.libnetty.handler.ssl.SslContextProvider sslContextProvider, int port)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description DefaultHttpServeraddHeaders(java.util.function.Consumer<io.netty.handler.codec.http.HttpHeaders> addHeaders)Set the function to add HTTP response headers.DefaultHttpServeraddHeaders(java.util.function.Consumer<io.netty.handler.codec.http.HttpHeaders> addHeaders, boolean force)Set the function to add HTTP response headers.java.net.InetAddressaddress()Returns the network address to which the server should bind.DefaultHttpServeraddress(java.net.InetAddress address)Set the network address to which the server should bindDefaultHttpServerapplyCompressionSettings(java.util.function.Consumer<com.github.fmjsjx.libnetty.http.HttpContentCompressorFactory.Builder> action)Enable HTTP content compression feature and apply compression settings.io.netty.channel.ServerChannelchannel()Returns the bindingServerChannel.<T> DefaultHttpServerchildOption(io.netty.channel.ChannelOption<T> childOption, T value)Allow to specify aChannelOptionwhich is used for theChannelinstances once they get created (after the acceptor accepted theChannel).DefaultHttpServercomponent(HttpServerComponent component)Set anHttpServerComponent.DefaultHttpServercorsConfig(io.netty.handler.codec.http.cors.CorsConfig corsConfig)Set theCorsConfigof this server.DefaultHttpServerHandlerProviderdefaultHandlerProvider()Use and returns theDefaultHttpServerHandlerProviderfor this server.DefaultHttpServerdisableSsl()Disable SSL support.DefaultHttpServerenableSsl(com.github.fmjsjx.libnetty.handler.ssl.SslContextProvider sslContextProvider)Enable SSL support and set theSslContextProvider.DefaultHttpServerhandler(HttpServerHandler handler)Set the singletonHttpServerHandlerof this server.DefaultHttpServerhandlerProvider(HttpServerHandlerProvider handlerProvider)Set theHttpServerHandlerProviderof this server.java.lang.Stringhost()Returns the host name to which the server should bind.DefaultHttpServerhost(java.lang.String host)Set the host name to which the server should bind.intioThreads()Returns the number of I/O threads to create for this server.DefaultHttpServerioThreads(int ioThreads)Set the number of I/O threads to create for this server.booleanisRunning()Returns if this server isrunningor not.booleanisSslEnabled()Returns whether to enable SSL support.intmaxContentLength()Returns the maximum length of HTTP content.DefaultHttpServermaxContentLength(int maxContentLength)Set the maximum length of HTTP content.java.lang.Stringname()Returns the display name of this server.DefaultHttpServername(java.lang.String name)Set the name of this server.DefaultHttpServerneverTimeout()Let connections never timeout.<T> DefaultHttpServeroption(io.netty.channel.ChannelOption<T> option, T value)Allow to specify aChannelOptionwhich is used for theChannelinstances once they got created.intport()Returns the listening port of this server.DefaultHttpServerport(int port)Set the listening port of this server.DefaultHttpServerreset()Reset all settings of this server.HttpServershutdown()Shut down this server.DefaultHttpServersoBackLog(int value)SetSO_BACKLOG.HttpServerstartup()Start up this server.DefaultHttpServersupportJson()Support JSON features.DefaultHttpServertcpNoDelay()EnableTCP_NODELAY(disable/enable Nagle's algorithm).DefaultHttpServertimeout(java.time.Duration timeout)Set the time that connectors wait for another HTTP request before closing the connection.inttimeoutSeconds()Returns the time in seconds that connectors wait for another HTTP request before closing the connection.DefaultHttpServertimeoutSeconds(int timeoutSeconds)Set the time in seconds that connectors wait for another HTTP request before closing the connection.java.lang.StringtoString()DefaultHttpServertransport(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.DefaultHttpServertransport(io.netty.channel.EventLoopGroup group, java.lang.Class<? extends io.netty.channel.ServerChannel> channelClass)Specify the transport components of this server.
-
-
-
Constructor Detail
-
DefaultHttpServer
public DefaultHttpServer(java.lang.String name, int port)- Parameters:
name- the name of the serverport- the port
-
DefaultHttpServer
public DefaultHttpServer(int port)
Constructs a newDefaultHttpServerwith the specifiedport.- Parameters:
port- the port
-
DefaultHttpServer
public DefaultHttpServer()
Constructs a newDefaultHttpServerwith the default HTTP port (80).
-
DefaultHttpServer
public DefaultHttpServer(java.lang.String name)
- Parameters:
name- the name of the server
-
DefaultHttpServer
public DefaultHttpServer(com.github.fmjsjx.libnetty.handler.ssl.SslContextProvider sslContextProvider)
Constructs a newDefaultHttpServerwith the specifiedsslContextProviderand default HTTPs port (443).- Parameters:
sslContextProvider- thesslContextProvider
-
DefaultHttpServer
public DefaultHttpServer(java.lang.String name, com.github.fmjsjx.libnetty.handler.ssl.SslContextProvider sslContextProvider)Constructs a newDefaultHttpServerwith the specifiedname,sslContextProviderand default HTTPs port (443).- Parameters:
name- the name of the serversslContextProvider- thesslContextProvider
-
DefaultHttpServer
public DefaultHttpServer(java.lang.String name, com.github.fmjsjx.libnetty.handler.ssl.SslContextProvider sslContextProvider, int port)- Parameters:
name- the name of the serversslContextProvider- thesslContextProviderport- the port
-
-
Method Detail
-
name
public java.lang.String name()
Description copied from interface:HttpServerReturns the display name of this server.- Specified by:
namein interfaceHttpServer- 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 bindThe 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 is0, the default, the number is derived from the number of available processors x2.- 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- aEventLoopGroupused for both the parent (acceptor) and the child (client)channelClass- aClasswhich is used to createChannelinstances- 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- aEventLoopGroupused for the parent (acceptor)childGroup- aEventLoopGroupused for the child (client)channelClass- aClasswhich is used to createChannelinstances- Returns:
- this server
-
corsConfig
public DefaultHttpServer corsConfig(io.netty.handler.codec.http.cors.CorsConfig corsConfig)
Set theCorsConfigof this server.The default value,
null, means this server does not allow cross domain.- Parameters:
corsConfig- theCorsConfigto 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
60seconds.- Parameters:
timeout- time asDurationtype- 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);ortimeout(Duration.ZERO);- Returns:
- this server
-
option
public <T> DefaultHttpServer option(io.netty.channel.ChannelOption<T> option, T value)
Allow to specify aChannelOptionwhich is used for theChannelinstances once they got created. Use a value ofnullto remove a previous setChannelOption.- Type Parameters:
T- the type of the value which is valid for theChannelOption- Parameters:
option- aChannelOptionvalue- the value- Returns:
- this server
- See Also:
ChannelOption
-
soBackLog
public DefaultHttpServer soBackLog(int value)
SetSO_BACKLOG.- Parameters:
value- the value- Returns:
- this server
-
childOption
public <T> DefaultHttpServer childOption(io.netty.channel.ChannelOption<T> childOption, T value)
Allow to specify aChannelOptionwhich is used for theChannelinstances once they get created (after the acceptor accepted theChannel). Use a value ofnullto remove a previous setChannelOption.- Type Parameters:
T- the type of the value which is valid for theChannelOption- Parameters:
childOption- aChannelOptionvalue- the value- Returns:
- this server
- See Also:
ChannelOption
-
tcpNoDelay
public DefaultHttpServer tcpNoDelay()
EnableTCP_NODELAY(disable/enable Nagle's algorithm).- Returns:
- this server
-
isSslEnabled
public boolean isSslEnabled()
Description copied from interface:HttpServerReturns whether to enable SSL support.- Specified by:
isSslEnabledin interfaceHttpServer- Returns:
trueif is enabled SSL support
-
enableSsl
public DefaultHttpServer enableSsl(com.github.fmjsjx.libnetty.handler.ssl.SslContextProvider sslContextProvider)
Enable SSL support and set theSslContextProvider.- Parameters:
sslContextProvider- aSslContextProvider- 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
-
handler
public DefaultHttpServer handler(HttpServerHandler handler)
Set the singletonHttpServerHandlerof this server.To use singleton handler, the implementation of
HttpServerHandlermust beChannelHandler.Sharable.- Parameters:
handler- anHttpServerHandler- Returns:
- this server
-
handlerProvider
public DefaultHttpServer handlerProvider(HttpServerHandlerProvider handlerProvider)
Set theHttpServerHandlerProviderof this server.- Parameters:
handlerProvider- anHttpServerHandlerProvider- Returns:
- this server
-
defaultHandlerProvider
public DefaultHttpServerHandlerProvider defaultHandlerProvider()
Use and returns theDefaultHttpServerHandlerProviderfor this server.- Returns:
- the
DefaultHttpServerHandlerProvider
-
component
public DefaultHttpServer component(HttpServerComponent component)
Set anHttpServerComponent.- Parameters:
component- the component- Returns:
- this server
- Since:
- 1.3
-
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 headersforce- iftruethen only invoke given function, iffalsethen 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:HttpServerReturns if this server isrunningor not.- Specified by:
isRunningin interfaceHttpServer- Returns:
trueif this server isrunning
-
startup
public HttpServer startup() throws java.lang.Exception
Description copied from interface:HttpServerStart up this server.- Specified by:
startupin interfaceHttpServer- Returns:
- this server
- Throws:
java.lang.Exception- if any error occurs
-
channel
public io.netty.channel.ServerChannel channel()
Description copied from interface:HttpServerReturns the bindingServerChannel.- Specified by:
channelin interfaceHttpServer- Returns:
- a
ServerChannel
-
shutdown
public HttpServer shutdown() throws java.lang.Exception
Description copied from interface:HttpServerShut down this server.- Specified by:
shutdownin interfaceHttpServer- Returns:
- this server
- Throws:
java.lang.Exception- if any error occurs
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-