org.xlightweb.server
Class HttpServer

Package class diagram package HttpServer
java.lang.Object
  extended by org.xsocket.connection.Server
      extended by org.xlightweb.server.HttpServer
All Implemented Interfaces:
Closeable, Runnable, IHttpServer, IServer

public class HttpServer
extends Server
implements IHttpServer

A HttpServer. The http server accepts incoming connections and forwards the request to the assigned IHttpRequestHandler. Example:

 
  // defining a request handler 
  class MyRequestHandler implements IHttpRequestHandler  {
  
     public void onRequest(IHttpExchange exchange) throws IOException {
        IHttpRequest request = exchange.getRequest();
        //...
        
        exchange.send(new HttpResponse(200, "text/plain", "it works"));
     }
  } 

 
  // creates a server
  IServer server = new HttpServer(8080, new MyRequestHandler());
 
  // setting some properties 
  server.setMaxTransactions(400);
  server.setRequestTimeoutMillis(5 * 60 * 1000);  
  //...
 
 
  // executing the server 
  server.run();  // (blocks forever)
 
  // or server.start();  (blocks until the server has been started)
  //...
 


Field Summary
 
Fields inherited from class org.xsocket.connection.Server
MIN_SIZE_WORKER_POOL, SIZE_WORKER_POOL, TASK_QUEUE_SIZE
 
Fields inherited from interface org.xsocket.connection.IServer
DEFAULT_CONNECTION_TIMEOUT_SEC, DEFAULT_IDLE_TIMEOUT_SEC, DEFAULT_READ_TRANSFER_PREALLOCATION_MIN_SIZE, DEFAULT_READ_TRANSFER_PREALLOCATION_SIZE, DEFAULT_READ_TRANSFER_USE_DIRECT, SO_RCVBUF, SO_REUSEADDR
 
Constructor Summary
HttpServer(IHttpRequestHandler requestHandler)
          constructor

HttpServer(IHttpRequestHandler handler, Map<String,Object> options)
          constructor

HttpServer(InetAddress address, int port, IHttpRequestHandler handler)
          constructor

HttpServer(InetAddress address, int port, IHttpRequestHandler handler, Map<String,Object> options, SSLContext sslContext, boolean sslOn)
          constructor

HttpServer(InetAddress address, int port, IHttpRequestHandler handler, SSLContext sslContext, boolean sslOn)
          constructor

HttpServer(int port, IHttpRequestHandler handler)
          constructor

HttpServer(int port, IHttpRequestHandler handler, int minPoolsize, int maxPoolsize)
          constructor

HttpServer(int port, IHttpRequestHandler handler, Map<String,Object> options)
          constructor

HttpServer(int port, IHttpRequestHandler handler, Map<String,Object> options, SSLContext sslContext, boolean sslOn)
          constructor

HttpServer(int port, IHttpRequestHandler handler, SSLContext sslContext, boolean sslOn)
          constructor

HttpServer(int port, IHttpRequestHandler handler, SSLContext sslContext, boolean sslOn, int minPoolsize, int maxPoolsize)
          constructor

HttpServer(String ipAddress, int port, IHttpRequestHandler handler)
          constructor

HttpServer(String ipAddress, int port, IHttpRequestHandler handler, Map<String,Object> options)
          constructor

HttpServer(String ipAddress, int port, IHttpRequestHandler handler, Map<String,Object> options, SSLContext sslContext, boolean sslOn)
          constructor

HttpServer(String ipAddress, int port, IHttpRequestHandler handler, SSLContext sslContext, boolean sslOn)
          constructor

 
