CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-glassfish-jersey-core--jersey-client

Jersey core client implementation for building RESTful web service clients using JAX-RS API

Pending
Overview
Eval results
Files

authentication.mddocs/

Authentication

HTTP authentication support for Jersey Client including Basic authentication, Digest authentication, and Universal authentication modes with both preemptive and non-preemptive options. This functionality provides comprehensive authentication capabilities for securing HTTP requests.

Capabilities

HTTP Authentication Feature

Main authentication feature providing Basic, Digest, and Universal authentication modes with configuration options for preemptive authentication and credential management.

public class HttpAuthenticationFeature implements Feature {
    // Property keys for authentication configuration
    public static final String HTTP_AUTHENTICATION_USERNAME = "jersey.config.client.http.auth.username";
    public static final String HTTP_AUTHENTICATION_PASSWORD = "jersey.config.client.http.auth.password";
    public static final String HTTP_AUTHENTICATION_BASIC_USERNAME = "jersey.config.client.http.auth.basic.username";
    public static final String HTTP_AUTHENTICATION_BASIC_PASSWORD = "jersey.config.client.http.auth.basic.password";
    public static final String HTTP_AUTHENTICATION_DIGEST_USERNAME = "jersey.config.client.http.auth.digest.username";
    public static final String HTTP_AUTHENTICATION_DIGEST_PASSWORD = "jersey.config.client.http.auth.digest.password";
    
    /**
     * Configure the authentication feature with the client
     * @param context feature context for configuration
     * @return true if feature was successfully configured
     */
    boolean configure(FeatureContext context);
}

Basic Authentication

HTTP Basic authentication support with preemptive and non-preemptive modes for username/password authentication.

public class HttpAuthenticationFeature implements Feature {
    /**
     * Create builder for Basic authentication configuration
     * @return Basic authentication builder
     */
    static BasicBuilder basicBuilder();
    
    /**
     * Create Basic authentication feature with credentials (preemptive by default)
     * @param username authentication username
     * @param password authentication password
     * @return configured Basic authentication feature
     */
    static HttpAuthenticationFeature basic(String username, String password);
    
    /**
     * Create Basic authentication feature with byte array password
     * @param username authentication username
     * @param password authentication password as byte array
     * @return configured Basic authentication feature
     */
    static HttpAuthenticationFeature basic(String username, byte[] password);
}

// Builder interface for Basic authentication
interface BasicBuilder extends Builder {
    /**
     * Configure non-preemptive Basic authentication (only send credentials after 401 challenge)
     * @return this builder instance
     */
    BasicBuilder nonPreemptive();
}

// Base builder interface
interface Builder {
    /**
     * Set authentication credentials
     * @param username authentication username
     * @param password authentication password
     * @return this builder instance
     */
    Builder credentials(String username, String password);
    
    /**
     * Set authentication credentials with byte array password
     * @param username authentication username
     * @param password authentication password as byte array
     * @return this builder instance
     */
    Builder credentials(String username, byte[] password);
    
    /**
     * Build the authentication feature
     * @return configured authentication feature
     */
    HttpAuthenticationFeature build();
}

Usage Examples:

import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

// Preemptive Basic authentication (default)
HttpAuthenticationFeature basicAuth = HttpAuthenticationFeature.basic("username", "password");
Client client = JerseyClientBuilder.createClient()
    .register(basicAuth);

// Non-preemptive Basic authentication (wait for 401 challenge)
HttpAuthenticationFeature nonPreemptiveBasic = HttpAuthenticationFeature.basicBuilder()
    .credentials("username", "password")
    .nonPreemptive()
    .build();

Client client2 = JerseyClientBuilder.createClient()
    .register(nonPreemptiveBasic);

// Using byte array for password (more secure)
byte[] passwordBytes = "password".getBytes(StandardCharsets.UTF_8);
HttpAuthenticationFeature secureBasic = HttpAuthenticationFeature.basic("username", passwordBytes);

Digest Authentication

HTTP Digest authentication support for more secure authentication compared to Basic authentication, using challenge-response mechanism.

