- java.lang.Object
-
- com.kttdevelopment.simplehttpserver.SimpleHttpServer
-
- Direct Known Subclasses:
SimpleHttpsServer
public abstract class SimpleHttpServer extends Object
This class is a simplified implementation ofHttpServer.
At least oneHttpHandlermust be created in order to process requests. When handling requests the server will use the most specific context. If no handler can be found it is rejected with a 404 response.
Contexts are case-sensitive.- Since:
- 02.00.00
- Version:
- 03.05.08
- Author:
- Ktt Development
- See Also:
HttpServer,HttpHandler,SimpleHttpsServer,SimpleHttpHandler
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedSimpleHttpServer()Creates an emptySimpleHttpServer.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract InetSocketAddressbind(int port)Binds the server to a port.abstract InetSocketAddressbind(int port, int backlog)Binds the server to a port.abstract voidbind(InetSocketAddress addr)Binds the server to a port.abstract voidbind(InetSocketAddress addr, int backlog)Binds the server to a port.static SimpleHttpServercreate()Creates aSimpleHttpServer.static SimpleHttpServercreate(int port)Creates aSimpleHttpServerbounded to a port.static SimpleHttpServercreate(int port, int backlog)Creates aSimpleHttpServerbounded to a port.abstract HttpContextcreateContext(String context)Creates an empty context.abstract HttpContextcreateContext(String context, Authenticator authenticator)Creates a context mapped to a specificHttpContextwith anAuthenticator.abstract HttpContextcreateContext(String context, HttpHandler handler)Creates a context mapped to a specifiedHttpHandler.abstract HttpContextcreateContext(String context, HttpHandler handler, Authenticator authenticator)Creates a context mapped to a specificHttpContextwith anAuthenticator.abstract InetSocketAddressgetAddress()Returns the address that the server is binded to.abstract HttpHandlergetContextHandler(HttpContext context)Returns the handler mapped to a context or null if it is not found.abstract HttpHandlergetContextHandler(String context)Returns the handler mapped to a context or null if none is found.abstract Map<HttpContext,HttpHandler>getContexts()Returns a copy of the server's contexts and their respective handlers.abstract ExecutorgetExecutor()Returns the server's executor or null if none exists.abstract HttpServergetHttpServer()Returns the native http server.abstract HttpSessiongetHttpSession(SimpleHttpExchange exchange)Returns the session associated with an exchange or null it no session handler exists.abstract HttpSessiongetHttpSession(HttpExchange exchange)Returns the session associated with an exchange or null if no session handler exists.abstract HttpSessionHandlergetHttpSessionHandler()Returns the session handler for the server or null if none exists.abstract StringgetRandomContext()Generates a random context at the root that can be used on the server.abstract StringgetRandomContext(String context)Generates a random context that can be used on the server.abstract voidremoveContext(HttpContext context)Removes the context from the server.abstract voidremoveContext(String context)Removes the context at the context.abstract voidsetExecutor(Executor executor)Sets the server's executor.abstract voidsetHttpSessionHandler(HttpSessionHandler sessionHandler)Sets a session handler for the server.abstract voidstart()Starts the server.abstract voidstop()Stops the server and all active requests.abstract voidstop(int delay)Stops the server with a delay for remaining requests.
-
-
-
Constructor Detail
-
SimpleHttpServer
protected SimpleHttpServer()
Creates an emptySimpleHttpServer. Server is created usingcreate(),create(int), orcreate(int, int).- Since:
- 02.00.00
- See Also:
create(),create(int),create(int, int),SimpleHttpServerImpl.createHttpServer(Integer, Integer)
-
-
Method Detail
-
create
public static SimpleHttpServer create() throws IOException
Creates aSimpleHttpServer.- Returns:
- a
SimpleHttpServer - Throws:
IOException- uncaught exception- Since:
- 02.00.00
-
create
public static SimpleHttpServer create(int port) throws IOException
Creates aSimpleHttpServerbounded to a port.- Parameters:
port- port to bind to- Returns:
- a
SimpleHttpServer - Throws:
BindException- if server can not bind to portNullPointerException- if address isnullIllegalArgumentException- if port is out of rangeIOException- uncaught exception- Since:
- 02.00.00
-
create
public static SimpleHttpServer create(int port, int backlog) throws IOException
Creates aSimpleHttpServerbounded to a port.- Parameters:
port- port to bind tobacklog- maximum amount of inbound connections allowed- Returns:
- a
SimpleHttpServer - Throws:
BindException- if server can not bind to portNullPointerException- if address isnullIllegalArgumentException- if port is out of rangeIOException- uncaught exception- Since:
- 02.00.00
-
getHttpServer
public abstract HttpServer getHttpServer()
Returns the native http server.- Returns:
- http server
- Since:
- 02.00.00
- See Also:
HttpServer
-
bind
public abstract InetSocketAddress bind(int port) throws IOException
Binds the server to a port.- Parameters:
port- port to bind the server to- Returns:
- address the server is binded to
- Throws:
BindException- if server could not be bound to port, or if it's already boundIllegalArgumentException- if port is out of rangeNullPointerException- if address isnullIOException- uncaught exception- Since:
- 02.00.00
- See Also:
bind(int, int),bind(InetSocketAddress),bind(InetSocketAddress, int)
-
bind
public abstract InetSocketAddress bind(int port, int backlog) throws IOException
Binds the server to a port.- Parameters:
port- port to bind the server tobacklog- maximum amount of inbound connections allowed- Returns:
- address the server is binded to
- Throws:
BindException- if server could not be bound to port, or if it's already boundIllegalArgumentException- if port is out of rangeNullPointerException- if address isnullIOException- uncaught exception- Since:
- 02.00.00
- See Also:
bind(int),bind(InetSocketAddress),bind(InetSocketAddress, int)
-
bind
public abstract void bind(InetSocketAddress addr) throws IOException
Binds the server to a port.- Parameters:
addr- address to bind the server to- Throws:
BindException- if server could not be bound to port, or if it's already boundIllegalArgumentException- if port is out of rangeNullPointerException- if address isnullIOException- uncaught exception- Since:
- 02.00.00
- See Also:
InetSocketAddress,bind(int),bind(int, int),bind(InetSocketAddress, int)
-
bind
public abstract void bind(InetSocketAddress addr, int backlog) throws IOException
Binds the server to a port.- Parameters:
addr- address to bind the server tobacklog- maximum amount of inbound connections allowed- Throws:
BindException- if server could not be bound to port, or if it's already boundNullPointerException- if address isnullIOException- uncaught exception- Since:
- 02.00.00
- See Also:
InetSocketAddress,bind(int),bind(int, int),bind(InetSocketAddress)
-
getAddress
public abstract InetSocketAddress getAddress()
Returns the address that the server is binded to.- Returns:
- binded address
- Throws:
NullPointerException- if server is not binded to a port- Since:
- 02.00.00
- See Also:
InetSocketAddress
-
setExecutor
public abstract void setExecutor(Executor executor)
Sets the server's executor.
For unlimited simultaneous threading useExecutors.newCachedThreadPool(); for limited simultaneous threading useExecutors.newFixedThreadPool(int).- Parameters:
executor- server executor- Since:
- 02.00.00
- See Also:
getExecutor(),Executor
-
getExecutor
public abstract Executor getExecutor()
Returns the server's executor or null if none exists.- Returns:
- server executor
- Since:
- 02.00.00
- See Also:
setExecutor(Executor),Executor
-
setHttpSessionHandler
public abstract void setHttpSessionHandler(HttpSessionHandler sessionHandler)
Sets a session handler for the server.- Parameters:
sessionHandler- session handler- Since:
- 03.03.00
- See Also:
HttpSessionHandler,getHttpSessionHandler()
-
getHttpSessionHandler
public abstract HttpSessionHandler getHttpSessionHandler()
Returns the session handler for the server or null if none exists.- Returns:
- server session handler
- Since:
- 03.03.00
- See Also:
HttpSessionHandler,setHttpSessionHandler(HttpSessionHandler)
-
getHttpSession
public abstract HttpSession getHttpSession(HttpExchange exchange)
Returns the session associated with an exchange or null if no session handler exists.- Parameters:
exchange- http exchange- Returns:
- http session
- Since:
- 03.03.00
- See Also:
HttpSession,getHttpSession(SimpleHttpExchange)
-
getHttpSession
public abstract HttpSession getHttpSession(SimpleHttpExchange exchange)
Returns the session associated with an exchange or null it no session handler exists.- Parameters:
exchange- http exchange- Returns:
- http session
- Since:
- 03.03.00
- See Also:
HttpSession,getHttpSession(HttpExchange)
-
createContext
public abstract HttpContext createContext(String context)
Creates an empty context.
*HttpServeroften does not throw an exception for duplicate contexts. #86- Parameters:
context- the context- Returns:
- the http context associated with the context
- Throws:
IllegalArgumentException- if the context is invalid or taken*NullPointerException- if the context is null- Since:
- 02.00.00
- See Also:
HttpContext,createContext(String, HttpHandler),removeContext(String),removeContext(HttpContext)
-
createContext
public abstract HttpContext createContext(String context, HttpHandler handler)
Creates a context mapped to a specifiedHttpHandler.
*HttpServeroften does not throw an exception for duplicate contexts. #86- Parameters:
context- the contexthandler- the handler- Returns:
- the http context associated with the context
- Throws:
IllegalArgumentException- if the context is invalid or taken*NullPointerException- if the context is null- Since:
- 02.00.00
- See Also:
HttpContext,HttpHandler,createContext(String),removeContext(String),removeContext(HttpContext)
-
createContext
public abstract HttpContext createContext(String context, Authenticator authenticator)
Creates a context mapped to a specificHttpContextwith anAuthenticator.
*HttpServeroften does not throw an exception for duplicate contexts. #86- Parameters:
context- the contextauthenticator- authenticator- Returns:
- the http context associated with the context
- Throws:
IllegalArgumentException- if the context is invalid or taken*NullPointerException- if the context is null- Since:
- 03.03.00
- See Also:
HttpContext,Authenticator,createContext(String, HttpHandler, Authenticator),removeContext(String),removeContext(HttpContext)
-
createContext
public abstract HttpContext createContext(String context, HttpHandler handler, Authenticator authenticator)
Creates a context mapped to a specificHttpContextwith anAuthenticator.
*HttpServeroften does not throw an exception for duplicate contexts. #86- Parameters:
context- the contexthandler- the handlerauthenticator- authenticator- Returns:
- the http context associated with the context
- Throws:
IllegalArgumentException- if the context is invalid or taken*NullPointerException- if the context is null- Since:
- 03.03.00
- See Also:
HttpContext,HttpHandler,Authenticator,createContext(String, Authenticator),removeContext(String),removeContext(HttpContext)
-
removeContext
public abstract void removeContext(String context)
Removes the context at the context.- Parameters:
context- the context to remove- Throws:
IllegalArgumentException- if no handler at that context existsNullPointerException- if the context is null- Since:
- 02.00.00
- See Also:
removeContext(HttpContext)
-
removeContext
public abstract void removeContext(HttpContext context)
Removes the context from the server.- Parameters:
context- the context to remove- Throws:
IllegalArgumentException- if no handler at that context existsNullPointerException- if the context is null- Since:
- 02.00.00
- See Also:
removeContext(String)
-
getContextHandler
public abstract HttpHandler getContextHandler(String context)
Returns the handler mapped to a context or null if none is found.- Parameters:
context- context to retrieve- Returns:
- handler associated with that context
- Since:
- 02.00.00
- See Also:
getContextHandler(HttpContext)
-
getContextHandler
public abstract HttpHandler getContextHandler(HttpContext context)
Returns the handler mapped to a context or null if it is not found.- Parameters:
context- context to retrieve- Returns:
- handler associated with that context
- Since:
- 02.00.00
- See Also:
getContextHandler(String)
-
getContexts
public abstract Map<HttpContext,HttpHandler> getContexts()
Returns a copy of the server's contexts and their respective handlers.- Returns:
- server's contexts
- Since:
- 02.00.00
-
getRandomContext
public abstract String getRandomContext()
Generates a random context at the root that can be used on the server.- Returns:
- random safe context
- Since:
- 03.03.00
- See Also:
getRandomContext(String),TemporaryHandler
-
getRandomContext
public abstract String getRandomContext(String context)
Generates a random context that can be used on the server.- Parameters:
context- where to generate the random context- Returns:
- random safe context
- Since:
- 03.03.00
- See Also:
getRandomContext(),TemporaryHandler
-
start
public abstract void start()
Starts the server.- Throws:
IllegalStateException- if server is not bound to a valid port- Since:
- 2.00.00
- See Also:
stop(),stop(int)
-
stop
public abstract void stop()
Stops the server and all active requests.
-
-