org.abstracthorizon.danube.http
Class HTTPConnectionImpl

java.lang.Object
  extended by org.abstracthorizon.danube.connection.ConnectionWrapper
      extended by org.abstracthorizon.danube.http.HTTPConnectionImpl
All Implemented Interfaces:
Adaptable, Connection, HTTPConnection

public class HTTPConnectionImpl
extends ConnectionWrapper
implements HTTPConnection

This connection represents one HTTP request and response. It can be reused over the same underlaying connection (multiple requests over the same socket).

This implementation handles HTTP request string, headers and parameters.

Author:
Daniel Sendula

Field Summary
protected  Map<String,Object> attributes
          Map of attributes
protected  HTTPBufferedInputStream bufferedInput
          Buffered input
protected  HTTPBufferedOutputStream bufferedOutput
          Buffered output
protected  BufferedReader cachedBufferedReader
          Cached buffered reader
protected  InputStream cachedInputStream
          Cached underlaying connection's input stream
protected  OutputStream cachedOutputStream
          Cached underlaying connection's output stream
protected  EncodingPrintWrtier cachedPrintWriter
          Cached print writer
protected  String cachedReadersEncoding
          Cached readers encoding
protected  String componentPath
          Current (processed) requestURI.
protected  int componentPointer
          Pointer to end of component part of the request path
protected  String componentResourcePath
          Current (processed) requestURI.
protected  String contextPath
          Current path up to the current component
protected  int contextPointer
          Pointer to end of context part of the request path
protected static String CRLF
          Helper array of CR and LF characters
protected  int defaultBufferSize
          Default buffer size
protected  boolean expectationIsHandled
          Is expectation header handled
protected  boolean headersCommitted
          Have headers been commited already.
protected  ConnectionHandler parent
          Reference to creator of this handler so forward can work from that point
protected  MultiStringHashMap requestHeaders
          Request headers
protected  String requestMethod
          Request method (GET, POST, etc)
protected  MultiStringHashMap requestParameters
          Request parameters.
protected  String requestPath
          Similar as raw URI but without parameters
protected  String requestProtocol
          Requested protocol (HTTP/1.0, HTTP/1.1 or null)
protected  String requestURI
          Raw URI request.
protected  MultiStringHashMap responseHeaders
          Response headers
protected  String responseProtocol
          Response protocol
protected  Status responseStatus
          Response status
protected  boolean suppressOutput
          Shell content output be suppressed or not.
protected  StringPrintWriter writer
          Writer
protected  boolean writerReturned
          Has writer been already returned
 
Fields inherited from class org.abstracthorizon.danube.connection.ConnectionWrapper
connection, logger
 
Fields inherited from interface org.abstracthorizon.danube.http.HTTPConnection
HEADER_DATE_FORMAT
 
Constructor Summary
HTTPConnectionImpl(Connection connection, ConnectionHandler parent, InputStream inputStream, OutputStream outputStream, int defaultBufferSize)
          Constructor.
HTTPConnectionImpl(Connection connection, ConnectionHandler parent, int defaultBufferSize)
          Constructor.
 
Method Summary
<T> T
adapt(Class<T> cls)
          Adapts this class to HTTPConnection
 void addComponentPathToContextPath()
          Updates context path adding new path element to it
static void addParam(MultiStringMap params, String name, String value)
          Adds parameter to the map.
 void commitHeaders()
          This method output response string and headers
protected  void createRequestHeaders()
           
protected  void createResponseHeaders()
           
 void forward(String uri)
          Redirects request
 Map<String,Object> getAttributes()
          Returns map of attributes.
 int getBufferSize()
          Returns buffer size
 String getComponentPath()
          Returns request uri
 String getComponentResourcePath()
          Returns remainder of path after context path and component path is removed
 HTTPBufferedInputStream getContentInputStream()
          Returns content input stream
 HTTPBufferedOutputStream getContentOutputStream()
          Returns output stream but creates and commits headers before.
 BufferedReader getContentReader()
          Returns content reader
 PrintWriter getContentWriter()
          Returns writer.
 String getContextPath()
          Returns portion of request path up to component path
protected  String getInputEncoding()
          Returns request encoding