public class HttpAuthenticationFeature implements Feature {
    /**
     * Create Digest authentication feature (no credentials, waits for challenge)
     * @return configured Digest authentication feature
     */
    static HttpAuthenticationFeature digest();
    
    /**
     * Create Digest authentication feature with credentials
     * @param username authentication username
     * @param password authentication password
     * @return configured Digest authentication feature
     */
    static HttpAuthenticationFeature digest(String username, String password);
    
    /**
     * Create Digest authentication feature with byte array password
     * @param username authentication username
     * @param password authentication password as byte array
     * @return configured Digest authentication feature
     */
    static HttpAuthenticationFeature digest(String username, byte[] password);
}

Usage Examples:

// Digest authentication with credentials
HttpAuthenticationFeature digestAuth = HttpAuthenticationFeature.digest("username", "password");
Client client = JerseyClientBuilder.createClient()
    .register(digestAuth);

// Digest authentication waiting for challenge (credentials provided later)
HttpAuthenticationFeature digestChallenge = HttpAuthenticationFeature.digest();
Client client2 = JerseyClientBuilder.createClient()
    .register(digestChallenge);

// Using configuration properties for credentials
ClientConfig config = new ClientConfig();
config.property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_DIGEST_USERNAME, "username");
config.property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_DIGEST_PASSWORD, "password");
config.register(HttpAuthenticationFeature.digest());
Client client3 = JerseyClientBuilder.createClient(config);

Universal Authentication

Universal authentication feature supporting both Basic and Digest authentication modes, automatically selecting the appropriate method based on server challenges.

public class HttpAuthenticationFeature implements Feature {
    /**
     * Create builder for Universal authentication configuration
     * @return Universal authentication builder
     */
    static UniversalBuilder universalBuilder();
    
    /**
     * Create Universal authentication feature with credentials for both Basic and Digest
     * @param username authentication username
     * @param password authentication password
     * @return configured Universal authentication feature
     */
    static HttpAuthenticationFeature universal(String username, String password);
    
    /**
     * Create Universal authentication feature with byte array password
     * @param username authentication username
     * @param password authentication password as byte array
     * @return configured Universal authentication feature
     */
    static HttpAuthenticationFeature universal(String username, byte[] password);
}

// Builder interface for Universal authentication
interface UniversalBuilder extends Builder {
    /**
     * Set credentials specifically for Basic authentication
     * @param username Basic authentication username
     * @param password Basic authentication password
     * @return this builder instance
     */
    UniversalBuilder credentialsForBasic(String username, String password);
    
    /**
     * Set credentials specifically for Basic authentication with byte array password
     * @param username Basic authentication username
     * @param password Basic authentication password as byte array
     * @return this builder instance
     */
    UniversalBuilder credentialsForBasic(String username, byte[] password);
    
    /**
     * Set credentials specifically for Digest authentication
     * @param username Digest authentication username
     * @param password Digest authentication password
     * @return this builder instance
     */
    UniversalBuilder credentialsForDigest(String username, String password);
    
    /**
     * Set credentials specifically for Digest authentication with byte array password
     * @param username Digest authentication username
     * @param password Digest authentication password as byte array
     * @return this builder instance
     */
    UniversalBuilder credentialsForDigest(String username, byte[] password);
}

Usage Examples:

// Universal authentication with same credentials for both Basic and Digest
HttpAuthenticationFeature universalAuth = HttpAuthenticationFeature.universal("username", "password");
Client client = JerseyClientBuilder.createClient()
    .register(universalAuth);

// Universal authentication with different credentials for Basic and Digest
HttpAuthenticationFeature customUniversal = HttpAuthenticationFeature.universalBuilder()
    .credentialsForBasic("basicUser", "basicPass")
    .credentialsForDigest("digestUser", "digestPass")
    .build();

Client client2 = JerseyClientBuilder.createClient()
    .register(customUniversal);

// Universal authentication with mixed credential types
HttpAuthenticationFeature mixedAuth = HttpAuthenticationFeature.universalBuilder()
    .credentialsForBasic("username", "password")
    .credentialsForDigest("username", "different-password".getBytes())
    .build();

Authentication Exceptions

