|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.acplt.oncrpc.web.HttpClientConnection
public class HttpClientConnection
The class HttpClientConnection
provides a simple HTTP/1.1
compliant connection from a client to an HTTP server. This class does not
provide a full-blown HTTP connection, but it is rather optimized for what
ONC/RPC clients need in order to tunnel ONC remote procedure calls through
ordinary HTTP connections, thus penetrating firewalls.
A HttpClientConnection
is not that clever as you would
first expect. Rather you have to do some things for yourself, like
reconnecting dropped connections, and so on. While this sometimes result
in more labour on the caller's shoulders, this keeps resource wasting at
a minimum, and gives you full control over redirections and other mess --
you do want full control, right?.
For this reason, for instance, an HttpClientConnection
does not buffer the whole request before sending it to the server but
rather relies on the caller to supply the right content-length information.
This avoids unnecessary double buffering but instead creates the bas64
encoded content on-the-fly.
Of course, this client connection object does not touch the content, it just suplies the pipe to swallow the data.
Nested Class Summary | |
---|---|
(package private) class |
HttpClientConnection.GetPropertyPrivilegedAction
|
Field Summary | |
---|---|
private byte[] |
asciiBuffer
Buffer receiving ASCII characters from Unicode strings when sending (header) strings to the HTTP server. |
private java.lang.String |
cachedProxyHost
Address of proxy host to contact instead of real HTTP server. |
private int |
cachedProxyPort
Port number of proxy HTTP server. |
private boolean |
chunkedTransfer
Indicates whether the HTTP server send its reply using the chunked transfer encoding. |
private java.lang.String |
contentType
Type of content sent by HTTP server. |
static byte[] |
CRLF
|
private boolean |
finalChunkSeen
Indicates whether the end-of-chunks chunk has been read (whow, another one for the Purlitzer price). |
private char[] |
headerLine
Dynamically growing buffer used during header parsing. |
private java.lang.String |
hostname
Host name of HTTP server to contact in the form of www.acplt.org . |
private static int |
HTTP_DEAD
|
static int |
HTTP_DEFAULTPORT
Default port where HTTP servers listen for incomming requests. |
private static int |
HTTP_IDLE
|
private static int |
HTTP_RECEIVING
|
private static int |
HTTP_SENDING
|
private java.io.InputStream |
in
|
private boolean |
keepAlive
|
private int |
mode
Indicates sending/receiving mode of the HTTP connection. |
private java.io.OutputStream |
out
Buffered output stream used for sending the HTTP headers. |
private int |
port
Port number where to contact the HTTP server. |
private int |
remainingChunkLength
Contains the amount of data still to be read for the current chunk. |
private int |
remainingContentLength
Indicates the amount of content data which still has to be sent or received. |
private int |
responseCode
Contains the HTTP response code from the last request sent to the HTTP server. |
private java.net.Socket |
socket
TCP/IP socket for communication with the HTTP server. |
private int |
timeout
Timeout (in milliseconds) for communication with an HTTP server. |
private boolean |
useProxy
Indicates whether a proxy HTTP server needs to be contacted in order to reach the real HTTP server. |
private java.lang.String |
userAgentId
|
Constructor Summary | |
---|---|
HttpClientConnection(java.lang.String hostname)
Constructs a new HttpClientConnection . |
|
HttpClientConnection(java.lang.String hostname,
int port)
Constructs a new HttpClientConnection . |
Method Summary | |
---|---|
int |
beginDecoding()
Begin receiving the content sent by the HTTP server. |
void |
beginPostRequest(java.lang.String path,
java.lang.String mimeType,
int contentLength)
Starts a new HTTP "POST" request and sends all necessary HTTP header fields. |
void |
close()
Closes the connection to the HTTP server and frees up some resources. |
private void |
connect()
Connects to the HTTP server. |
void |
endDecoding()
This method silently discards any unread content, if the caller has yet not read all content. |
void |
endPostRequest()
Ends the HTTP "POST" request. |
java.lang.String |
getContentType()
Returns the content type (MIME type, charset, etc.). |
boolean |
getKeepAlive()
|
private java.lang.String |
getProxyHost()
Retrieves the host name where the HTTP proxy server resided from the system properties. |
private int |
getProxyPort()
Retrieves the port number where the HTTP proxy server resides from the system properties. |
int |
getRemainingContentLength()
Returns amount of content still available (to be read). |
int |
getResponseCode()
|
int |
getTimeout()
Retrieve the current timeout set for remote procedure calls. |
protected void |
handleOption(java.lang.String option,
java.lang.String value)
Handle options sent by the HTTP server. |
int |
readContentBytes(byte[] buffer,
int offset,
int length)
Read content sent by the HTTP server. |
private boolean |
readHeaderLine(java.lang.String[] keyvalue)
Read in a header line coming over the HTTP connection from the server. |
private int |
readHeaders()
Read the HTTP headers sent by the servers and also parse them at the same time. |
private java.lang.String |
readLine()
Read exactly one line, termined by CRLF or either CR or LF, and return it. |
void |
setTimeout(int milliseconds)
Set the timout for sending or receiving information to/from the HTTP server. |
private void |
write(java.lang.String s)
Writes an ASCII string to the HTTP server (this is buffered first). |
void |
writeContentBytes(byte[] bytes,
int offset,
int length)
Send (part) of the content to the HTTP server. |
private void |
writeln(java.lang.String s)
Writes an ASCII string and appends a line termination in the form of CR followed by LF. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int HTTP_DEFAULTPORT
private java.lang.String hostname
www.acplt.org
.
private int port
private java.net.Socket socket
private int timeout
private boolean keepAlive
private boolean chunkedTransfer
private int remainingChunkLength
private boolean finalChunkSeen
private java.lang.String contentType
private java.io.OutputStream out
private java.io.InputStream in
private int responseCode
private byte[] asciiBuffer
private char[] headerLine
private java.lang.String userAgentId
private static final int HTTP_DEAD
private static final int HTTP_IDLE
private static final int HTTP_SENDING
private static final int HTTP_RECEIVING
private int mode
private int remainingContentLength
public static final byte[] CRLF
private boolean useProxy
private java.lang.String cachedProxyHost
private int cachedProxyPort
Constructor Detail |
---|
public HttpClientConnection(java.lang.String hostname)
HttpClientConnection
. The port used on
the HTTP server side is the default HTTP port, 80.
hostname
- name (DNS name or IP dotted address) of host running
a HTTP server to which we want to connect to.public HttpClientConnection(java.lang.String hostname, int port)
HttpClientConnection
.
hostname
- name (DNS name or IP dotted address) of host running
a HTTP server to which we should connect to.port
- Port number where the HTTP server can be contacted.Method Detail |
---|
public void close()
close
it is still possible to open a new
connection to the HTTP server once again.
public void beginPostRequest(java.lang.String path, java.lang.String mimeType, int contentLength) throws java.io.IOException
writeContentBytes(byte[], int, int)
method. Finally, to finish the request
he has to call the endPostRequest()
method.
path
- Path to server object which handles the POST request.
For instance, this can be a CGI script (although it better should
not be one, except in the case of FAST CGI).mimeType
- MIME-classified type of content to be sent.contentLength
- Length of content to be sent. If negative, the
length is not known in advance. In this case, keeping the connection
alive is not possible, so callers should avoid this situation,
if possible.
java.io.IOException
- if an I/O exception occurs when sending the
HTTP headers. In this case the connection is closed automatically
and the caller must not send any content. However, the caller
is free to give the request another try by calling
beginEncoding
again, thus opening a new HTTP connection.public void writeContentBytes(byte[] bytes, int offset, int length) throws java.io.IOException
bytes
- The data.offset
- Start offset in the data.length
- Number of bytes to write.
java.lang.RuntimeException
- if too much content was sent.
java.io.IOException
- if an I/O error occurs.
java.lang.NullPointerException
- if bytes
is
null
.
java.lang.IndexOutOfBoundsException
- if offset
is negative,
or length
is negative, or offset + length
is
greater than the length of the array bytes
.public void endPostRequest() throws java.io.IOException
java.io.IOException
protected void handleOption(java.lang.String option, java.lang.String value)
Currently the following options are handled by this class:
option
- Name of option sent by HTTP server.value
- Value of option.private int readHeaders() throws java.io.IOException
java.io.IOException
private boolean readHeaderLine(java.lang.String[] keyvalue) throws java.io.IOException
keyvalue
- An array with room for either exactly one or two
strings, receiving the header option and optionally its value. If
only room for a single return string is supplied, then
readHeaderLine
will only read in the header line (like
the first HTTP header) without separating the options's value from
its name. If a header option has no option, then null
is returned as the value string.
false
if the end of the headers has been reached.
java.io.IOException
private java.lang.String readLine() throws java.io.IOException
java.io.IOException
public int beginDecoding() throws java.io.IOException
java.io.IOException
public java.lang.String getContentType()
public int readContentBytes(byte[] buffer, int offset, int length) throws java.io.IOException
buffer
- Buffer to receive the content sent by the HTTP server.offset
- Start offset in the buffer.length
- Number of bytes to receive.
java.net.ProtocolException
- if not enough content was available (the
caller attempted to read more data than available) or if we received
junk information violating the HTTP protocol specification.
java.io.IOException
- if an I/O error occurs.
java.lang.NullPointerException
- if bytes
is
null
.
java.lang.IndexOutOfBoundsException
- if offset
is negative,
or length
is negative, or offset + length
is
greater than the length of the array bytes
.public int getRemainingContentLength()
readContentBytes(byte[], int, int)
.
public void endDecoding() throws java.io.IOException
This method silently discards any unread content, if the caller has yet not read all content.
java.io.IOException
public void setTimeout(int milliseconds)
milliseconds
- Timeout in milliseconds.public int getTimeout()
public int getResponseCode()
public boolean getKeepAlive()
private void connect() throws java.io.IOException
If the connection is in keep-alive mode and already open, then the connection is reused.
java.lang.SecurityException
- if a security manager exists and its
checkConnect
method does not allow the connect
operation.
java.io.IOException
private void write(java.lang.String s) throws java.io.IOException
s
- String to write.
java.io.IOException
private void writeln(java.lang.String s) throws java.io.IOException
s
- String to write.
java.io.IOException
private java.lang.String getProxyHost()
null
if no proxy has been
configured.private int getProxyPort()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |