Package com.onlinepayments
Class DefaultCommunicator
java.lang.Object
com.onlinepayments.DefaultCommunicator
- All Implemented Interfaces:
Communicator,LoggingCapable,ObfuscationCapable,Closeable,AutoCloseable
Used to communicate with the Online Payments platform web services.
It contains all the logic to transform a request object to a HTTP request and a HTTP response to a response object.
Thread-safe.
-
Constructor Summary
ConstructorsConstructorDescriptionDefaultCommunicator(URI apiEndpoint, Connection connection, Authenticator authenticator, MetadataProvider metadataProvider, Marshaller marshaller) -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddGenericHeaders(String httpMethod, URI uri, List<RequestHeader> requestHeaders, CallContext context) Adds the necessary headers to the given list of headers.voidclose()Releases any system resources associated with this object.voidUtility method that delegates the call to this communicator's connection if that's an instance ofPooledConnection.voidcloseIdleConnections(long idleTime, TimeUnit timeUnit) Utility method that delegates the call to this communicator's connection if that's an instance ofPooledConnection.voiddelete(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, BodyHandler bodyHandler, CallContext context) Corresponds to the HTTP DELETE method.<O> Odelete(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, Class<O> responseType, CallContext context) Corresponds to the HTTP DELETE method.voidTurns off logging.voidenableLogging(CommunicatorLogger communicatorLogger) Turns on logging using the given communicator logger.voidget(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, BodyHandler bodyHandler, CallContext context) Corresponds to the HTTP GET method.<O> Oget(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, Class<O> responseType, CallContext context) Corresponds to the HTTP GET method.protected static StringReturns the date in the preferred format for the HTTP date header (RFC1123).voidpost(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, Object requestBody, BodyHandler bodyHandler, CallContext context) Corresponds to the HTTP POST method.<O> Opost(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, Object requestBody, Class<O> responseType, CallContext context) Corresponds to the HTTP POST method.protected voidprocessResponse(int statusCode, InputStream bodyStream, List<ResponseHeader> headers, BodyHandler bodyHandler, String requestPath, CallContext context) protected <O> OprocessResponse(int statusCode, InputStream bodyStream, List<ResponseHeader> headers, Class<O> responseType, String requestPath, CallContext context) voidput(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, Object requestBody, BodyHandler bodyHandler, CallContext context) Corresponds to the HTTP PUT method.<O> Oput(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, Object requestBody, Class<O> responseType, CallContext context) Corresponds to the HTTP PUT method.voidsetBodyObfuscator(BodyObfuscator bodyObfuscator) Sets the current non-nullbody obfuscator to use.voidsetHeaderObfuscator(HeaderObfuscator headerObfuscator) Sets the current non-nullheader obfuscator to use.protected static voidthrowExceptionIfNecessary(int statusCode, InputStream bodyStream, List<ResponseHeader> headers, String requestPath) Checks the status code and headers for errors and throws an exception if necessary.protected URItoAbsoluteURI(String relativePath, List<RequestParam> requestParameters) protected static voidupdateContext(List<ResponseHeader> headers, CallContext context) Updates the given call context based on the contents of the given response.
-
Constructor Details
-
DefaultCommunicator
public DefaultCommunicator(URI apiEndpoint, Connection connection, Authenticator authenticator, MetadataProvider metadataProvider, Marshaller marshaller)
-
-
Method Details
-
close
Releases any system resources associated with this object.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
get
public <O> O get(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, Class<O> responseType, CallContext context) Corresponds to the HTTP GET method.- Specified by:
getin interfaceCommunicator- Parameters:
relativePath- The path to call, relative to the base URI.requestHeaders- An optional list of request headers.requestParameters- An optional set of request parameters.responseType- The type of response to return.context- The optional call context to use.
-
get
public void get(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, BodyHandler bodyHandler, CallContext context) Corresponds to the HTTP GET method.- Specified by:
getin interfaceCommunicator- Parameters:
relativePath- The path to call, relative to the base URI.requestHeaders- An optional list of request headers.requestParameters- An optional set of request parameters.bodyHandler- The handler for the response body.context- The optional call context to use.
-
delete
public <O> O delete(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, Class<O> responseType, CallContext context) Corresponds to the HTTP DELETE method.- Specified by:
deletein interfaceCommunicator- Parameters:
relativePath- The path to call, relative to the base URI.requestHeaders- An optional list of request headers.requestParameters- An optional set of request parameters.responseType- The type of response to return.context- The optional call context to use.
-
delete
public void delete(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, BodyHandler bodyHandler, CallContext context) Corresponds to the HTTP DELETE method.- Specified by:
deletein interfaceCommunicator- Parameters:
relativePath- The path to call, relative to the base URI.requestHeaders- An optional list of request headers.requestParameters- An optional set of request parameters.bodyHandler- The handler for the response body.context- The optional call context to use.
-
post
public <O> O post(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, Object requestBody, Class<O> responseType, CallContext context) Corresponds to the HTTP POST method.- Specified by:
postin interfaceCommunicator- Parameters:
relativePath- The path to call, relative to the base URI.requestHeaders- An optional list of request headers.requestParameters- An optional set of request parameters.requestBody- The optional request body to send.responseType- The type of response to return.context- The optional call context to use.
-
post
public void post(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, Object requestBody, BodyHandler bodyHandler, CallContext context) Corresponds to the HTTP POST method.- Specified by:
postin interfaceCommunicator- Parameters:
relativePath- The path to call, relative to the base URI.requestHeaders- An optional list of request headers.requestParameters- An optional set of request parameters.requestBody- The optional request body to send.bodyHandler- The handler for the response body.context- The optional call context to use.
-
put
public <O> O put(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, Object requestBody, Class<O> responseType, CallContext context) Corresponds to the HTTP PUT method.- Specified by:
putin interfaceCommunicator- Parameters:
relativePath- The path to call, relative to the base URI.requestHeaders- An optional list of request headers.requestParameters- An optional set of request parameters.requestBody- The optional request body to send.responseType- The type of response to return.context- The optional call context to use.
-
put
public void put(String relativePath, List<RequestHeader> requestHeaders, ParamRequest requestParameters, Object requestBody, BodyHandler bodyHandler, CallContext context) Corresponds to the HTTP PUT method.- Specified by:
putin interfaceCommunicator- Parameters:
relativePath- The path to call, relative to the base URI.requestHeaders- An optional list of request headers.requestParameters- An optional set of request parameters.requestBody- The optional request body to send.bodyHandler- The handler for the response body.context- The optional call context to use.
-
getMarshaller
- Specified by:
getMarshallerin interfaceCommunicator- Returns:
- The
Marshallerobject associated with this communicator. Nevernull.
-
toAbsoluteURI
-
addGenericHeaders
protected void addGenericHeaders(String httpMethod, URI uri, List<RequestHeader> requestHeaders, CallContext context) Adds the necessary headers to the given list of headers. This includes the authorization header, which uses other headers, so when you need to override this method, make sure to callsuper.addGenericHeadersat the end of your overridden method. -
getHeaderDateString
Returns the date in the preferred format for the HTTP date header (RFC1123). -
processResponse
protected <O> O processResponse(int statusCode, InputStream bodyStream, List<ResponseHeader> headers, Class<O> responseType, String requestPath, CallContext context) -
processResponse
protected void processResponse(int statusCode, InputStream bodyStream, List<ResponseHeader> headers, BodyHandler bodyHandler, String requestPath, CallContext context) -
updateContext
Updates the given call context based on the contents of the given response. -
throwExceptionIfNecessary
protected static void throwExceptionIfNecessary(int statusCode, InputStream bodyStream, List<ResponseHeader> headers, String requestPath) Checks the status code and headers for errors and throws an exception if necessary. -
closeIdleConnections
Utility method that delegates the call to this communicator's connection if that's an instance ofPooledConnection. If not this method does nothing.- Specified by:
closeIdleConnectionsin interfaceCommunicator- See Also:
-
closeExpiredConnections
public void closeExpiredConnections()Utility method that delegates the call to this communicator's connection if that's an instance ofPooledConnection. If not this method does nothing.- Specified by:
closeExpiredConnectionsin interfaceCommunicator- See Also:
-
setBodyObfuscator
Sets the current non-nullbody obfuscator to use.- Specified by:
setBodyObfuscatorin interfaceObfuscationCapable
-
setHeaderObfuscator
Sets the current non-nullheader obfuscator to use.- Specified by:
setHeaderObfuscatorin interfaceObfuscationCapable
-
enableLogging
Turns on logging using the given communicator logger.- Specified by:
enableLoggingin interfaceLoggingCapable
-
disableLogging
public void disableLogging()Turns off logging.- Specified by:
disableLoggingin interfaceLoggingCapable
-