protected  String getOutputEncoding()
          Retuns response encoding
 MultiStringMap getRequestHeaders()
          Returns request headers map
 String getRequestMethod()
          Returns request method
 MultiStringMap getRequestParameters()
          Returns request parameters map.
 String getRequestPath()
          This is similar to getRequestURI() but without parameters part
 String getRequestProtocol()
          Returns request protocol
 String getRequestURI()
          Returns raw requested uri along with all parameters if supplied (GET method)
 MultiStringMap getResponseHeaders()
          Returns response headers map
 String getResponseProtocol()
          Returns response protocol
 Status getResponseStatus()
          Returns response status
protected  void handleExpectationHeader()
           
 boolean isCommited()
          Returns true if headers are already send back to the client
 boolean isSuppressOutput()
          Should content output be suppressed or not.
protected  void parseGetParameters()
          Retrieves get parameters
protected  void parseHttpRequestLine()
           
protected  void parsePostParameters()
          Retrieves post parameters
 void processRequest()
          This method processes request.
 String readLine()
          Utility method that reads a line from input stream
 void reset()
          This method processes request.
protected  void retrieveHeaders()
          Retrieves headers
protected static void retrieveParams(MultiStringMap params, Reader r, int len)
          This method extracts parameters from givem reader.
 void setBufferSize(int size)
          Sets buffer size
 void setComponentPath(String requestURI)
          Sets request uri.
 void setComponentResourcePath(String resourcePath)
          Sets component resource path
 void setResponseProtocol(String protocol)
          Sets response protocol
 void setResponseStatus(Status status)
          Sets response status
 void setSuppressOutput(boolean suppressOutput)
          Should output be suppressed or not.
protected  void setupRequestPaths()
          Retrieves get parameters
protected  void updateInputStreamLen()
          Updates content input stream's length or chunked encoding
 
Methods inherited from class org.abstracthorizon.danube.connection.ConnectionWrapper
close, isClosed
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.abstracthorizon.danube.connection.Connection
close, isClosed
 

Field Detail

CRLF

protected static final String CRLF
Helper array of CR and LF characters

See Also:
Constant Field Values

cachedInputStream

protected InputStream cachedInputStream
Cached underlaying connection's input stream


cachedOutputStream

protected OutputStream cachedOutputStream
Cached underlaying connection's output stream


requestHeaders

protected MultiStringHashMap requestHeaders
Request headers


requestParameters

protected MultiStringHashMap requestParameters
Request parameters. If more then one parameter with the same name is supplied then instead of a String as a type, a List is going to be used.


requestMethod

protected String requestMethod
Request method (GET, POST, etc)


requestProtocol

protected String requestProtocol
Requested protocol (HTTP/1.0, HTTP/1.1 or null)


requestURI

protected String requestURI
Raw URI request. It contains everything, even get parameters...


requestPath

protected String requestPath
Similar as raw URI but without parameters


contextPointer

protected int contextPointer
Pointer to end of context part of the request path


componentPointer

protected int componentPointer
Pointer to end of component part of the request path


contextPath

protected String contextPath
Current path up to the current component


componentPath

protected String componentPath
Current (processed) requestURI. This URI is only upto parameters


componentResourcePath

protected String componentResourcePath
Current (processed) requestURI. This URI is only upto parameters


responseHeaders

protected MultiStringHashMap responseHeaders
Response headers


responseStatus

protected Status responseStatus
Response status


responseProtocol

protected String responseProtocol
Response protocol


headersCommitted

protected boolean headersCommitted
Have headers been commited already.


writer

protected StringPrintWriter writer
Writer


attributes

protected Map<String,Object> attributes
Map of attributes


parent

protected ConnectionHandler parent
Reference to creator of this handler so forward can work from that point


writerReturned

protected boolean writerReturned
Has writer been already returned


suppressOutput

protected boolean suppressOutput
Shell content output be suppressed or not. This is needed for HEAD method.


bufferedOutput

protected HTTPBufferedOutputStream bufferedOutput
Buffered output


bufferedInput

protected HTTPBufferedInputStream bufferedInput
Buffered input


cachedPrintWriter

