|
Lightweight HTTP Server 1.0.0.Beta1 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.jboss.com.sun.net.httpserver.HttpHost
org.jboss.com.sun.net.httpserver.HttpServer
public abstract class HttpServer
This class implements a simple HTTP server. A HttpServer is bound to an IP address
and port number and listens for incoming TCP connections from clients on this address.
The sub-class HttpsServer
implements a server which handles HTTPS requests.
One or more HttpHandler
objects must be associated with a server
in order to process requests. Each such HttpHandler is registered
with a root URI path which represents the
location of the application or service on this server. The mapping of a handler
to a HttpServer is encapsulated by a HttpContext
object. HttpContexts
are created by calling createContext(String,HttpHandler)
.
Any request for which no handler can be found is rejected with a 404 response.
Management of threads can be done external to this object by providing a
Executor
object. If none is provided a default
implementation is used.
Mapping request URIs to HttpContext paths
When a HTTP request is received, the appropriate HttpContext (and handler) is located by finding the context whose path is the longest matching prefix of the request URI's path. Paths are matched literally, which means that the strings are compared case sensitively, and with no conversion to or from any encoded forms. For example. Given a HttpServer with the following HttpContexts configured.
Context | Context path |
ctx1 | "/" |
ctx2 | "/apps/" |
ctx3 | "/apps/foo/" |
the following table shows some request URIs and which, if any context they would match with.
Request URI | Matches context |
"http://foo.com/apps/foo/bar" | ctx3 |
"http://foo.com/apps/Foo/bar" | no match, wrong case |
"http://foo.com/apps/app1" | ctx2 |
"http://foo.com/foo" | ctx1 |
Note about socket backlogs
When binding to an address and port number, the application can also specify an integer backlog parameter. This represents the maximum number of incoming TCP connections which the system will queue internally. Connections are queued while they are waiting to be accepted by the HttpServer. When the limit is reached, further connections may be rejected (or possibly ignored) by the underlying TCP implementation. Setting the right backlog value is a compromise between efficient resource usage in the TCP layer (not setting it too high) and allowing adequate throughput of incoming requests (not setting it too low).
Constructor Summary | |
---|---|
protected |
HttpServer()
|
Method Summary | |
---|---|
abstract void |
bind(InetSocketAddress addr,
int backlog)
Binds a currently unbound HttpServer to the given address and port number. |
static HttpServer |
create()
creates a HttpServer instance which is initially not bound to any local address/port. |
static HttpServer |
create(InetSocketAddress addr,
int backlog)
Create a HttpServer instance which will bind to the
specified InetSocketAddress (IP address and port number)
A maximum backlog can also be specified. |
abstract HttpContext |
createContext(String path)
Creates a HttpContext without initially specifying a handler. |
abstract HttpContext |
createContext(String path,
HttpHandler handler)
Creates a HttpContext . |
HttpHost |
createVirtualHost(String pattern)
Creates a virtual host with the given pattern. |
abstract InetSocketAddress |
getAddress()
returns the address this server is listening on |
abstract Executor |
getExecutor()
returns this server's Executor object if one was specified with setExecutor(Executor) , or null if none was
specified. |
abstract void |
removeContext(HttpContext context)
Removes the given context from the server. |
abstract void |
removeContext(String path)
Removes the context identified by the given path from the server. |
void |
removeVirtualHost(HttpHost host)
Remove the given virtual host from the server. |
void |
removeVirtualHost(String pattern)
Removes the virtual host identified by the given pattern from the server. |
abstract void |
setExecutor(Executor executor)
sets this server's Executor object. |
abstract void |
start()
Starts this server in a new background thread. |
abstract void |
stop(int delay)
stops this server by closing the listening socket and disallowing any new exchanges from being processed. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
protected HttpServer()
Method Detail |
---|
public static HttpServer create() throws IOException
HttpServerProvider
The server must be bound using bind(InetSocketAddress,int)
before it can be used.
IOException
public static HttpServer create(InetSocketAddress addr, int backlog) throws IOException
HttpServer
instance which will bind to the
specified InetSocketAddress
(IP address and port number)
A maximum backlog can also be specified. This is the maximum number of
queued incoming connections to allow on the listening socket.
Queued TCP connections exceeding this limit may be rejected by the TCP implementation.
The HttpServer is acquired from the currently installed HttpServerProvider
addr
- the address to listen on, if null
then bind() must be called
to set the addressbacklog
- the socket backlog. If this value is less than or equal to zero,
then a system default value is used.
BindException
- if the server cannot bind to the requested address,
or if the server is already bound.
IOException
public abstract void bind(InetSocketAddress addr, int backlog) throws IOException
addr
- the address to listen onbacklog
- the socket backlog. If this value is less than or equal to zero,
then a system default value is used.
BindException
- if the server cannot bind to the requested address or if the server
is already bound.
NullPointerException
- if addr is null
IOException
public abstract void start()
public abstract void setExecutor(Executor executor)
Executor
object. An
Executor must be established before start()
is called.
All HTTP requests are handled in tasks given to the executor.
If this method is not called (before start()) or if it is
called with a null
Executor, then
a default implementation is used, which uses the thread
which was created by the start()
method.
executor
- the Executor to set, or null
for default
implementation
IllegalStateException
- if the server is already startedpublic abstract Executor getExecutor()
setExecutor(Executor)
, or null
if none was
specified.
null
if not set.public abstract void stop(int delay)
delay
- the maximum time in seconds to wait until exchanges have finished.
IllegalArgumentException
- if delay is less than zero.public abstract HttpContext createContext(String path, HttpHandler handler)
HttpContext
. A HttpContext
represents a mapping from a
URI path to a exchange handler on this HttpHost
. Once created, all requests
received by the server for the path will be handled by calling
the given handler object. The context is identified by the path, and
can later be removed from the server using this with the HttpHost.removeContext(String)
method.
The path specifies the root URI path for this context. The first character of path must be
'/'
.
The class overview describes how incoming request URIs are mapped to HttpContext instances.
createContext
in class HttpHost
path
- the root URI path to associate the context withhandler
- the handler to invoke for incoming requests.public abstract HttpContext createContext(String path)
HttpContext
without initially specifying a handler. The handler must later be specified using
com.sun.net.httpserver.HttpContext#setHandler(com.sun.net.httpserver.HttpHandler)
. A HttpContext
represents a mapping from a
URI path to an exchange handler on this HttpHost
. Once created, and when
the handler has been set, all requests
received by the server for the path will be handled by calling
the handler object. The context is identified by the path, and
can later be removed from the server using this with the HttpHost.removeContext(String)
method.
The path specifies the root URI path for this context. The first character of path must be
'/'
.
The class overview describes how incoming request URIs are mapped to HttpContext instances.
createContext
in class HttpHost
path
- the root URI path to associate the context withpublic abstract void removeContext(String path) throws IllegalArgumentException
removeContext
in class HttpHost
path
- the path of the handler to remove
IllegalArgumentException
- if no handler corresponding to this
path exists.public abstract void removeContext(HttpContext context) throws IllegalArgumentException
removeContext
in class HttpHost
context
- the context to remove
IllegalArgumentException
- if the given context is not registered on this hostpublic HttpHost createVirtualHost(String pattern)
"."
, optionally preceded by a
"*."
wildcard designator which means "any host or sequence of
hosts".
The base implementation throws java.lang.UnsupportedOperationException
to avoid breaking backwards compatibility with existing implementations.
pattern
- the virtual host pattern to match
public void removeVirtualHost(String pattern) throws IllegalArgumentException
pattern
- the virtual host pattern previously registered
IllegalArgumentException
- if no such pattern is registeredpublic void removeVirtualHost(HttpHost host) throws IllegalArgumentException
host
- the host to remove
IllegalArgumentException
- if no such host is registeredpublic abstract InetSocketAddress getAddress()
|
Lightweight HTTP Server 1.0.0.Beta1 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |