org.xlightweb
Class Context

Package class diagram package Context
java.lang.Object
  extended by org.xlightweb.Context
All Implemented Interfaces:
Cloneable, IHttpRequestHandler, IHttpRequestTimeoutHandler, IWebHandler, ILifeCycle

public class Context
extends Object
implements IHttpRequestHandler, IHttpRequestTimeoutHandler, ILifeCycle, Cloneable

By using the 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();
 


Field Summary
 
Fields inherited from interface org.xlightweb.IHttpRequestHandler
DEFAULT_EXECUTION_MODE, DEFAULT_INVOKE_ON_MODE, DEFAULT_SYNCHRONIZED_ON_MODE
 
Fields inherited from interface org.xlightweb.IHttpRequestTimeoutHandler
DEFAULT_EXECUTION_MODE
 
Constructor Summary
Context(Context parentContext, String contextPath)
          constructor
Context(String contextPath)
          constructor
Context(String contextPath, Map<String,IHttpRequestHandler> handlers)
          constructor
 
Method Summary
 void addHandler(IHttpRequestHandler requestHandler)
          adds an annotated handler to the current context
 void addHandler(String pattern, IHttpRequestHandler requestHandler)
          adds a handler to the current context
protected  Object clone()
          
 String getContextPath()
          returns the context path
 List<IHttpRequestHandler> getHandlers()
          returns the handlers
 void onDestroy()
          
 void onInit()
          
 void onRequest(IHttpExchange exchange)
          call back method, which will be called if a request message (header) is received
 boolean onRequestTimeout(IHttpConnection connection)
          see IConnectionTimeoutHandler
 int size()
          returns the current size
 String toString()
          
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

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 path
handlers - handler map

Context

public Context(Context parentContext,
               String contextPath)
constructor

Parameters:
parentContext - the parent context
contextPath - the context path
Method Detail

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 pattern
requestHandler - 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