Method Summary
 void addConnectionHandler(IHttpConnectionHandler connectionHandler)
          adds a connection handler
 int getAutoCompressThresholdBytes()
          gets the autocompress threshold of responses
 long getBodyDataReceiveTimeoutMillis()
          get the body data receive timeout
 String getImplementationDate()
          returns the implementation date
 String getImplementationVersion()
          returns the implementation version
 int getMaxTransactions()
          get the max transactions per connection.
 String getRequestBodyDefaultEncoding()
          get the request body default encoding.
 IHttpRequestHandler getRequestHandler()
          returns the request handler
 long getRequestTimeoutMillis()
          gets the message receive timeout
 ISessionManager getSessionManager()
          returns the session manager
 int getSessionMaxInactiveIntervalSec()
          gets the session max inactive interval in seconds
 boolean isAutoUncompress()
          return true, if the request will be uncompressed (if compressed)
 boolean isCloseOnSendingError()
          returns if the server-side connection will closed, if an error message (4xx or 5xx) will be sent
 boolean isUsingCookies()
          returns true, if cookies is used for session state management
 void setAutoCompressThresholdBytes(int autocompressThresholdBytes)
          sets the autocompress threshold of responses.
 void setAutoUncompress(boolean isAutoUncompress)
          set if the request will be uncompressed (if compressed)
 void setBodyDataReceiveTimeoutMillis(long bodyDataReceiveTimeoutMillis)
          set the body data receive timeout
 void setCloseOnSendingError(boolean isCloseOnSendingError)
          set is if the server-side connection will closed, if an error message (4xx or 5xx) is sent
 void setMaxTransactions(int maxTransactions)
          set the max transactions per connection.
 void setRequestBodyDefaultEncoding(String defaultEncoding)
          set the request body default encoding.
 void setRequestTimeoutMillis(long receivetimeout)
          sets the message receive timeout
 void setSessionManager(ISessionManager sessionManager)
          sets the session manager
 void setSessionMaxInactiveIntervalSec(int sessionMaxInactiveIntervalSec)
          sets the session max inactive interval in seconds
 void setUsingCookies(boolean useCookies)
          sets if cookies is used for session state management
 
Methods inherited from class org.xsocket.connection.Server
addListener, close, getAutoflush, getConnectionTimeoutMillis, getFlushmode, getHandler, getIdleTimeoutMillis, getLocalAddress, getLocalPort, getOpenConnections, getOption, getOptions, getServerName, getStartUpLogMessage, getWorkerpool, isOpen, onClosed, removeListener, run, setAutoflush, setConnectionTimeoutMillis, setFlushmode, setHandler, setIdleTimeoutMillis, setMaxConcurrentConnections, setMaxReadBufferThreshold, setServerName, setStartUpLogMessage, setWorkerpool, setWriteTransferRate, start, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.xsocket.connection.IServer
addListener, getAutoflush, getConnectionTimeoutMillis, getFlushmode, getHandler, getIdleTimeoutMillis, getLocalAddress, getLocalPort, getOption, getOptions, getStartUpLogMessage, getWorkerpool, isOpen, removeListener, setAutoflush, setConnectionTimeoutMillis, setFlushmode, setIdleTimeoutMillis, setStartUpLogMessage, setWorkerpool, setWriteTransferRate, start
 
Methods inherited from interface java.lang.Runnable
run
 
Methods inherited from interface java.io.Closeable
close
 

Constructor Detail

HttpServer

public HttpServer(IHttpRequestHandler requestHandler)
           throws UnknownHostException,
                  IOException
constructor

Parameters:
requestHandler - the requestHandler
Throws:
IOException - If some other I/O error occurs
UnknownHostException - if the local host cannot determined

HttpServer

public HttpServer(IHttpRequestHandler handler,
                  Map<String,Object> options)
           throws UnknownHostException,
                  IOException
constructor

Parameters:
handler - the handler to use
options - the socket options
Throws:
IOException - If some other I/O error occurs
UnknownHostException - if the local host cannot determined

HttpServer

public HttpServer(int port,
                  IHttpRequestHandler handler)
           throws UnknownHostException,
                  IOException
constructor

Parameters:
port - the local port
handler - the handler to use
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

HttpServer

public HttpServer(int port,
                  IHttpRequestHandler handler,
                  int minPoolsize,
                  int maxPoolsize)
           throws UnknownHostException,
                  IOException
constructor

Parameters:
port - the local port
handler - the handler to use
minPoolsize - the min workerpool size
maxPoolsize - the max workerpool size
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

HttpServer

public HttpServer(int port,
                  IHttpRequestHandler handler,
                  Map<String,Object> options)
           throws UnknownHostException,
                  IOException
constructor

Parameters:
port - the local port
handler - the handler to use
options - the acceptor socket options
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

HttpServer

public HttpServer(InetAddress address,
                  int port,
                  IHttpRequestHandler handler)
           throws UnknownHostException,
                  IOException
constructor

Parameters:
address - the local address
port - the local port
handler - the handler to use
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

HttpServer

public HttpServer(String ipAddress,
                  int port,
                  IHttpRequestHandler handler)
           throws UnknownHostException,
                  IOException
constructor

Parameters:
ipAddress - the local ip address
port - the local port
handler - the handler to use
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

