Skip to content

Commit 3eec065

Browse files
ehsavoiekabir
authored andcommitted
feat(http-client)!: unify configuration and redirect handling
- Configure provider selection, SSE limits, and redirect headers centrally. - Apply one-hop POST redirects consistently across HTTP implementations. Signed-off-by: Emmanuel Hugonnet <ehugonne@redhat.com>
1 parent f538765 commit 3eec065

43 files changed

Lines changed: 1898 additions & 430 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎client/base/src/main/java/org/a2aproject/sdk/A2A.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import org.a2aproject.sdk.client.http.A2ACardResolver;
88
import org.a2aproject.sdk.client.http.A2AHttpClient;
9+
import org.a2aproject.sdk.client.http.A2AHttpClientConfiguration;
910
import org.a2aproject.sdk.client.http.A2AHttpClientFactory;
1011
import org.a2aproject.sdk.spec.A2AClientError;
1112
import org.a2aproject.sdk.spec.A2AClientJSONError;
@@ -286,7 +287,7 @@ private static Message toMessage(List<Part<?>> parts, Message.Role role, String
286287
* @see AgentCard
287288
*/
288289
public static AgentCard getAgentCard(String agentUrl) throws A2AClientError, A2AClientJSONError {
289-
return getAgentCard(A2AHttpClientFactory.create(), agentUrl);
290+
return getAgentCard(A2AHttpClientFactory.create(A2AHttpClientConfiguration.DEFAULT), agentUrl);
290291
}
291292

292293
/**
@@ -357,7 +358,7 @@ public static AgentCard getAgentCard(A2AHttpClient httpClient, String agentUrl)
357358
* @throws org.a2aproject.sdk.spec.A2AClientJSONError if the response body cannot be decoded as JSON or validated against the AgentCard schema
358359
*/
359360
public static AgentCard getAgentCard(String agentUrl, String relativeCardPath, Map<String, String> authHeaders) throws A2AClientError, A2AClientJSONError {
360-
return getAgentCard(A2AHttpClientFactory.create(), agentUrl, relativeCardPath, authHeaders);
361+
return getAgentCard(A2AHttpClientFactory.create(A2AHttpClientConfiguration.DEFAULT), agentUrl, relativeCardPath, authHeaders);
361362
}
362363

363364
/**

‎client/transport/jsonrpc/src/main/java/org/a2aproject/sdk/client/transport/jsonrpc/JSONRPCTransport.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.protobuf.MessageOrBuilder;
2424

2525
import org.a2aproject.sdk.client.http.A2AHttpClient;
26+
import org.a2aproject.sdk.client.http.A2AHttpClientConfiguration;
2627
import org.a2aproject.sdk.common.A2AHeaders;
2728
import org.a2aproject.sdk.client.http.A2AHttpClientFactory;
2829
import org.a2aproject.sdk.client.http.A2AHttpResponse;
@@ -85,7 +86,7 @@ public JSONRPCTransport(AgentCard agentCard) {
8586

8687
public JSONRPCTransport(@Nullable A2AHttpClient httpClient, @Nullable AgentCard agentCard,
8788
AgentInterface agentInterface, @Nullable List<ClientCallInterceptor> interceptors) {
88-
this.httpClient = httpClient == null ? A2AHttpClientFactory.create() : httpClient;
89+
this.httpClient = httpClient == null ? A2AHttpClientFactory.create(A2AHttpClientConfiguration.DEFAULT) : httpClient;
8990
this.agentCard = agentCard;
9091
this.agentInterface = agentInterface;
9192
this.interceptors = interceptors;
@@ -378,4 +379,4 @@ private <T extends A2AResponse<?>> T unmarshalResponse(String response, String m
378379
private @Nullable Map<String, String> getHttpHeaders(@Nullable ClientCallContext context) {
379380
return context != null ? context.getHeaders() : null;
380381
}
381-
}
382+
}

‎client/transport/jsonrpc/src/main/java/org/a2aproject/sdk/client/transport/jsonrpc/JSONRPCTransportConfigBuilder.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.a2aproject.sdk.client.transport.jsonrpc;
22

33
import org.a2aproject.sdk.client.http.A2AHttpClient;
4+
import org.a2aproject.sdk.client.http.A2AHttpClientConfiguration;
45
import org.a2aproject.sdk.client.http.A2AHttpClientFactory;
56
import org.a2aproject.sdk.client.transport.spi.ClientTransportConfigBuilder;
67
import org.jspecify.annotations.Nullable;
@@ -65,6 +66,7 @@
6566
public class JSONRPCTransportConfigBuilder extends ClientTransportConfigBuilder<JSONRPCTransportConfig, JSONRPCTransportConfigBuilder> {
6667

6768
private @Nullable A2AHttpClient httpClient;
69+
private A2AHttpClientConfiguration httpClientConfiguration = A2AHttpClientConfiguration.DEFAULT;
6870

6971
/**
7072
* Set the HTTP client to use for JSON-RPC requests.
@@ -98,6 +100,13 @@ public JSONRPCTransportConfigBuilder httpClient(A2AHttpClient httpClient) {
98100
return this;
99101
}
100102

103+
/** Sets the configuration used when this builder creates the HTTP client. */
104+
public JSONRPCTransportConfigBuilder httpClientConfiguration(A2AHttpClientConfiguration httpClientConfiguration) {
105+
this.httpClientConfiguration = org.a2aproject.sdk.util.Assert.checkNotNullParam(
106+
"httpClientConfiguration", httpClientConfiguration);
107+
return this;
108+
}
109+
101110
/**
102111
* Build the JSON-RPC transport configuration.
103112
* <p>
@@ -110,7 +119,7 @@ public JSONRPCTransportConfigBuilder httpClient(A2AHttpClient httpClient) {
110119
public JSONRPCTransportConfig build() {
111120
// No HTTP client provided, use factory to get best available implementation
112121
if (httpClient == null) {
113-
httpClient = A2AHttpClientFactory.create();
122+
httpClient = A2AHttpClientFactory.create(httpClientConfiguration);
114123
}
115124

116125
JSONRPCTransportConfig config = new JSONRPCTransportConfig(httpClient);

‎client/transport/jsonrpc/src/main/java/org/a2aproject/sdk/client/transport/jsonrpc/JSONRPCTransportProvider.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.a2aproject.sdk.client.transport.jsonrpc;
22

33
import org.a2aproject.sdk.client.http.A2AHttpClientFactory;
4+
import org.a2aproject.sdk.client.http.A2AHttpClientConfiguration;
45
import org.a2aproject.sdk.client.transport.spi.ClientTransportProvider;
56
import org.a2aproject.sdk.spec.A2AClientException;
67
import org.a2aproject.sdk.spec.AgentCard;
@@ -14,7 +15,8 @@ public class JSONRPCTransportProvider implements ClientTransportProvider<JSONRPC
1415
public JSONRPCTransport create(@Nullable JSONRPCTransportConfig clientTransportConfig, AgentCard agentCard, AgentInterface agentInterface) throws A2AClientException {
1516
JSONRPCTransportConfig currentClientTransportConfig = clientTransportConfig;
1617
if (currentClientTransportConfig == null) {
17-
currentClientTransportConfig = new JSONRPCTransportConfig(A2AHttpClientFactory.create());
18+
currentClientTransportConfig = new JSONRPCTransportConfig(
19+
A2AHttpClientFactory.create(A2AHttpClientConfiguration.DEFAULT));
1820
}
1921
return new JSONRPCTransport(currentClientTransportConfig.getHttpClient(), agentCard, agentInterface, currentClientTransportConfig.getInterceptors());
2022
}

‎client/transport/rest/src/main/java/org/a2aproject/sdk/client/transport/rest/RestTransport.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.google.protobuf.MessageOrBuilder;
3030
import com.google.protobuf.util.JsonFormat;
3131
import org.a2aproject.sdk.client.http.A2AHttpClient;
32+
import org.a2aproject.sdk.client.http.A2AHttpClientConfiguration;
3233
import org.a2aproject.sdk.client.http.A2AHttpClientFactory;
3334
import org.a2aproject.sdk.client.http.A2AHttpResponse;
3435
import org.a2aproject.sdk.common.A2AHeaders;
@@ -77,7 +78,7 @@ public RestTransport(AgentCard agentCard) {
7778

7879
public RestTransport(@Nullable A2AHttpClient httpClient, AgentCard agentCard,
7980
AgentInterface agentInterface, @Nullable List<ClientCallInterceptor> interceptors) {
80-
this.httpClient = httpClient == null ? A2AHttpClientFactory.create() : httpClient;
81+
this.httpClient = httpClient == null ? A2AHttpClientFactory.create(A2AHttpClientConfiguration.DEFAULT) : httpClient;
8182
this.agentCard = agentCard;
8283
this.agentInterface = agentInterface;
8384
this.interceptors = interceptors;

‎client/transport/rest/src/main/java/org/a2aproject/sdk/client/transport/rest/RestTransportConfigBuilder.java‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.a2aproject.sdk.client.transport.rest;
22

33
import org.a2aproject.sdk.client.http.A2AHttpClient;
4+
import org.a2aproject.sdk.client.http.A2AHttpClientConfiguration;
45
import org.a2aproject.sdk.client.http.A2AHttpClientFactory;
56
import org.a2aproject.sdk.client.transport.spi.ClientTransportConfigBuilder;
67
import org.jspecify.annotations.Nullable;
@@ -65,6 +66,7 @@
6566
public class RestTransportConfigBuilder extends ClientTransportConfigBuilder<RestTransportConfig, RestTransportConfigBuilder> {
6667

6768
private @Nullable A2AHttpClient httpClient;
69+
private A2AHttpClientConfiguration httpClientConfiguration = A2AHttpClientConfiguration.DEFAULT;
6870

6971
/**
7072
* Set the HTTP client to use for REST requests.
@@ -98,6 +100,13 @@ public RestTransportConfigBuilder httpClient(A2AHttpClient httpClient) {
98100
return this;
99101
}
100102

103+
/** Sets the configuration used when this builder creates the HTTP client. */
104+
public RestTransportConfigBuilder httpClientConfiguration(A2AHttpClientConfiguration httpClientConfiguration) {
105+
this.httpClientConfiguration = org.a2aproject.sdk.util.Assert.checkNotNullParam(
106+
"httpClientConfiguration", httpClientConfiguration);
107+
return this;
108+
}
109+
101110
/**
102111
* Build the REST transport configuration.
103112
* <p>
@@ -110,11 +119,11 @@ public RestTransportConfigBuilder httpClient(A2AHttpClient httpClient) {
110119
public RestTransportConfig build() {
111120
// No HTTP client provided, use factory to get best available implementation
112121
if (httpClient == null) {
113-
httpClient = A2AHttpClientFactory.create();
122+
httpClient = A2AHttpClientFactory.create(httpClientConfiguration);
114123
}
115124

116125
RestTransportConfig config = new RestTransportConfig(httpClient);
117126
config.setInterceptors(this.interceptors);
118127
return config;
119128
}
120-
}
129+
}

‎client/transport/rest/src/main/java/org/a2aproject/sdk/client/transport/rest/RestTransportProvider.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.a2aproject.sdk.client.transport.rest;
22

33
import org.a2aproject.sdk.client.http.A2AHttpClientFactory;
4+
import org.a2aproject.sdk.client.http.A2AHttpClientConfiguration;
45
import org.a2aproject.sdk.client.transport.spi.ClientTransportProvider;
56
import org.a2aproject.sdk.spec.A2AClientException;
67
import org.a2aproject.sdk.spec.AgentCard;
@@ -18,7 +19,7 @@ public String getTransportProtocol() {
1819
public RestTransport create(RestTransportConfig clientTransportConfig, AgentCard agentCard, AgentInterface agentInterface) throws A2AClientException {
1920
RestTransportConfig transportConfig = clientTransportConfig;
2021
if (transportConfig == null) {
21-
transportConfig = new RestTransportConfig(A2AHttpClientFactory.create());
22+
transportConfig = new RestTransportConfig(A2AHttpClientFactory.create(A2AHttpClientConfiguration.DEFAULT));
2223
}
2324
return new RestTransport(transportConfig.getHttpClient(), agentCard, agentInterface, transportConfig.getInterceptors());
2425
}

‎docs/content/dev/client.md‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,9 @@ client.deleteTaskPushNotificationConfigurations(
138138
### JSON-RPC with a Custom HTTP Client
139139

140140
```java
141-
// Use a custom JDK HTTP client
141+
// Native redirects must remain disabled when using followRedirects(true), so the SDK can apply RedirectHeaderPolicy.
142142
HttpClient jdkHttpClient = HttpClient.newBuilder()
143143
.connectTimeout(Duration.ofSeconds(5))
144-
.followRedirects(HttpClient.Redirect.NORMAL)
145144
.build();
146145

147146
Client client = Client

‎docs/content/dev/extra/http-clients.md‎

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ For non-Quarkus environments, also add `vertx-web-client`:
5555

5656
The `VertxA2AHttpClientProvider` has **priority 100**. The SDK's `A2AHttpClientFactory` discovers all registered providers via `ServiceLoader`, sorts them by descending priority, and tries each in order, falling back to the next one on failure.
5757

58-
To force a specific provider by name (useful in tests), use `A2AHttpClientFactory.create(String providerName)`:
58+
Use `A2AHttpClientFactory.create(A2AHttpClientConfiguration)` for both the default client and
59+
custom provider selection. To force the JDK provider, exclude the other built-in providers:
5960

6061
```java
6162
// Force the JDK client regardless of what else is on the classpath
62-
try (A2AHttpClient client = A2AHttpClientFactory.create("jdk")) {
63+
try (A2AHttpClient client = A2AHttpClientFactory.create(
64+
A2AHttpClientConfiguration.builder()
65+
.excludedProviderNames(List.of("cdi", "android", "vertx"))
66+
.build())) {
6367
// ...
6468
}
6569
```
@@ -79,12 +83,53 @@ Client client = Client.builder(card)
7983

8084
If Vert.x classes are not on the classpath, the provider returns priority `-1` and the SDK falls back to the next available provider.
8185

86+
## Configuring an A2A HTTP Client
87+
88+
Use `A2AHttpClientConfiguration` to select providers and configure optional SSE parsing and
89+
redirect-header settings in one place. Providers that do not support a requested setting are
90+
skipped, so configuration is never silently ignored.
91+
92+
```java
93+
import java.net.URI;
94+
import java.util.List;
95+
import java.util.Set;
96+
97+
import org.a2aproject.sdk.client.http.A2AHttpClient;
98+
import org.a2aproject.sdk.client.http.A2AHttpClientConfiguration;
99+
import org.a2aproject.sdk.client.http.A2AHttpClientFactory;
100+
import org.a2aproject.sdk.client.http.RedirectHeaderPolicy;
101+
import org.a2aproject.sdk.client.http.SSEParserConfig;
102+
103+
SSEParserConfig sseConfig = SSEParserConfig.builder()
104+
.maxLineLength(64 * 1024)
105+
.build();
106+
107+
RedirectHeaderPolicy redirectPolicy = new RedirectHeaderPolicy(
108+
Set.of("X-Trusted-Header"),
109+
Set.of(URI.create("https://trusted-agent.example")));
110+
111+
A2AHttpClientConfiguration configuration = A2AHttpClientConfiguration.builder()
112+
.excludedProviderNames(List.of("cdi"))
113+
.sseParserConfig(sseConfig)
114+
.redirectHeaderPolicy(redirectPolicy)
115+
.build();
116+
117+
A2AHttpClient client = A2AHttpClientFactory.create(configuration);
118+
```
119+
120+
The additional header is sent only when the redirect target has the trusted origin's scheme,
121+
host, and port. Other cross-origin redirects receive only the default safe headers. Configure
122+
trusted origins with HTTPS when forwarding credentials or other sensitive headers.
123+
124+
For default provider selection without extra settings, use
125+
`A2AHttpClientFactory.create(A2AHttpClientConfiguration.DEFAULT)`.
126+
82127
### Usage Examples
83128

84129
#### GET Request
85130

86131
```java
87-
try (A2AHttpClient client = A2AHttpClientFactory.create()) {
132+
try (A2AHttpClient client = A2AHttpClientFactory.create(A2AHttpClientConfiguration.DEFAULT)) {
88133
A2AHttpResponse response = client.createGet()
89134
.url("https://api.example.com/data")
90135
.addHeader("Authorization", "Bearer token")
@@ -99,7 +144,7 @@ try (A2AHttpClient client = A2AHttpClientFactory.create()) {
99144
#### POST Request with JSON Body
100145

101146
```java
102-
try (A2AHttpClient client = A2AHttpClientFactory.create()) {
147+
try (A2AHttpClient client = A2AHttpClientFactory.create(A2AHttpClientConfiguration.DEFAULT)) {
103148
A2AHttpResponse response = client.createPost()
104149
.url("https://api.example.com/submit")
105150
.addHeader("Content-Type", "application/json")
@@ -113,7 +158,7 @@ try (A2AHttpClient client = A2AHttpClientFactory.create()) {
113158
#### Server-Sent Events (SSE) Streaming
114159

115160
```java
116-
try (A2AHttpClient client = A2AHttpClientFactory.create()) {
161+
try (A2AHttpClient client = A2AHttpClientFactory.create(A2AHttpClientConfiguration.DEFAULT)) {
117162
CompletableFuture<Void> future = client.createGet()
118163
.url("https://api.example.com/stream")
119164
.getAsyncSSE(

‎docs/content/dev/security.md‎

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,58 @@ Additionally, API keys are only injected when the security scheme explicitly spe
4343

4444
## HTTP Redirect Handling
4545

46-
SDK-managed HTTP clients do not follow redirects automatically by default. This prevents credentials from being inadvertently forwarded to third-party origins during redirect chains.
46+
SDK-managed HTTP clients do not follow redirects automatically by default. When explicitly enabled
47+
on a synchronous POST builder with `followRedirects(true)`, they follow at most one HTTP(S)
48+
redirect and apply the shared `RedirectHeaderPolicy`. JDK and Vert.x clients follow cross-origin
49+
301, 302, and 303 redirects as GET requests with only the default safe headers, including
50+
`A2A-Version` and `A2A-Extensions`, unless additional headers are configured for that exact target
51+
origin. No client follows a cross-origin 307 or 308 redirect, so the original POST body is never
52+
forwarded to another origin. HTTPS-to-HTTP redirects are not followed; the client returns the
53+
original redirect response. Redirect following does not apply to
54+
`postAsyncSSE()`.
4755

4856
### JDK HTTP Client
4957

50-
The default `JdkA2AHttpClient` is configured with `HttpClient.Redirect.NEVER`. If your application requires redirect following, provide a custom `HttpClient` instance:
58+
The default `JdkA2AHttpClient` uses `HttpClient.Redirect.NEVER`. Native JDK redirect following
59+
must remain disabled so the SDK can apply its redirect policy. Enable the SDK-managed one-hop
60+
redirect on an individual synchronous `post()` request:
5161

5262
```java
53-
HttpClient customClient = HttpClient.newBuilder()
54-
.followRedirects(HttpClient.Redirect.NORMAL)
55-
.build();
56-
57-
JdkA2AHttpClient client = new JdkA2AHttpClient(customClient);
63+
JdkA2AHttpClient client = new JdkA2AHttpClient();
64+
client.createPost()
65+
.url("https://agent.example/rpc")
66+
.followRedirects(true)
67+
.body("{}")
68+
.post();
5869
```
5970

71+
When supplying your own `HttpClient`, configure `HttpClient.Redirect.NEVER` before using
72+
`followRedirects(true)`. The SDK rejects native redirect policies for that request with an
73+
`IOException`; native handling would bypass the SDK redirect policy.
74+
6075
### Vert.x HTTP Client
6176

62-
The default `VertxA2AHttpClient` is configured with `setFollowRedirects(false)`. If redirect following is needed, create a custom `WebClient` with the desired policy and pass the underlying `Vertx` instance to the constructor.
77+
The default `VertxA2AHttpClient` is configured with `setFollowRedirects(false)`. Use
78+
`followRedirects(true)` on a synchronous `post()` builder to enable the same SDK-managed one-hop
79+
redirect policy. `postAsyncSSE()` does not follow redirects.
6380

6481
### Android HTTP Client
6582

66-
The `AndroidA2AHttpClient` disables automatic redirect following by calling `setInstanceFollowRedirects(false)` on all `HttpURLConnection` instances. Applications requiring redirect handling must implement it manually.
83+
The `AndroidA2AHttpClient` disables automatic redirect following by calling
84+
`setInstanceFollowRedirects(false)` on all `HttpURLConnection` instances. Use
85+
`followRedirects(true)` on a synchronous `post()` builder to enable same-origin one-hop
86+
redirects. Android always returns the original redirect response for a cross-origin target:
87+
`HttpURLConnection` cannot isolate process-wide `CookieHandler` or `Authenticator` state for an
88+
individual follow-up request, and those globals can change concurrently.
89+
`postAsyncSSE()` does not follow redirects.
6790

6891
## Best Practices
6992

7093
1. **Use Standard Authentication Headers**: When implementing custom agents, use standard authentication header names from the safe allowlist to ensure credentials are properly injected.
7194

7295
2. **Validate Agent Cards**: Before connecting to an agent, review its security schemes to ensure they use appropriate authentication methods and header names.
7396

74-
3. **Handle Redirects Carefully**: If your application requires redirect following, implement it with caution and ensure credentials are not forwarded to untrusted origins.
97+
3. **Handle Redirects Carefully**: Enable SDK-managed redirects only when needed. A custom `RedirectHeaderPolicy` requires explicit trusted target origins before forwarding additional headers; use HTTPS origins for credentials.
7598

7699
4. **Keep Dependencies Updated**: Regularly update the SDK and its dependencies to receive the latest security patches.
77100

0 commit comments

Comments
 (0)