com.alarexgroup.m2mplf.net.http
Class HttpCommunicator

java.lang.Object
  extended by com.alarexgroup.m2mplf.net.http.HttpCommunicator
public class HttpCommunicator
extends java.lang.Object

Class communicates throughHTTP protocol.

Example: simple request to method GET

HttpCommunicator com = new HttpCommunicator("bearer_type=gprs;access_point=internet;timeout=40");
if (com.openConnection("http://www.google.com",HttpCommunicator.METHOD_GET)==HttpCommunicator.ERROR_CODE_ALL_OK) {
  com.addHeaderProperty(Header,Value)//adds parameter to heading
  int resp_code = com.sendRequest()//request sending
  byte [] b = com.receiveResponse()//dates input
  com.closeConnection()//coses conection
else {
  //can't open connection
}


Example: Work with Streams - Streams enables work with bigger dates. Isn't neccessary allocate array with some data size, but because it works with Streams is possible to process dates straightaway.
HttpCommunicator com = new HttpCommunicator("bearer_type=gprs;access_point=internet;timeout=40");
if (com.openConnection("http://www.google.com",HttpCommunicator.METHOD_GET)==HttpCommunicator.ERROR_CODE_ALL_OK){
  com.addHeaderProperty(Header,value);
  OutputStream os = com.getDataOutputStream();
  try {
    os.write("Haloo".getBytes())//sends dates
    int error_code = com.sendRequest();
    InputStream is = com.getDataInputStream();
     is.read()//receives dates
  catch (IOException ex) {
    //handle exception
  }
  com.closeConnection();
else {
  //is not possible to open conection
}

Field Summary
static int ERROR_CODE_ALL_OK
          all right
static int ERROR_CODE_CONNECTION_NOT_FOUND
          connection is not possible
static int ERROR_CODE_INVALID_PARAMETERS
          wrong URL form or connectionProfile
static int ERROR_CODE_INVALID_STATE
          error of HTTP communicator's state
static int ERROR_CODE_IO_EXCEPTION
          IO exception
static int ERROR_CODE_SECURITY_EXCEPTION
          J2ME security exception
static int ERROR_CODE_UNKNOWN_ERROR
          unknown error
static java.lang.String METHOD_GET
          HTTP QUERY METHOD - GET
static java.lang.String METHOD_HEAD
          HTTP QUERY METHOD - HEAD
static java.lang.String METHOD_POST
          HTTP QUERY METHOD - POST
static int STATE_CLOSED
          communicator logged off
static int STATE_CREATED
          communicator created
static int STATE_DATA_RECEIVED
          Answer received
static int STATE_DATA_RECEIVING
          Answer receiving
static int STATE_DATA_SENDING
          Data sending
static int STATE_DATA_SENT
          data was send
static int STATE_HEAD_COMPLETED
          HTTP header finalized
static int STATE_INIT
          communicator initialized
 
Constructor Summary
HttpCommunicator()
          Creates new communicatro
HttpCommunicator(java.lang.String connectionProfile)
          Creates new communicatro - connectionProfile already set
 
Method Summary
 int addHeaderProperty(java.lang.String key, java.lang.String value)
          Appends a parameter to HTTP header.
 int closeConnection()
          This method closes connection.
 java.io.DataInputStream getDataInputStream()
          Returns InputStream, which includes answer to HTTP request.
 java.io.DataOutputStream getDataOutputStream()
          Returns OutputStream for data sending.
 int getErrorCode()
          Returns error code
 java.lang.String getMethod()
          Returns HTTP Method
 java.lang.String getURL()
          Returns URL request
 int openConnection(java.lang.String url, java.lang.String method)
          Opens connection
 byte[] receiveResponse()
          This method returns loaded answer as an array of bytes.
 int sendRequest()
          Completes OutpuStream's content sending and returns HTTP response code
 int sendRequest(byte[] message)
          Sends array of bytes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

METHOD_POST

public static final java.lang.String METHOD_POST
HTTP QUERY METHOD - POST

METHOD_GET

public static final java.lang.String METHOD_GET
HTTP QUERY METHOD - GET

METHOD_HEAD

public static final java.lang.String METHOD_HEAD
HTTP QUERY METHOD - HEAD

ERROR_CODE_ALL_OK

public static final int ERROR_CODE_ALL_OK
all right

See Also:
Constant Field Values

ERROR_CODE_CONNECTION_NOT_FOUND

public static final int ERROR_CODE_CONNECTION_NOT_FOUND
connection is not possible

See Also:
Constant Field Values

ERROR_CODE_INVALID_PARAMETERS

public static final int ERROR_CODE_INVALID_PARAMETERS
wrong URL form or connectionProfile

See Also:
Constant Field Values

ERROR_CODE_SECURITY_EXCEPTION

public static final int ERROR_CODE_SECURITY_EXCEPTION
J2ME security exception

See Also:
Constant Field Values

ERROR_CODE_IO_EXCEPTION

public static final int ERROR_CODE_IO_EXCEPTION
IO exception

See Also:
Constant Field Values

ERROR_CODE_INVALID_STATE

public static final int ERROR_CODE_INVALID_STATE
error of HTTP communicator's state

See Also:
Constant Field Values

ERROR_CODE_UNKNOWN_ERROR

public static final int ERROR_CODE_UNKNOWN_ERROR
unknown error

See Also:
Constant Field Values

STATE_CREATED

public static final int STATE_CREATED
communicator created

See Also:
Constant Field Values

STATE_INIT

public static final int STATE_INIT
communicator initialized

See Also:
Constant Field Values

STATE_HEAD_COMPLETED

public static final int STATE_HEAD_COMPLETED
HTTP header finalized

See Also:
Constant Field Values

STATE_DATA_SENDING

public static final int STATE_DATA_SENDING
Data sending

See Also:
Constant Field Values

STATE_DATA_SENT

public static final int STATE_DATA_SENT
data was send

See Also:
Constant Field Values

STATE_DATA_RECEIVING

public static final int STATE_DATA_RECEIVING
Answer receiving

See Also:
Constant Field Values

STATE_DATA_RECEIVED

public static final int STATE_DATA_RECEIVED
Answer received

See Also:
Constant Field Values

STATE_CLOSED

public static final int STATE_CLOSED
communicator logged off

See Also:
Constant Field Values
Constructor Detail

HttpCommunicator

public HttpCommunicator()
Creates new communicatro

HttpCommunicator

public HttpCommunicator(java.lang.String connectionProfile)
Creates new communicatro - connectionProfile already set

Parameters:
connectionProfile - - example: bearer_type=gprs;access_point=internet;username=someone;password=something;timeout=40
Method Detail

openConnection

public int openConnection(java.lang.String url,
                          java.lang.String method)
Opens connection

Parameters:
url - - url example: http://www.google.com
method - - HTTP method - METHOD_GET, METHOD_POST
Returns:
errorCode
See Also:
METHOD_GET, METHOD_POST, METHOD_HEAD

addHeaderProperty

public int addHeaderProperty(java.lang.String key,
                             java.lang.String value)
Appends a parameter to HTTP header. Parameters can only be appended before sending the request/data itself.

Parameters:
key - - name of HTTP parameter
value - - value of HTTP parameter
Returns:
error code

getDataOutputStream

public java.io.DataOutputStream getDataOutputStream()
Returns OutputStream for data sending. If the method was already used, use addHeader() again is not possible.

Returns:
dataoutputstream - null if an error occurred

getDataInputStream

public java.io.DataInputStream getDataInputStream()
Returns InputStream, which includes answer to HTTP request.

Returns:
dataoutpustream - null if error occurred

sendRequest

public int sendRequest()
Completes OutpuStream's content sending and returns HTTP response code

Returns:
HTTP response code

sendRequest

public int sendRequest(byte[] message)
Sends array of bytes. It's possible to use it only ones.

Parameters:
message - - dates
Returns:
HTTP response code

receiveResponse

public byte[] receiveResponse()
This method returns loaded answer as an array of bytes.

Returns:
response

closeConnection

public int closeConnection()
This method closes connection. Next request is possible to create by openConnection()

Returns:
errorcode

getURL

public java.lang.String getURL()
Returns URL request

Returns:
url

getMethod

public java.lang.String getMethod()
Returns HTTP Method

Returns:
method

getErrorCode

public int getErrorCode()
Returns error code

Returns:
error