Class JdkHttpRestClient

java.lang.Object
org.refcodes.rest.AbstractRestClient
org.refcodes.rest.JdkHttpRestClient
All Implemented Interfaces:
org.refcodes.component.Closable, org.refcodes.component.ConnectionComponent<org.refcodes.web.HttpClientContext>, org.refcodes.component.ConnectionOpenable<org.refcodes.web.HttpClientContext>, org.refcodes.component.ConnectionStatusAccessor, org.refcodes.component.LinkComponent, org.refcodes.component.Openable, org.refcodes.component.Openable.OpenBuilder<HttpRestClient>, org.refcodes.component.OpenedAccessor, DeleteRestClient, GetRestClient, HttpRestClient, PostRestClient, PutRestClient, RestClient, RestRequestClient, RestRequestHandler, org.refcodes.security.TrustStoreDescriptorAccessor, org.refcodes.security.TrustStoreDescriptorAccessor.TrustStoreDescriptorBuilder<HttpRestClient>, org.refcodes.security.TrustStoreDescriptorAccessor.TrustStoreDescriptorMutator, org.refcodes.security.TrustStoreDescriptorAccessor.TrustStoreDescriptorProperty, org.refcodes.web.BaseUrlAccessor, org.refcodes.web.BaseUrlAccessor.BaseUrlBuilder<HttpRestClient>, org.refcodes.web.BaseUrlAccessor.BaseUrlMutator, org.refcodes.web.BaseUrlAccessor.BaseUrlProperty, org.refcodes.web.BasicAuthCredentialsAccessor, org.refcodes.web.BasicAuthCredentialsAccessor.BasicAuthCredentialsBuilder<RestClient>, org.refcodes.web.BasicAuthCredentialsAccessor.BasicAuthCredentialsMutator, org.refcodes.web.BasicAuthCredentialsAccessor.BasicAuthCredentialsProperty, org.refcodes.web.HttpClientInterceptable, 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.PostHttpClientInterceptable, org.refcodes.web.PostHttpInterceptable<org.refcodes.web.PostHttpClientInterceptor>, org.refcodes.web.PreHttpClientInterceptable, org.refcodes.web.PreHttpInterceptable<org.refcodes.web.PreHttpClientInterceptor>, org.refcodes.web.UserAgentAccessor, org.refcodes.web.UserAgentAccessor.UserAgentBuilder<RestClient>, org.refcodes.web.UserAgentAccessor.UserAgentMutator, org.refcodes.web.UserAgentAccessor.UserAgentProperty
Direct Known Subclasses:
JdkHttpRestClientSingleton

public class JdkHttpRestClient extends AbstractRestClient implements HttpRestClient

The JdkHttpRestClient implements the HttpRestClient interface. The JdkHttpRestClient is being initialized with some common MediaTypeFactory instances (as implemented by the AbstractRestClient). At the time of writing this document the MediaTypeFactory instances being preconfigured are:

  • JsonMediaTypeFactory
  • XmlMediaTypeFactory
  • TextMediaTypeFactory
  • FormMediaTypeFactory
  • HtmlMediaTypeFactory

