validator) {
+ AgentCard card = agentCardSupplier.get();
+ if (transportValidated.compareAndSet(false, true)) {
+ try {
+ validator.accept(card);
+ } catch (RuntimeException e) {
+ transportValidated.set(false);
+ throw e;
+ }
+ }
+ return card;
+ }
+
/**
* Validates the transport configuration of an AgentCard against available transports found on the classpath.
* Logs warnings for missing transports and errors for unsupported transports.
- *
+ *
* @param agentCard the agent card to validate
*/
public static void validateTransportConfiguration(AgentCard agentCard) {
diff --git a/server-common/src/main/java/org/a2aproject/sdk/server/ResolvedInstance.java b/server-common/src/main/java/org/a2aproject/sdk/server/FixedInstance.java
similarity index 63%
rename from server-common/src/main/java/org/a2aproject/sdk/server/ResolvedInstance.java
rename to server-common/src/main/java/org/a2aproject/sdk/server/FixedInstance.java
index 3383858d9..8f9b9bcb6 100644
--- a/server-common/src/main/java/org/a2aproject/sdk/server/ResolvedInstance.java
+++ b/server-common/src/main/java/org/a2aproject/sdk/server/FixedInstance.java
@@ -11,19 +11,44 @@
import org.jspecify.annotations.Nullable;
/**
- * An {@link Instance} wrapper for a pre-resolved value, for use in non-CDI contexts
- * such as convenience constructors and tests.
+ * A static {@link Instance} wrapper holding a pre-resolved (or absent) value,
+ * for use in non-CDI contexts such as convenience constructors and tests.
+ * A {@code null} value represents an unsatisfied instance.
+ *
+ * Usage example (wrapping a concrete value):
+ *
{@code
+ * Instance instance = new FixedInstance<>(myAgentCard);
+ * }
+ *
+ * Usage example (empty / unsatisfied):
+ *
{@code
+ * Instance empty = FixedInstance.empty();
+ * }
*
* @param the bean type
*/
-public class ResolvedInstance implements Instance {
+public class FixedInstance implements Instance {
+
+ @SuppressWarnings("rawtypes")
+ private static final FixedInstance EMPTY = new FixedInstance<>(null);
private final @Nullable T value;
- public ResolvedInstance(@Nullable T value) {
+ public FixedInstance(@Nullable T value) {
this.value = value;
}
+ /**
+ * Returns a {@code FixedInstance} with no value (unsatisfied).
+ *
+ * @param the bean type
+ * @return an empty instance
+ */
+ @SuppressWarnings("unchecked")
+ public static FixedInstance empty() {
+ return (FixedInstance) EMPTY;
+ }
+
@Override
public T get() {
if (value == null) {
diff --git a/server-common/src/test/java/org/a2aproject/sdk/server/AgentCardValidatorTest.java b/server-common/src/test/java/org/a2aproject/sdk/server/AgentCardValidatorTest.java
index 273de7f5f..7e675942f 100644
--- a/server-common/src/test/java/org/a2aproject/sdk/server/AgentCardValidatorTest.java
+++ b/server-common/src/test/java/org/a2aproject/sdk/server/AgentCardValidatorTest.java
@@ -1,6 +1,7 @@
package org.a2aproject.sdk.server;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -8,6 +9,8 @@
import java.util.Collections;
import java.util.List;
import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
@@ -229,6 +232,44 @@ void testSkipPropertiesFilterWarnings() {
}
}
+ @Test
+ void resolveAndValidateOnceRetriesAfterFailure() {
+ AgentCard card = createTestAgentCardBuilder().build();
+ AtomicBoolean guard = new AtomicBoolean(false);
+ AtomicInteger validationCount = new AtomicInteger(0);
+
+ assertThrows(IllegalStateException.class, () ->
+ AgentCardValidator.resolveAndValidateOnce(
+ () -> card, guard, c -> {
+ validationCount.incrementAndGet();
+ throw new IllegalStateException("transient failure");
+ }));
+
+ assertFalse(guard.get(), "guard should be reset after failure");
+ assertEquals(1, validationCount.get());
+
+ AgentCard result = AgentCardValidator.resolveAndValidateOnce(
+ () -> card, guard, c -> validationCount.incrementAndGet());
+
+ assertTrue(guard.get(), "guard should be set after success");
+ assertEquals(2, validationCount.get());
+ assertEquals(card, result);
+ }
+
+ @Test
+ void resolveAndValidateOnceSkipsValidationOnSubsequentCalls() {
+ AgentCard card = createTestAgentCardBuilder().build();
+ AtomicBoolean guard = new AtomicBoolean(false);
+ AtomicInteger validationCount = new AtomicInteger(0);
+
+ AgentCardValidator.resolveAndValidateOnce(
+ () -> card, guard, c -> validationCount.incrementAndGet());
+ AgentCardValidator.resolveAndValidateOnce(
+ () -> card, guard, c -> validationCount.incrementAndGet());
+
+ assertEquals(1, validationCount.get(), "validation should run only once");
+ }
+
// A simple log handler for testing
private static class TestLogHandler extends Handler {
private final List logMessages = new java.util.ArrayList<>();
diff --git a/server-common/src/test/java/org/a2aproject/sdk/server/TestInstances.java b/server-common/src/test/java/org/a2aproject/sdk/server/TestInstances.java
new file mode 100644
index 000000000..b33b1e228
--- /dev/null
+++ b/server-common/src/test/java/org/a2aproject/sdk/server/TestInstances.java
@@ -0,0 +1,33 @@
+package org.a2aproject.sdk.server;
+
+import jakarta.enterprise.inject.Instance;
+
+/**
+ * Factory methods for {@link Instance} test doubles built on {@link FixedInstance}.
+ */
+public final class TestInstances {
+
+ private TestInstances() {
+ }
+
+ /**
+ * Returns an {@link Instance} that throws {@link AssertionError} on {@link Instance#get()},
+ * useful for verifying that a constructor does not eagerly resolve the instance.
+ *
+ * @param the bean type
+ * @return an instance that throws on resolution
+ */
+ public static Instance throwOnGet() {
+ return new FixedInstance<>(null) {
+ @Override
+ public T get() {
+ throw new AssertionError("Instance.get() must not be called during construction");
+ }
+
+ @Override
+ public boolean isUnsatisfied() {
+ return false;
+ }
+ };
+ }
+}
diff --git a/transport/grpc/src/main/java/org/a2aproject/sdk/transport/grpc/handler/GrpcHandler.java b/transport/grpc/src/main/java/org/a2aproject/sdk/transport/grpc/handler/GrpcHandler.java
index b450f089c..6eb84607b 100644
--- a/transport/grpc/src/main/java/org/a2aproject/sdk/transport/grpc/handler/GrpcHandler.java
+++ b/transport/grpc/src/main/java/org/a2aproject/sdk/transport/grpc/handler/GrpcHandler.java
@@ -159,7 +159,7 @@ public abstract class GrpcHandler extends A2AServiceGrpc.A2AServiceImplBase {
// Without this we get intermittent failures
private static volatile @Nullable Runnable streamingSubscribedRunnable;
- private final AtomicBoolean initialised = new AtomicBoolean(false);
+ private final AtomicBoolean transportValidated = new AtomicBoolean(false);
private static final Logger LOGGER = Logger.getLogger(GrpcHandler.class.getName());
@@ -279,7 +279,7 @@ public void cancelTask(org.a2aproject.sdk.grpc.CancelTaskRequest request,
@Override
public void createTaskPushNotificationConfig(org.a2aproject.sdk.grpc.TaskPushNotificationConfig request,
StreamObserver responseObserver) {
- if (!getAgentCardInternal().capabilities().pushNotifications()) {
+ if (!resolveAgentCard().capabilities().pushNotifications()) {
handleError(responseObserver, new PushNotificationNotSupportedError());
return;
}
@@ -302,7 +302,7 @@ public void createTaskPushNotificationConfig(org.a2aproject.sdk.grpc.TaskPushNot
@Override
public void getTaskPushNotificationConfig(org.a2aproject.sdk.grpc.GetTaskPushNotificationConfigRequest request,
StreamObserver responseObserver) {
- if (!getAgentCardInternal().capabilities().pushNotifications()) {
+ if (!resolveAgentCard().capabilities().pushNotifications()) {
handleError(responseObserver, new PushNotificationNotSupportedError());
return;
}
@@ -325,7 +325,7 @@ public void getTaskPushNotificationConfig(org.a2aproject.sdk.grpc.GetTaskPushNot
@Override
public void listTaskPushNotificationConfigs(org.a2aproject.sdk.grpc.ListTaskPushNotificationConfigsRequest request,
StreamObserver responseObserver) {
- if (!getAgentCardInternal().capabilities().pushNotifications()) {
+ if (!resolveAgentCard().capabilities().pushNotifications()) {
handleError(responseObserver, new PushNotificationNotSupportedError());
return;
}
@@ -386,7 +386,7 @@ public void listTaskPushNotificationConfigs(org.a2aproject.sdk.grpc.ListTaskPush
@Override
public void sendStreamingMessage(org.a2aproject.sdk.grpc.SendMessageRequest request,
StreamObserver responseObserver) {
- if (!getAgentCardInternal().capabilities().streaming()) {
+ if (!resolveAgentCard().capabilities().streaming()) {
handleError(responseObserver,
new UnsupportedOperationError(null, "Streaming is not supported by the agent", null));
return;
@@ -410,7 +410,7 @@ public void sendStreamingMessage(org.a2aproject.sdk.grpc.SendMessageRequest requ
@Override
public void subscribeToTask(org.a2aproject.sdk.grpc.SubscribeToTaskRequest request,
StreamObserver responseObserver) {
- if (!getAgentCardInternal().capabilities().streaming()) {
+ if (!resolveAgentCard().capabilities().streaming()) {
handleError(responseObserver,
new UnsupportedOperationError(null, "Streaming is not supported by the agent", null));
return;
@@ -584,7 +584,7 @@ public void getExtendedAgentCard(org.a2aproject.sdk.grpc.GetExtendedAgentCardReq
@Override
public void deleteTaskPushNotificationConfig(org.a2aproject.sdk.grpc.DeleteTaskPushNotificationConfigRequest request,
StreamObserver responseObserver) {
- if (!getAgentCardInternal().capabilities().pushNotifications()) {
+ if (!resolveAgentCard().capabilities().pushNotifications()) {
handleError(responseObserver, new PushNotificationNotSupportedError());
return;
}
@@ -707,8 +707,8 @@ private ServerCallContext createCallContext(StreamObserver responseObserv
context = factory.create(responseObserver); // Fall back to basic create() method for now
}
- A2AVersionValidator.validateProtocolVersion(getAgentCardInternal(), context);
- A2AExtensions.validateRequiredExtensions(getAgentCardInternal(), context);
+ A2AVersionValidator.validateProtocolVersion(resolveAgentCard(), context);
+ A2AExtensions.validateRequiredExtensions(resolveAgentCard(), context);
return context;
}
@@ -835,18 +835,9 @@ private void handleInternalError(StreamObserver responseObserver, Throwab
}
- private AgentCard getAgentCardInternal() {
- AgentCard agentCard = getAgentCard();
- if (initialised.compareAndSet(false, true)) {
- try {
- // Validate transport configuration with proper classloader context
- validateTransportConfigurationWithCorrectClassLoader(agentCard);
- } catch (RuntimeException e) {
- initialised.set(false);
- throw e;
- }
- }
- return agentCard;
+ private AgentCard resolveAgentCard() {
+ return AgentCardValidator.resolveAndValidateOnce(
+ this::getAgentCard, transportValidated, this::validateTransportConfigurationWithCorrectClassLoader);
}
private void validateTransportConfigurationWithCorrectClassLoader(AgentCard agentCard) {
diff --git a/transport/jsonrpc/src/main/java/org/a2aproject/sdk/transport/jsonrpc/handler/JSONRPCHandler.java b/transport/jsonrpc/src/main/java/org/a2aproject/sdk/transport/jsonrpc/handler/JSONRPCHandler.java
index 2655534e7..a10c8de6e 100644
--- a/transport/jsonrpc/src/main/java/org/a2aproject/sdk/transport/jsonrpc/handler/JSONRPCHandler.java
+++ b/transport/jsonrpc/src/main/java/org/a2aproject/sdk/transport/jsonrpc/handler/JSONRPCHandler.java
@@ -38,8 +38,8 @@
import org.a2aproject.sdk.jsonrpc.common.wrappers.SubscribeToTaskRequest;
import org.a2aproject.sdk.server.AgentCardValidator;
import org.a2aproject.sdk.server.ExtendedAgentCard;
+import org.a2aproject.sdk.server.FixedInstance;
import org.a2aproject.sdk.server.PublicAgentCard;
-import org.a2aproject.sdk.server.ResolvedInstance;
import org.a2aproject.sdk.server.ServerCallContext;
import org.a2aproject.sdk.server.auth.TaskOperation;
import org.a2aproject.sdk.server.extensions.A2AExtensions;
@@ -186,7 +186,7 @@ public JSONRPCHandler(@PublicAgentCard Instance agentCardInstance,
* @param executor the executor for asynchronous operations
*/
public JSONRPCHandler(@PublicAgentCard AgentCard agentCard, RequestHandler requestHandler, Executor executor) {
- this(new ResolvedInstance<>(agentCard), null, requestHandler, executor);
+ this(new FixedInstance<>(agentCard), null, requestHandler, executor);
}
/**
@@ -233,8 +233,8 @@ public JSONRPCHandler(@PublicAgentCard AgentCard agentCard, RequestHandler reque
*/
public SendMessageResponse onMessageSend(SendMessageRequest request, ServerCallContext context) {
try {
- A2AVersionValidator.validateProtocolVersion(agentCard(), context);
- A2AExtensions.validateRequiredExtensions(agentCard(), context);
+ A2AVersionValidator.validateProtocolVersion(resolveAgentCard(), context);
+ A2AExtensions.validateRequiredExtensions(resolveAgentCard(), context);
EventKind taskOrMessage = requestHandler.onMessageSend(request.getParams(), context);
return new SendMessageResponse(request.getId(), taskOrMessage);
} catch (A2AError e) {
@@ -282,7 +282,7 @@ public SendMessageResponse onMessageSend(SendMessageRequest request, ServerCallC
*/
public Flow.Publisher onMessageSendStream(
SendStreamingMessageRequest request, ServerCallContext context) {
- if (!agentCard().capabilities().streaming()) {
+ if (!resolveAgentCard().capabilities().streaming()) {
return ZeroPublisher.fromItems(
new SendStreamingMessageResponse(
request.getId(),
@@ -290,8 +290,8 @@ public Flow.Publisher onMessageSendStream(
}
try {
- A2AVersionValidator.validateProtocolVersion(agentCard(), context);
- A2AExtensions.validateRequiredExtensions(agentCard(), context);
+ A2AVersionValidator.validateProtocolVersion(resolveAgentCard(), context);
+ A2AExtensions.validateRequiredExtensions(resolveAgentCard(), context);
Flow.Publisher publisher =
requestHandler.onMessageSendStream(request.getParams(), context);
// We can't use the convertingProcessor convenience method since that propagates any errors as an error handled
@@ -379,7 +379,7 @@ public CancelTaskResponse onCancelTask(CancelTaskRequest request, ServerCallCont
*/
public Flow.Publisher onSubscribeToTask(
SubscribeToTaskRequest request, ServerCallContext context) throws A2AError {
- if (!agentCard().capabilities().streaming()) {
+ if (!resolveAgentCard().capabilities().streaming()) {
return ZeroPublisher.fromItems(
new SendStreamingMessageResponse(
request.getId(),
@@ -426,7 +426,7 @@ public Flow.Publisher onSubscribeToTask(
*/
public GetTaskPushNotificationConfigResponse getPushNotificationConfig(
GetTaskPushNotificationConfigRequest request, ServerCallContext context) {
- if (!agentCard().capabilities().pushNotifications()) {
+ if (!resolveAgentCard().capabilities().pushNotifications()) {
return new GetTaskPushNotificationConfigResponse(request.getId(),
new PushNotificationNotSupportedError());
}
@@ -468,7 +468,7 @@ public GetTaskPushNotificationConfigResponse getPushNotificationConfig(
*/
public CreateTaskPushNotificationConfigResponse setPushNotificationConfig(
CreateTaskPushNotificationConfigRequest request, ServerCallContext context) {
- if (!agentCard().capabilities().pushNotifications()) {
+ if (!resolveAgentCard().capabilities().pushNotifications()) {
return new CreateTaskPushNotificationConfigResponse(request.getId(),
new PushNotificationNotSupportedError());
}
@@ -591,7 +591,7 @@ public ListTasksResponse onListTasks(ListTasksRequest request, ServerCallContext
*/
public ListTaskPushNotificationConfigsResponse listPushNotificationConfigs(
ListTaskPushNotificationConfigsRequest request, ServerCallContext context) {
- if (!agentCard().capabilities().pushNotifications()) {
+ if (!resolveAgentCard().capabilities().pushNotifications()) {
return new ListTaskPushNotificationConfigsResponse(request.getId(),
new PushNotificationNotSupportedError());
}
@@ -633,7 +633,7 @@ public ListTaskPushNotificationConfigsResponse listPushNotificationConfigs(
*/
public DeleteTaskPushNotificationConfigResponse deletePushNotificationConfig(
DeleteTaskPushNotificationConfigRequest request, ServerCallContext context) {
- if (!agentCard().capabilities().pushNotifications()) {
+ if (!resolveAgentCard().capabilities().pushNotifications()) {
return new DeleteTaskPushNotificationConfigResponse(request.getId(),
new PushNotificationNotSupportedError());
}
@@ -673,7 +673,7 @@ public DeleteTaskPushNotificationConfigResponse deletePushNotificationConfig(
// TODO: Add authentication (https://github.com/a2aproject/a2a-java/issues/77)
public GetExtendedAgentCardResponse onGetExtendedCardRequest(
GetExtendedAgentCardRequest request, ServerCallContext context) {
- if (!agentCard().capabilities().extendedAgentCard()) {
+ if (!resolveAgentCard().capabilities().extendedAgentCard()) {
return new GetExtendedAgentCardResponse(request.getId(), new UnsupportedOperationError());
}
if (extendedAgentCard == null || !extendedAgentCard.isResolvable()) {
@@ -700,20 +700,11 @@ public GetExtendedAgentCardResponse onGetExtendedCardRequest(
* @see AgentCard
*/
public AgentCard getAgentCard() {
- return agentCard();
+ return resolveAgentCard();
}
- private AgentCard agentCard() {
- AgentCard card = agentCardInstance.get();
- if (transportValidated.compareAndSet(false, true)) {
- try {
- AgentCardValidator.validateTransportConfiguration(card);
- } catch (RuntimeException e) {
- transportValidated.set(false);
- throw e;
- }
- }
- return card;
+ private AgentCard resolveAgentCard() {
+ return AgentCardValidator.resolveAndValidateOnce(agentCardInstance, transportValidated);
}
private Flow.Publisher convertToSendStreamingMessageResponse(
diff --git a/transport/jsonrpc/src/test/java/org/a2aproject/sdk/transport/jsonrpc/handler/JSONRPCHandlerTest.java b/transport/jsonrpc/src/test/java/org/a2aproject/sdk/transport/jsonrpc/handler/JSONRPCHandlerTest.java
index b436de140..e476cab80 100644
--- a/transport/jsonrpc/src/test/java/org/a2aproject/sdk/transport/jsonrpc/handler/JSONRPCHandlerTest.java
+++ b/transport/jsonrpc/src/test/java/org/a2aproject/sdk/transport/jsonrpc/handler/JSONRPCHandlerTest.java
@@ -80,9 +80,12 @@
import org.a2aproject.sdk.spec.TaskStatus;
import org.a2aproject.sdk.spec.TaskStatusUpdateEvent;
import org.a2aproject.sdk.spec.TextPart;
+import org.a2aproject.sdk.server.TestInstances;
import org.a2aproject.sdk.spec.UnsupportedOperationError;
import org.a2aproject.sdk.spec.VersionNotSupportedError;
+
import jakarta.enterprise.inject.Instance;
+
import mutiny.zero.ZeroPublisher;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
@@ -2028,10 +2031,8 @@ public void testListTasksEmptyResultIncludesAllFields() {
}
@Test
- void constructorDoesNotResolveAgentCardInstance() {
- @SuppressWarnings("unchecked")
- Instance throwOnGet = Mockito.mock(Instance.class);
- Mockito.when(throwOnGet.get()).thenThrow(new AssertionError("Instance.get() must not be called during construction"));
+ void constructorDoesNotResolveAgentCardInstances() {
+ Instance throwOnGet = TestInstances.throwOnGet();
assertDoesNotThrow(() -> new JSONRPCHandler(throwOnGet, null, requestHandler, internalExecutor));
}
diff --git a/transport/rest/src/main/java/org/a2aproject/sdk/transport/rest/handler/RestHandler.java b/transport/rest/src/main/java/org/a2aproject/sdk/transport/rest/handler/RestHandler.java
index e6d08a299..38a2eb3fa 100644
--- a/transport/rest/src/main/java/org/a2aproject/sdk/transport/rest/handler/RestHandler.java
+++ b/transport/rest/src/main/java/org/a2aproject/sdk/transport/rest/handler/RestHandler.java
@@ -34,8 +34,8 @@
import org.a2aproject.sdk.server.AgentCardCacheMetadata;
import org.a2aproject.sdk.server.AgentCardValidator;
import org.a2aproject.sdk.server.ExtendedAgentCard;
+import org.a2aproject.sdk.server.FixedInstance;
import org.a2aproject.sdk.server.PublicAgentCard;
-import org.a2aproject.sdk.server.ResolvedInstance;
import org.a2aproject.sdk.server.ServerCallContext;
import org.a2aproject.sdk.server.auth.TaskOperation;
import org.a2aproject.sdk.server.extensions.A2AExtensions;
@@ -174,7 +174,7 @@ public RestHandler(@PublicAgentCard Instance agentCardInstance,
*/
public RestHandler(AgentCard agentCard, AgentCardCacheMetadata cacheMetadata,
RequestHandler requestHandler, Executor executor) {
- this.agentCardInstance = new ResolvedInstance<>(agentCard);
+ this.agentCardInstance = new FixedInstance<>(agentCard);
this.cacheMetadata = cacheMetadata;
this.requestHandler = requestHandler;
this.executor = executor;
@@ -228,8 +228,8 @@ public RestHandler(AgentCard agentCard, AgentCardCacheMetadata cacheMetadata,
*/
public HTTPRestResponse sendMessage(ServerCallContext context, String tenant, String body) {
try {
- A2AVersionValidator.validateProtocolVersion(agentCard(), context);
- A2AExtensions.validateRequiredExtensions(agentCard(), context);
+ A2AVersionValidator.validateProtocolVersion(resolveAgentCard(), context);
+ A2AExtensions.validateRequiredExtensions(resolveAgentCard(), context);
org.a2aproject.sdk.grpc.SendMessageRequest.Builder request = org.a2aproject.sdk.grpc.SendMessageRequest.newBuilder();
parseRequestBody(body, request);
request.setTenant(tenant);
@@ -293,11 +293,11 @@ public HTTPRestResponse sendMessage(ServerCallContext context, String tenant, St
*/
public HTTPRestResponse sendStreamingMessage(ServerCallContext context, String tenant, String body) {
try {
- if (!agentCard().capabilities().streaming()) {
+ if (!resolveAgentCard().capabilities().streaming()) {
return createErrorResponse(new UnsupportedOperationError(null, "Streaming is not supported by the agent", null));
}
- A2AVersionValidator.validateProtocolVersion(agentCard(), context);
- A2AExtensions.validateRequiredExtensions(agentCard(), context);
+ A2AVersionValidator.validateProtocolVersion(resolveAgentCard(), context);
+ A2AExtensions.validateRequiredExtensions(resolveAgentCard(), context);
org.a2aproject.sdk.grpc.SendMessageRequest.Builder request = org.a2aproject.sdk.grpc.SendMessageRequest.newBuilder();
parseRequestBody(body, request);
request.setTenant(tenant);
@@ -370,7 +370,7 @@ public HTTPRestResponse cancelTask(ServerCallContext context, String tenant, Str
*/
public HTTPRestResponse createTaskPushNotificationConfiguration(ServerCallContext context, String tenant, String body, String taskId) {
try {
- if (!agentCard().capabilities().pushNotifications()) {
+ if (!resolveAgentCard().capabilities().pushNotifications()) {
throw new PushNotificationNotSupportedError();
}
org.a2aproject.sdk.grpc.TaskPushNotificationConfig.Builder builder = org.a2aproject.sdk.grpc.TaskPushNotificationConfig.newBuilder();
@@ -425,7 +425,7 @@ public HTTPRestResponse createTaskPushNotificationConfiguration(ServerCallContex
*/
public HTTPRestResponse subscribeToTask(ServerCallContext context, String tenant, String taskId) {
try {
- if (!agentCard().capabilities().streaming()) {
+ if (!resolveAgentCard().capabilities().streaming()) {
return createErrorResponse(new UnsupportedOperationError(null, "Streaming is not supported by the agent", null));
}
TaskIdParams params = TaskIdParams.builder().id(taskId).tenant(tenant).build();
@@ -582,7 +582,7 @@ public HTTPRestResponse listTasks(ServerCallContext context, String tenant,
*/
public HTTPRestResponse getTaskPushNotificationConfiguration(ServerCallContext context, String tenant, String taskId, String configId) {
try {
- if (!agentCard().capabilities().pushNotifications()) {
+ if (!resolveAgentCard().capabilities().pushNotifications()) {
throw new PushNotificationNotSupportedError();
}
GetTaskPushNotificationConfigParams params = new GetTaskPushNotificationConfigParams(taskId, configId, tenant);
@@ -607,7 +607,7 @@ public HTTPRestResponse getTaskPushNotificationConfiguration(ServerCallContext c
*/
public HTTPRestResponse listTaskPushNotificationConfigurations(ServerCallContext context, String tenant, String taskId, int pageSize, String pageToken) {
try {
- if (!agentCard().capabilities().pushNotifications()) {
+ if (!resolveAgentCard().capabilities().pushNotifications()) {
throw new PushNotificationNotSupportedError();
}
ListTaskPushNotificationConfigsParams params = new ListTaskPushNotificationConfigsParams(taskId, pageSize, pageToken, tenant);
@@ -631,7 +631,7 @@ public HTTPRestResponse listTaskPushNotificationConfigurations(ServerCallContext
*/
public HTTPRestResponse deleteTaskPushNotificationConfiguration(ServerCallContext context, String tenant, String taskId, String configId) {
try {
- if (!agentCard().capabilities().pushNotifications()) {
+ if (!resolveAgentCard().capabilities().pushNotifications()) {
throw new PushNotificationNotSupportedError();
}
DeleteTaskPushNotificationConfigParams params = new DeleteTaskPushNotificationConfigParams(taskId, configId, tenant);
@@ -815,7 +815,7 @@ private static int mapErrorToHttpStatus(A2AError error) {
*/
public HTTPRestResponse getExtendedAgentCard(ServerCallContext context, String tenant) {
try {
- if (!agentCard().capabilities().extendedAgentCard()) {
+ if (!resolveAgentCard().capabilities().extendedAgentCard()) {
throw new UnsupportedOperationError();
}
if (extendedAgentCard == null || !extendedAgentCard.isResolvable()) {
@@ -864,22 +864,13 @@ public HTTPRestResponse getExtendedAgentCard(ServerCallContext context, String t
* @see AgentCard
* @see #getExtendedAgentCard(ServerCallContext, String)
*/
- private AgentCard agentCard() {
- AgentCard card = agentCardInstance.get();
- if (transportValidated.compareAndSet(false, true)) {
- try {
- AgentCardValidator.validateTransportConfiguration(card);
- } catch (RuntimeException e) {
- transportValidated.set(false);
- throw e;
- }
- }
- return card;
+ private AgentCard resolveAgentCard() {
+ return AgentCardValidator.resolveAndValidateOnce(agentCardInstance, transportValidated);
}
public HTTPRestResponse getAgentCard() {
try {
- return new HTTPRestResponse(200, APPLICATION_JSON, JsonUtil.toJson(agentCard()),
+ return new HTTPRestResponse(200, APPLICATION_JSON, JsonUtil.toJson(resolveAgentCard()),
cacheMetadata.getHttpHeadersMap());
} catch (Throwable t) {
return createErrorResponse(500, internalError(t));
diff --git a/transport/rest/src/test/java/org/a2aproject/sdk/transport/rest/handler/RestHandlerTest.java b/transport/rest/src/test/java/org/a2aproject/sdk/transport/rest/handler/RestHandlerTest.java
index 9e9668e8c..61ed4d4fe 100644
--- a/transport/rest/src/test/java/org/a2aproject/sdk/transport/rest/handler/RestHandlerTest.java
+++ b/transport/rest/src/test/java/org/a2aproject/sdk/transport/rest/handler/RestHandlerTest.java
@@ -1,6 +1,5 @@
package org.a2aproject.sdk.transport.rest.handler;
-
import static org.a2aproject.sdk.common.MediaType.APPLICATION_JSON;
import java.util.Collections;
@@ -18,8 +17,9 @@
import com.google.protobuf.InvalidProtocolBufferException;
import jakarta.enterprise.inject.Instance;
-import org.a2aproject.sdk.common.MediaType;
+
import org.a2aproject.sdk.server.AgentCardCacheMetadata;
+import org.a2aproject.sdk.server.TestInstances;
import org.a2aproject.sdk.server.ServerCallContext;
import org.a2aproject.sdk.server.auth.UnauthenticatedUser;
import org.a2aproject.sdk.server.config.DefaultValuesConfigProvider;
@@ -1081,10 +1081,8 @@ public void testListTasksEmptyResultIncludesAllFields() {
}
@Test
- void constructorDoesNotResolveAgentCardInstance() {
- @SuppressWarnings("unchecked")
- Instance throwOnGet = Mockito.mock(Instance.class);
- Mockito.when(throwOnGet.get()).thenThrow(new AssertionError("Instance.get() must not be called during construction"));
+ void constructorDoesNotResolveAgentCardInstances() {
+ Instance throwOnGet = TestInstances.throwOnGet();
Assertions.assertDoesNotThrow(() -> new RestHandler(throwOnGet, throwOnGet,
createCacheMetadata(), requestHandler, internalExecutor));