HttpServer

public HttpServer(String ipAddress,
                  int port,
                  IHttpRequestHandler handler,
                  Map<String,Object> options)
           throws UnknownHostException,
                  IOException
constructor

Parameters:
ipAddress - the local ip address
port - the local port
handler - the handler to use
options - the socket options
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

HttpServer

public HttpServer(int port,
                  IHttpRequestHandler handler,
                  SSLContext sslContext,
                  boolean sslOn)
           throws UnknownHostException,
                  IOException
constructor

Parameters:
port - local port
handler - the handler to use
sslOn - true, is SSL should be activated
sslContext - the ssl context to use
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

HttpServer

public HttpServer(int port,
                  IHttpRequestHandler handler,
                  SSLContext sslContext,
                  boolean sslOn,
                  int minPoolsize,
                  int maxPoolsize)
           throws UnknownHostException,
                  IOException
constructor

Parameters:
port - local port
handler - the handler to use
sslOn - true, is SSL should be activated
sslContext - the ssl context to use
minPoolsize - the min workerpool size
maxPoolsize - the max workerpool size
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

HttpServer

public HttpServer(int port,
                  IHttpRequestHandler handler,
                  Map<String,Object> options,
                  SSLContext sslContext,
                  boolean sslOn)
           throws UnknownHostException,
                  IOException
constructor

Parameters:
port - local port
options - the acceptor socket options
handler - the handler to use
sslOn - true, is SSL should be activated
sslContext - the ssl context to use
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

HttpServer

public HttpServer(String ipAddress,
                  int port,
                  IHttpRequestHandler handler,
                  SSLContext sslContext,
                  boolean sslOn)
           throws UnknownHostException,
                  IOException
constructor

Parameters:
ipAddress - local ip address
port - local port
handler - the handler to use
sslOn - true, is SSL should be activated
sslContext - the ssl context to use
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

HttpServer

public HttpServer(String ipAddress,
                  int port,
                  IHttpRequestHandler handler,
                  Map<String,Object> options,
                  SSLContext sslContext,
                  boolean sslOn)
           throws UnknownHostException,
                  IOException
constructor

Parameters:
ipAddress - local ip address
port - local port
options - the acceptor socket options
handler - the handler to use
sslOn - true, is SSL should be activated
sslContext - the ssl context to use
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

HttpServer

public HttpServer(InetAddress address,
                  int port,
                  IHttpRequestHandler handler,
                  SSLContext sslContext,
                  boolean sslOn)
           throws UnknownHostException,
                  IOException
constructor

Parameters:
address - local address
port - local port
handler - the request handler
sslOn - true, is SSL should be activated
sslContext - the ssl context to use
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

HttpServer

public HttpServer(InetAddress address,
                  int port,
                  IHttpRequestHandler handler,
                  Map<String,Object> options,
                  SSLContext sslContext,
                  boolean sslOn)
           throws UnknownHostException,
                  IOException
constructor

Parameters:
address - local address
port - local port
options - the socket options
handler - the request handler
sslOn - true, is SSL should be activated
sslContext - the ssl context to use
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs
Method Detail

addConnectionHandler

public void addConnectionHandler(IHttpConnectionHandler connectionHandler)
adds a connection handler

Parameters:
connectionHandler - the connection handler to add

getImplementationVersion

public String getImplementationVersion()
returns the implementation version

Overrides:
getImplementationVersion in class Server
Returns:
the implementation version

getImplementationDate

public String getImplementationDate()
returns the implementation date

Overrides:
getImplementationDate in class Server
Returns:
the implementation date

getRequestHandler

public IHttpRequestHandler getRequestHandler()
returns the request handler

Specified by:
getRequestHandler in interface IHttpServer
Returns:
the request handler

setRequestTimeoutMillis

public void setRequestTimeoutMillis(long receivetimeout)
sets the message receive timeout

Specified by:
setRequestTimeoutMillis in interface IHttpServer
Parameters:
receivetimeout - the message receive timeout

setAutoCompressThresholdBytes

public void setAutoCompressThresholdBytes(int autocompressThresholdBytes)
sets the autocompress threshold of responses. Setting the threshold to Integer.MAX_VALUE deactivates autocompressing

Specified by:
setAutoCompressThresholdBytes in interface IHttpServer
Parameters:
autocompressThresholdBytes - the autocompress threshold

getAutoCompressThresholdBytes