The JdkHttpRestClient supports HTTP as well as HTTPS protocols as being based on the HttpURLConnection. For configuring HTTPS capabilities, refer to the methods such as open(Url, TrustStoreDescriptor) or open(Url, TrustStoreDescriptor).

  • Constructor Details

  • Method Details

    • close

      public void close() throws IOException
      Specified by:
      close in interface org.refcodes.component.Closable
      Throws:
      IOException
    • getBaseUrl

      public org.refcodes.web.Url getBaseUrl()
      Specified by:
      getBaseUrl in interface org.refcodes.web.BaseUrlAccessor
    • getConnectionStatus

      public org.refcodes.component.ConnectionStatus getConnectionStatus()
      Specified by:
      getConnectionStatus in interface org.refcodes.component.ConnectionStatusAccessor
    • getTrustStoreDescriptor

      public org.refcodes.security.TrustStoreDescriptor getTrustStoreDescriptor()
      Specified by:
      getTrustStoreDescriptor in interface org.refcodes.security.TrustStoreDescriptorAccessor
    • open

      public void open(org.refcodes.web.Url aBaseUrl, org.refcodes.security.TrustStoreDescriptor aStoreDescriptor) throws IOException
      Configures the HTTPS client connection with the provided configuration parameters.
      Specified by:
      open in interface HttpRestClient
      Parameters:
      aBaseUrl - The base Url to be used.
      aStoreDescriptor - The TrustStoreDescriptor pointing to your KeyStore.
      Throws:
      IOException - thrown in case something went wrong.
    • setBaseUrl

      public void setBaseUrl(org.refcodes.web.Url aBaseUrl)
      Specified by:
      setBaseUrl in interface org.refcodes.web.BaseUrlAccessor.BaseUrlMutator
    • setBaseUrl

      public void setBaseUrl(URL aBaseUrl)
      Specified by:
      setBaseUrl in interface org.refcodes.web.BaseUrlAccessor.BaseUrlMutator
    • setTrustStoreDescriptor

      public void setTrustStoreDescriptor(org.refcodes.security.TrustStoreDescriptor aTrustStoreDescriptor)
      Specified by:
      setTrustStoreDescriptor in interface org.refcodes.security.TrustStoreDescriptorAccessor.TrustStoreDescriptorMutator
    • hasPreHttpInterceptor

      public boolean hasPreHttpInterceptor(org.refcodes.web.PreHttpClientInterceptor aPreInterceptor)
      Specified by:
      hasPreHttpInterceptor in interface org.refcodes.web.PreHttpInterceptable<org.refcodes.web.PreHttpClientInterceptor>
    • addPreHttpInterceptor

      public boolean addPreHttpInterceptor(org.refcodes.web.PreHttpClientInterceptor aPreInterceptor)
      Specified by:
      addPreHttpInterceptor in interface org.refcodes.web.PreHttpInterceptable<org.refcodes.web.PreHttpClientInterceptor>
    • removePreHttpInterceptor

      public boolean removePreHttpInterceptor(org.refcodes.web.PreHttpClientInterceptor aPreInterceptor)
      Specified by:
      removePreHttpInterceptor in interface org.refcodes.web.PreHttpInterceptable<org.refcodes.web.PreHttpClientInterceptor>
    • hasPostHttpInterceptor

      public boolean hasPostHttpInterceptor(org.refcodes.web.PostHttpClientInterceptor aPostInterceptor)
      Specified by:
      hasPostHttpInterceptor in interface org.refcodes.web.PostHttpInterceptable<org.refcodes.web.PostHttpClientInterceptor>
    • addPostHttpInterceptor

      public boolean addPostHttpInterceptor(org.refcodes.web.PostHttpClientInterceptor aPostInterceptor)
      Specified by:
      addPostHttpInterceptor in interface org.refcodes.web.PostHttpInterceptable<org.refcodes.web.PostHttpClientInterceptor>
    • removePostHttpInterceptor

      public boolean removePostHttpInterceptor(org.refcodes.web.PostHttpClientInterceptor aPostInterceptor)
      Specified by:
      removePostHttpInterceptor in interface org.refcodes.web.PostHttpInterceptable<org.refcodes.web.PostHttpClientInterceptor>
    • withOpen

      public JdkHttpRestClient withOpen() throws IOException
      Open the component's connection(s).
      Specified by:
      withOpen in interface HttpRestClient
      Specified by:
      withOpen in interface org.refcodes.component.Openable.OpenBuilder<HttpRestClient>
      Returns:
      This instance as of the builder pattern.
      Throws:
      IOException - Thrown in case opening or accessing an open line (connection, junction, link) caused problems.
    • withOpen

      public JdkHttpRestClient withOpen(org.refcodes.web.HttpClientContext aCtx) throws IOException
      Opens the component with the given connection.
      Specified by:
      withOpen in interface HttpRestClient
      Parameters:
      aCtx - The context used for opening the connection.
      Returns:
      This instance as of the builder pattern.
      Throws:
      IOException - Thrown in case opening or accessing an open line (connection, junction, link) caused problems.
    • withOpen

      public JdkHttpRestClient withOpen(org.refcodes.security.TrustStoreDescriptor aStoreDescriptor) throws IOException
      Configures the HTTPS client connection with the provided configuration parameters.
      Specified by:
      withOpen in interface HttpRestClient
      Parameters:
      aStoreDescriptor - The TrustStoreDescriptor pointing to your KeyStore.
      Returns:
      This instance as of the builder pattern.
      Throws:
      IOException - thrown in case something went wrong.
    • withOpen

      public JdkHttpRestClient withOpen(org.refcodes.web.Url aBaseUrl) throws IOException
      Configures the HTTPS client connection with the provided configuration parameters.
      Specified by:
      withOpen in interface HttpRestClient
      Parameters:
      aBaseUrl - The base Url to be used.
      Returns:
      This instance as of the builder pattern.
      Throws:
      IOException - thrown in case something went wrong.
    • withOpen

      public JdkHttpRestClient withOpen(org.refcodes.web.Url aBaseUrl, org.refcodes.security.TrustStoreDescriptor aStoreDescriptor) throws IOException
      Configures the HTTPS client connection with the provided configuration parameters.
      Specified by:
      withOpen in interface HttpRestClient
      Parameters:
      aBaseUrl - The base Url to be used.
      aStoreDescriptor - The TrustStoreDescriptor pointing to your KeyStore.
      Returns:
      This instance as of the builder pattern.
      Throws:
      IOException - thrown in case something went wrong.
    • withBaseUrl

      public JdkHttpRestClient withBaseUrl(String aBaseUrl) throws MalformedURLException
      Specified by:
      withBaseUrl in interface org.refcodes.web.BaseUrlAccessor.BaseUrlBuilder<HttpRestClient>
      Specified by:
      withBaseUrl in interface HttpRestClient
      Throws:
      MalformedURLException
    • withBaseUrl

      public JdkHttpRestClient withBaseUrl(org.refcodes.web.Url aBaseUrl)
      Specified by:
      withBaseUrl in interface org.refcodes.web.BaseUrlAccessor.BaseUrlBuilder<HttpRestClient>
      Specified by:
      withBaseUrl in interface HttpRestClient
    • withBaseUrl

      public JdkHttpRestClient withBaseUrl(URL aBaseURL)
      Specified by:
      withBaseUrl in interface org.refcodes.web.BaseUrlAccessor.BaseUrlBuilder<HttpRestClient>
      Specified by:
      withBaseUrl in interface HttpRestClient
    • withBasicAuthCredentials

      public JdkHttpRestClient withBasicAuthCredentials(org.refcodes.web.BasicAuthCredentials aBasicAuthCredentials)
      Specified by:
      withBasicAuthCredentials in interface org.refcodes.web.BasicAuthCredentialsAccessor.BasicAuthCredentialsBuilder<RestClient>
      Specified by:
      withBasicAuthCredentials in interface HttpRestClient
    • withBasicAuthCredentials

      public JdkHttpRestClient withBasicAuthCredentials(String aUserName, String aSecret)
      Specified by:
      withBasicAuthCredentials in interface org.refcodes.web.BasicAuthCredentialsAccessor.BasicAuthCredentialsBuilder<RestClient>
      Specified by:
      withBasicAuthCredentials in interface HttpRestClient
    • withOAuthToken

      public JdkHttpRestClient withOAuthToken(org.refcodes.web.OauthToken aOauthToken)
      Specified by:
      withOAuthToken in interface HttpRestClient
      Specified by:
      withOAuthToken in interface org.refcodes.web.OauthTokenAccessor.OauthTokenBuilder<RestClient>
    • withTrustStoreDescriptor

      public JdkHttpRestClient withTrustStoreDescriptor(org.refcodes.security.TrustStoreDescriptor aStoreDescriptor)
      Specified by:
      withTrustStoreDescriptor in interface HttpRestClient
      Specified by:
      withTrustStoreDescriptor in interface org.refcodes.security.TrustStoreDescriptorAccessor.TrustStoreDescriptorBuilder<HttpRestClient>
    • withUserAgent

      public JdkHttpRestClient withUserAgent(String aUserAgent)
      Specified by:
      withUserAgent in interface HttpRestClient
      Specified by:
      withUserAgent in interface org.refcodes.web.UserAgentAccessor.UserAgentBuilder<RestClient>
    • withBaseUrl

      public JdkHttpRestClient withBaseUrl(String aProtocol, String aHost) throws MalformedURLException
      Specified by:
      withBaseUrl in interface org.refcodes.web.BaseUrlAccessor.BaseUrlBuilder<HttpRestClient>
      Throws:
      MalformedURLException
    • withBaseUrl

      public JdkHttpRestClient withBaseUrl(org.refcodes.data.Scheme aScheme, String aHost) throws MalformedURLException
      Specified by:
      withBaseUrl in interface org.refcodes.web.BaseUrlAccessor.BaseUrlBuilder<HttpRestClient>
      Throws:
      MalformedURLException
    • withBaseUrl

      public JdkHttpRestClient withBaseUrl(String aProtocol, String aHost, String aPath) throws MalformedURLException
      Specified by:
      withBaseUrl in interface org.refcodes.web.BaseUrlAccessor.BaseUrlBuilder<HttpRestClient>
      Throws:
      MalformedURLException
    • withBaseUrl

      public JdkHttpRestClient withBaseUrl(org.refcodes.data.Scheme aScheme, String aHost, String aPath) throws MalformedURLException
      Specified by:
      withBaseUrl in interface org.refcodes.web.BaseUrlAccessor.BaseUrlBuilder<HttpRestClient>
      Throws:
      MalformedURLException
    • withBaseUrl

      public JdkHttpRestClient withBaseUrl(String aProtocol, String aHost, int aPort) throws MalformedURLException
      Specified by:
      withBaseUrl in interface org.refcodes.web.BaseUrlAccessor.BaseUrlBuilder<HttpRestClient>
      Throws:
      MalformedURLException
    • withBaseUrl

      public JdkHttpRestClient withBaseUrl(org.refcodes.data.Scheme aScheme, String aHost, int aPort) throws MalformedURLException
      Specified by:
      withBaseUrl in interface org.refcodes.web.BaseUrlAccessor.BaseUrlBuilder<HttpRestClient>
      Throws:
      MalformedURLException
    • withBaseUrl

      public JdkHttpRestClient withBaseUrl(String aProtocol, String aHost, int aPort, String aPath) throws MalformedURLException
      Specified by:
      withBaseUrl in interface org.refcodes.web.BaseUrlAccessor.BaseUrlBuilder<HttpRestClient>
      Throws:
      MalformedURLException
    • withBaseUrl

      public JdkHttpRestClient withBaseUrl(org.refcodes.data.Scheme aScheme, String aHost, int aPort, String aPath) throws MalformedURLException
      Specified by:
      withBaseUrl in interface org.refcodes.web.BaseUrlAccessor.BaseUrlBuilder<HttpRestClient>
      Throws:
      MalformedURLException
    • withOpenUnchecked

      public JdkHttpRestClient withOpenUnchecked()
      Specified by:
      withOpenUnchecked in interface org.refcodes.component.Openable.OpenBuilder<HttpRestClient>
    • pipe

      protected static void pipe(InputStream aInputStream, OutputStream aOutoutStream) throws IOException
      Pipes the InputStream to the OutputStream.
      Parameters:
      aInputStream - The InputStream from which to pipe.
      aOutoutStream - The OutputStream to pipe to.
      Throws:
      IOException - Signals that an I/O exception has occurred.
    • preIntercept

      protected void preIntercept(org.refcodes.web.HttpClientRequest aRequest, org.refcodes.web.HttpClientResponse aResponse)
      Invoked to pre-process a HttpClientRequest alongside a HttpClientResponse.
      Overrides:
      preIntercept in class AbstractRestClient
      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.
      Overrides:
      postIntercept in class AbstractRestClient
      Parameters:
      aRequest - The HttpClientRequest to post-process.
      aResponse - The HttpClientResponse to post-process.