org.mortbay.http

Class HttpConnection

public class HttpConnection extends 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.94 2006/02/28 12:45:01 gregwilkins Exp $

Author: Greg Wilkins (gregw)

See Also: HttpListener HttpServer

Field Summary
protected int_dotVersion
protected boolean_keepAlive
protected boolean_persistent
protected HttpRequest_request
protected HttpResponse_response
Constructor Summary
HttpConnection(HttpListener listener, InetAddress remoteAddr, InputStream in, OutputStream out, Object connection)
Constructor.
Method Summary
protected voidassociateThread()
voidclose()
Close the connection.
protected voidcommit()
protected voiddestroy()
Destroy the connection. called by handle when handleNext returns false.
protected voiddisassociateThread()
protected voidfirstWrite()
Setup the reponse output stream.
voidforceClose()
Force the connection to not be persistent.
ObjectgetConnection()
Get the underlying connection object.
StringgetDefaultScheme()
Get the listeners Default scheme.
HttpServergetHttpServer()
Get the listeners HttpServer .
HttpTunnelgetHttpTunnel()
HttpInputStreamgetInputStream()
Get the connections InputStream.
HttpListenergetListener()
Get the connections listener.
ObjectgetObject()
Get associated object.
HttpOutputStreamgetOutputStream()
Get the connections OutputStream.
StringgetRemoteAddr()
Get the Remote address.
StringgetRemoteHost()
Get the Remote address.
InetAddressgetRemoteInetAddress()
Get the Remote address.
intgetRemotePort()
Get the remote Port .
HttpRequestgetRequest()
Get the request.
HttpResponsegetResponse()
Get the response.
StringgetServerAddr()
Get the listeners HttpServer.
StringgetServerName()
Get the listeners HttpServer.
intgetServerPort()
Get the listeners Port .
voidhandle()
Handle the connection.
booleanhandleNext()
Handle next request off the connection.
booleanisThrottled()
voidoutputNotify(OutputStream out, int action, Object ignoredData)
Output Notifications.
protected voidreadRequest()
protected voidrecycle()
Recycle the connection. called by handle when handleNext returns true.
protected HttpContextservice(HttpRequest request, HttpResponse response)
Service a Request.
voidsetHttpTunnel(HttpTunnel tunnel)
Set a HttpTunnel for the connection.
voidsetObject(Object o)
Set associated object.
voidsetThrottled(boolean throttled)
protected voidstatsRequestEnd()
protected voidstatsRequestStart()

Field Detail

_dotVersion

protected int _dotVersion

_keepAlive

protected boolean _keepAlive

_persistent

protected boolean _persistent

_request

protected HttpRequest _request

_response

protected HttpResponse _response

Constructor Detail

HttpConnection

public HttpConnection(HttpListener listener, InetAddress remoteAddr, InputStream in, OutputStream out, 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

associateThread

protected void associateThread()

close

public void close()
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: IOException

commit

protected void commit()

destroy

protected void destroy()
Destroy the connection. called by handle when handleNext returns false.

disassociateThread

protected void disassociateThread()

firstWrite

protected void firstWrite()
Setup the reponse output stream. Use the current state of the request and response, to set tranfer parameters such as chunking and content length.

forceClose

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

getConnection

public 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

getDefaultScheme

public String getDefaultScheme()
Get the listeners Default scheme. Conveniance method equivalent to getListener().getDefaultProtocol().

Returns: HttpServer.

getHttpServer

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

Returns: HttpServer.

getHttpTunnel

public HttpTunnel getHttpTunnel()

Returns: The HttpTunnel set for the connection or null.

getInputStream

public HttpInputStream getInputStream()
Get the connections InputStream.

Returns: the connections InputStream

getListener

public HttpListener getListener()
Get the connections listener.

Returns: HttpListener that created this Connection.

getObject

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

getOutputStream

public HttpOutputStream getOutputStream()
Get the connections OutputStream.

Returns: the connections OutputStream

getRemoteAddr

public String getRemoteAddr()
Get the Remote address.

Returns: the remote host name

getRemoteHost

public String getRemoteHost()
Get the Remote address.

Returns: the remote host name

getRemoteInetAddress

public InetAddress getRemoteInetAddress()
Get the Remote address.

Returns: the remote address

getRemotePort

public int getRemotePort()
Get the remote Port .

Returns: remote port.

getRequest

public HttpRequest getRequest()
Get the request.

Returns: the request

getResponse

public HttpResponse getResponse()
Get the response.

Returns: the response

getServerAddr

public String getServerAddr()
Get the listeners HttpServer.

Returns: HttpServer.

getServerName

public String getServerName()
Get the listeners HttpServer. But if the name is 0.0.0.0, then the real interface address is used.

Returns: HttpServer.

getServerPort

public int getServerPort()
Get the listeners Port . Conveniance method equivalent to getListener().getPort().

Returns: local port.

handle

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

If a HttpTunnel has been set on this connection, it's handle method is called and when that completes, false is return from this method.

The Connection is set as a ThreadLocal of the calling thread and is available via the getHttpConnection() method.

Returns: true if the connection is still open and may provide more requests.

isThrottled

public boolean isThrottled()

Returns: True if this connections state has been altered due to low resources.

outputNotify

public void outputNotify(OutputStream out, int action, Object ignoredData)
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 HttpOutputStream.

Parameters: out The output stream observed. action The action.

See Also: HttpOutputStream

readRequest

protected void readRequest()

recycle

protected void recycle()
Recycle the connection. called by handle when handleNext returns true.

service

protected HttpContext service(HttpRequest request, HttpResponse response)
Service a Request. This implementation passes the request and response to the service method of the HttpServer for this connections listener. If no HttpServer has been associated, the 503 is returned. This method may be specialized to implement other ways of servicing a request.

Parameters: request The request response The response

Returns: The HttpContext that completed handling of the request or null.

Throws: HttpException IOException

setHttpTunnel

public void setHttpTunnel(HttpTunnel tunnel)
Set a HttpTunnel for the connection. A HTTP tunnel is used if the connection is to be taken over for non-HTTP communications. An example of this is the CONNECT method in proxy handling. If a HttpTunnel is set on a connection, then it's handle method is called bu the next call to handleNext().

Parameters: tunnel The HttpTunnel set for the connection or null.

setObject

public void setObject(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.

setThrottled

public void setThrottled(boolean throttled)

Parameters: throttled True if this connections state has been altered due to low resources.

statsRequestEnd

protected void statsRequestEnd()

statsRequestStart

protected void statsRequestStart()
Copyright © 2004 Mortbay Consulting Pty. Ltd. All Rights Reserved.