org.xlightweb.client
Class HttpClientConnection

Package class diagram package HttpClientConnection
java.lang.Object
  extended by org.xlightweb.AbstractHttpConnection
      extended by org.xlightweb.client.HttpClientConnection
All Implemented Interfaces:
Closeable, IHttpClientEndpoint, IHttpConnection, IConnection, IDestroyable

public final class HttpClientConnection
extends org.xlightweb.AbstractHttpConnection
implements IHttpClientEndpoint

Represents the client side endpoint implementation of a http connection. The HttpClientConnection supports constructors which accepts the remote address or a existing INonBlockingConnection. A INonBlockingConnection can become a HttpClientConnection at any time.


Nested Class Summary
 
Nested classes/interfaces inherited from class org.xlightweb.AbstractHttpConnection
org.xlightweb.AbstractHttpConnection.AbstractExchange, org.xlightweb.AbstractHttpConnection.IMessageHandler, org.xlightweb.AbstractHttpConnection.IMessageHeaderHandler, org.xlightweb.AbstractHttpConnection.IMultimodeExecutor, org.xlightweb.AbstractHttpConnection.RequestHandlerAdapter, org.xlightweb.AbstractHttpConnection.ResponseHandlerAdapter
 
Nested classes/interfaces inherited from interface org.xsocket.connection.IConnection
IConnection.FlushMode
 
Field Summary
 
Fields inherited from interface org.xlightweb.client.IHttpClientEndpoint
DEFAULT_AUTOHANDLE_100CONTINUE_RESPONSE
 
Fields inherited from interface org.xlightweb.IHttpConnection
DEFAULT_CONNECT_TIMEOUT_MILLIS, DEFAULT_DATA_RESPONSE_TIMEOUT_MILLIS, DEFAULT_RESPONSE_TIMEOUT_MILLIS
 
Fields inherited from interface org.xsocket.connection.IConnection
DEFAULT_AUTOFLUSH, DEFAULT_CONNECTION_TIMEOUT_MILLIS, DEFAULT_FLUSH_MODE, DEFAULT_IDLE_TIMEOUT_MILLIS, INITIAL_DEFAULT_ENCODING, MAX_TIMEOUT_MILLIS, SO_KEEPALIVE, SO_LINGER, SO_RCVBUF, SO_REUSEADDR, SO_SNDBUF, SO_TIMEOUT, TCP_NODELAY
 
Constructor Summary
HttpClientConnection(InetSocketAddress address)
          constructor
HttpClientConnection(INonBlockingConnection connection)
          constructor
HttpClientConnection(String host, int port)
          constructor
HttpClientConnection(String host, int port, IHttpConnectionHandler connectionHandler)
          constructor
 
Method Summary
protected static int availableSilence(NonBlockingBodyDataSource dataSource)
           
 IHttpResponse call(IHttpRequest request)
          performs a request.
 void close()
          
protected static void forward(NonBlockingBodyDataSource dataSource, BodyDataSink dataSink)
           
protected static String generateErrorMessageHtml(int errorCode, String msg, String id)
          generates a error page
protected static IBodyDataHandler getDataHandlerSilence(NonBlockingBodyDataSource dataSource)
           
protected static int getDataReceived(NonBlockingBodyDataSource dataSource)
           
protected static NonBlockingBodyDataSource getDataSourceOfInMemoryBodyDataSink(BodyDataSink dataSink)
           
protected  org.xlightweb.AbstractHttpConnection.IMessageHeaderHandler getMessageHeaderHandler()
          
protected static IHeader getReceivedHeader(ProtocolException pe)
           
 long getResponseTimeoutMillis()
          returns the response timeout
protected static boolean isComplete(NonBlockingBodyDataSource body)
           
protected static boolean isNetworkendpoint(NonBlockingBodyDataSource dataSource)
           
 boolean isServerSide()
          
protected static boolean isSupports100Contine(IHttpResponseHandler handler)
           
protected static BodyDataSink newInMemoryBodyDataSink(String id, IHttpMessageHeader header)
           
protected  void onConnectionTimeout()
          
protected  void onDisconnect()
           
protected  void onIdleTimeout()
          
protected  void onMessageCompleteReceived(IHttpMessageHeader header)
          
protected static ByteBuffer[] readByteBufferByLengthSilence(NonBlockingBodyDataSource dataSource, int length)
           
protected  void removeMessageHandler(org.xlightweb.AbstractHttpConnection.IMessageHeaderHandler messageHandler)
          
protected static void schedule(TimerTask task, long delay, long period)
          schedules a timer task
 IFutureResponse send(IHttpRequest request)
          send the request.
 BodyDataSink send(IHttpRequestHeader requestHeader, IHttpResponseHandler responseHandler)
          send the request.
 BodyDataSink send(IHttpRequestHeader requestHeader, int contentLength, IHttpResponseHandler responseHandler)
          send the request in a plain body mode

Connection-LifeCycle support: If the request header contains a 'Connection: close' entry, the connection will be closed after the HTTP transaction
 void send(IHttpRequest request, IHttpResponseHandler responseHandler)
          send the request.
