com.funambol.platform
Class HttpConnectionAdapterWrapper

java.lang.Object
  extended by com.funambol.platform.HttpConnectionAdapter
      extended by com.funambol.platform.HttpConnectionAdapterWrapper

public class HttpConnectionAdapterWrapper
extends HttpConnectionAdapter

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
 
Fields inherited from class com.funambol.platform.HttpConnectionAdapter
GET, HEAD, HTTP_ACCEPTED, HTTP_BAD_GATEWAY, HTTP_BAD_METHOD, HTTP_BAD_REQUEST, HTTP_CLIENT_TIMEOUT, HTTP_CONFLICT, HTTP_CREATED, HTTP_ENTITY_TOO_LARGE, HTTP_EXPECT_FAILED, HTTP_FORBIDDEN, HTTP_GATEWAY_TIMEOUT, HTTP_GONE, HTTP_INTERNAL_ERROR, HTTP_LENGTH_REQUIRED, HTTP_MOVED_PERM, HTTP_MOVED_TEMP, HTTP_MULT_CHOICE, HTTP_NO_CONTENT, HTTP_NOT_ACCEPTABLE, HTTP_NOT_AUTHORITATIVE, HTTP_NOT_FOUND, HTTP_NOT_IMPLEMENTED, HTTP_NOT_MODIFIED, HTTP_OK, HTTP_PARTIAL, HTTP_PAYMENT_REQUIRED, HTTP_PRECON_FAILED, HTTP_PROXY_AUTH, HTTP_REQ_TOO_LONG, HTTP_RESET, HTTP_SEE_OTHER, HTTP_TEMP_REDIRECT, HTTP_UNAUTHORIZED, HTTP_UNAVAILABLE, HTTP_UNSUPPORTED_RANGE, HTTP_UNSUPPORTED_TYPE, HTTP_USE_PROXY, HTTP_VERSION, POST
 
Constructor Summary
HttpConnectionAdapterWrapper()
           
 
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
 

Constructor Detail

HttpConnectionAdapterWrapper

public HttpConnectionAdapterWrapper()
Method Detail

open

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

Overrides:
open in class HttpConnectionAdapter
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)

Overrides:
close in class HttpConnectionAdapter
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.

Overrides:
openInputStream in class HttpConnectionAdapter
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.

Overrides:
openOutputStream in class HttpConnectionAdapter
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.

Overrides:
getResponseCode in class HttpConnectionAdapter
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.

Overrides:
getResponseMessage in class HttpConnectionAdapter
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.

Overrides:
setRequestMethod in class HttpConnectionAdapter
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.

Overrides:
setChunkedStreamingMode in class HttpConnectionAdapter
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.

Overrides:
setRequestProperty in class HttpConnectionAdapter
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.

Overrides:
getHeaderField in class HttpConnectionAdapter
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.

Overrides:
getHeaderFieldKey in class HttpConnectionAdapter
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)

Overrides:
getLength in class HttpConnectionAdapter
Throws:
java.io.IOException


Copyright © 2001-2009 Funambol.