com.funambol.platform
Class HttpConnectionAdapter

java.lang.Object
  extended by com.funambol.platform.HttpConnectionAdapter
Direct Known Subclasses:
HttpConnectionAdapterWrapper

public class HttpConnectionAdapter
extends java.lang.Object

This class is a simple HttpConnection class that wraps the underlying microedition HttpConnection. Requests/responses can be written/read accessing the corresponding input and output streams. A portable code must use this class only to perform http connections, and must take care of closing the connection when not used anymore.

 Example:
 
   void httpConnectionExample(String url) throws IOException {
      HttpConnectionAdapter conn = new HttpConnectionAdapter();

      // Open the connection
      conn.open(url);

      conn.setRequestMethod(HttpConnectionAdapter.POST);
      conn.setRequestProperty("CUSTOM-HEADER", "CUSTOM-VALUE");

      OutputStream os = conn.openOutputStream();
      os.write("TEST");
      os.close();

      // Suppose the answer is bound to 1KB
      byte anwser[] = new byte[1024];
      InputStream is = conn.openInputStream();
      is.read(answer);
      is.close();

      // Close the connection
      conn.close();
 


Field Summary
static java.lang.String GET
           
static java.lang.String HEAD
           
static int HTTP_ACCEPTED
           
static int HTTP_BAD_GATEWAY
           
static int HTTP_BAD_METHOD
           
static int HTTP_BAD_REQUEST
           
static int HTTP_CLIENT_TIMEOUT
           
static int HTTP_CONFLICT
           
static int HTTP_CREATED
           
static int HTTP_ENTITY_TOO_LARGE
           
static int HTTP_EXPECT_FAILED
           
static int HTTP_FORBIDDEN
           
static int HTTP_GATEWAY_TIMEOUT
           
static int HTTP_GONE
           
static int HTTP_INTERNAL_ERROR
           
static int HTTP_LENGTH_REQUIRED
           
static int HTTP_MOVED_PERM
           
static int HTTP_MOVED_TEMP
           
static int HTTP_MULT_CHOICE
           
static int HTTP_NO_CONTENT
           
static int HTTP_NOT_ACCEPTABLE
           
static int HTTP_NOT_AUTHORITATIVE
           
static int HTTP_NOT_FOUND
           
static int HTTP_NOT_IMPLEMENTED
           
static int HTTP_NOT_MODIFIED
           
static int HTTP_OK
           
static int HTTP_PARTIAL
           
static int HTTP_PAYMENT_REQUIRED
           
static int HTTP_PRECON_FAILED
           
static int HTTP_PROXY_AUTH
           
static int HTTP_REQ_TOO_LONG
           
static int HTTP_RESET
           
static int HTTP_SEE_OTHER
           
static int HTTP_TEMP_REDIRECT
           
static int HTTP_UNAUTHORIZED
           
static int HTTP_UNAVAILABLE
           
static int HTTP_UNSUPPORTED_RANGE
           
static int HTTP_UNSUPPORTED_TYPE
           
static int HTTP_USE_PROXY
           
static int HTTP_VERSION
           
static java.lang.String POST
           
 
Constructor Summary
HttpConnectionAdapter()
           
 
Method Summary
 void close()
          This method closes this connection.
 java.lang.String getHeaderField(java.lang.String key)
          Returns the value of the named header field.
 java.lang.String getHeaderFieldKey(int num)
          Returns the key for the nth header field.
 int getLength()
          Returns the answer length (excluding headers.
 int getResponseCode()
          Returns the HTTP response status code.
 java.lang.String getResponseMessage()
          Returns the HTTP response message.
 void open(java.lang.String url)
          Open the connection to the given url.
 java.io.InputStream openInputStream()
          Open the input stream.
 java.io.OutputStream openOutputStream()
          Open the output stream.
 void setChunkedStreamingMode(int chunkLength)
          Set chunked encoding for the file to be uploaded.
 void setRequestMethod(java.lang.String method)
          Set the method for the URL request, one of: GET POST HEAD are legal, subject to protocol restrictions.
 void setRequestProperty(java.lang.String key, java.lang.String value)
          Sets the general request property.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HTTP_ACCEPTED

public static int HTTP_ACCEPTED

HTTP_BAD_GATEWAY

public static int HTTP_BAD_GATEWAY

HTTP_BAD_METHOD

public static int HTTP_BAD_METHOD

HTTP_BAD_REQUEST

public static int HTTP_BAD_REQUEST

HTTP_CLIENT_TIMEOUT

public static int HTTP_CLIENT_TIMEOUT

HTTP_CONFLICT

public static int HTTP_CONFLICT

HTTP_CREATED

public static int HTTP_CREATED

HTTP_ENTITY_TOO_LARGE

public static int HTTP_ENTITY_TOO_LARGE

HTTP_FORBIDDEN

public static int HTTP_FORBIDDEN

HTTP_GATEWAY_TIMEOUT

public static int HTTP_GATEWAY_TIMEOUT

HTTP_GONE

public static int HTTP_GONE

HTTP_INTERNAL_ERROR

public static int HTTP_INTERNAL_ERROR

HTTP_LENGTH_REQUIRED

public static int HTTP_LENGTH_REQUIRED

HTTP_MOVED_PERM

public static int HTTP_MOVED_PERM

HTTP_MOVED_TEMP

public static int HTTP_MOVED_TEMP

HTTP_MULT_CHOICE

public static int HTTP_MULT_CHOICE

HTTP_NO_CONTENT

public static int HTTP_NO_CONTENT

HTTP_NOT_ACCEPTABLE

public static int HTTP_NOT_ACCEPTABLE

HTTP_NOT_AUTHORITATIVE

public static int HTTP_NOT_AUTHORITATIVE

HTTP_NOT_FOUND

public static int HTTP_NOT_FOUND

HTTP_NOT_IMPLEMENTED

public static int HTTP_NOT_IMPLEMENTED

HTTP_NOT_MODIFIED

public static int HTTP_NOT_MODIFIED

HTTP_OK

public static int HTTP_OK

HTTP_PARTIAL

public static int HTTP_PARTIAL

HTTP_PAYMENT_REQUIRED

public static int HTTP_PAYMENT_REQUIRED

HTTP_PRECON_FAILED

public static int HTTP_PRECON_FAILED

HTTP_PROXY_AUTH

public static int HTTP_PROXY_AUTH

HTTP_REQ_TOO_LONG

public static int HTTP_REQ_TOO_LONG

HTTP_RESET

public static int HTTP_RESET

HTTP_SEE_OTHER

public static int HTTP_SEE_OTHER

HTTP_TEMP_REDIRECT

public static int HTTP_TEMP_REDIRECT

HTTP_UNAUTHORIZED

public static int HTTP_UNAUTHORIZED

HTTP_UNAVAILABLE

public static int HTTP_UNAVAILABLE

HTTP_UNSUPPORTED_TYPE

public static int HTTP_UNSUPPORTED_TYPE

HTTP_USE_PROXY

public static int HTTP_USE_PROXY

HTTP_VERSION

public static int HTTP_VERSION

HTTP_EXPECT_FAILED

public static int HTTP_EXPECT_FAILED

HTTP_UNSUPPORTED_RANGE

public static int HTTP_UNSUPPORTED_RANGE

GET

public static final java.lang.String GET
See Also:
Constant Field Values

POST

public static final java.lang.String POST
See Also:
Constant Field Values

HEAD

public static final java.lang.String HEAD
See Also:
Constant Field Values
Constructor Detail

HttpConnectionAdapter

public HttpConnectionAdapter()
Method Detail

open

public void open(java.lang.String url)
          throws java.io.IOException
Open the connection to the given url.

Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
This method closes this connection. It does not close the corresponding input and output stream which need to be closed separately (if they were previously opened)

Throws:
java.io.IOException - if the connection cannot be closed

openInputStream

public java.io.InputStream openInputStream()
                                    throws java.io.IOException
Open the input stream. The ownership of the stream is transferred to the caller which is responsbile to close and release the resource once it is no longer used. This method shall be called only once per connection.

Throws:
java.io.IOException - if the input stream cannot be opened or the output stream has not been closed yet.

openOutputStream

public java.io.OutputStream openOutputStream()
                                      throws java.io.IOException
Open the output stream. The ownership of the stream is transferred to the caller which is responsbile to close and release the resource once it is no longer used. This method shall be called only once per connection.

Throws:
java.io.IOException - if the output stream cannot be opened.

getResponseCode

public int getResponseCode()
                    throws java.io.IOException
Returns the HTTP response status code. It parses responses like: HTTP/1.0 200 OK HTTP/1.0 401 Unauthorized and extracts the ints 200 and 401 respectively. from the response (i.e., the response is not valid HTTP). Returns: the HTTP Status-Code or -1 if no status code can be discerned. Throws: IOException - if an error occurred connecting to the server.

Throws:
java.io.IOException

getResponseMessage

public java.lang.String getResponseMessage()
                                    throws java.io.IOException
Returns the HTTP response message. It parses responses like: HTTP/1.0 200 OK HTTP/1.0 401 Unauthorized and extracts the strings OK and Unauthorized respectively. from the response (i.e., the response is not valid HTTP). Returns: the HTTP Response-Code or null if no status message can be discerned. Throws: IOException - if an error occurred connecting to the server.

Throws:
java.io.IOException

setRequestMethod

public void setRequestMethod(java.lang.String method)
                      throws java.io.IOException
Set the method for the URL request, one of: GET POST HEAD are legal, subject to protocol restrictions. The default method is GET.

Throws:
java.io.IOException

setChunkedStreamingMode

public void setChunkedStreamingMode(int chunkLength)
                             throws java.io.IOException
Set chunked encoding for the file to be uploaded. This avoid the output stream to buffer all data before transmitting it. This is currently not supported by this implementation and the method has no effect.

Parameters:
chunkLength - the length of the single chunk
Throws:
java.io.IOException

setRequestProperty

public void setRequestProperty(java.lang.String key,
                               java.lang.String value)
                        throws java.io.IOException
Sets the general request property. If a property with the key already exists, overwrite its value with the new value. NOTE: HTTP requires all request properties which can legally have multiple instances with the same key to use a comma-seperated list syntax which enables multiple properties to be appended into a single property.

Parameters:
key - the keyword by which the request is known (e.g., "accept").
value - the value associated with it.
Throws:
java.io.IOException

getHeaderField

public java.lang.String getHeaderField(java.lang.String key)
                                throws java.io.IOException
Returns the value of the named header field.

Parameters:
key - name of a header field.
Returns:
the value of the named header field, or null if there is no such field in the header.
Throws:
java.io.IOException - if an error occurred connecting to the server.

getHeaderFieldKey

public java.lang.String getHeaderFieldKey(int num)
                                   throws java.io.IOException
Returns the key for the nth header field. Some implementations may treat the 0th header field as special, i.e. as the status line returned by the HTTP server. In this case, getHeaderField(0) returns the status line, but getHeaderFieldKey(0) returns null.

Throws:
java.io.IOException

getLength

public int getLength()
              throws java.io.IOException
Returns the answer length (excluding headers. This is the content-length field length)

Throws:
java.io.IOException


Copyright © 2001-2009 Funambol.