CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-seleniumhq-selenium--selenium-http

HTTP client and server abstractions for Selenium WebDriver communication

Pending
Overview
Eval results
Files

http-client.mddocs/

HTTP Client

Core HTTP client functionality for making synchronous and asynchronous requests with full configuration support, connection pooling, and WebSocket capabilities.

Capabilities

HttpClient Interface

Main interface for making HTTP requests with support for both synchronous and asynchronous execution.

/**
 * Main interface for making HTTP requests
 * Extends Closeable for resource management and HttpHandler for request execution
 */
public interface HttpClient extends Closeable, HttpHandler {
    /**
     * Opens a WebSocket connection using the provided request and listener
     * @param request HTTP request for WebSocket handshake
     * @param listener WebSocket event listener for handling messages
     * @return WebSocket instance for sending messages
     */
    WebSocket openSocket(HttpRequest request, WebSocket.Listener listener);
    
    /**
     * Executes HTTP request asynchronously
     * @param req HTTP request to execute
     * @return CompletableFuture containing the HTTP response
     */
    default CompletableFuture<HttpResponse> executeAsync(HttpRequest req);
    
    /**
     * Closes the HTTP client and releases resources
     * Default implementation does nothing
     */
    default void close();
}

Usage Examples:

import org.openqa.selenium.remote.http.*;
import java.net.URL;
import java.util.concurrent.CompletableFuture;

// Create client with default factory
HttpClient client = HttpClient.Factory.createDefault()
    .createClient(new URL("https://api.example.com"));

// Synchronous request
HttpRequest request = new HttpRequest(HttpMethod.GET, "/users/123");
request.addHeader("Accept", "application/json");
HttpResponse response = client.execute(request);

// Asynchronous request
CompletableFuture<HttpResponse> future = client.executeAsync(request);
future.thenAccept(resp -> {
    System.out.println("Status: " + resp.getStatus());
    System.out.println("Body: " + Contents.string(resp));
});

// WebSocket connection
WebSocket socket = client.openSocket(
    new HttpRequest(HttpMethod.GET, "/websocket"),
    new WebSocket.Listener() {
        @Override
        public void onText(CharSequence data) {
            System.out.println("Received: " + data);
        }
    }
);

socket.sendText("Hello WebSocket");
socket.close();
client.close();

HttpClient Factory

Factory interface for creating HTTP client instances with pluggable implementations using Java's ServiceLoader mechanism.

/**
 * Factory interface for creating HttpClient instances
 * Uses ServiceLoader mechanism for pluggable implementations
 */
public interface Factory {
    /**
     * Creates factory instance by name using ServiceLoader
     * Looks for implementations with @HttpClientName annotation
     * @param name Name of the HTTP client implementation
     * @return Factory instance for the specified implementation
     * @throws IllegalArgumentException if no implementation found
     * @throws IllegalStateException if multiple implementations found
     */
    static Factory create(String name);
    
    /**
     * Creates default factory using system property 'webdriver.http.factory'
     * Defaults to 'jdk-http-client' if property not set
     * @return Default factory instance
     */
    static Factory createDefault();
    
    /**
     * Creates HTTP client with base URL using default configuration
     * @param url Base URL for all requests
     * @return Configured HttpClient instance
     */
    default HttpClient createClient(URL url);
    
    /**
     * Creates HTTP client with custom configuration
     * @param config Client configuration with timeouts, filters, etc.
     * @return Configured HttpClient instance
     */
    HttpClient createClient(ClientConfig config);
    
    /**
     * Closes idle client connections
     * Default implementation does nothing
     */
    default void cleanupIdleClients();
}

Usage Examples:

import org.openqa.selenium.remote.http.*;
import java.net.URL;

// Create specific HTTP client implementation
HttpClient.Factory factory = HttpClient.Factory.create("jdk-http-client");
HttpClient client = factory.createClient(new URL("https://api.example.com"));

