Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

refactor(bedrock-agentcore): extract shared error/JSON helpers into BedrockAgentCoreControllerSupport (#3637)

Priamo Germosen3c96327ac481
diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreControlController.java b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreControlController.java
index 07feeb7a9c..42e672e2df 100644
--- a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreControlController.java
+++ b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreControlController.java
@@ -4,8 +4,6 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
-import io.github.hectorvent.floci.core.common.AwsErrorResponse;
-import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.Pagination;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.RegionResolver;
@@ -29,7 +27,6 @@ import jakarta.ws.rs.core.Response;
import org.jboss.logging.Logger;
import java.time.Instant;
-import java.util.HashMap;
import java.util.Map;
/**
@@ -409,8 +406,7 @@ public class BedrockAgentCoreControlController {
}
private static String text(JsonNode node, String field) {
- JsonNode v = node.get(field);
- return (v == null || v.isNull()) ? null : v.asText();
+ return BedrockAgentCoreControllerSupport.text(node, field);
}
private static JsonNode obj(JsonNode node, String field) {
@@ -419,27 +415,10 @@ public class BedrockAgentCoreControlController {
}
private static Map<String, String> stringMap(JsonNode node) {
- if (node == null || !node.isObject()) {
- return null;
- }
- Map<String, String> map = new HashMap<>();
- node.fields().forEachRemaining(e -> map.put(e.getKey(), e.getValue().asText()));
- return map;
+ return BedrockAgentCoreControllerSupport.stringMap(node);
}
private Response error(Exception e, String action) {
- if (e instanceof AwsException aws) {
- return Response.status(aws.getHttpStatus())
- .type(MediaType.APPLICATION_JSON)
- .header("X-Amzn-Errortype", aws.jsonType())
- .entity(new AwsErrorResponse(aws.jsonType(), aws.getMessage()))
- .build();
- }
- LOG.errorv(e, "Error {0}", action);
- return Response.status(400)
- .type(MediaType.APPLICATION_JSON)
- .header("X-Amzn-Errortype", "ValidationException")
- .entity(new AwsErrorResponse("ValidationException", e.getMessage()))
- .build();
+ return BedrockAgentCoreControllerSupport.error(LOG, e, action);
}
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreControllerSupport.java b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreControllerSupport.java
new file mode 100644
index 0000000000..dd82f6f423
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreControllerSupport.java
@@ -0,0 +1,65 @@
+package io.github.hectorvent.floci.services.bedrockagentcorecontrol;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import io.github.hectorvent.floci.core.common.AwsErrorResponse;
+import io.github.hectorvent.floci.core.common.AwsException;
+import jakarta.ws.rs.core.MediaType;
+import jakarta.ws.rs.core.Response;
+import org.jboss.logging.Logger;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Helpers shared by the sibling REST controllers under this package (control, gateway, gateway
+ * rule, identity, memory, resource policy, tools). Each used to carry its own copy of the same
+ * error-response builder and JSON-to-map conversion, found duplicated by CPD across all seven.
+ *
+ * <p>{@code putInstant} deliberately stays out of here: different AgentCore resource types encode
+ * timestamps differently (ISO-8601 strings for runtimes, epoch seconds for workload identities and
+ * memories), so unlike {@code error} and {@code stringMap}/{@code text}, that one isn't actually
+ * one shared behavior wearing several copies, it's genuinely different behavior per resource type.
+ */
+final class BedrockAgentCoreControllerSupport {
+
+ private BedrockAgentCoreControllerSupport() {
+ }
+
+ /**
+ * Renders an exception as this protocol's REST JSON error response: an {@link AwsException}
+ * maps directly to its own status/code/message, anything else falls back to a 400
+ * {@code ValidationException} and gets logged under the caller's own logger so log lines still
+ * attribute to the right controller.
+ */
+ static Response error(Logger log, Exception e, String action) {
+ if (e instanceof AwsException aws) {
+ return Response.status(aws.getHttpStatus())
+ .type(MediaType.APPLICATION_JSON)
+ .header("X-Amzn-Errortype", aws.jsonType())
+ .entity(new AwsErrorResponse(aws.jsonType(), aws.getMessage()))
+ .build();
+ }
+ log.errorv(e, "Error {0}", action);
+ return Response.status(400)
+ .type(MediaType.APPLICATION_JSON)
+ .header("X-Amzn-Errortype", "ValidationException")
+ .entity(new AwsErrorResponse("ValidationException", e.getMessage()))
+ .build();
+ }
+
+ /** Converts a JSON object node into a flat string map, or null when node isn't an object. */
+ static Map<String, String> stringMap(JsonNode node) {
+ if (node == null || !node.isObject()) {
+ return null;
+ }
+ Map<String, String> map = new HashMap<>();
+ node.fields().forEachRemaining(e -> map.put(e.getKey(), e.getValue().asText()));
+ return map;
+ }
+
+ /** Reads a text field from a JSON node, or null when the field is absent or JSON null. */
+ static String text(JsonNode node, String field) {
+ JsonNode v = node.get(field);
+ return (v == null || v.isNull()) ? null : v.asText();
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreGatewayController.java b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreGatewayController.java
index d76b1391ca..bed23d754e 100644
--- a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreGatewayController.java
+++ b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreGatewayController.java
@@ -4,8 +4,6 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
-import io.github.hectorvent.floci.core.common.AwsErrorResponse;
-import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.Pagination;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.RegionResolver;
@@ -28,6 +26,7 @@ import jakarta.ws.rs.core.Response;
import org.jboss.logging.Logger;
import java.time.Instant;
+import java.util.Map;
/** Standard-REST endpoints for AgentCore gateways and gateway targets. */
@Path("/gateways")
@@ -293,8 +292,7 @@ public class BedrockAgentCoreGatewayController {
}
private static String text(JsonNode node, String field) {
- JsonNode v = node.get(field);
- return (v == null || v.isNull()) ? null : v.asText();
+ return BedrockAgentCoreControllerSupport.text(node, field);
}
private static JsonNode obj(JsonNode node, String field) {
@@ -302,28 +300,11 @@ public class BedrockAgentCoreGatewayController {
return (v == null || v.isNull() || v.isMissingNode()) ? null : v;
}
- private static java.util.Map<String, String> stringMap(JsonNode node) {
- if (node == null || !node.isObject()) {
- return null;
- }
- java.util.Map<String, String> map = new java.util.HashMap<>();
- node.fields().forEachRemaining(e -> map.put(e.getKey(), e.getValue().asText()));
- return map;
+ private static Map<String, String> stringMap(JsonNode node) {
+ return BedrockAgentCoreControllerSupport.stringMap(node);
}
private Response error(Exception e, String action) {
- if (e instanceof AwsException aws) {
- return Response.status(aws.getHttpStatus())
- .type(MediaType.APPLICATION_JSON)
- .header("X-Amzn-Errortype", aws.jsonType())
- .entity(new AwsErrorResponse(aws.jsonType(), aws.getMessage()))
- .build();
- }
- LOG.errorv(e, "Error {0}", action);
- return Response.status(400)
- .type(MediaType.APPLICATION_JSON)
- .header("X-Amzn-Errortype", "ValidationException")
- .entity(new AwsErrorResponse("ValidationException", e.getMessage()))
- .build();
+ return BedrockAgentCoreControllerSupport.error(LOG, e, action);
}
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreGatewayRuleController.java b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreGatewayRuleController.java
index d4fbdb56b7..5523a0f3f9 100644
--- a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreGatewayRuleController.java
+++ b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreGatewayRuleController.java
@@ -3,7 +3,6 @@ package io.github.hectorvent.floci.services.bedrockagentcorecontrol;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
-import io.github.hectorvent.floci.core.common.AwsErrorResponse;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.Pagination;
import io.github.hectorvent.floci.core.common.RegionResolver;
@@ -139,18 +138,6 @@ public class BedrockAgentCoreGatewayRuleController {
}
private Response error(Exception e, String action) {
- if (e instanceof AwsException aws) {
- return Response.status(aws.getHttpStatus())
- .type(MediaType.APPLICATION_JSON)
- .header("X-Amzn-Errortype", aws.jsonType())
- .entity(new AwsErrorResponse(aws.jsonType(), aws.getMessage()))
- .build();
- }
- LOG.errorv(e, "Error {0}", action);
- return Response.status(400)
- .type(MediaType.APPLICATION_JSON)
- .header("X-Amzn-Errortype", "ValidationException")
- .entity(new AwsErrorResponse("ValidationException", e.getMessage()))
- .build();
+ return BedrockAgentCoreControllerSupport.error(LOG, e, action);
}
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreIdentityController.java b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreIdentityController.java
index 5074647240..2a72476651 100644
--- a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreIdentityController.java
+++ b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreIdentityController.java
@@ -4,7 +4,6 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
-import io.github.hectorvent.floci.core.common.AwsErrorResponse;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.RegionResolver;
@@ -326,8 +325,7 @@ public class BedrockAgentCoreIdentityController {
}
private static String text(JsonNode node, String field) {
- JsonNode v = node.get(field);
- return (v == null || v.isNull()) ? null : v.asText();
+ return BedrockAgentCoreControllerSupport.text(node, field);
}
private static List<String> urls(JsonNode node) {
@@ -340,18 +338,6 @@ public class BedrockAgentCoreIdentityController {
}
private Response error(Exception e, String action) {
- if (e instanceof AwsException aws) {
- return Response.status(aws.getHttpStatus())
- .type(MediaType.APPLICATION_JSON)
- .header("X-Amzn-Errortype", aws.jsonType())
- .entity(new AwsErrorResponse(aws.jsonType(), aws.getMessage()))
- .build();
- }
- LOG.errorv(e, "Error {0}", action);
- return Response.status(400)
- .type(MediaType.APPLICATION_JSON)
- .header("X-Amzn-Errortype", "ValidationException")
- .entity(new AwsErrorResponse("ValidationException", e.getMessage()))
- .build();
+ return BedrockAgentCoreControllerSupport.error(LOG, e, action);
}
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreMemoryController.java b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreMemoryController.java
index 224e6ad807..5e0d682b51 100644
--- a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreMemoryController.java
+++ b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreMemoryController.java
@@ -4,11 +4,14 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
-import io.github.hectorvent.floci.core.common.AwsErrorResponse;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.Pagination;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.RegionResolver;
+import io.github.hectorvent.floci.services.bedrockagentcore.BedrockAgentCoreEventService;
+import io.github.hectorvent.floci.services.bedrockagentcore.model.Branch;
+import io.github.hectorvent.floci.services.bedrockagentcore.model.MemoryEvent;
+import io.github.hectorvent.floci.services.bedrockagentcore.model.PayloadType;
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.model.Memory;
import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
@@ -17,13 +20,6 @@ import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
-import io.github.hectorvent.floci.services.bedrockagentcore.BedrockAgentCoreEventService;
-import io.github.hectorvent.floci.services.bedrockagentcore.model.Branch;
-import io.github.hectorvent.floci.services.bedrockagentcore.model.MemoryEvent;
-import io.github.hectorvent.floci.services.bedrockagentcore.model.PayloadType;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
@@ -34,6 +30,9 @@ import jakarta.ws.rs.core.Response;
import org.jboss.logging.Logger;
import java.time.Instant;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
/**
* AgentCore memory endpoints. The operation is a literal path suffix
@@ -180,33 +179,15 @@ public class BedrockAgentCoreMemoryController {
}
private static String text(JsonNode node, String field) {
- JsonNode v = node.get(field);
- return (v == null || v.isNull()) ? null : v.asText();
+ return BedrockAgentCoreControllerSupport.text(node, field);
}
- private static java.util.Map<String, String> stringMap(JsonNode node) {
- if (node == null || !node.isObject()) {
- return null;
- }
- java.util.Map<String, String> map = new java.util.HashMap<>();
- node.fields().forEachRemaining(e -> map.put(e.getKey(), e.getValue().asText()));
- return map;
+ private static Map<String, String> stringMap(JsonNode node) {
+ return BedrockAgentCoreControllerSupport.stringMap(node);
}
private Response error(Exception e, String action) {
- if (e instanceof AwsException aws) {
- return Response.status(aws.getHttpStatus())
- .type(MediaType.APPLICATION_JSON)
- .header("X-Amzn-Errortype", aws.jsonType())
- .entity(new AwsErrorResponse(aws.jsonType(), aws.getMessage()))
- .build();
- }
- LOG.errorv(e, "Error {0}", action);
- return Response.status(400)
- .type(MediaType.APPLICATION_JSON)
- .header("X-Amzn-Errortype", "ValidationException")
- .entity(new AwsErrorResponse("ValidationException", e.getMessage()))
- .build();
+ return BedrockAgentCoreControllerSupport.error(LOG, e, action);
}
// ── AgentCore Memory events (data plane) ─────────────────────
@@ -233,7 +214,7 @@ public class BedrockAgentCoreMemoryController {
// AWS answers CreateEvent with 201, not 200.
return Response.status(201).entity(Map.of("event", event)).build();
} catch (AwsException e) {
- return awsError(e);
+ return error(e, "creating event");
}
}
@@ -260,7 +241,7 @@ public class BedrockAgentCoreMemoryController {
}
return Response.ok(response).build();
} catch (AwsException e) {
- return awsError(e);
+ return error(e, "listing events");
}
}
@@ -277,7 +258,7 @@ public class BedrockAgentCoreMemoryController {
return Response.ok(Map.of("event",
eventService.getEvent(memoryId, actorId, sessionId, eventId, region))).build();
} catch (AwsException e) {
- return awsError(e);
+ return error(e, "getting event");
}
}
@@ -295,22 +276,10 @@ public class BedrockAgentCoreMemoryController {
return Response.ok(Map.of("eventId",
eventService.deleteEvent(memoryId, actorId, sessionId, eventId, region))).build();
} catch (AwsException e) {
- return awsError(e);
+ return error(e, "deleting event");
}
}
- /**
- * Renders an {@link AwsException} the way this protocol needs it: the SDK maps the error by the
- * {@code X-Amzn-Errortype} header, which the shared exception mapper does not set.
- */
- private static Response awsError(AwsException e) {
- return Response.status(e.getHttpStatus())
- .type(MediaType.APPLICATION_JSON)
- .header("X-Amzn-Errortype", e.getErrorCode())
- .entity(new AwsErrorResponse(e.getErrorCode(), e.getMessage()))
- .build();
- }
-
/** Request body of {@code CreateEvent}; {@code sessionId} and {@code branch} are optional. */
public record CreateEventRequest(String actorId, String sessionId, Double eventTimestamp,
List<PayloadType> payload, Branch branch) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreResourcePolicyController.java b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreResourcePolicyController.java
index 3b7b46a864..d205b9c5d8 100644
--- a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreResourcePolicyController.java
+++ b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreResourcePolicyController.java
@@ -3,7 +3,6 @@ package io.github.hectorvent.floci.services.bedrockagentcorecontrol;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
-import io.github.hectorvent.floci.core.common.AwsErrorResponse;
import io.github.hectorvent.floci.core.common.AwsException;
import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
@@ -74,18 +73,6 @@ public class BedrockAgentCoreResourcePolicyController {
}
private Response error(Exception e, String action) {
- if (e instanceof AwsException aws) {
- return Response.status(aws.getHttpStatus())
- .type(MediaType.APPLICATION_JSON)
- .header("X-Amzn-Errortype", aws.jsonType())
- .entity(new AwsErrorResponse(aws.jsonType(), aws.getMessage()))
- .build();
- }
- LOG.errorv(e, "Error {0}", action);
- return Response.status(400)
- .type(MediaType.APPLICATION_JSON)
- .header("X-Amzn-Errortype", "ValidationException")
- .entity(new AwsErrorResponse("ValidationException", e.getMessage()))
- .build();
+ return BedrockAgentCoreControllerSupport.error(LOG, e, action);
}
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreToolsController.java b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreToolsController.java
index 2bd7dd6157..38ed88b518 100644
--- a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreToolsController.java
+++ b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreToolsController.java
@@ -3,7 +3,6 @@ package io.github.hectorvent.floci.services.bedrockagentcorecontrol;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
-import io.github.hectorvent.floci.core.common.AwsErrorResponse;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.Pagination;
import io.github.hectorvent.floci.core.common.RegionResolver;
@@ -309,18 +308,6 @@ public class BedrockAgentCoreToolsController {
}
private Response error(Exception e, String action) {
- if (e instanceof AwsException aws) {
- return Response.status(aws.getHttpStatus())
- .type(MediaType.APPLICATION_JSON)
- .header("X-Amzn-Errortype", aws.jsonType())
- .entity(new AwsErrorResponse(aws.jsonType(), aws.getMessage()))
- .build();
- }
- LOG.errorv(e, "Error {0}", action);
- return Response.status(400)
- .type(MediaType.APPLICATION_JSON)
- .header("X-Amzn-Errortype", "ValidationException")
- .entity(new AwsErrorResponse("ValidationException", e.getMessage()))
- .build();
+ return BedrockAgentCoreControllerSupport.error(LOG, e, action);
}
}