com.autonomy.utilities
Class AutnHttpURLConnection

java.lang.Object
  |
  +--com.autonomy.utilities.AutnHttpURLConnection
Direct Known Subclasses:
AutnHttpURLConnectionBinary

public class AutnHttpURLConnection
extends java.lang.Object

This class is intended to simulate some aspects of java.net.HttpURLConnection while allowing the user to set a timeout on the underlying socket's read operations. This class only impliments the HTTP protocol, trying to construct an AutnHttpURLConnection object using another protocol (e.g. AutnHttpURLConnection("ftp://host:21/") will throw a MalformedURLException).

Usage:

AutnHttpURLConnection conn = new AutnHttpURLConnection(sURL);
if(bPost)
{
conn.setRequestMethod("POST");
conn.setBody(sData);
}

conn.setTimeout(cm_nTimeout);

conn.connect();

conn.skipResponseHeaders();

bReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

// ... read from input

conn.disconnect();

Since this class represents an HTTP connection, port 80 is used if no port number is given in the URL.

See Also:
HttpURLConnection

Constructor Summary
AutnHttpURLConnection(java.lang.String sURL)
          Construct an AutnHttpURLConnection object to communicate with the given URL.
AutnHttpURLConnection(java.lang.String sHost, int nPort, java.lang.String sResource)
          Construct an AutnHttpURLConnection object to communicate with the given host, port and resource.
AutnHttpURLConnection(java.net.URL uURL)
          Construct an AutnHttpURLConnection object to communicate with the given URL.
 
Method Summary
 void addHeader(java.lang.String sKey, java.lang.String sValue)
          Add a header line to the HTTP request as sKey: sValue.
 void allowSlackHTTP()
          Enables use of \n\n to detect the end of the response header section (as opposed to correct \r\n\r\n)
 void connect()
          Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.
 void disconnect()
          Disconnect from the underlying socket, close the io streams and set them to null.
 java.lang.String getBody()
          Returns the content of this message.
 java.lang.String getHeaderField(int n)
          Returns the value for the nth header field.
 java.lang.String getHeaderField(java.lang.String sKey)
          Returns the value for the header field called sKey.
 java.lang.String getHeaderFieldKey(int n)
          Returns the name for the nth header field.
 java.io.InputStream getInputStream()
          Returns an input stream that reads from this open connection.
 int getNumHeaders()
          Returns the number of headers in this request
 java.io.OutputStream getOutputStream()
          Returns an output stream that writes to this connection.
 java.lang.String getRequestMethod()
          Get the request method
 int getResponseCode()
          Gets HTTP response status.
 java.lang.String getResponseMessage()
          Gets the HTTP response message, if any, returned along with the response code from a server.
 int getTimeout()
          Returns setting for SO_TIMEOUT.
 java.net.URL getURL()
          Gets the URL this connection is using.
 java.lang.String getVersion()
          Returns the HTTP version being used
 void setBody(java.lang.String sBody)
          Set the content to be sent with this message.
 void setContentType(java.lang.String sContentType)
          Set the Content-Type header value.
 void setEncoding(java.lang.String sSendEncoding, java.lang.String sRecvEncoding)
          Set the encoding used to write data to the socket.
Setting any of these to null will uses the JVM's default encoding
 void setHeader(java.lang.String sKey, java.lang.String sValue)
          Add a header line to the HTTP request as sKey: sValue.
 void setRequestMethod(java.lang.String sMethod)
          Set the method for the URL request, one of: GET POST are legal, subject to protocol restrictions.
 void setTimeout(int nTimeout)
          Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
 void setVersion(java.lang.String sVersion)
          Sets the HTTP version to use.
 void skipResponseHeaders()
          Moves the input stream past initial response line and headers so response body is next to be read.
 java.lang.String toString()
          Constructs the string buffer to send as this HTTP request
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AutnHttpURLConnection

public AutnHttpURLConnection(java.net.URL uURL)
                      throws java.net.MalformedURLException
Construct an AutnHttpURLConnection object to communicate with the given URL.

Parameters:
uURL - the URL to connect to.

AutnHttpURLConnection

public AutnHttpURLConnection(java.lang.String sURL)
                      throws java.net.MalformedURLException
Construct an AutnHttpURLConnection object to communicate with the given URL.

Parameters:
sURL - the URL to connect to.

AutnHttpURLConnection

public AutnHttpURLConnection(java.lang.String sHost,
                             int nPort,
                             java.lang.String sResource)
Construct an AutnHttpURLConnection object to communicate with the given host, port and resource.

Parameters:
sHost - the server host to connect to.
nPort - the port on which the server is listening.
sResource - the host resource.
Method Detail

allowSlackHTTP

public void allowSlackHTTP()
Enables use of \n\n to detect the end of the response header section (as opposed to correct \r\n\r\n)


getInputStream

public java.io.InputStream getInputStream()
Returns an input stream that reads from this open connection.

