ix.util.http
Class HttpObjectClient

java.lang.Object
  extended by ix.util.http.HttpObjectClient
Direct Known Subclasses:
HttpStringClient

public class HttpObjectClient
extends java.lang.Object

Makes HTTP requests that can be regarded as sending an object and receiving one in reply. The usual I-X XML encoding is used; however, it's possible to change that in a subclass.

Certain reasonable assumptions are made about the requests:

Both can be changed by calling "set" methods, or in a subclass.

Connect and read timeouts may also be set.

Both POST and GET requests can be made.

See Also:
HttpObjectServlet, HttpServer, XMLTranslator

Field Summary
protected  int connectTimeout
           
protected  int readTimeout
           
protected  java.lang.String requestCharsetName
           
protected  java.lang.String requestContentType
           
protected  HttpUtilities util
           
 
Constructor Summary
HttpObjectClient()
           
 
Method Summary
protected  java.lang.Object decodeReceived(java.lang.String contents)
          Turns response contents into an object.
protected  void describeResponse(java.net.HttpURLConnection conn)
          Utility method that produces debugging output that describes the response.
protected  byte[] encodeForSend(java.lang.Object contents)
          Turns an object into request contents.
protected  boolean handleResponseCode(java.net.HttpURLConnection conn)
          Checks whether the response is a success or a failure and whether there's content to be read.
protected  boolean handleResponseCode(int status, java.lang.String message)
          Checks whether the response is a success or a failure and whether there's content to be read.
static void main(java.lang.String[] argv)
          Main program for testing.
protected static void mainLoop(java.lang.String[] argv, HttpObjectClient http, Fn1<java.lang.String,java.lang.Object> preprocess)
           
protected  java.lang.String readReply(java.net.HttpURLConnection conn)
          Utility method that reads a reply from the connection and returns it as a String.
protected  void sendBytes(byte[] bytes, java.net.URL url, java.net.HttpURLConnection conn)
          Utility method that writes bytes to an HttpURLConnection.
 java.lang.Object sendGetRequest(java.net.URL url)
          Send a GET request and get an object in reply.
 java.lang.Object sendRequest(java.net.URL url, java.lang.Object contentsToSend)
          Send an object and receive one in reply.
 void setConnectTimeout(int milliseconds)
          Sets the timeout in millisecond for opening a communications link when sending a request.
 void setReadTimeout(int milliseconds)
          Sets the timeout in millisecond for reading a response.
 void setRequestCharsetName(java.lang.String name)
          Set the character set used when sending requests.
 void setRequestContentType(java.lang.String type)
          Set the Content-Type used when sending requests.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

util

protected HttpUtilities util

requestContentType

protected java.lang.String requestContentType

requestCharsetName

protected java.lang.String requestCharsetName

connectTimeout

protected int connectTimeout

readTimeout

protected int readTimeout
Constructor Detail

HttpObjectClient

public HttpObjectClient()
Method Detail

setRequestContentType

public void setRequestContentType(java.lang.String type)
Set the Content-Type used when sending requests.


setRequestCharsetName

public void setRequestCharsetName(java.lang.String name)
Set the character set used when sending requests.


setConnectTimeout

public void setConnectTimeout(int milliseconds)
Sets the timeout in millisecond for opening a communications link when sending a request. A timout of zero means to wait forever and is the default. A java.net.SocketTimeoutException is thrown if the timeout expires.


setReadTimeout

public void setReadTimeout(int milliseconds)
Sets the timeout in millisecond for reading a response. A timout of zero means to wait forever and is the default. A java.net.SocketTimeoutException is thrown if the timeout is exceeded before data is available to read.


sendRequest

public java.lang.Object sendRequest(java.net.URL url,
                                    java.lang.Object contentsToSend)
                             throws RethrownIOException
Send an object and receive one in reply. The usual I-X XML encoding is used. This makes a POST request.

Throws:
RethrownIOException
See Also:
sendGetRequest(URL)

encodeForSend

protected byte[] encodeForSend(java.lang.Object contents)
                        throws java.io.UnsupportedEncodingException
Turns an object into request contents.

Throws:
java.io.UnsupportedEncodingException
See Also:
decodeReceived(String contents)

sendBytes

protected void sendBytes(byte[] bytes,
                         java.net.URL url,
                         java.net.HttpURLConnection conn)
                  throws java.io.IOException
Utility method that writes bytes to an HttpURLConnection. The URL is used to set the requests "Host" header field.

Throws:
java.io.IOException

sendGetRequest

public java.lang.Object sendGetRequest(java.net.URL url)
                                throws RethrownIOException
Send a GET request and get an object in reply. The usual I-X XML syntax should be used in the reply. To send an object using a POST request, use sendRequest(URL, Object).

Throws:
RethrownIOException

describeResponse

protected void describeResponse(java.net.HttpURLConnection conn)
                         throws java.io.IOException
Utility method that produces debugging output that describes the response.

Throws:
java.io.IOException

handleResponseCode

protected boolean handleResponseCode(java.net.HttpURLConnection conn)
                              throws HttpRequestException,
                                     java.io.IOException
Checks whether the response is a success or a failure and whether there's content to be read. This method just gets the response code (status) and message from the connection and calls handleResponseCode(int status, String message).

Throws:
HttpRequestException
java.io.IOException

handleResponseCode

protected boolean handleResponseCode(int status,
                                     java.lang.String message)
                              throws HttpRequestException
Checks whether the response is a success or a failure and whether there's content to be read. Failure is indicated by throwing an HttpRequestException; otherwise, the true/false result indicates whether there's content to be read or not. It's possible to have a successful request that does not return any content; normally, the response code is then NO_CONTENT (204).

As defined in the HttpObjectClient class, this method returns true if the status is HTTP_OK and throws an HttpRequestException if it isn't. Subclasses should override this method if they want to accept anything other than HTTP_OK or if they want to return false in some case.

Returns:
true if the response indicates there's content to be read, otherwise false.
Throws:
HttpRequestException - for more serious failures.

readReply

protected java.lang.String readReply(java.net.HttpURLConnection conn)
                              throws java.io.EOFException,
                                     java.io.IOException
Utility method that reads a reply from the connection and returns it as a String. N.B. Can return null in NO_CONTENT cases.

Throws:
java.io.EOFException
java.io.IOException

decodeReceived

protected java.lang.Object decodeReceived(java.lang.String contents)
Turns response contents into an object. For example, the content might be an XML representation of an object.

See Also:
encodeForSend(Object contents)

main

public static void main(java.lang.String[] argv)
                 throws java.lang.Exception
Main program for testing.

Throws:
java.lang.Exception

mainLoop

protected static void mainLoop(java.lang.String[] argv,
                               HttpObjectClient http,
                               Fn1<java.lang.String,java.lang.Object> preprocess)
                        throws java.lang.Exception
Throws:
java.lang.Exception