Class DefaultHttpClient

  • All Implemented Interfaces:
    HttpClient

    public class DefaultHttpClient
    extends java.lang.Object
    implements HttpClient
    A default client for executing JSON-based HTTP requests.

    Basic Usage

     final HttpClient httpClient = new DefaultHttpClient.Builder();
    
     final SomePOJORequest postPojo = new MyPOJO("id", "data");
     final Response<SomePOJOResponse> postResponse = httpClient.post("http://localhost/pojo",
                                                                     postPojo,
                                                                     SomePOJOResponse.class);
    
     final Response<SomePOJOResponse> getResponse = httpClient.get("http://localhost/pojo/id",
                                                                     SomePOJOResponse.class);
    
     final SomePOJORequest putPojo = new MyPOJO("updated-data");
     final Response<SomePOJOResponse> postResponse = httpClient.post("http://localhost/pojo/id",
                                                                     putPojo,
                                                                     SomePOJOResponse.class);
    
     final Response<Map> deleteResponse = httpClient.delete("http://localhost/pojo/id");
     
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.net.HttpURLConnection createHttpUrlConnection​(java.lang.String url)
      Override this method if you could like to implement a custom URL connection.
      <B> Response<B> delete​(java.lang.String url, java.util.List<Parameter> parameters, java.util.Map<java.lang.String,​java.lang.String> additionalHeaders, java.lang.Class<B> clazz)
      Perform DELETE operation against an endpoint.
      <B> Response<B> get​(java.lang.String url, java.util.List<Parameter> parameters, java.util.Map<java.lang.String,​java.lang.String> additionalHeaders, java.lang.Class<B> clazz)
      Perform GET operation against an endpoint.
      void get​(java.lang.String url, java.util.List<Parameter> parameters, java.util.Map<java.lang.String,​java.lang.String> additionalHeaders, java.nio.file.Path dest, int retries)
      Perform GET operation that downloads a file to the given path.
      protected java.io.InputStream getInputStream​(java.net.HttpURLConnection httpUrlConnection, java.lang.String body, java.lang.String method, java.util.Map<java.lang.String,​java.lang.String> additionalHeaders)  
      <R,​B>
      Response<B>
      post​(java.lang.String url, R request, java.util.List<Parameter> parameters, java.util.Map<java.lang.String,​java.lang.String> additionalHeaders, java.lang.Class<B> clazz)
      Perform POST operation against an endpoint.
      <R,​B>
      Response<B>
      put​(java.lang.String url, R request, java.util.List<Parameter> parameters, java.util.Map<java.lang.String,​java.lang.String> additionalHeaders, java.lang.Class<B> clazz)
      Perform PUT operation against an endpoint.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • get

        public <B> Response<B> get​(java.lang.String url,
                                   java.util.List<Parameter> parameters,
                                   java.util.Map<java.lang.String,​java.lang.String> additionalHeaders,
                                   java.lang.Class<B> clazz)
        Description copied from interface: HttpClient
        Perform GET operation against an endpoint.
        Specified by:
        get in interface HttpClient
        Type Parameters:
        B - The response body type.
        Parameters:
        url - The URL on which to perform the operation.
        parameters - An arbitrary number of parameters to add to the URL.
        additionalHeaders - Additional headers for the request.
        clazz - The class for the Response's body.
        Returns:
        The results of the query.
      • get

        public void get​(java.lang.String url,
                        java.util.List<Parameter> parameters,
                        java.util.Map<java.lang.String,​java.lang.String> additionalHeaders,
                        java.nio.file.Path dest,
                        int retries)
                 throws java.lang.InterruptedException
        Description copied from interface: HttpClient
        Perform GET operation that downloads a file to the given path.
        Specified by:
        get in interface HttpClient
        Parameters:
        url - The URL on which to perform the operation.
        parameters - An arbitrary number of parameters to add to the URL.
        additionalHeaders - Additional headers for the request.
        dest - The destination to which the file will be downloaded.
        retries - The retry attempt index, if download fails.
        Throws:
        java.lang.InterruptedException
      • post

        public <R,​B> Response<B> post​(java.lang.String url,
                                            R request,
                                            java.util.List<Parameter> parameters,
                                            java.util.Map<java.lang.String,​java.lang.String> additionalHeaders,
                                            java.lang.Class<B> clazz)
        Description copied from interface: HttpClient
        Perform POST operation against an endpoint.
        Specified by:
        post in interface HttpClient
        Type Parameters:
        R - The Request type.
        B - The Response body type.
        Parameters:
        url - The URL on which to perform the operation.
        request - The element to be serialized into the request body.
        parameters - An arbitrary number of parameters to add to the URL.
        additionalHeaders - Additional headers for the request.
        clazz - The class for the Response's body.
        Returns:
        The results of the query.
      • put

        public <R,​B> Response<B> put​(java.lang.String url,
                                           R request,
                                           java.util.List<Parameter> parameters,
                                           java.util.Map<java.lang.String,​java.lang.String> additionalHeaders,
                                           java.lang.Class<B> clazz)
        Description copied from interface: HttpClient
        Perform PUT operation against an endpoint.
        Specified by:
        put in interface HttpClient
        Type Parameters:
        R - The Request type.
        B - The Response body type.
        Parameters:
        url - The URL on which to perform the operation.
        request - The element to be serialized into the request body.
        parameters - An arbitrary number of parameters to add to the URL.
        additionalHeaders - Additional headers for the request.
        clazz - The class for the Response's body.
        Returns:
        The results of the query.
      • delete

        public <B> Response<B> delete​(java.lang.String url,
                                      java.util.List<Parameter> parameters,
                                      java.util.Map<java.lang.String,​java.lang.String> additionalHeaders,
                                      java.lang.Class<B> clazz)
        Description copied from interface: HttpClient
        Perform DELETE operation against an endpoint.
        Specified by:
        delete in interface HttpClient
        Type Parameters:
        B - The Response body type.
        Parameters:
        url - The URL on which to perform the operation.
        parameters - An arbitrary number of parameters to add to the URL.
        additionalHeaders - Additional headers for the request.
        clazz - The class for the Response's body.
        Returns:
        The results of the query.
      • createHttpUrlConnection

        protected java.net.HttpURLConnection createHttpUrlConnection​(java.lang.String url)
                                                              throws java.io.IOException
        Override this method if you could like to implement a custom URL connection.
        Parameters:
        url - The URL to connect to.
        Returns:
        A URL connection.
        Throws:
        java.io.IOException - An I/O exception has occurred.
      • getInputStream

        protected java.io.InputStream getInputStream​(java.net.HttpURLConnection httpUrlConnection,
                                                     java.lang.String body,
                                                     java.lang.String method,
                                                     java.util.Map<java.lang.String,​java.lang.String> additionalHeaders)
                                              throws java.io.IOException
        Throws:
        java.io.IOException