Returns:
an input stream that reads from this open connection.

getOutputStream

public java.io.OutputStream getOutputStream()
Returns an output stream that writes to this connection.

Returns:
an output stream that writes to this connection.

setRequestMethod

public void setRequestMethod(java.lang.String sMethod)
Set the method for the URL request, one of: are legal, subject to protocol restrictions. The default method is GET. Can only be called while disconnected.

Parameters:
sMethod - method string (upper- or lowercase)
Throws:
java.net.ProtocolException - - if the method cannot be reset or if the requested method isn't valid for HTTP.

getRequestMethod

public java.lang.String getRequestMethod()
Get the request method

Returns:
the request method

setVersion

public void setVersion(java.lang.String sVersion)
Sets the HTTP version to use. Default is HTTP/1.0. Can only be called while disconnected.


setEncoding

public void setEncoding(java.lang.String sSendEncoding,
                        java.lang.String sRecvEncoding)
Set the encoding used to write data to the socket.
Setting any of these to null will uses the JVM's default encoding

Parameters:
sSendEncoding - String used to represent encoding used to send the request, eg. 8859_7

getVersion

public java.lang.String getVersion()
Returns the HTTP version being used

Returns:
the HTTP version being used in the form "HTTP/*.*"

setTimeout

public void setTimeout(int nTimeout)
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a read() call on the InputStream associated with the Socket used by this connection will block for only this amount of time. If the timeout expires, a java.io.InterruptedIOException is raised, though the Socket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout. Can only be called while disconnected.


getTimeout

public int getTimeout()
Returns setting for SO_TIMEOUT. 0 returns implies that the option is disabled (i.e. timeout of infinity),


setBody

public void setBody(java.lang.String sBody)
Set the content to be sent with this message. Content is only sent if the request method is POST which must be set before calling this method. Content type header is set to "text/html". Can only be called while disconnected.

Parameters:
sBody - request content as a string. Can be null,

getBody

public java.lang.String getBody()
Returns the content of this message.

Returns:
the content of this message.

setContentType

public void setContentType(java.lang.String sContentType)
Set the Content-Type header value. Only useful if the body in a POST request is not of type "text/html". Can only be called while disconnected.

Parameters:
sContentType - the string value of the content type header.

setHeader

public void setHeader(java.lang.String sKey,
                      java.lang.String sValue)
Add a header line to the HTTP request as
sKey: sValue
. If a header with the given header name exists it is overwritten. Can only be called while disconnected.

Parameters:
sKey - the header name
sValue - the header value. Is allowed to be null

addHeader

public void addHeader(java.lang.String sKey,
                      java.lang.String sValue)
Add a header line to the HTTP request as
sKey: sValue
. If a header with the given header name exists, a second entry is created using the given value. Can only be called while disconnected.

Parameters:
sKey - the header name
sValue - the header value. Is allowed to be null.

getHeaderField

public java.lang.String getHeaderField(int n)
Returns the value for the nth header field. It returns null if there are fewer than n fields.

Parameters:
n - an header index (0 <= n < getNumHeaders()).
Returns:
the value of the nth header.

getHeaderField

public java.lang.String getHeaderField(java.lang.String sKey)
Returns the value for the header field called sKey. It returns null if there is no header field called sKey.

Returns:
the value of the named header field.

getHeaderFieldKey

public java.lang.String getHeaderFieldKey(int n)
Returns the name for the nth header field. It returns null if there are fewer than n fields.

Parameters:
n - an header index(0 <= n < getNumHeaders()).
Returns:
the name of the nth header.

getNumHeaders

public int getNumHeaders()
Returns the number of headers in this request

Returns:
the number of headers in this request

getResponseCode

public int getResponseCode()
Gets HTTP response status. From responses like: Extracts the ints 200 and 401 respectively. Returns -1 if none can be discerned from the response (i.e., the response is not valid HTTP).


getResponseMessage

public java.lang.String getResponseMessage()
Gets the HTTP response message, if any, returned along with the response code from a server. From responses like: Extracts the Strings "OK" and "Not Found" respectively. Returns null if none could be discerned from the responses (the result was not valid HTTP).


getURL

public java.net.URL getURL()
Gets the URL this connection is using.

Returns:
the URL this connection is using.

connect

public void connect()
             throws java.io.IOException
Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.

Throws:
java.io.IOException - - if an I/O error occurs while opening the connection.

disconnect

public void disconnect()
Disconnect from the underlying socket, close the io streams and set them to null.


skipResponseHeaders

public void skipResponseHeaders()
                         throws java.io.IOException
Moves the input stream past initial response line and headers so response body is next to be read.

Throws:
java.io.IOException - if there is a problem reading from the socket InputStream - likely to be InterruptedIOException.

toString

public java.lang.String toString()
Constructs the string buffer to send as this HTTP request

Overrides:
toString in class java.lang.Object