Exception classes for handling authentication-related errors during request processing and response handling.

/**
 * Exception thrown during authentication request processing
 */
public class RequestAuthenticationException extends ProcessingException {
    /**
     * Create exception with cause
     * @param cause underlying cause of authentication failure
     */
    public RequestAuthenticationException(Throwable cause);
    
    /**
     * Create exception with message
     * @param message descriptive error message
     */
    public RequestAuthenticationException(String message);
    
    /**
     * Create exception with message and cause
     * @param message descriptive error message
     * @param cause underlying cause of authentication failure
     */
    public RequestAuthenticationException(String message, Throwable cause);
}

/**
 * Exception thrown during authentication response processing
 */
public class ResponseAuthenticationException extends ResponseProcessingException {
    /**
     * Create exception with response and cause
     * @param response HTTP response that caused the exception
     * @param cause underlying cause of authentication failure
     */
    public ResponseAuthenticationException(Response response, Throwable cause);
    
    /**
     * Create exception with response and message
     * @param response HTTP response that caused the exception
     * @param message descriptive error message
     */
    public ResponseAuthenticationException(Response response, String message);
    
    /**
     * Create exception with response, message, and cause
     * @param response HTTP response that caused the exception
     * @param message descriptive error message
     * @param cause underlying cause of authentication failure
     */
    public ResponseAuthenticationException(Response response, String message, Throwable cause);
}

Usage Examples:

try {
    Response response = target.request().get();
} catch (RequestAuthenticationException e) {
    System.err.println("Authentication request failed: " + e.getMessage());
    // Handle request-side authentication error
} catch (ResponseAuthenticationException e) {
    System.err.println("Authentication response failed: " + e.getMessage());
    Response failedResponse = e.getResponse();
    int status = failedResponse.getStatus(); // Likely 401 or 403
    // Handle response-side authentication error
}

Configuration Properties

Authentication configuration properties for setting credentials and authentication options through client configuration.

// Example of using configuration properties for authentication
ClientConfig config = new ClientConfig();

// Global authentication credentials
config.property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_USERNAME, "globalUser");
config.property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_PASSWORD, "globalPass");

// Basic authentication specific credentials
config.property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_BASIC_USERNAME, "basicUser");
config.property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_BASIC_PASSWORD, "basicPass");

// Digest authentication specific credentials
config.property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_DIGEST_USERNAME, "digestUser");
config.property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_DIGEST_PASSWORD, "digestPass");

// Register authentication feature to use configured properties
config.register(HttpAuthenticationFeature.universal());

Client client = JerseyClientBuilder.createClient(config);

Advanced Authentication Scenarios

Complex authentication scenarios combining multiple authentication types and configuration options.

Usage Examples:

// Per-request authentication override
Client client = JerseyClientBuilder.createClient();

// Different authentication for different endpoints
HttpAuthenticationFeature adminAuth = HttpAuthenticationFeature.basic("admin", "admin-password");
HttpAuthenticationFeature userAuth = HttpAuthenticationFeature.digest("user", "user-password");

// Register different auth for different targets
WebTarget adminTarget = client.target("https://api.example.com/admin")
    .register(adminAuth);

WebTarget userTarget = client.target("https://api.example.com/user")
    .register(userAuth);

// Mix authentication with other features
Client fullClient = JerseyClientBuilder.createClient()
    .register(HttpAuthenticationFeature.basic("username", "password"))
    .register(new LoggingFeature())
    .property(ClientProperties.FOLLOW_REDIRECTS, true);

// Programmatic credential management
String username = getUsernameFromSecureStorage();
byte[] password = getPasswordFromSecureStorage();
HttpAuthenticationFeature secureAuth = HttpAuthenticationFeature.basic(username, password);

// Clear sensitive data
Arrays.fill(password, (byte) 0);

Client secureClient = JerseyClientBuilder.createClient()
    .register(secureAuth);

Install with Tessl CLI

npx tessl i tessl/maven-org-glassfish-jersey-core--jersey-client

docs

authentication.md

client-config.md

connectors-spi.md

filters-features.md

index.md

request-execution.md

tile.json