Class Utils
This class contains static utility methods for JSON serialization/deserialization, null-safe operations, artifact management, and other common tasks used throughout the A2A Java SDK.
Key capabilities:
- JSON processing with pre-configured
Gson - Null-safe value defaults via
defaultIfNull(Object, Object) - Artifact streaming support via
appendArtifactToTask(Task, TaskArtifactUpdateEvent, String) - Type-safe exception rethrowing via
rethrow(Throwable)
- See Also:
-
for JSON processingfor streaming artifact updates
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic TaskappendArtifactToTask(Task task, TaskArtifactUpdateEvent event, String taskId) Appends or updates an artifact in a task based on aTaskArtifactUpdateEvent.static StringbuildBaseUrl(String baseUrl, @Nullable String tenant) Builds a base URL by combining a raw base URL string with an optional tenant path.static StringbuildBaseUrl(AgentInterface agentInterface, @Nullable String tenant) Builds a base URL for A2A operations by combining the agent's URL with a tenant path.static StringbuildCardUrl(String baseUrl, String cardPath) NormalizesbaseUrlandcardPathand concatenates them into a full card URL.static <T> TdefaultIfNull(@Nullable T value, T defaultValue) Returns the provided value if non-null, otherwise returns the default value.static AgentInterfacegetFavoriteInterface(AgentCard agentCard) Get the first defined URL in the supported interaces of the agent card.static <T extends Throwable>
voidRethrows a checked exception as an unchecked exception.static StringstripWellKnownSuffix(String baseUrl) Strips any trailing slash and the standard well-known suffix frombaseUrlso thatbuildCardUrl(java.lang.String, java.lang.String)can append the desired path without doubling it.static voidValidates thaturlis a syntactically valid absolute URI.
-
Field Details
-
DEFAULT_AGENT_CARD_PATH
- See Also:
-
-
Method Details
-
defaultIfNull
public static <T> T defaultIfNull(@Nullable T value, T defaultValue) Returns the provided value if non-null, otherwise returns the default value.This is a null-safe utility for providing default values when a parameter might be null.
- Type Parameters:
T- the value type- Parameters:
value- the value to checkdefaultValue- the default value to return if value is null- Returns:
- value if non-null, otherwise defaultValue
-
rethrow
Rethrows a checked exception as an unchecked exception.This method uses type erasure to bypass checked exception handling, allowing checked exceptions to be thrown without explicit declaration. Use with caution as it bypasses Java's compile-time exception checking.
- Type Parameters:
T- the throwable type- Parameters:
t- the throwable to rethrow- Throws:
T- the rethrown exception
-
appendArtifactToTask
Appends or updates an artifact in a task based on aTaskArtifactUpdateEvent.This method handles streaming artifact updates, supporting both:
- Adding new artifacts to the task
- Replacing existing artifacts (when
append=false) - Appending parts to existing artifacts (when
append=true)
The
appendflag in the event determines the behavior:falseornull: Replace/add the entire artifacttrue: Append the new artifact's parts to an existing artifact with matchingartifactId
- Parameters:
task- the current task to updateevent- the artifact update event containing the new/updated artifacttaskId- the task ID (for logging purposes)- Returns:
- a new Task instance with the updated artifacts list
- See Also:
-
validateAbsoluteUrl
Validates thaturlis a syntactically valid absolute URI.- Parameters:
url- the URL to validate- Throws:
URISyntaxException- if the URL is syntactically invalid or not absolute
-
buildCardUrl
NormalizesbaseUrlandcardPathand concatenates them into a full card URL.Strips any trailing slash from
baseUrland ensurescardPathstarts with a leading slash before concatenating, so bothhttp://host/base/andhttp://host/baseproduce the same result.- Parameters:
baseUrl- the agent base URL, must not be nullcardPath- the card endpoint path, must not be null- Returns:
- the normalized card URL
-
stripWellKnownSuffix
Strips any trailing slash and the standard well-known suffix frombaseUrlso thatbuildCardUrl(java.lang.String, java.lang.String)can append the desired path without doubling it.Only
DEFAULT_AGENT_CARD_PATHis stripped; custom paths are never inferred from the URL structure.- Parameters:
baseUrl- the URL to strip- Returns:
- the URL with any trailing slash and well-known suffix removed
-
buildBaseUrl
Builds a base URL by combining a raw base URL string with an optional tenant path.Normalizes trailing slashes on the base URL and validates/normalizes the tenant path.
- Parameters:
baseUrl- the base URL string, must not be nulltenant- the tenant path override, may be null for no tenant- Returns:
- the complete base URL with tenant path appended
- Throws:
IllegalArgumentException- if tenant validation fails
-
getFavoriteInterface
Get the first defined URL in the supported interaces of the agent card.- Parameters:
agentCard- the agentcard where the interfaces are defined.- Returns:
- the first defined URL in the supported interaces of the agent card.
- Throws:
A2AClientException- if no server interface is available in the AgentCard
-
buildBaseUrl
Builds a base URL for A2A operations by combining the agent's URL with a tenant path.This method:
- Uses the tenant from the
AgentInterfaceas the default - Allows overriding with a custom tenant path if provided
- Normalizes trailing slashes on the base URL
- Validates and normalizes the tenant path
Example:
AgentInterface iface = new AgentInterface("jsonrpc", "http://example.com", "default-tenant"); String url = Utils.buildBaseUrl(iface, null); // Returns: "http://example.com/default-tenant" String url2 = Utils.buildBaseUrl(iface, "custom-tenant"); // Returns: "http://example.com/custom-tenant"- Parameters:
agentInterface- the agent interface containing the base URL and default tenant, must not be nulltenant- the tenant override from the request, may be null to use the interface default- Returns:
- the complete base URL with tenant path appended
- Throws:
IllegalArgumentException- if agentInterface is null or tenant validation fails
- Uses the tenant from the
-