Class AbstractRestfulClient

java.lang.Object
org.refcodes.rest.AbstractRestfulClient
All Implemented Interfaces:
RestDeleteClient, RestfulClient, RestGetClient, RestPostClient, RestPutClient, RestRequestClient, RestRequestHandler, org.refcodes.web.BasicAuthCredentialsAccessor, org.refcodes.web.BasicAuthCredentialsAccessor.BasicAuthCredentialsBuilder<RestfulClient>, 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<RestfulClient>, org.refcodes.web.OauthTokenAccessor.OauthTokenMutator, org.refcodes.web.OauthTokenAccessor.OauthTokenProperty, org.refcodes.web.UserAgentAccessor, org.refcodes.web.UserAgentAccessor.UserAgentBuilder<RestfulClient>, org.refcodes.web.UserAgentAccessor.UserAgentMutator, org.refcodes.web.UserAgentAccessor.UserAgentProperty
Direct Known Subclasses:
HttpRestClient, LoopbackRestClient

public abstract class AbstractRestfulClient extends Object implements RestfulClient
Abstract base implementation of the RestfulClient interface being the foundation for various RestfulClient implementations such as HttpRestClient or LoopbackRestClient. The AbstractRestfulClient 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.
  • Constructor Details

    • AbstractRestfulClient

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

      public AbstractRestfulClient()
      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
    • 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
    • preIntercept

      protected void preIntercept(org.refcodes.web.HttpClientRequest aRequest, org.refcodes.web.HttpClientResponse aResponse)
      Invoked to pre-process a HttpClientRequest alongside a HttpClientResponse.
      Parameters:
      aRequest - The HttpClientRequest to pre-process.
      aResponse - The HttpClientResponse to post-process.
    • postIntercept

      protected void postIntercept(org.refcodes.web.HttpClientRequest aRequest, org.refcodes.web.HttpClientResponse aResponse)
      Invoked to post-process a HttpClientRequest alongside a HttpClientResponse.
      Parameters:
      aRequest - The HttpClientRequest to post-process.
      aResponse - The HttpClientResponse to post-process.
    • 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.
      Returns:
      the request header fields