public int getAutoCompressThresholdBytes()
gets the autocompress threshold of responses

Specified by:
getAutoCompressThresholdBytes in interface IHttpServer
Returns:
the autocompress threshold

setAutoUncompress

public void setAutoUncompress(boolean isAutoUncompress)
set if the request will be uncompressed (if compressed)

Specified by:
setAutoUncompress in interface IHttpServer
Parameters:
isAutoUncompress - true, if the request will be uncompressed (if compressed)

isAutoUncompress

public final boolean isAutoUncompress()
return true, if the request will be uncompressed (if compressed)

Specified by:
isAutoUncompress in interface IHttpServer
Returns:
true, if the request will be uncompressed (if compressed)

getRequestTimeoutMillis

public long getRequestTimeoutMillis()
gets the message receive timeout

Specified by:
getRequestTimeoutMillis in interface IHttpServer
Returns:
the message receive timeout

setBodyDataReceiveTimeoutMillis

public final void setBodyDataReceiveTimeoutMillis(long bodyDataReceiveTimeoutMillis)
set the body data receive timeout

Specified by:
setBodyDataReceiveTimeoutMillis in interface IHttpServer

getBodyDataReceiveTimeoutMillis

public long getBodyDataReceiveTimeoutMillis()
get the body data receive timeout

Specified by:
getBodyDataReceiveTimeoutMillis in interface IHttpServer
Returns:
the timeout

setSessionMaxInactiveIntervalSec

public void setSessionMaxInactiveIntervalSec(int sessionMaxInactiveIntervalSec)
sets the session max inactive interval in seconds

Specified by:
setSessionMaxInactiveIntervalSec in interface IHttpServer
Parameters:
sessionMaxInactiveIntervalSec - the session max inactive interval in seconds

getSessionMaxInactiveIntervalSec

public int getSessionMaxInactiveIntervalSec()
gets the session max inactive interval in seconds

Specified by:
getSessionMaxInactiveIntervalSec in interface IHttpServer
Returns:
the session max inactive interval in seconds

setCloseOnSendingError

public void setCloseOnSendingError(boolean isCloseOnSendingError)
set is if the server-side connection will closed, if an error message (4xx or 5xx) is sent

Specified by:
setCloseOnSendingError in interface IHttpServer
Parameters:
isCloseOnSendingError - if the connection will closed, if an error message is sent

isCloseOnSendingError

public boolean isCloseOnSendingError()
returns if the server-side connection will closed, if an error message (4xx or 5xx) will be sent

Specified by:
isCloseOnSendingError in interface IHttpServer
Returns:
true, if the connection will closed by sending an error message

setRequestBodyDefaultEncoding

public void setRequestBodyDefaultEncoding(String defaultEncoding)
set the request body default encoding. According to RFC 2616 the initial value is ISO-8859-1

Specified by:
setRequestBodyDefaultEncoding in interface IHttpServer

getRequestBodyDefaultEncoding

public String getRequestBodyDefaultEncoding()
get the request body default encoding. According to RFC 2616 the initial value is ISO-8859-1

Specified by:
getRequestBodyDefaultEncoding in interface IHttpServer
Returns:
the defaultEncoding

setMaxTransactions

public void setMaxTransactions(int maxTransactions)
set the max transactions per connection. Setting this filed causes that a keep-alive response header will be added

Specified by:
setMaxTransactions in interface IHttpServer
Parameters:
maxTransactions - the max transactions

getMaxTransactions

public int getMaxTransactions()
get the max transactions per connection. Setting this filed causes that a keep-alive response header will be added

Specified by:
getMaxTransactions in interface IHttpServer
Returns:
the max transactions

setSessionManager

public void setSessionManager(ISessionManager sessionManager)
sets the session manager

Specified by:
setSessionManager in interface IHttpServer
Parameters:
sessionManager - the session manager

getSessionManager

public ISessionManager getSessionManager()
returns the session manager

Specified by:
getSessionManager in interface IHttpServer
Returns:
the session manager

setUsingCookies

public void setUsingCookies(boolean useCookies)
sets if cookies is used for session state management

Specified by:
setUsingCookies in interface IHttpServer
Parameters:
useCookies - true, if cookies isused for session state management

isUsingCookies

public boolean isUsingCookies()
returns true, if cookies is used for session state management

Specified by:
isUsingCookies in interface IHttpServer
Returns:
true, if cookies is used for session state management