Package com.guinetik.rr.http
Class RocketHeaders
- java.lang.Object
-
- com.guinetik.rr.http.RocketHeaders
-
public class RocketHeaders extends Object
Fluent builder for HTTP request headers with convenience methods for common headers.This class provides a type-safe way to build and manage HTTP headers for requests, with predefined constants for common header names and content types.
Building Headers
// Create headers with fluent API RocketHeaders headers = new RocketHeaders() .contentType(RocketHeaders.ContentTypes.APPLICATION_JSON) .accept(RocketHeaders.ContentTypes.APPLICATION_JSON) .set("X-Custom-Header", "custom-value"); // Use in request RequestSpec request = RequestBuilder.post("/users") .headers(headers) .body(user) .responseType(User.class) .build();Authentication Headers
// Bearer token authentication RocketHeaders headers = new RocketHeaders() .bearerAuth("my-jwt-token"); // Basic authentication RocketHeaders basicAuth = new RocketHeaders() .basicAuth("username", "password");Default JSON Headers
// Get pre-configured JSON headers RocketHeaders jsonHeaders = RocketHeaders.defaultJson(); // Equivalent to Content-Type: application/json + Accept: application/jsonMerging Headers
RocketHeaders base = RocketHeaders.defaultJson(); RocketHeaders auth = new RocketHeaders().bearerAuth("token"); // Merge headers (auth values take precedence) RocketHeaders merged = base.merge(auth);- Since:
- 1.0.0
- Author:
- guinetik <guinetik@gmail.com>
- See Also:
RequestBuilder
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classRocketHeaders.ContentTypesCommon content types as constants.static classRocketHeaders.NamesStandard HTTP header names as constants.
-
Constructor Summary
Constructors Constructor Description RocketHeaders()Creates a new empty header container.RocketHeaders(Map<String,String> headers)Creates a new header container with the specified headers.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description RocketHeadersaccept(String accept)Sets the Accept header.Map<String,String>asMap()Gets all headers as a Map.RocketHeadersbasicAuth(String username, String password)Sets the Authorization header with Basic authentication.RocketHeadersbearerAuth(String token)Sets the Authorization header with a Bearer token.booleancontains(String name)Checks if a header is present.RocketHeaderscontentType(String contentType)Sets the Content-Type header.static RocketHeadersdefaultJson()Creates a set of default headers with JSON content type.Stringget(String name)Gets a header value.RocketHeadersmerge(RocketHeaders other)Merges this header with another, with values from the other taking precedence.RocketHeadersremove(String name)Removes a header.RocketHeadersset(String name, String value)Sets a header value.
-
-
-
Method Detail
-
set
public RocketHeaders set(String name, String value)
Sets a header value.- Parameters:
name- The header namevalue- The header value- Returns:
- This HttpHeader instance for chaining
-
get
public String get(String name)
Gets a header value.- Parameters:
name- The header name- Returns:
- The header value or null if not present
-
remove
public RocketHeaders remove(String name)
Removes a header.- Parameters:
name- The header name- Returns:
- This HttpHeader instance for chaining
-
contains
public boolean contains(String name)
Checks if a header is present.- Parameters:
name- The header name- Returns:
- true if the header is present
-
contentType
public RocketHeaders contentType(String contentType)
Sets the Content-Type header.- Parameters:
contentType- The content type- Returns:
- This HttpHeader instance for chaining
-
accept
public RocketHeaders accept(String accept)
Sets the Accept header.- Parameters:
accept- The accept value- Returns:
- This HttpHeader instance for chaining
-
bearerAuth
public RocketHeaders bearerAuth(String token)
Sets the Authorization header with a Bearer token.- Parameters:
token- The token- Returns:
- This HttpHeader instance for chaining
-
basicAuth
public RocketHeaders basicAuth(String username, String password)
Sets the Authorization header with Basic authentication.- Parameters:
username- The usernamepassword- The password- Returns:
- This HttpHeader instance for chaining
-
asMap
public Map<String,String> asMap()
Gets all headers as a Map.- Returns:
- An unmodifiable view of the header map
-
merge
public RocketHeaders merge(RocketHeaders other)
Merges this header with another, with values from the other taking precedence.- Parameters:
other- The other HttpHeader instance- Returns:
- A new HttpHeader instance with merged values
-
defaultJson
public static RocketHeaders defaultJson()
Creates a set of default headers with JSON content type.- Returns:
- A new HttpHeader with JSON content type and accept headers
-
-