Class AbstractRestClient

java.lang.Object
org.refcodes.rest.AbstractRestClient
All Implemented Interfaces:
RestClient, RestDeleteClient, RestGetClient, RestPostClient, RestPutClient, RestRequestClient, RestRequestHandler, org.refcodes.runtime.RequestCorrelation<RestClient>, org.refcodes.runtime.SessionCorrelation<RestClient>, org.refcodes.web.BasicAuthCredentialsAccessor, org.refcodes.web.BasicAuthCredentialsAccessor.BasicAuthCredentialsBuilder<RestClient>, org.refcodes.web.BasicAuthCredentialsAccessor.BasicAuthCredentialsMutator, org.refcodes.web.BasicAuthCredentialsAccessor.BasicAuthCredentialsProperty, org.refcodes.web.MediaTypeFactoryLookup, org.refcodes.web.MediaTypeFactoryLookup.MutableMediaTypeFactoryLookup, org.refcodes.web.OauthTokenAccessor, org.refcodes.web.OauthTokenAccessor.OauthTokenBuilder<RestClient>, org.refcodes.web.OauthTokenAccessor.OauthTokenMutator, org.refcodes.web.OauthTokenAccessor.OauthTokenProperty, org.refcodes.web.UserAgentAccessor, org.refcodes.web.UserAgentAccessor.UserAgentBuilder<RestClient>, org.refcodes.web.UserAgentAccessor.UserAgentMutator, org.refcodes.web.UserAgentAccessor.UserAgentProperty
Direct Known Subclasses:
HttpRestClientImpl, LoopbackRestClientImpl

public abstract class AbstractRestClient extends Object implements RestClient
Abstract base implementation of the RestClient interface being the foundation for various RestClient implementations such as HttpRestClientImpl or LoopbackRestClientImpl. The AbstractRestClient is preconfigured with the following MediaTypeFactory instances:
  • JsonMediaTypeFactory
  • XmlMediaTypeFactory
  • TextMediaTypeFactory
  • FormMediaTypeFactory
  • HtmlMediaTypeFactory
