org.mortbay.http
Class HttpConnection

java.lang.Object
  |
  +--org.mortbay.http.HttpConnection
All Implemented Interfaces:
OutputObserver

public class HttpConnection
extends java.lang.Object
implements OutputObserver

A HTTP Connection. This class provides the generic HTTP handling for a connection to a HTTP server. An instance of HttpConnection is normally created by a HttpListener and then given control in order to run the protocol handling before and after passing a request to the HttpServer of the HttpListener. This class is not synchronized as it should only ever be known to a single thread.

Version:
$Id: HttpConnection.java,v 1.6 2001/10/25 00:00:03 gregwilkins Exp $
Author:
Greg Wilkins (gregw)
See Also:
HttpListener, HttpServer

Fields inherited from interface org.mortbay.http.OutputObserver
__CLOSED, __CLOSING, __COMMITED, __COMMITING, __FIRST_WRITE, __RESET_BUFFER
 
Constructor Summary
HttpConnection(HttpListener listener, java.net.InetAddress remoteAddr, java.io.InputStream in, java.io.OutputStream out, java.lang.Object connection)
          Constructor.
 
Method Summary
 void close()
          Close the connection.
 void forceClose()
          Force the connection to not be persistent.
 java.lang.Object getConnection()
          Get the underlying connection object.
 java.lang.String getDefaultScheme()
          Get the listeners Default scheme.
 java.lang.String getHost()
          Get the listeners HttpServer .
 HttpServer getHttpServer()
          Get the listeners HttpServer .
 ChunkableInputStream getInputStream()
          Get the connections InputStream.
 HttpListener getListener()
          Get the connections listener.
 java.lang.Object getObject()
          Get associated object.
 ChunkableOutputStream getOutputStream()
          Get the connections OutputStream.
 int getPort()
          Get the listeners Port .
 java.net.InetAddress getRemoteAddr()
          Get the Remote address.
 java.lang.String getRemoteHost()
          Get the Remote address as a String.
 HttpRequest getRequest()
          Get the request.
 HttpResponse getResponse()
          Get the response.
 void handle()
          Handle the connection.
 boolean handleNext()
          Handle next request off the connection.
 void outputNotify(ChunkableOutputStream out, int action)
          Output Notifications.
 void setObject(java.lang.Object o)
          Set associated object.
 void setupOutputStream()
          Setup the reponse output stream.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HttpConnection

public HttpConnection(HttpListener listener,
                      java.net.InetAddress remoteAddr,
                      java.io.InputStream in,
                      java.io.OutputStream out,
                      java.lang.Object connection)
Constructor.
Parameters:
listener - The listener that created this connection.
remoteAddr - The address of the remote end or null.
in - InputStream to read request(s) from.
out - OutputputStream to write response(s) to.
connection - The underlying connection object, most likely a socket. This is not used by HttpConnection other than to make it available via getConnection().
Method Detail

getRemoteAddr

public java.net.InetAddress getRemoteAddr()
Get the Remote address.
Returns:
the remote address

getRemoteHost

public java.lang.String getRemoteHost()
Get the Remote address as a String.
Returns:
the remote address

getInputStream

public ChunkableInputStream getInputStream()
Get the connections InputStream.
Returns:
the connections InputStream

getOutputStream

public ChunkableOutputStream getOutputStream()
Get the connections OutputStream.
Returns:
the connections OutputStream

getConnection

public java.lang.Object getConnection()
Get the underlying connection object. This opaque object, most likely a socket. This is not used by HttpConnection other than to make it available via getConnection().
Returns:
Connection abject

getRequest

public HttpRequest getRequest()
Get the request.
Returns:
the request

getResponse

public HttpResponse getResponse()
Get the response.
Returns:
the response

forceClose

public void forceClose()
Force the connection to not be persistent.

close

public void close()
           throws java.io.IOException
Close the connection. This method calls close on the input and output streams and interrupts any thread in the handle method. may be specialized to close sockets etc.
Throws:
java.io.IOException -  

getListener

public HttpListener getListener()
Get the connections listener.
Returns:
HttpListener that created this Connection.

getHttpServer

public HttpServer getHttpServer()
Get the listeners HttpServer . Conveniance method equivalent to getListener().getHttpServer().
Returns:
HttpServer.

getDefaultScheme

public java.lang.String getDefaultScheme()
Get the listeners Default scheme. Conveniance method equivalent to getListener().getDefaultProtocol().
Returns:
HttpServer.

getHost

public java.lang.String getHost()
Get the listeners HttpServer . Conveniance method equivalent to getListener().getHost().
Returns:
HttpServer.

getPort

public int getPort()
Get the listeners Port . Conveniance method equivalent to getListener().getPort().
Returns:
HttpServer.

getObject

public java.lang.Object getObject()
Get associated object. Used by a particular HttpListener implementation to associate private datastructures with the connection.
Returns:
An object associated with the connecton by setObject.

setObject

public void setObject(java.lang.Object o)
Set associated object. Used by a particular HttpListener implementation to associate private datastructures with the connection.
Parameters:
o - An object associated with the connecton.

outputNotify

public void outputNotify(ChunkableOutputStream out,
                         int action)
                  throws java.io.IOException
Output Notifications. Trigger header and/or filters from output stream observations. Also finalizes method of indicating response content length. Called as a result of the connection subscribing for notifications to the ChunkableOutputStream.
Specified by:
outputNotify in interface OutputObserver
Parameters:
out - The output stream observed.
action - The action.
See Also:
ChunkableOutputStream

setupOutputStream

public void setupOutputStream()
                       throws java.io.IOException
Setup the reponse output stream. Use the current state of the request and response, to set tranfer parameters such as chunking and content length.

handle

public void handle()
Handle the connection. Once the connection has been created, this method is called to handle one or more requests that may be received on the connection. The method only returns once all requests have been handled, an error has been returned to the requestor or the connection has been closed. The handleNext() is called in a loop until it returns false,

handleNext

public boolean handleNext()
Handle next request off the connection. The service(request,response) method is called by handle to service each request received on the connection. If the thread is a PoolThread, the thread is set as inactive when waiting for a request.
Returns:
true if the connection is still open and may provide more requests.


Copyright © 2000 Mortbay Consulting Pty. Ltd. All Rights Reserved.