org.xlightweb
Class RequestHandlerChain

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

public class RequestHandlerChain
extends Object
implements IHttpRequestHandler, IHttpRequestTimeoutHandler, ILifeCycle

Implements a handler chain. The next handler of the chain will be called (in the registering order) by forwarding the request. See IHttpExchange. Example:

 @Execution(Execution.NONTHREADED)
 class HeaderLogInterceptor implements IHttpRequestHandler {
     
    public void onRequest(final IHttpExchange exchange) throws IOException {
       
       IHttpResponseHandler respHdl = new IHttpResponseHandler() {
       
          public void onResponse(IHttpResponse response) throws IOException {
             System.out.println(response.getResponseHeader());
             exchange.send(response);
          }
          
          public void onException(IOException ioe) {
             System.out.println(ioe.toString());
             exchange.sendError(500);
          }
       };
       
       System.out.println(exchange.getRequest().getRequestHeader());
       exchange.forward(exchange.getRequest(), respHdl);
    }
 }
 
 
 RequestHandlerChain chain = new RequestHandlerChain();
 chain.addLast(new HeaderLogInterceptor());
 chain.addLast(new MySeviceHandler());
 
 IServer server = new HttpServer(8080, chain);
 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
RequestHandlerChain()
          constructor
RequestHandlerChain(List<IHttpRequestHandler> handlers)
          constructor
 
Method Summary
 void addFirst(IHttpRequestHandler handler)
          add a handler to the top of the chain
 void addLast(IHttpRequestHandler handler)
          add a handler to the end of the chain
 void clear()
          removes all handler
 List<IHttpRequestHandler> getHandlers()
          returns all handler
 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
 void remove(IHttpRequestHandler handler)
          removes the handler
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

RequestHandlerChain

public RequestHandlerChain()
constructor


RequestHandlerChain

public RequestHandlerChain(List<IHttpRequestHandler> handlers)
constructor

Parameters:
handlers - the initial handlers
Method Detail

addFirst

public void addFirst(IHttpRequestHandler handler)
add a handler to the top of the chain

Parameters:
handler - the handler to add

addLast

public void addLast(IHttpRequestHandler handler)
add a handler to the end of the chain

Parameters:
handler - the handler to add

remove

public void remove(IHttpRequestHandler handler)
removes the handler

Parameters:
handler - the handler to remove

clear

public void clear()
removes all handler


getHandlers

public List<IHttpRequestHandler> getHandlers()
returns all handler

Returns:
the handlers

onInit

public void onInit()

Specified by:
onInit in interface ILifeCycle

onDestroy

public void onDestroy()
               throws IOException

Specified by:
onDestroy in interface ILifeCycle
Throws:
IOException

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
Description copied from interface: IHttpRequestTimeoutHandler
see IConnectionTimeoutHandler

Specified by:
onRequestTimeout in interface IHttpRequestTimeoutHandler
Throws:
IOException

toString

public String toString()
Overrides:
toString in class Object