In your sub-classes, overwrite the method initMedaTypeFactories(), therein calling addMediaTypeFactory(MediaTypeFactory) to add (by also invoking super's initMedaTypeFactories()) or to set your own (without invoking super's initMedaTypeFactories()) MediaTypeFactory instances.
  • Field Details

    • _hasRequestCorrelation

      protected boolean _hasRequestCorrelation
    • _hasSessionCorrelation

      protected boolean _hasSessionCorrelation
  • Constructor Details

    • AbstractRestClient

      public AbstractRestClient(ExecutorService aExecutorService)
      Instantiates a new abstract rest client.
      Parameters:
      aExecutorService - the executor service
    • AbstractRestClient

      public AbstractRestClient()
      Instantiates a new abstract rest client.
  • Method Details

    • initMedaTypeFactories

      protected void initMedaTypeFactories()
      Adds the default MediaTypeFactory instances. Can be overridden.
    • getBasicAuthCredentials

      public org.refcodes.web.BasicAuthCredentials getBasicAuthCredentials()
      Specified by:
      getBasicAuthCredentials in interface org.refcodes.web.BasicAuthCredentialsAccessor
    • setBasicAuthCredentials

      public void setBasicAuthCredentials(org.refcodes.web.BasicAuthCredentials aBasicAuthCredentials)
      Specified by:
      setBasicAuthCredentials in interface org.refcodes.web.BasicAuthCredentialsAccessor.BasicAuthCredentialsMutator
    • setOauthToken

      public void setOauthToken(org.refcodes.web.OauthToken aOauthToken)
      Specified by:
      setOauthToken in interface org.refcodes.web.OauthTokenAccessor.OauthTokenMutator
    • getOauthToken

      public org.refcodes.web.OauthToken getOauthToken()
      Specified by:
      getOauthToken in interface org.refcodes.web.OauthTokenAccessor
    • setRequestCorrelation

      public void setRequestCorrelation(boolean hasRequestCorrelation)
      Specified by:
      setRequestCorrelation in interface org.refcodes.runtime.RequestCorrelation<RestClient>
    • hasRequestCorrelation

      public boolean hasRequestCorrelation()
      Specified by:
      hasRequestCorrelation in interface org.refcodes.runtime.RequestCorrelation<RestClient>
    • setSessionCorrelation

      public void setSessionCorrelation(boolean hasSessionCorrelation)
      Specified by:
      setSessionCorrelation in interface org.refcodes.runtime.SessionCorrelation<RestClient>
    • hasSessionCorrelation

      public boolean hasSessionCorrelation()
      Specified by:
      hasSessionCorrelation in interface org.refcodes.runtime.SessionCorrelation<RestClient>
    • doRequest

      public RestResponse doRequest(org.refcodes.web.HttpMethod aHttpMethod, org.refcodes.web.Url aUrl, org.refcodes.web.RequestHeaderFields aHeaderFields, Object aRequest, int aRedirectDepth) throws org.refcodes.web.HttpResponseException
      Sends a request with the possible attributes and returns the according request's RestResponse instance synchronously.
      Specified by:
      doRequest in interface RestRequestClient
      Parameters:
      aHttpMethod - The HTTP-Method for the request.
      aUrl - The Url from which to take the URL specific data.
      aHeaderFields - The HTTP-Header's fields to be used for the request.
      aRequest - The body to be sent with the request (e.g. when doing a HttpMethod.POST request.
      aRedirectDepth - The redirect depth provides the count of HTTP-Request and HTTP-Response cycles where the response represents a redirect as of HttpStatusCode.isRedirectStatus(). A value of -1 represents the default behavior, e.g. using HttpURLConnection's redirection means.
      Returns:
      The RestResponse for this request.
      Throws:
      org.refcodes.web.HttpResponseException - thrown in case of some unexpected response.
    • onResponse

      public RestResponseHandler onResponse(org.refcodes.web.HttpMethod aHttpMethod, org.refcodes.web.Url aUrl, org.refcodes.web.RequestHeaderFields aHeaderFields, Object aRequest, int aRedirectDepth, RestResponseConsumer aResponseConsumer)
      Creates a request with the possible attributes and returns the according RestResponseHandler instance used for the request. The HTTP-Request is actually sent not earlier than you calling RestResponseHandler.open() on the returned RestResponseHandler as the RestResponseHandler still may be modified after invoking this method!
      Specified by:
      onResponse in interface RestRequestClient
      Parameters:
      aHttpMethod - The HTTP-Method for the request.
      aUrl - The Url from which to take the URL specific data.
      aHeaderFields - The HTTP-Header's fields to be used for the request.
      aRequest - The body to be sent with the request (e.g. when doing a HttpMethod.POST request.
      aRedirectDepth - The redirect depth provides the count of HTTP-Request and HTTP-Response cycles where the response represents a redirect as of HttpStatusCode.isRedirectStatus(). A value of -1 represents the default behavior, e.g. using HttpURLConnection's redirection means.
      aResponseConsumer - The listener processing a response targeted at this RestResponseHandler.
      Returns:
      The RestResponseHandler which is used by the request.
    • onResponse

      public RestResponseResult onResponse(org.refcodes.web.HttpMethod aHttpMethod, org.refcodes.web.Url aUrl, org.refcodes.web.RequestHeaderFields aHeaderFields, Object aRequest, int aRedirectDepth)
      Creates a request with the possible attributes and returns the according RestResponseResult instance used for the request. The HTTP-Request is actually sent not earlier than you calling RestResponseResult.open() on the returned RestResponseResult as the RestResponseResult still may be modified after invoking this method!
      Specified by:
      onResponse in interface RestRequestClient
      Parameters:
      aHttpMethod - The HTTP-Method for the request.
      aUrl - The Url from which to take the URL specific data.
      aHeaderFields - The HTTP-Header's fields to be used for the request.
      aRequest - The body to be sent with the request (e.g. when doing a HttpMethod.POST request.
      aRedirectDepth - The redirect depth provides the count of HTTP-Request and HTTP-Response cycles where the response represents a redirect as of HttpStatusCode.isRedirectStatus(). A value of -1 represents the default behavior, e.g. using HttpURLConnection's redirection means.
      Returns:
      The RestResponseResult which is used by the request.
    • buildRequest

      public RestRequestBuilder buildRequest(org.refcodes.web.HttpMethod aHttpMethod, org.refcodes.web.Url aUrl, org.refcodes.web.RequestHeaderFields aHeaderFields, Object aRequest, int aRedirectDepth)
      Prepares a request builder with the possible attributes and returns the according request's RestRequestBuilder instance. Invoking RestRequestBuilder.toRestResponse() starts the request and synchronously returns the response.
      Specified by:
      buildRequest in interface RestRequestClient
      Parameters:
      aHttpMethod - The HTTP-Method for the request.
      aUrl - The Url from which to take the URL specific data.
      aHeaderFields - The HTTP-Header's fields to be used for the request.
      aRequest - The body to be sent with the request (e.g. when doing a HttpMethod.POST request.
      aRedirectDepth - The number of redirect ping-pong cycles to follow before when issuing the HTTP-Request.
      Returns:
      The RestRequestBuilder at this request.
    • addMediaTypeFactory

      public boolean addMediaTypeFactory(org.refcodes.web.MediaTypeFactory aMediaTypeFactory)
      Specified by:
      addMediaTypeFactory in interface org.refcodes.web.MediaTypeFactoryLookup.MutableMediaTypeFactoryLookup
    • toMediaTypeFactory

      public org.refcodes.web.MediaTypeFactory toMediaTypeFactory(org.refcodes.web.MediaType aMediaType)
      Specified by:
      toMediaTypeFactory in interface org.refcodes.web.MediaTypeFactoryLookup
    • getFactoryMediaTypes

      public org.refcodes.web.MediaType[] getFactoryMediaTypes()
      Specified by:
      getFactoryMediaTypes in interface org.refcodes.web.MediaTypeFactoryLookup
    • getUserAgent

      public String getUserAgent()
      Specified by:
      getUserAgent in interface org.refcodes.web.UserAgentAccessor
    • setUserAgent

      public void setUserAgent(String aUserAgent)
      Specified by:
      setUserAgent in interface org.refcodes.web.UserAgentAccessor.UserAgentMutator
    • onRestRequest

      protected void onRestRequest(RestRequestHandler aHandler)
      Sets the hook receiving a prepared RestResponseHandler instance to be used to do the technical request with the technology chosen by the implementing sub-class.
      Parameters:
      aHandler - The RestRequestHandler for handling the HTTP request.
    • preProcessHeaderFields

      protected org.refcodes.web.RequestHeaderFields preProcessHeaderFields(org.refcodes.web.RequestHeaderFields aHeaderFields)
      Pre-processes the given header fields before the HTTP-Request is issued. By default the User-Agent is set and (if present) the OAuth token.
      Parameters:
      aHeaderFields - The HeaderFields to be pre-prcessed.
    • doRequestCorrelation

      protected void doRequestCorrelation(org.refcodes.web.RequestHeaderFields aRequestHeaderFields)
      Do request correlation.
      Parameters:
      aRequestHeaderFields - the request Header-Fields
    • doSessionCorrelation

      protected void doSessionCorrelation(org.refcodes.web.RequestHeaderFields aRequestHeaderFields)
      Do session correlation.
      Parameters:
      aRequestHeaderFields - the request Header-Fields
    • doRequestCorrelation

      protected void doRequestCorrelation(org.refcodes.web.ResponseHeaderFields aResponseHeaderFields)
      Do request correlation.
      Parameters:
      aResponseHeaderFields - the response Header-Fields
    • doSessionCorrelation

      protected void doSessionCorrelation(org.refcodes.web.ResponseHeaderFields aResponseHeaderFields)
      Do session correlation.
      Parameters:
      aResponseHeaderFields - the response Header-Fields