protected EncodingPrintWrtier cachedPrintWriter
Cached print writer


cachedBufferedReader

protected BufferedReader cachedBufferedReader
Cached buffered reader


cachedReadersEncoding

protected String cachedReadersEncoding
Cached readers encoding


defaultBufferSize

protected int defaultBufferSize
Default buffer size


expectationIsHandled

protected boolean expectationIsHandled
Is expectation header handled

Constructor Detail

HTTPConnectionImpl

public HTTPConnectionImpl(Connection connection,
                          ConnectionHandler parent,
                          int defaultBufferSize)
Constructor.

Parameters:
connection - original connection
parent - parent connection handler needed for forwarding
defaultBufferSize - default buffer size

HTTPConnectionImpl

public HTTPConnectionImpl(Connection connection,
                          ConnectionHandler parent,
                          InputStream inputStream,
                          OutputStream outputStream,
                          int defaultBufferSize)
Constructor.

Parameters:
connection - original connection
parent - parent connection handler needed for forwarding
inputStream - input stream to be read from
outputStrema - output stream to be written to
defaultBufferSize - default buffer size
Method Detail

reset

public void reset()
This method processes request. It extracts method, uri, parameters (GET or POST), protocol version and headers.

Specified by:
reset in interface HTTPConnection
Throws:
IOException

isSuppressOutput

public boolean isSuppressOutput()
Should content output be suppressed or not.

Returns:
is content output suppressed or not

setSuppressOutput

public void setSuppressOutput(boolean suppressOutput)
Should output be suppressed or not.

Parameters:
suppressOutput - should the output be suppressed or not.

getBufferSize

public int getBufferSize()
Returns buffer size

Specified by:
getBufferSize in interface HTTPConnection
Returns:
buffer size

setBufferSize

public void setBufferSize(int size)
Sets buffer size

Specified by:
setBufferSize in interface HTTPConnection
Parameters:
size - buffer size

processRequest

public void processRequest()
                    throws IOException
This method processes request. It extracts method, uri, parameters (GET or POST), protocol version and headers.

Throws:
IOException

parseHttpRequestLine

protected void parseHttpRequestLine()
                             throws IOException
Throws:
IOException

parseGetParameters

protected void parseGetParameters()
Retrieves get parameters

Throws:
IOException

setupRequestPaths

protected void setupRequestPaths()
Retrieves get parameters

Throws:
IOException

parsePostParameters

protected void parsePostParameters()
                            throws IOException
Retrieves post parameters

Throws:
IOException

retrieveHeaders

protected void retrieveHeaders()
                        throws IOException
Retrieves headers

Throws:
IOException

retrieveParams

protected static void retrieveParams(MultiStringMap params,
                                     Reader r,
                                     int len)
                              throws IOException
This method extracts parameters from givem reader.

Parameters:
params - parameters map
r - reader
len - number of chars to be read from reader
Throws:
IOException

addParam

public static void addParam(MultiStringMap params,
                            String name,
                            String value)
Adds parameter to the map. If parameter already exists and is of String type then it is replaced with a List and then old and new parameter stored under it.

Parameters:
params - parameter map
name - name of parameter
value - parameter's value

readLine

public String readLine()
                throws IOException
Utility method that reads a line from input stream

Returns:
line string or null if EOF is reached
Throws:
IOException

getRequestHeaders

public MultiStringMap getRequestHeaders()
Returns request headers map

Specified by:
getRequestHeaders in interface HTTPConnection
Returns:
request headers map

createRequestHeaders

protected void createRequestHeaders()

getRequestParameters

public MultiStringMap getRequestParameters()
Returns request parameters map. If more then one parameter is supplied with the same name then List returned with all parameter values in it.

Specified by:
getRequestParameters in interface HTTPConnection
Returns:
request parameters map

getRequestMethod

public String getRequestMethod()
Returns request method

Specified by:
getRequestMethod in interface HTTPConnection
Returns:
request method

getRequestProtocol

public String getRequestProtocol()
Returns request protocol

Specified by:
getRequestProtocol in interface HTTPConnection
Returns:
request protocol

getContextPath

public String getContextPath()
Returns portion of request path up to component path