protected static void setDataHandlerSilence(NonBlockingBodyDataSource dataSource, IBodyDataHandler dataHandler)
           
 void setResponseBodyDefaultEncoding(String defaultEncoding)
          set the response body default encoding.
 void setResponseTimeoutMillis(long responseTimeoutMillis)
          set the response time out by performing the call or send method.
 String toString()
          
 
Methods inherited from class org.xlightweb.AbstractHttpConnection
activateSecuredMode, addConnectionHandler, closeQuitly, destroy, destroy, destroy, destroy, forwardBody, getAttachment, getBodyDataReceiveTimeoutMillis, getConnectionTimeoutMillis, getCountMessagesReceived, getCountMessagesSent, getCountReceivedBytes, getExecutor, getId, getIdleTimeoutMillis, getLastTimeDataReceivedMillis, getLastTimeHeaderReceivedMillis, getLastTimeMessageTailReceivedMillis, getLastTimeWritten, getLocalAddress, getLocalPort, getMaxWriteBufferSize, getNumOpenTransactions, getOption, getOptions, getReceviedHeader, getRemainingMillisToConnectionTimeout, getRemainingMillisToIdleTimeout, getRemoteAddress, getRemotePort, getSizeDataReceived, getSizeWritten, getUnderlyingTcpConnection, getWorkerpool, incCountMessageReceived, incCountMessageSent, init, isAcceptingChunkedResponseBody, isAutoUncompress, isContentTypeSupportsCharset, isEmpty, isForwardable, isOpen, isPersistent, isReceivingSuspended, isReuseable, isSecure, newDetailedClosedChannelException, newDoNothingResponseHandler, newEmtpyBodyDataSink, newFormEncodedRequestWrapper, newMultimodeExecutor, onConnect, onMessageWritten, onProtocolException, parseEncoding, removeConnectionHandler, resumeReceiving, schedule, setAttachment, setAutocompressThreshold, setAutoUncompress, setBodyCloseListener, setBodyDataReceiveTimeoutMillis, setBodyDefaultEncoding, setConnectionTimeoutMillis, setIdleTimeoutMillis, setLastTimeDataReceivedMillis, setLastTimeHeaderReceivedMillis, setLastTimeMessageTailReceivedMillis, setNetworkBodyDataSinkIgnoreWriteError, setOption, setPersistent, setWorkerpool, setWriteTransferRate, suspendReceiving, writeMessage, writeMessage, writeMessage, writeMessageSilence
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.xlightweb.client.IHttpClientEndpoint
getId
 

Constructor Detail

HttpClientConnection

public HttpClientConnection(String host,
                            int port)
                     throws IOException,
                            ConnectException
constructor

Parameters:
host - the remote host
port - the remote port
Throws:
ConnectException - if an error occurred while attempting to connect to a remote address and port.
IOException - if an exception occurs

HttpClientConnection

public HttpClientConnection(InetSocketAddress address)
                     throws IOException,
                            ConnectException
constructor

Parameters:
address - the server address
Throws:
ConnectException - if an error occurred while attempting to connect to a remote address and port.
IOException - if an exception occurs

HttpClientConnection

public HttpClientConnection(INonBlockingConnection connection)
                     throws IOException
constructor

Parameters:
connection - the underlying tcp connection
Throws:
IOException - if an exception occurs

HttpClientConnection

public HttpClientConnection(String host,
                            int port,
                            IHttpConnectionHandler connectionHandler)
                     throws IOException,
                            ConnectException
constructor

Parameters:
host - the remote host
port - the remote port
connectionHandler - the connection handler
Throws:
ConnectException - if an error occurred while attempting to connect to a remote address and port.
IOException - if an exception occurs
Method Detail

isComplete

protected static boolean isComplete(NonBlockingBodyDataSource body)

isNetworkendpoint

protected static boolean isNetworkendpoint(NonBlockingBodyDataSource dataSource)

isSupports100Contine

protected static boolean isSupports100Contine(IHttpResponseHandler handler)

forward

protected static void forward(NonBlockingBodyDataSource dataSource,
                              BodyDataSink dataSink)
                       throws IOException
Throws:
IOException

getDataReceived

protected static int getDataReceived(NonBlockingBodyDataSource dataSource)

newInMemoryBodyDataSink

protected static BodyDataSink newInMemoryBodyDataSink(String id,
                                                      IHttpMessageHeader header)
                                               throws IOException
Throws:
IOException

getReceivedHeader

protected static IHeader getReceivedHeader(ProtocolException pe)

getDataSourceOfInMemoryBodyDataSink

protected static NonBlockingBodyDataSource getDataSourceOfInMemoryBodyDataSink(BodyDataSink dataSink)

setDataHandlerSilence

protected static void setDataHandlerSilence(NonBlockingBodyDataSource dataSource,
                                            IBodyDataHandler dataHandler)

getDataHandlerSilence

protected static IBodyDataHandler getDataHandlerSilence(NonBlockingBodyDataSource dataSource)