// Create client with default factory (uses system property)
HttpClient defaultClient = HttpClient.Factory.createDefault()
    .createClient(new URL("https://api.example.com"));

// Create client with custom configuration
ClientConfig config = ClientConfig.defaultConfig()
    .baseUrl(new URL("https://api.example.com"))
    .connectionTimeout(Duration.ofSeconds(30))
    .readTimeout(Duration.ofMinutes(5))
    .withRetries();

HttpClient configuredClient = HttpClient.Factory.createDefault()
    .createClient(config);

// Cleanup idle connections
factory.cleanupIdleClients();

JDK HTTP Client Implementation

Production-ready HTTP client implementation using JDK's built-in HTTP client with full WebSocket support.

/**
 * JDK-based HTTP client implementation
 * Uses Java's built-in HTTP client for requests and WebSocket connections
 */
public class JdkHttpClient implements HttpClient {
    public static final Logger LOG;
    
    /**
     * Opens WebSocket connection using JDK WebSocket client
     * @param request HTTP request for WebSocket handshake
     * @param listener WebSocket event listener
     * @return WebSocket instance for communication
     */
    public WebSocket openSocket(HttpRequest request, WebSocket.Listener listener);
    
    /**
     * Executes HTTP request asynchronously using JDK HTTP client
     * @param request HTTP request to execute
     * @return CompletableFuture with HTTP response
     */
    public CompletableFuture<HttpResponse> executeAsync(HttpRequest request);
    
    /**
     * Executes HTTP request synchronously
     * @param req HTTP request to execute
     * @return HTTP response
     */
    public HttpResponse execute(HttpRequest req);
    
    /**
     * Closes HTTP client and releases resources
     */
    public void close();
    
    /**
     * Factory for creating JDK HTTP client instances
     * Annotated with @HttpClientName("jdk-http-client")
     */
    @AutoService(HttpClient.Factory.class)
    @HttpClientName("jdk-http-client")
    public static class Factory implements HttpClient.Factory {
        /**
         * Creates JDK HTTP client with specified configuration
         * @param config Client configuration
         * @return JdkHttpClient instance
         */
        public HttpClient createClient(ClientConfig config);
    }
}

Usage Examples:

import org.openqa.selenium.remote.http.*;
import org.openqa.selenium.remote.http.jdk.JdkHttpClient;
import java.net.URL;

// Create JDK HTTP client directly
ClientConfig config = ClientConfig.defaultConfig()
    .baseUrl(new URL("https://api.example.com"))
    .connectionTimeout(Duration.ofSeconds(30));

JdkHttpClient.Factory factory = new JdkHttpClient.Factory();
HttpClient client = factory.createClient(config);

// Use via factory system
HttpClient.Factory defaultFactory = HttpClient.Factory.create("jdk-http-client");
HttpClient jdkClient = defaultFactory.createClient(config);

// Execute requests
HttpRequest request = new HttpRequest(HttpMethod.POST, "/data");
request.setContent(Contents.asJson(Map.of("key", "value")));
HttpResponse response = client.execute(request);

System.out.println("Response status: " + response.getStatus());
System.out.println("Response body: " + Contents.string(response));

Service Loader Configuration

To create custom HTTP client implementations, implement the HttpClient.Factory interface and annotate with @HttpClientName:

@AutoService(HttpClient.Factory.class)
@HttpClientName("my-custom-client")
public class MyCustomHttpClientFactory implements HttpClient.Factory {
    @Override
    public HttpClient createClient(ClientConfig config) {
        return new MyCustomHttpClient(config);
    }
}

The implementation will be automatically discovered by the ServiceLoader mechanism and can be used via:

HttpClient.Factory factory = HttpClient.Factory.create("my-custom-client");

Install with Tessl CLI

npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-http

docs

client-config.md

content-handling.md

filtering.md

http-client.md

index.md

request-response.md

routing.md

websocket.md

tile.json