org.xlightweb
Class HttpRequestHandler

Package class diagram package HttpRequestHandler
java.lang.Object
  extended by org.xlightweb.HttpRequestHandler
All Implemented Interfaces:
IHttpRequestHandler

public class HttpRequestHandler
extends Object
implements IHttpRequestHandler

Provides an abstract class to be subclassed to implement a IHttpRequestHandler. A subclass of HttpRequestHandlerAdapter must override at least one method, usually one of these:

If the method for a dedicated HTTP method is not sub classes, a 405 error will be returned.

Example:
 class MyRequestHandler extends HttpRequestHandler {
 
    @Override
    protected void doGet(IHttpExchange exchange) throws IOException, BadMessageException {
       exchange.send(new HttpResponse(200, "text/plain", "GET called"));
    }
    
    @Override
    protected void doPost(IHttpExchange exchange) throws IOException, BadMessageException {
       exchange.send(new HttpResponse(200, "text/plain", "POST called"));
    }
 }
 
For more examples see IHttpRequestHandler


Field Summary
 
Fields inherited from interface org.xlightweb.IHttpRequestHandler
DEFAULT_EXECUTION_MODE, DEFAULT_INVOKE_ON_MODE, DEFAULT_SYNCHRONIZED_ON_MODE
 
Constructor Summary
HttpRequestHandler()
           
 
Method Summary
 void doDelete(IHttpExchange exchange)
          call back which will be called, if a DELETE request is received This method does not need to be either safe or idempotent.
 void doGet(IHttpExchange exchange)
          call back which will be called, if a GET request is received Overriding this method to support a GET request also automatically supports an HTTP HEAD request.
 void doHead(IHttpExchange exchange)
          call back which will be called, if a HEAD request is received The client sends a HEAD request when it wants to see only the headers of a response, such as Content-Type or Content-Length.
 void doOptions(IHttpExchange exchange)
          call back which will be called, if a OPTIONS request is received
 void doPost(IHttpExchange exchange)
          call back which will be called, if a POST request is received This method does not need to be either safe or idempotent.
 void doPut(IHttpExchange exchange)
          call back which will be called, if a PUT request is received This method does not need to be either safe or idempotent.
 void doTrace(IHttpExchange exchange)
          call back which will be called, if a TRACE request is received
 void onRequest(IHttpExchange exchange)
          call back method, which will be called if a request message (header) is received
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HttpRequestHandler

public HttpRequestHandler()
Method Detail

onRequest

public final 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

doGet

public void doGet(IHttpExchange exchange)
           throws IOException,
                  BadMessageException
call back which will be called, if a GET request is received

Overriding this method to support a GET request also automatically supports an HTTP HEAD request. A HEAD request is a GET request that returns no body in the response, only the request header fields.

The GET method should also be idempotent, meaning that it can be safely repeated.

Parameters:
exchange - the exchange
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

doPost

public void doPost(IHttpExchange exchange)
            throws IOException,
                   BadMessageException
call back which will be called, if a POST request is received

This method does not need to be either safe or idempotent. Operations requested through POST can have side effects

Parameters:
exchange - the exchange
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

doPut

public void doPut(IHttpExchange exchange)
           throws IOException,
                  BadMessageException
call back which will be called, if a PUT request is received

This method does not need to be either safe or idempotent. Operations that doPut performs can have side effects

Parameters:
exchange - the exchange
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

doDelete

public void doDelete(IHttpExchange exchange)
              throws IOException,
                     BadMessageException
call back which will be called, if a DELETE request is received

This method does not need to be either safe or idempotent. Operations requested through DELETE can have side effects

Parameters:
exchange - the exchange
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

doHead

public void doHead(IHttpExchange exchange)
            throws IOException,
                   BadMessageException
call back which will be called, if a HEAD request is received

The client sends a HEAD request when it wants to see only the headers of a response, such as Content-Type or Content-Length.

Parameters:
exchange - the exchange
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

doOptions

public void doOptions(IHttpExchange exchange)
               throws IOException,
                      BadMessageException
call back which will be called, if a OPTIONS request is received

Parameters:
exchange - the exchange
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

doTrace

public void doTrace(IHttpExchange exchange)
             throws IOException,
                    BadMessageException
call back which will be called, if a TRACE request is received

Parameters:
exchange - the exchange
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