availableSilence

protected static int availableSilence(NonBlockingBodyDataSource dataSource)
                               throws IOException
Throws:
IOException

readByteBufferByLengthSilence

protected static ByteBuffer[] readByteBufferByLengthSilence(NonBlockingBodyDataSource dataSource,
                                                            int length)
                                                     throws IOException
Throws:
IOException

close

public void close()
           throws IOException

Specified by:
close in interface Closeable
Overrides:
close in class org.xlightweb.AbstractHttpConnection
Throws:
IOException

onIdleTimeout

protected void onIdleTimeout()

Overrides:
onIdleTimeout in class org.xlightweb.AbstractHttpConnection

onConnectionTimeout

protected void onConnectionTimeout()

Overrides:
onConnectionTimeout in class org.xlightweb.AbstractHttpConnection

onDisconnect

protected void onDisconnect()
Overrides:
onDisconnect in class org.xlightweb.AbstractHttpConnection

generateErrorMessageHtml

protected static String generateErrorMessageHtml(int errorCode,
                                                 String msg,
                                                 String id)
generates a error page

Parameters:
errorCode - the error code
msg - the message
id - the connection id
Returns:
the error page

schedule

protected static void schedule(TimerTask task,
                               long delay,
                               long period)
schedules a timer task

Parameters:
task - the timer task
delay - the delay
period - the period

call

public IHttpResponse call(IHttpRequest request)
                   throws IOException,
                          ConnectException,
                          SocketTimeoutException
performs a request. This method blocks until the response (header) is received.

Specified by:
call in interface IHttpClientEndpoint
Parameters:
request - the request
Returns:
the response
Throws:
IOException - if an exception occurs
ConnectException - if an error occurred while attempting to connect to a remote address and port.
SocketTimeoutException - if the received timeout is exceed

onMessageCompleteReceived

protected void onMessageCompleteReceived(IHttpMessageHeader header)

Overrides:
onMessageCompleteReceived in class org.xlightweb.AbstractHttpConnection

isServerSide

public boolean isServerSide()

Specified by:
isServerSide in interface IConnection

setResponseBodyDefaultEncoding

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

Parameters:
encoding - the defaultEncoding

setResponseTimeoutMillis

public void setResponseTimeoutMillis(long responseTimeoutMillis)
set the response time out by performing the call or send method.

Specified by:
setResponseTimeoutMillis in interface IHttpClientEndpoint
Parameters:
responseTimeoutMillis - the response timeout

getResponseTimeoutMillis

public long getResponseTimeoutMillis()
returns the response timeout

Specified by:
getResponseTimeoutMillis in interface IHttpClientEndpoint
Returns:
the response timeout

send

public BodyDataSink send(IHttpRequestHeader requestHeader,
                         IHttpResponseHandler responseHandler)
                  throws IOException,
                         ConnectException
send the request. If the content-length header is not set, the message will send in chunked mode

Specified by:
send in interface IHttpClientEndpoint
Parameters:
requestHeader - the request header
responseHandler - the response handler or null (supported: IHttpRequestHandler, IHttpSocketTimeoutHandler)
Returns:
the body handle to write
Throws:
ConnectException - if an error occurred while attempting to connect to a remote address and port.
IOException - if an exception occurs

send

public BodyDataSink send(IHttpRequestHeader requestHeader,
                         int contentLength,
                         IHttpResponseHandler responseHandler)
                  throws IOException,
                         ConnectException
send the request in a plain body mode

Specified by:
send in interface IHttpClientEndpoint
Parameters:
requestHeader - the request header
contentLength - the content length
responseHandler - the response handler or null (supported: IHttpRequestHandler, IHttpSocketTimeoutHandler)
Returns:
the body handle to write
Throws:
ConnectException - if an error occurred while attempting to connect to a remote address and port.
IOException - if an exception occurs

send

public IFutureResponse send(IHttpRequest request)
                     throws IOException,
                            ConnectException
send the request.

Specified by:
send in interface IHttpClientEndpoint
Parameters:
request - the request
Throws:
ConnectException - if an error occurred while attempting to connect to a remote address and port.
IOException - if an exception occurs

send

public void send(IHttpRequest request,
                 IHttpResponseHandler responseHandler)
          throws IOException,
                 ConnectException
send the request.

Specified by:
send in interface IHttpClientEndpoint
Parameters:
request - the request
responseHandler - the response handler or null (supported: IHttpRequestHandler, IHttpSocketTimeoutHandler)
Throws:
ConnectException - if an error occurred while attempting to connect to a remote address and port.
IOException - if an exception occurs

getMessageHeaderHandler

protected org.xlightweb.AbstractHttpConnection.IMessageHeaderHandler getMessageHeaderHandler()

Specified by:
getMessageHeaderHandler in class org.xlightweb.AbstractHttpConnection

removeMessageHandler

protected void removeMessageHandler(org.xlightweb.AbstractHttpConnection.IMessageHeaderHandler messageHandler)


toString

public String toString()

Overrides:
toString in class org.xlightweb.AbstractHttpConnection