-
Notifications
You must be signed in to change notification settings - Fork 140
feat: Add HTTP caching headers to Agent Card endpoint #761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kabir
wants to merge
1
commit into
a2aproject:main
Choose a base branch
from
kabir:issue-749
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+343
−44
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
server-common/src/main/java/io/a2a/server/AgentCardCacheMetadata.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| package io.a2a.server; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.security.MessageDigest; | ||
| import java.security.NoSuchAlgorithmException; | ||
| import java.time.Instant; | ||
| import java.time.ZoneOffset; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.HashMap; | ||
| import java.util.HexFormat; | ||
| import java.util.Map; | ||
| import java.util.function.Consumer; | ||
|
|
||
| import jakarta.annotation.PostConstruct; | ||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.enterprise.inject.Instance; | ||
| import jakarta.inject.Inject; | ||
|
|
||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| import io.a2a.jsonrpc.common.json.JsonProcessingException; | ||
| import io.a2a.jsonrpc.common.json.JsonUtil; | ||
| import io.a2a.server.config.A2AConfigProvider; | ||
| import io.a2a.spec.AgentCard; | ||
|
|
||
| /** | ||
| * Provides HTTP caching metadata for Agent Card responses. | ||
| * | ||
| * <p>This bean computes and caches HTTP caching headers (Cache-Control, ETag, Last-Modified) | ||
| * for the Agent Card endpoint as specified in the A2A protocol specification section 8.6. | ||
| * | ||
| * <p>The metadata is computed once at initialization: | ||
| * <ul> | ||
| * <li><b>Cache-Control:</b> Configured via {@code a2a.agent-card.cache.max-age} (default: 3600 seconds)</li> | ||
| * <li><b>ETag:</b> MD5 hash of the serialized Agent Card JSON</li> | ||
| * <li><b>Last-Modified:</b> Timestamp when the bean was initialized (RFC 1123 format)</li> | ||
| * </ul> | ||
| * | ||
| * <p>Since the Agent Card is {@code @ApplicationScoped}, these values remain stable | ||
| * throughout the application lifecycle unless the application is restarted. | ||
| * | ||
| * @see <a href="https://github.com/a2aproject/A2A/blob/main/docs/specification.md#86-caching">A2A Specification - Agent Card Caching</a> | ||
| */ | ||
| @ApplicationScoped | ||
| public class AgentCardCacheMetadata { | ||
|
|
||
| private static final String CONFIG_KEY_MAX_AGE = "a2a.agent-card.cache.max-age"; | ||
| private static final String DEFAULT_MAX_AGE = "3600"; // 1 hour | ||
| private static final DateTimeFormatter RFC_1123_FORMATTER = DateTimeFormatter.RFC_1123_DATE_TIME; | ||
|
|
||
| @Inject | ||
| @PublicAgentCard | ||
| Instance<AgentCard> agentCardInstance; | ||
|
|
||
| @Inject | ||
| Instance<A2AConfigProvider> configInstance; | ||
|
|
||
| private @Nullable AgentCard agentCard; | ||
| private @Nullable A2AConfigProvider config; | ||
|
|
||
| @SuppressWarnings("NullAway") // Initialized in @PostConstruct when agentCard is available | ||
| private String etag; | ||
| @SuppressWarnings("NullAway") // Initialized in @PostConstruct when agentCard is available | ||
| private String lastModified; | ||
| @SuppressWarnings("NullAway") // Initialized in @PostConstruct when agentCard is available | ||
| private String cacheControl; | ||
|
|
||
| /** | ||
| * Package-private no-arg constructor for CDI. | ||
| */ | ||
| AgentCardCacheMetadata() { | ||
| // For CDI | ||
| } | ||
|
|
||
| /** | ||
| * Public constructor for testing purposes. | ||
| * | ||
| * @param agentCard the agent card | ||
| * @param config the configuration provider | ||
| */ | ||
| public AgentCardCacheMetadata(AgentCard agentCard, A2AConfigProvider config) { | ||
| this.agentCard = agentCard; | ||
| this.config = config; | ||
| init(); | ||
| } | ||
|
|
||
| @PostConstruct | ||
| @SuppressWarnings("NullAway") // agentCard and config are guaranteed non-null in both paths | ||
| void init() { | ||
| // Handle two initialization paths: | ||
| // 1. CDI injection: get beans from Instance if available | ||
| // 2. Direct constructor: agentCard and config already set | ||
|
|
||
| if (agentCard == null && agentCardInstance != null) { | ||
| // CDI path - only initialize if AgentCard bean is available | ||
| if (agentCardInstance.isUnsatisfied() || configInstance.isUnsatisfied()) { | ||
| return; | ||
| } | ||
| this.agentCard = agentCardInstance.get(); | ||
| this.config = configInstance.get(); | ||
| } | ||
|
|
||
| // At this point, agentCard and config should be set (either via CDI or constructor) | ||
| if (agentCard == null || config == null) { | ||
| return; | ||
| } | ||
|
|
||
| // Calculate ETag from the serialized JSON representation | ||
| this.etag = calculateETag(agentCard); | ||
|
|
||
| // Set Last-Modified to the initialization time | ||
| this.lastModified = RFC_1123_FORMATTER.format(Instant.now().atZone(ZoneOffset.UTC)); | ||
|
|
||
| // Configure Cache-Control with max-age directive | ||
| String maxAge = config.getOptionalValue(CONFIG_KEY_MAX_AGE).orElse(DEFAULT_MAX_AGE); | ||
| this.cacheControl = "public, max-age=" + maxAge; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the ETag header value for the Agent Card. | ||
| * | ||
| * <p>The ETag is an MD5 hash of the serialized Agent Card JSON, quoted per HTTP specification. | ||
| * | ||
| * @return the ETag header value (e.g., {@code "a1b2c3d4..."}) | ||
| */ | ||
| public String getETag() { | ||
| return etag; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the Last-Modified header value for the Agent Card. | ||
| * | ||
| * <p>The timestamp represents when the bean was initialized, in RFC 1123 format. | ||
| * | ||
| * @return the Last-Modified header value (e.g., {@code "Mon, 17 Mar 2025 10:00:00 GMT"}) | ||
| */ | ||
| public String getLastModified() { | ||
| return lastModified; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the Cache-Control header value for the Agent Card. | ||
| * | ||
| * <p>The value includes {@code public} and a {@code max-age} directive configured | ||
| * via {@code a2a.agent-card.cache.max-age} (default: 3600 seconds). | ||
| * | ||
| * @return the Cache-Control header value (e.g., {@code "public, max-age=3600"}) | ||
| */ | ||
| public String getCacheControl() { | ||
| return cacheControl; | ||
| } | ||
|
|
||
| /** | ||
| * Calculates an MD5 hash of the Agent Card JSON for use as an ETag. | ||
| * | ||
| * @param card the agent card to hash | ||
| * @return the hex-encoded MD5 hash, quoted per HTTP specification | ||
| */ | ||
| private String calculateETag(AgentCard card) { | ||
| try { | ||
| String json = JsonUtil.toJson(card); | ||
| MessageDigest md = MessageDigest.getInstance("MD5"); | ||
| byte[] hash = md.digest(json.getBytes(StandardCharsets.UTF_8)); | ||
| return "\"" + HexFormat.of().formatHex(hash) + "\""; | ||
| } catch (NoSuchAlgorithmException e) { | ||
| throw new IllegalStateException("MD5 algorithm not available", e); | ||
| } catch (JsonProcessingException e) { | ||
| throw new IllegalStateException("Failed to serialize Agent Card for ETag calculation", e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Populates a map with header names and header values stored in this instance. | ||
| * | ||
| * @return a map of the headers | ||
| */ | ||
| public Map<String, String> getHttpHeadersMap() { | ||
| Map<String, String> headers = new HashMap<>(); | ||
| if (cacheControl != null) { | ||
| headers.put("Cache-Control", cacheControl); | ||
| } | ||
| if (lastModified != null) { | ||
| headers.put("Last-Modified", lastModified); | ||
| } | ||
| if (etag != null) { | ||
| headers.put("ETag", etag); | ||
| } | ||
| return headers; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.