Specified by:
getContextPath in interface HTTPConnection
Returns:
portion of request path up to component path

addComponentPathToContextPath

public void addComponentPathToContextPath()
Updates context path adding new path element to it

Specified by:
addComponentPathToContextPath in interface HTTPConnection

getComponentPath

public String getComponentPath()
Returns request uri

Specified by:
getComponentPath in interface HTTPConnection
Returns:
request uri

setComponentPath

public void setComponentPath(String requestURI)
Sets request uri. This is to be called from selectors not applicaiton code.

Specified by:
setComponentPath in interface HTTPConnection
Parameters:
requestURI -

getComponentResourcePath

public String getComponentResourcePath()
Returns remainder of path after context path and component path is removed

Specified by:
getComponentResourcePath in interface HTTPConnection
Returns:
remainder of path after context path and component path is removed

setComponentResourcePath

public void setComponentResourcePath(String resourcePath)
Sets component resource path

Specified by:
setComponentResourcePath in interface HTTPConnection
Parameters:
resourcePath - component resource path

getRequestPath

public String getRequestPath()
This is similar to getRequestURI() but without parameters part

Specified by:
getRequestPath in interface HTTPConnection
Returns:
full (unchanged) uri

getRequestURI

public String getRequestURI()
Returns raw requested uri along with all parameters if supplied (GET method)

Specified by:
getRequestURI in interface HTTPConnection
Returns:
raw requested uri

getResponseHeaders

public MultiStringMap getResponseHeaders()
Returns response headers map

Specified by:
getResponseHeaders in interface HTTPConnection
Returns:
response headers map

createResponseHeaders

protected void createResponseHeaders()

getResponseStatus

public Status getResponseStatus()
Returns response status

Specified by:
getResponseStatus in interface HTTPConnection
Returns:
response status

setResponseStatus

public void setResponseStatus(Status status)
Sets response status

Specified by:
setResponseStatus in interface HTTPConnection
Parameters:
status - response status

getResponseProtocol

public String getResponseProtocol()
Returns response protocol

Specified by:
getResponseProtocol in interface HTTPConnection
Returns:
response protocol

setResponseProtocol

public void setResponseProtocol(String protocol)
Sets response protocol

Specified by:
setResponseProtocol in interface HTTPConnection
Parameters:
protocol - response protocol

isCommited

public boolean isCommited()
Returns true if headers are already send back to the client

Specified by:
isCommited in interface HTTPConnection
Returns:
true if headers are already send back to the client

commitHeaders

public void commitHeaders()
This method output response string and headers

Throws:
IOException

getContentOutputStream

public HTTPBufferedOutputStream getContentOutputStream()
Returns output stream but creates and commits headers before.

Returns:
output stream obtained from Connection#getOutputStream

getContentInputStream

public HTTPBufferedInputStream getContentInputStream()
Returns content input stream

Returns:
content input stream

handleExpectationHeader

protected void handleExpectationHeader()

updateInputStreamLen

protected void updateInputStreamLen()
Updates content input stream's length or chunked encoding


getContentWriter

public PrintWriter getContentWriter()
                             throws RuntimeIOException
Returns writer. If writer is not created yet it will be created on the fly.

Returns:
writer
Throws:
RuntimeIOException

getOutputEncoding

protected String getOutputEncoding()
Retuns response encoding

Returns:
response encoding

getContentReader

public BufferedReader getContentReader()
Returns content reader

Returns:
content reader

getInputEncoding

protected String getInputEncoding()
Returns request encoding

Returns:
request encoding

getAttributes

public Map<String,Object> getAttributes()
Returns map of attributes. This method performs lazy instantition of the map.

Specified by:
getAttributes in interface HTTPConnection
Returns:
map of attribtues

forward

public void forward(String uri)
Redirects request

Specified by:
forward in interface HTTPConnection
Parameters:
uri - uri to be redirected to

adapt

public <T> T adapt(Class<T> cls)
Adapts this class to HTTPConnection

Specified by:
adapt in interface Adaptable
Overrides:
adapt in class ConnectionWrapper
Parameters:
cls - class
Returns:
adapter


Copyright © 2005-2009 Abstract Horizon. All Rights Reserved.