|
| 1 | +package io.a2a.server; |
| 2 | + |
| 3 | +import java.nio.charset.StandardCharsets; |
| 4 | +import java.security.MessageDigest; |
| 5 | +import java.security.NoSuchAlgorithmException; |
| 6 | +import java.time.Instant; |
| 7 | +import java.time.ZoneOffset; |
| 8 | +import java.time.format.DateTimeFormatter; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.HexFormat; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.function.Consumer; |
| 13 | + |
| 14 | +import jakarta.annotation.PostConstruct; |
| 15 | +import jakarta.enterprise.context.ApplicationScoped; |
| 16 | +import jakarta.enterprise.inject.Instance; |
| 17 | +import jakarta.inject.Inject; |
| 18 | + |
| 19 | +import org.jspecify.annotations.Nullable; |
| 20 | + |
| 21 | +import io.a2a.jsonrpc.common.json.JsonProcessingException; |
| 22 | +import io.a2a.jsonrpc.common.json.JsonUtil; |
| 23 | +import io.a2a.server.config.A2AConfigProvider; |
| 24 | +import io.a2a.spec.AgentCard; |
| 25 | + |
| 26 | +/** |
| 27 | + * Provides HTTP caching metadata for Agent Card responses. |
| 28 | + * |
| 29 | + * <p>This bean computes and caches HTTP caching headers (Cache-Control, ETag, Last-Modified) |
| 30 | + * for the Agent Card endpoint as specified in the A2A protocol specification section 8.6. |
| 31 | + * |
| 32 | + * <p>The metadata is computed once at initialization: |
| 33 | + * <ul> |
| 34 | + * <li><b>Cache-Control:</b> Configured via {@code a2a.agent-card.cache.max-age} (default: 3600 seconds)</li> |
| 35 | + * <li><b>ETag:</b> MD5 hash of the serialized Agent Card JSON</li> |
| 36 | + * <li><b>Last-Modified:</b> Timestamp when the bean was initialized (RFC 1123 format)</li> |
| 37 | + * </ul> |
| 38 | + * |
| 39 | + * <p>Since the Agent Card is {@code @ApplicationScoped}, these values remain stable |
| 40 | + * throughout the application lifecycle unless the application is restarted. |
| 41 | + * |
| 42 | + * @see <a href="https://github.com/a2aproject/A2A/blob/main/docs/specification.md#86-caching">A2A Specification - Agent Card Caching</a> |
| 43 | + */ |
| 44 | +@ApplicationScoped |
| 45 | +public class AgentCardCacheMetadata { |
| 46 | + |
| 47 | + private static final String CONFIG_KEY_MAX_AGE = "a2a.agent-card.cache.max-age"; |
| 48 | + private static final String DEFAULT_MAX_AGE = "3600"; // 1 hour |
| 49 | + private static final DateTimeFormatter RFC_1123_FORMATTER = DateTimeFormatter.RFC_1123_DATE_TIME; |
| 50 | + |
| 51 | + @Inject |
| 52 | + @PublicAgentCard |
| 53 | + Instance<AgentCard> agentCardInstance; |
| 54 | + |
| 55 | + @Inject |
| 56 | + Instance<A2AConfigProvider> configInstance; |
| 57 | + |
| 58 | + private @Nullable AgentCard agentCard; |
| 59 | + private @Nullable A2AConfigProvider config; |
| 60 | + |
| 61 | + @SuppressWarnings("NullAway") // Initialized in @PostConstruct when agentCard is available |
| 62 | + private String etag; |
| 63 | + @SuppressWarnings("NullAway") // Initialized in @PostConstruct when agentCard is available |
| 64 | + private String lastModified; |
| 65 | + @SuppressWarnings("NullAway") // Initialized in @PostConstruct when agentCard is available |
| 66 | + private String cacheControl; |
| 67 | + |
| 68 | + /** |
| 69 | + * Package-private no-arg constructor for CDI. |
| 70 | + */ |
| 71 | + AgentCardCacheMetadata() { |
| 72 | + // For CDI |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Public constructor for testing purposes. |
| 77 | + * |
| 78 | + * @param agentCard the agent card |
| 79 | + * @param config the configuration provider |
| 80 | + */ |
| 81 | + public AgentCardCacheMetadata(AgentCard agentCard, A2AConfigProvider config) { |
| 82 | + this.agentCard = agentCard; |
| 83 | + this.config = config; |
| 84 | + init(); |
| 85 | + } |
| 86 | + |
| 87 | + @PostConstruct |
| 88 | + @SuppressWarnings("NullAway") // agentCard and config are guaranteed non-null in both paths |
| 89 | + void init() { |
| 90 | + // Handle two initialization paths: |
| 91 | + // 1. CDI injection: get beans from Instance if available |
| 92 | + // 2. Direct constructor: agentCard and config already set |
| 93 | + |
| 94 | + if (agentCard == null && agentCardInstance != null) { |
| 95 | + // CDI path - only initialize if AgentCard bean is available |
| 96 | + if (agentCardInstance.isUnsatisfied() || configInstance.isUnsatisfied()) { |
| 97 | + return; |
| 98 | + } |
| 99 | + this.agentCard = agentCardInstance.get(); |
| 100 | + this.config = configInstance.get(); |
| 101 | + } |
| 102 | + |
| 103 | + // At this point, agentCard and config should be set (either via CDI or constructor) |
| 104 | + if (agentCard == null || config == null) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + // Calculate ETag from the serialized JSON representation |
| 109 | + this.etag = calculateETag(agentCard); |
| 110 | + |
| 111 | + // Set Last-Modified to the initialization time |
| 112 | + this.lastModified = RFC_1123_FORMATTER.format(Instant.now().atZone(ZoneOffset.UTC)); |
| 113 | + |
| 114 | + // Configure Cache-Control with max-age directive |
| 115 | + String maxAge = config.getOptionalValue(CONFIG_KEY_MAX_AGE).orElse(DEFAULT_MAX_AGE); |
| 116 | + this.cacheControl = "public, max-age=" + maxAge; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Returns the ETag header value for the Agent Card. |
| 121 | + * |
| 122 | + * <p>The ETag is an MD5 hash of the serialized Agent Card JSON, quoted per HTTP specification. |
| 123 | + * |
| 124 | + * @return the ETag header value (e.g., {@code "a1b2c3d4..."}) |
| 125 | + */ |
| 126 | + public String getETag() { |
| 127 | + return etag; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Returns the Last-Modified header value for the Agent Card. |
| 132 | + * |
| 133 | + * <p>The timestamp represents when the bean was initialized, in RFC 1123 format. |
| 134 | + * |
| 135 | + * @return the Last-Modified header value (e.g., {@code "Mon, 17 Mar 2025 10:00:00 GMT"}) |
| 136 | + */ |
| 137 | + public String getLastModified() { |
| 138 | + return lastModified; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Returns the Cache-Control header value for the Agent Card. |
| 143 | + * |
| 144 | + * <p>The value includes {@code public} and a {@code max-age} directive configured |
| 145 | + * via {@code a2a.agent-card.cache.max-age} (default: 3600 seconds). |
| 146 | + * |
| 147 | + * @return the Cache-Control header value (e.g., {@code "public, max-age=3600"}) |
| 148 | + */ |
| 149 | + public String getCacheControl() { |
| 150 | + return cacheControl; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Calculates an MD5 hash of the Agent Card JSON for use as an ETag. |
| 155 | + * |
| 156 | + * @param card the agent card to hash |
| 157 | + * @return the hex-encoded MD5 hash, quoted per HTTP specification |
| 158 | + */ |
| 159 | + private String calculateETag(AgentCard card) { |
| 160 | + try { |
| 161 | + String json = JsonUtil.toJson(card); |
| 162 | + MessageDigest md = MessageDigest.getInstance("MD5"); |
| 163 | + byte[] hash = md.digest(json.getBytes(StandardCharsets.UTF_8)); |
| 164 | + return "\"" + HexFormat.of().formatHex(hash) + "\""; |
| 165 | + } catch (NoSuchAlgorithmException e) { |
| 166 | + throw new IllegalStateException("MD5 algorithm not available", e); |
| 167 | + } catch (JsonProcessingException e) { |
| 168 | + throw new IllegalStateException("Failed to serialize Agent Card for ETag calculation", e); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Populates a map with header names and header values stored in this instance. |
| 174 | + * |
| 175 | + * @return a map of the headers |
| 176 | + */ |
| 177 | + public Map<String, String> getHttpHeadersMap() { |
| 178 | + Map<String, String> headers = new HashMap<>(); |
| 179 | + if (cacheControl != null) { |
| 180 | + headers.put("Cache-Control", cacheControl); |
| 181 | + } |
| 182 | + if (lastModified != null) { |
| 183 | + headers.put("Last-Modified", lastModified); |
| 184 | + } |
| 185 | + if (etag != null) { |
| 186 | + headers.put("ETag", etag); |
| 187 | + } |
| 188 | + return headers; |
| 189 | + } |
| 190 | +} |
0 commit comments