org.apache.xmlrpc.webserver
Class Connection

java.lang.Object
  extended by org.apache.xmlrpc.webserver.Connection
All Implemented Interfaces:
org.apache.xmlrpc.common.ServerStreamConnection, org.apache.xmlrpc.util.ThreadPool.InterruptableTask, org.apache.xmlrpc.util.ThreadPool.Task

public class Connection
extends java.lang.Object
implements org.apache.xmlrpc.util.ThreadPool.InterruptableTask, org.apache.xmlrpc.common.ServerStreamConnection

Handler for a single clients connection. This implementation is able to do HTTP keepalive. In other words, it can serve multiple requests via a single, physical connection.


Nested Class Summary
private static class Connection.BadEncodingException
           
private static class Connection.BadRequestException
           
private static class Connection.RequestException
           
 
Field Summary
private  byte[] buffer
           
private static byte[] clength
           
private static byte[] conclose
           
private static byte[] conkeep
           
private static byte[] ctype
           
private static byte[] doubleNewline
           
private  boolean firstByte
           
private  java.util.Map headers
           
private  java.io.InputStream input
           
private static byte[] newline
           
private static byte[] ok
           
private  java.io.OutputStream output
           
private  RequestData requestData
           
private  XmlRpcStreamServer server
           
private static byte[] serverName
           
private  boolean shuttingDown
           
private  java.net.Socket socket
           
private static java.lang.String US_ASCII
           
private  WebServer webServer
           
private static byte[] wwwAuthenticate
           
 
Constructor Summary
Connection(WebServer pWebServer, XmlRpcStreamServer pServer, java.net.Socket pSocket)
          Creates a new webserver connection on the given socket.
 
Method Summary
 void close()
           
private  RequestData getRequestConfig()
          Returns the connections request configuration by merging the HTTP request headers and the servers configuration.
 java.io.InputStream newInputStream()
           
 java.io.OutputStream newOutputStream()
           
private  java.lang.String readLine()
           
 void run()
           
 void setResponseHeader(java.lang.String pHeader, java.lang.String pValue)
          Sets a response header value.
 void shutdown()
           
private static byte[] toHTTPBytes(java.lang.String text)
          Returns the US-ASCII encoded byte representation of text for HTTP use (as per section 2.2 of RFC 2068).
private  void writeContentLengthHeader(int pContentLength)
           
 void writeError(RequestData pData, java.lang.Throwable pError, java.io.ByteArrayOutputStream pStream)
          Writes an error response to the output stream.
 void writeErrorHeader(RequestData pData, java.lang.Throwable pError, int pContentLength)
          Writes an error responses headers to the output stream.
 void writeResponse(RequestData pData, java.io.OutputStream pBuffer)
          Writes the response header and the response to the output stream.
 void writeResponseHeader(RequestData pData, int pContentLength)
          Writes the response header to the output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

US_ASCII

private static final java.lang.String US_ASCII
See Also:
Constant Field Values

ctype

private static final byte[] ctype

clength

private static final byte[] clength

newline

private static final byte[] newline

doubleNewline

private static final byte[] doubleNewline

conkeep

private static final byte[] conkeep

conclose

private static final byte[] conclose

ok

private static final byte[] ok

serverName

private static final byte[] serverName

wwwAuthenticate

private static final byte[] wwwAuthenticate

webServer

private final WebServer webServer

socket

private final java.net.Socket socket

input

private final java.io.InputStream input

output

private final java.io.OutputStream output

server

private final XmlRpcStreamServer server

buffer

private byte[] buffer

headers

private java.util.Map headers

requestData

private RequestData requestData

shuttingDown

private boolean shuttingDown

firstByte

private boolean firstByte
Constructor Detail

Connection

public Connection(WebServer pWebServer,
                  XmlRpcStreamServer pServer,
                  java.net.Socket pSocket)
           throws java.io.IOException
Creates a new webserver connection on the given socket.

Parameters:
pWebServer - The webserver maintaining this connection.
pServer - The server being used to execute requests.
pSocket - The server socket to handle; the Connection is responsible for closing this socket.
Throws:
java.io.IOException
Method Detail

toHTTPBytes

private static final byte[] toHTTPBytes(java.lang.String text)
Returns the US-ASCII encoded byte representation of text for HTTP use (as per section 2.2 of RFC 2068).


getRequestConfig

private RequestData getRequestConfig()
                              throws java.io.IOException
Returns the connections request configuration by merging the HTTP request headers and the servers configuration.

Returns:
The connections request configuration.
Throws:
java.io.IOException - Reading the request headers failed.

run

public void run()
Specified by:
run in interface org.apache.xmlrpc.util.ThreadPool.Task

readLine

private java.lang.String readLine()
                           throws java.io.IOException
Throws:
java.io.IOException

writeResponse

public void writeResponse(RequestData pData,
                          java.io.OutputStream pBuffer)
                   throws java.io.IOException
Writes the response header and the response to the output stream.

Parameters:
pData - The request data.
pBuffer - The ByteArrayOutputStream holding the response.
Throws:
java.io.IOException - Writing the response failed.

writeResponseHeader

public void writeResponseHeader(RequestData pData,
                                int pContentLength)
                         throws java.io.IOException
Writes the response header to the output stream. *

Parameters:
pData - The request data
pContentLength - The content length, if known, or -1.
Throws:
java.io.IOException - Writing the response failed.

writeError

public void writeError(RequestData pData,
                       java.lang.Throwable pError,
                       java.io.ByteArrayOutputStream pStream)
                throws java.io.IOException
Writes an error response to the output stream.

Parameters:
pData - The request data.
pError - The error being reported.
pStream - The ByteArrayOutputStream with the error response.
Throws:
java.io.IOException - Writing the response failed.

writeErrorHeader

public void writeErrorHeader(RequestData pData,
                             java.lang.Throwable pError,
                             int pContentLength)
                      throws java.io.IOException
Writes an error responses headers to the output stream.

Parameters:
pData - The request data.
pError - The error being reported.
pContentLength - The response length, if known, or -1.
Throws:
java.io.IOException - Writing the response failed.

writeContentLengthHeader

private void writeContentLengthHeader(int pContentLength)
                               throws java.io.IOException
Throws:
java.io.IOException

setResponseHeader

public void setResponseHeader(java.lang.String pHeader,
                              java.lang.String pValue)
Sets a response header value.


newOutputStream

public java.io.OutputStream newOutputStream()
                                     throws java.io.IOException
Specified by:
newOutputStream in interface org.apache.xmlrpc.common.ServerStreamConnection
Throws:
java.io.IOException

newInputStream

public java.io.InputStream newInputStream()
                                   throws java.io.IOException
Specified by:
newInputStream in interface org.apache.xmlrpc.common.ServerStreamConnection
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Specified by:
close in interface org.apache.xmlrpc.common.ServerStreamConnection
Throws:
java.io.IOException

shutdown

public void shutdown()
              throws java.lang.Throwable
Specified by:
shutdown in interface org.apache.xmlrpc.util.ThreadPool.InterruptableTask
Throws:
java.lang.Throwable