Class HttpConnection
- java.lang.Object
-
- com.aquima.interactions.foundation.connectivity.impl.HttpConnection
-
- All Implemented Interfaces:
IConnection
,IHttpConnection
public class HttpConnection extends Object implements IHttpConnection
Class providing a simple implementation to interact over http connections. (basic http authentication is supported)- Since:
- 9.0
- Author:
- Jon van Leuven, Martijn van Tilburg
-
-
Constructor Summary
Constructors Constructor Description HttpConnection(String name, String url)
Create a simple http connection for a specific Url.HttpConnection(String name, String url, String username, String password)
Create a simple http connection for a specific Url with basic http authorization.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected HttpURLConnection
createConnection(IHttpRequest request)
Create the HTTP connection that will be used for the request.protected HttpResponse
createHttpResponse(HttpURLConnection httpConn)
Create the HTTP response for the HTTP request.IHttpConnectionFiller
fill()
Returns an object which can fill in the missing configurations of this connection.List<String>
getHeaderNames()
This method returns a list of header names that are to be put on the http requestString
getName()
This method returns the name of the connection object.protected String
getPassword()
ConnectionType
getType()
This method returns the type of the connection object.String
getUrl()
This method returns the url used for connecting to the endpoint.protected String
getUsername()
void
invoke(IHttpRequest request, IHttpResponseHandler responseHandler)
This method should be called to transfer data over http and handle its response.protected void
setAuthentication(HttpURLConnection httpConn)
Set authentication for the HTTP request.void
setHeaderNames(List<String> headerNames)
Sets the names of the headers to send with a HTTP requestvoid
setTimeout(int timeout)
Sets the read timeout to a specified timeout, in milliseconds.String
toString()
-
-
-
Constructor Detail
-
HttpConnection
public HttpConnection(String name, String url)
Create a simple http connection for a specific Url.- Parameters:
name
- The name of this connection (required).url
- The url of the http endpoint (required).
-
HttpConnection
public HttpConnection(String name, String url, String username, String password)
Create a simple http connection for a specific Url with basic http authorization. With http authorization the username and password will be send in the http request header using base64 encoding.- Parameters:
name
- The name of this connection (required).url
- The url of the http endpoint.username
- The username used for authorization when connecting to the url.password
- The password used for authorization when connecting to the url.
-
-
Method Detail
-
setTimeout
public void setTimeout(int timeout)
Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from Input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout. Some non-standard implementation of this method ignores the specified timeout.- Parameters:
timeout
- An int that specifies the timeout value to be used in milliseconds
-
fill
public IHttpConnectionFiller fill()
Description copied from interface:IHttpConnection
Returns an object which can fill in the missing configurations of this connection.- Specified by:
fill
in interfaceIHttpConnection
- Returns:
- a filler for this connection's settings.
-
createConnection
protected HttpURLConnection createConnection(IHttpRequest request) throws IOException
Create the HTTP connection that will be used for the request. The default implementation concatenates 'this.url' and 'request.getQueryString()' to construct an URL. A connection to this URL is opened and returned as the result of this method- Parameters:
request
- The object that is responsible for the HTTP request data- Returns:
- the HTTP connection for the request
- Throws:
IOException
- if an I/O exception occurs while opening the connection
-
setAuthentication
protected void setAuthentication(HttpURLConnection httpConn)
Set authentication for the HTTP request. The default implementation uses 'username' and 'password' for BASIC authentication- Parameters:
httpConn
- The HTTP connection instance used for this request
-
createHttpResponse
protected HttpResponse createHttpResponse(HttpURLConnection httpConn) throws IOException
Create the HTTP response for the HTTP request. The default implementation uses the response code and all response headers from the HTTP connection- Parameters:
httpConn
- The HTTP connection instance used for this request- Returns:
- the HTTP response
- Throws:
IOException
- if an error occurred connecting to the server
-
invoke
public void invoke(IHttpRequest request, IHttpResponseHandler responseHandler) throws IOException
Description copied from interface:IHttpConnection
This method should be called to transfer data over http and handle its response.- Specified by:
invoke
in interfaceIHttpConnection
- Parameters:
request
- The object that is responsible for the http request data.responseHandler
- The object that is used to handle the http response.- Throws:
IOException
- Is thrown when IO error occurs.
-
getName
public String getName()
Description copied from interface:IConnection
This method returns the name of the connection object. The name is used to uniquely identify the connection object.- Specified by:
getName
in interfaceIConnection
- Returns:
- the name of the connection object.
-
getType
public ConnectionType getType()
Description copied from interface:IConnection
This method returns the type of the connection object.- Specified by:
getType
in interfaceIConnection
- Returns:
- the type of the connection object.
-
getUrl
public String getUrl()
Description copied from interface:IHttpConnection
This method returns the url used for connecting to the endpoint.- Specified by:
getUrl
in interfaceIHttpConnection
- Returns:
- The url (never null).
-
getUsername
protected String getUsername()
-
getPassword
protected String getPassword()
-
getHeaderNames
public List<String> getHeaderNames()
Description copied from interface:IHttpConnection
This method returns a list of header names that are to be put on the http request- Specified by:
getHeaderNames
in interfaceIHttpConnection
- Returns:
- a list of header names, never null
-
setHeaderNames
public void setHeaderNames(List<String> headerNames)
Sets the names of the headers to send with a HTTP request- Parameters:
headerNames
- the list of header names
-
-