HttpSender

public class HttpSender implements Runnable

The HttpSender is top base class for sending HTTP request. TODO: javadoc

Author:Twinsen Tsang

Fields

log

protected FileLogger log

properties

protected Data properties

serviceEndPoint

protected URL serviceEndPoint

Constructors

HttpSender

public HttpSender()

SPA Constructor. It is used when the HTTP Sender is a component in the SPA.

HttpSender

public HttpSender(FileLogger logger, Data d)

Explicit Constructor.

Parameters:
  • logger – The logger used for log message and exception.
  • d – The data used for sending HTTP request.

HttpSender

public HttpSender(FileLogger logger, Data d, String username, String password)

Explicit Constructor.

Parameters:
  • logger – The logger used for log message and exception.
  • d – The data used for sending HTTP request.
  • username – The username for authentication
  • password – The password for authentication

HttpSender

public HttpSender(FileLogger l, Data d, String endpoint)

Explicit Constructor.

Parameters:
  • l – The logger used for log message and exception.
  • d – The data used for sending HTTP request.
  • endpoint – The URL of service end point.

HttpSender

public HttpSender(FileLogger l, Data d, URL endpoint)

Explicit Constructor.

Parameters:
  • l – The logger used for log message and exception.
  • d – The data used for sending HTTP request.
  • endpoint – The URL of service end point.

HttpSender

public HttpSender(FileLogger l, Data d, String endpoint, String username, String password)

Explicit Constructor.

Parameters:
  • l – The logger used for log message and exception.
  • d – The data used for sending HTTP request.
  • endpoint – The URL of service end point.
  • username – The username for authentication
  • password – The password for authentication

HttpSender

public HttpSender(FileLogger l, Data d, URL endpoint, String username, String password)

Explicit Constructor.

Parameters:
  • l – The logger used for log message and exception.
  • d – The data used for sending HTTP request.
  • endpoint – The URL of service end point.
  • username – The username for authentication
  • password – The password for authentication

Methods

closeResponse

public void closeResponse(CloseableHttpResponse response)

Close http response with exception handling

Parameters:
  • response – the response to close

getCurrentLoopTimes

public int getCurrentLoopTimes()

Get what is the current loop times for looping

getExecutedMethod

public HttpRequestBase getExecutedMethod()

Get the last executed HTTP method. This method should be invoked during onResponse().

Returns:the last executed HTTP method.

getLoopTimes

public int getLoopTimes()

Get how many times should the sender to be send.

getResponse

public CloseableHttpResponse getResponse()

Get the response of the last executed HTTP method. This method should be invoked during onResponse().

Returns:the response of the last executed HTTP method

getServiceEndPoint

public URL getServiceEndPoint()

Get the service end-point.

Returns:the service end-point URL.

getUserObject

public Object getUserObject()

Get a user object.

initializeMessage

protected void initializeMessage()

Implements this method if you want to send messages without much different between other message to sent.

isAuthenticationRequired

public boolean isAuthenticationRequired()
Returns:true if HTTP authentication is required.

onBeforeRequest

protected void onBeforeRequest(HttpClient client, HttpRequestBase request)

[@EVENT] This method is invoked just before sending the request to HTTP service end-point.

Parameters:
  • client – The HTTP Connection used for sending SOAP request.
  • request – The request created by onCreateRequest().
Throws:
  • Exception – Any type of exception will be processed at onError(throwable t).

onCreateRequest

protected HttpRequestBase onCreateRequest()

[@EVENT] This method is invoked when the sender is required to create a HTTP Request from configuration. By default, this method return a PostMethod pointing to getServiceEndPoint().

Throws:
  • Exception – Sub-class implementation-specific exception

onEachLoopStart

protected void onEachLoopStart()

[@EVENT] This method is invoked when each loop iteration start.

Throws:
  • Exception – Any type of exception will be processed at onError(throw-able t).

onEnd

protected void onEnd()

[@EVENT] This method is invoked when the sending execution is ended.

onError

protected void onError(Throwable t)

[@EVENT] This method is invoked when there is any exception thrown during web service call. By default, it log the throw-able t to the instance logger.

onResponse

protected void onResponse()

[@EVENT] This method is invoked when received the reply HTTP response from the server. Developer can use getExecutedMethod() to get the HTTP method generated thru onCreateRequest()

Throws:
  • Exception – Any type of exception will be processed at onError(throwable t).

onStart

protected void onStart()

[@EVENT] This method is invoked when the sender begins to execute the run method.

run

public void run()

The thread execution method.

setLoopTimes

public void setLoopTimes(int loopTimes)

Set how many times should the sender to be send.

Parameters:
  • loopTimes – the new loopTimes.

setServiceEndPoint

public void setServiceEndPoint(URL endpoint)

Set the service end-point.

Parameters:
  • endpoint – The URL of the web service end-point.

setServiceEndPoint

public void setServiceEndPoint(String endpoint)

Set the service end-point.

Parameters:
  • endpoint – The String of the web service end-point.

setUserObject

public void setUserObject(Object obj)

Set a user object for call-back.

Parameters:
  • obj – The user object.