Package com.guinetik.rr.http
Class DefaultHttpClient
- java.lang.Object
-
- com.guinetik.rr.http.DefaultHttpClient
-
- All Implemented Interfaces:
RocketClient
public class DefaultHttpClient extends Object implements RocketClient
Default implementation ofRocketClientusing Java's built-inHttpURLConnection.This client provides HTTP request execution without external dependencies, making it suitable for environments where third-party HTTP libraries cannot be used. It supports all standard HTTP methods, custom headers, SSL/TLS configuration, and JSON serialization.
Features
- Zero external HTTP dependencies (uses java.net.HttpURLConnection)
- Automatic JSON serialization/deserialization via Jackson
- SSL/TLS support with custom certificate configuration
- Configurable timeouts and request options
- Query parameter encoding and URL building
- Comprehensive error handling with status codes
Direct Usage
// Create a client DefaultHttpClient client = new DefaultHttpClient("https://api.example.com"); // Build and execute a request RequestSpec<Void, User> request = RequestBuilder.<Void, User>get("/users/1") .responseType(User.class) .build(); User user = client.execute(request);With Custom Options
RocketRestOptions options = new RocketRestOptions(); options.set(RocketRestOptions.LOGGING_ENABLED, true); options.set(RocketRestOptions.LOG_RESPONSE_BODY, true); DefaultHttpClient client = new DefaultHttpClient( "https://api.example.com", options );Note
For most use cases, prefer using
RocketRestfacade orRocketClientFactoryinstead of instantiating this class directly.- Since:
- 1.0.0
- Author:
- guinetik <guinetik@gmail.com>
- See Also:
RocketClient,RocketClientFactory,RocketRest
-
-
Constructor Summary
Constructors Constructor Description DefaultHttpClient(String baseUrl)Creates a new DefaultHttpClient with the specified base URL.DefaultHttpClient(String baseUrl, RocketRestOptions clientOptions)Creates a new DefaultHttpClient with the specified base URL and client options.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidconfigureSsl(SSLContext sslContext)Sets the SSL context to be used for HTTPS requests.<Req,Res>
Resexecute(RequestSpec<Req,Res> requestSpec)Executes an HTTP request based on the provided request specification.RocketRestOptionsgetClientOptions()Gets the client options.voidsetBaseUrl(String baseUrl)
-
-
-
Constructor Detail
-
DefaultHttpClient
public DefaultHttpClient(String baseUrl)
Creates a new DefaultHttpClient with the specified base URL.- Parameters:
baseUrl- The base URL for all requests
-
DefaultHttpClient
public DefaultHttpClient(String baseUrl, RocketRestOptions clientOptions)
Creates a new DefaultHttpClient with the specified base URL and client options.- Parameters:
baseUrl- The base URL for all requestsclientOptions- The client options
-
-
Method Detail
-
configureSsl
public void configureSsl(SSLContext sslContext)
Description copied from interface:RocketClientSets the SSL context to be used for HTTPS requests.- Specified by:
configureSslin interfaceRocketClient- Parameters:
sslContext- The SSL context to use.
-
setBaseUrl
public void setBaseUrl(String baseUrl)
- Specified by:
setBaseUrlin interfaceRocketClient
-
getClientOptions
public RocketRestOptions getClientOptions()
Gets the client options.- Returns:
- The client options
-
execute
public <Req,Res> Res execute(RequestSpec<Req,Res> requestSpec) throws RocketRestException
Description copied from interface:RocketClientExecutes an HTTP request based on the provided request specification.- Specified by:
executein interfaceRocketClient- Type Parameters:
Req- The type of the request body.Res- The type of the response.- Parameters:
requestSpec- The specification of the request to be executed.- Returns:
- The response object.
- Throws:
RocketRestException- If an error occurs during the request execution.
-
-