- java.lang.Object
-
- com.github.alexdlaird.http.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");
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDefaultHttpClient.BuilderBuilder for aDefaultHttpClient, see docs for that class for example usage.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.net.HttpURLConnectioncreateHttpUrlConnection(java.lang.String url)Override this method if you would 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)PerformDELETEoperation 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)PerformGEToperation against an endpoint.voidget(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)PerformGEToperation that downloads a file to the given path.protected java.io.InputStreamgetInputStream(java.net.HttpURLConnection httpUrlConnection, java.lang.String body, java.lang.String method, java.util.Map<java.lang.String,java.lang.String> additionalHeaders)Initialize the connection with the given parameters, then return the provisioned input stream.<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)PerformPOSToperation 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)PerformPUToperation against an endpoint.protected java.lang.StringurlWithParameters(java.lang.String url, java.util.List<Parameter> parameters)Rebuild the URL with parameters appended.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.github.alexdlaird.http.HttpClient
delete, delete, get, get, modifyConnection, post, put
-
-
-
-
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:HttpClientPerformGEToperation against an endpoint.- Specified by:
getin interfaceHttpClient- 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.InterruptedExceptionDescription copied from interface:HttpClientPerformGEToperation that downloads a file to the given path.- Specified by:
getin interfaceHttpClient- 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- The thread was interrupted during retries.
-
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:HttpClientPerformPOSToperation against an endpoint.- Specified by:
postin interfaceHttpClient- 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:HttpClientPerformPUToperation against an endpoint.- Specified by:
putin interfaceHttpClient- 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:HttpClientPerformDELETEoperation against an endpoint.- Specified by:
deletein interfaceHttpClient- 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.IOExceptionOverride this method if you would 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 occurred.
-
urlWithParameters
protected java.lang.String urlWithParameters(java.lang.String url, java.util.List<Parameter> parameters) throws java.io.UnsupportedEncodingExceptionRebuild the URL with parameters appended.- Parameters:
url- The URL to rebuild.parameters- The parameters to append.- Returns:
- The parameter-appended URL.
- Throws:
java.io.UnsupportedEncodingException- An encoding exception 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.IOExceptionInitialize the connection with the given parameters, then return the provisioned input stream.- Parameters:
httpUrlConnection- The connection to initialize.body- The request body for the stream.method- The HTTP method.additionalHeaders- Additional headers for the request.- Returns:
- The provisioned input stream.
- Throws:
java.io.IOException- An I/O exception occurred.
-
-