Context specific request handlers can be assigned
via a url-pattern to a set of URLs (Routing). The url-pattern syntax is equals
to the Servlet API. A request handler will be assigned to a url by using a Context.
Typically, this approach is required, if static resources have to be
supported as well as dynamic resources. See example:
Context ctx = new Context("");
ctx.addHandler("/site/*", new FileServiceRequestHandler(basePath, true));
ctx.addHandler("/rpc/*", new MyBusinessHandler());
ctx.addHandler(new MappingAnnotatedHandler());
Server server = new HttpServer(8080, ctx);
server.start();
Context
public Context(String contextPath)
- constructor
- Parameters:
contextPath - the context path
Context
public Context(String contextPath,
Map<String,IHttpRequestHandler> handlers)
- constructor
- Parameters:
contextPath - the context pathhandlers - handler map
Context
public Context(Context parentContext,
String contextPath)
- constructor
- Parameters:
parentContext - the parent contextcontextPath - the context path
addHandler
public void addHandler(IHttpRequestHandler requestHandler)
- adds an annotated handler to the current context
- Parameters:
requestHandler - the annotated handler (supported: IHttpRequestHandler, ILifeCycle)
addHandler
public void addHandler(String pattern,
IHttpRequestHandler requestHandler)
- adds a handler to the current context
- Parameters:
pattern - the patternrequestHandler - the handler (supported: IHttpRequestHandler, ILifeCycle)
onInit
public void onInit()
-
- Specified by:
onInit in interface ILifeCycle
onDestroy
public void onDestroy()
throws IOException
-
- Specified by:
onDestroy in interface ILifeCycle
- Throws:
IOException
getContextPath
public String getContextPath()
- returns the context path
- Returns:
- the context path
getHandlers
public List<IHttpRequestHandler> getHandlers()
- returns the handlers
- Returns:
- the handlers
size
public int size()
- returns the current size
- Returns:
- the current site
onRequest
public void onRequest(IHttpExchange exchange)
throws IOException
- call back method, which will be called if a request message (header) is received
- Specified by:
onRequest in interface IHttpRequestHandler
- Parameters:
exchange - the exchange contains the request from the client is used to send the response
- Throws:
IOException - if an exception occurred. By throwing this exception an error http response message
will be sent by xSocket, if one or more requests are unanswered. The underlying
connection will be closed
BadMessageException - By throwing this exception an error http response message will be sent by xSocket,
which contains the exception message. The underlying connection will be closed
onRequestTimeout
public boolean onRequestTimeout(IHttpConnection connection)
throws IOException
- see
IConnectionTimeoutHandler
- Specified by:
onRequestTimeout in interface IHttpRequestTimeoutHandler
- Throws:
IOException
clone
protected Object clone()
throws CloneNotSupportedException
-
- Overrides:
clone in class Object
- Throws:
CloneNotSupportedException
toString
public String toString()
-
- Overrides:
toString in class Object