feat(textract): add Textract service support and integration tests (#719)

* feat(textract): add Textract service support and integration tests
* chore(textract): Implement PR suggestions
* fix(textract): remove completed async jobs to prevent memory growth
This commit is contained in:
Roméo authored and GitHub committed 2026-05-07 21:08:39 -05:00
1 parent d971288d02
commit d7ec2b284f
11 files changed
+792 -3

No files matched your search

@@ -284,6 +284,7 @@ public interface EmulatorConfig {
BackupServiceConfig backup();
Route53ServiceConfig route53();
TransferServiceConfig transfer();
TextractServiceConfig textract();
}
interface TransferServiceConfig {
@@ -619,6 +620,11 @@ public interface EmulatorConfig {
boolean enabled();
}
interface TextractServiceConfig {
@WithDefault("true")
boolean enabled();
}
interface EcrServiceConfig {
@WithDefault("true")
boolean enabled();
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.services.ecs.EcsJsonHandler;
import io.github.hectorvent.floci.services.firehose.FirehoseJsonHandler;
import io.github.hectorvent.floci.services.glue.GlueJsonHandler;
import io.github.hectorvent.floci.services.resourcegroupstagging.ResourceGroupsTaggingJsonHandler;
import io.github.hectorvent.floci.services.textract.TextractJsonHandler;
import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2JsonHandler;
import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsHandler;
import io.github.hectorvent.floci.services.cognito.CognitoJsonHandler;
@@ -64,6 +65,7 @@ public class AwsJson11Controller {
private final CodeDeployJsonHandler codeDeployJsonHandler;
private final Ec2MessagesJsonHandler ec2MessagesJsonHandler;
private final TransferHandler transferHandler;
private final TextractJsonHandler textractJsonHandler;
@Inject
public AwsJson11Controller(ObjectMapper objectMapper, ResolvedServiceCatalog catalog,
@@ -82,7 +84,8 @@ public class AwsJson11Controller {
CodeBuildJsonHandler codeBuildJsonHandler,
CodeDeployJsonHandler codeDeployJsonHandler,
Ec2MessagesJsonHandler ec2MessagesJsonHandler,
TransferHandler transferHandler) {
TransferHandler transferHandler,
TextractJsonHandler textractJsonHandler) {
this.objectMapper = objectMapper;
this.catalog = catalog;
this.regionResolver = regionResolver;
@@ -105,6 +108,7 @@ public class AwsJson11Controller {
this.codeDeployJsonHandler = codeDeployJsonHandler;
this.ec2MessagesJsonHandler = ec2MessagesJsonHandler;
this.transferHandler = transferHandler;
this.textractJsonHandler = textractJsonHandler;
}
@POST
@@ -152,6 +156,7 @@ public class AwsJson11Controller {
case "codedeploy" -> codeDeployJsonHandler.handle(action, request, region);
case "ec2messages" -> ec2MessagesJsonHandler.handle(action, request, region);
case "transfer" -> transferHandler.handle(action, request, region);
case "textract" -> textractJsonHandler.handle(action, request, region);
default -> null;
};
// catalog.matchTarget is protocol-agnostic: a JSON 1.0 target
@@ -239,7 +239,11 @@ public class ResolvedServiceCatalog {
descriptor("route53", "route53", config.services().route53().enabled(), true,
"route53", config.storage().mode(), 5000L, null, ServiceProtocol.REST_XML,
protocols(ServiceProtocol.REST_XML),
Set.of(), Set.of("route53"), Set.of(), Set.of(Route53Controller.class))
Set.of(), Set.of("route53"), Set.of(), Set.of(Route53Controller.class)),
descriptor("textract", "textract", config.services().textract().enabled(), true,
null, null, 5000L, null, ServiceProtocol.JSON,
protocols(ServiceProtocol.JSON),
Set.of("Textract."), Set.of("textract"), Set.of(), Set.of())
));
}
@@ -0,0 +1,47 @@
package io.github.hectorvent.floci.services.textract;
import com.fasterxml.jackson.databind.JsonNode;
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import org.jboss.logging.Logger;
/**
* JSON 1.1 handler for Amazon Textract API operations.
* Dispatches X-Amz-Target: Textract.* actions to {@link TextractService}.
*
* @see <a href="https://docs.aws.amazon.com/textract/latest/dg/API_Operations.html">Textract API Reference</a>
*/
@ApplicationScoped
public class TextractJsonHandler {
private static final Logger LOG = Logger.getLogger(TextractJsonHandler.class);
private final TextractService textractService;
@Inject
public TextractJsonHandler(TextractService textractService) {
this.textractService = textractService;
}
/**
* Dispatches Textract actions received via the AwsJson11Controller.
* The request body is accepted but not parsed — stub ignores document input.
*/
public Response handle(String action, JsonNode request, String region) {
LOG.debugv("Textract action: {0}", action);
return switch (action) {
case "DetectDocumentText" -> textractService.detectDocumentText();
case "AnalyzeDocument" -> textractService.analyzeDocument();
case "StartDocumentTextDetection" -> textractService.startDocumentTextDetection();
case "GetDocumentTextDetection" -> textractService.getDocumentTextDetection(
getStringField(request, "JobId"));
case "StartDocumentAnalysis" -> textractService.startDocumentAnalysis();
case "GetDocumentAnalysis" -> textractService.getDocumentAnalysis(
getStringField(request, "JobId"));
default -> Response.status(400)
.entity(new AwsErrorResponse("UnknownOperationException",
"Unknown operation: Textract." + action))
.build();
};
}
private String getStringField(JsonNode node, String field) {
JsonNode value = node == null ? null : node.get(field);
return (value != null && !value.isNull()) ? value.asText() : null;
}
}
@@ -0,0 +1,201 @@
package io.github.hectorvent.floci.services.textract;
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.AwsException;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
/**
* Dummy response builder for Amazon Textract. Stateless for sync operations.
* Async operations (Start* and Get*) use an in-memory job store.
* No real OCR or document analysis is performed: every call returns a fixed
* stub Block list matching the AWS Textract wire format.
*
* @see <a href="https://docs.aws.amazon.com/textract/latest/dg/API_Operations.html">Textract API Reference</a>
*/
@ApplicationScoped
public class TextractService {
static final String MODEL_VERSION = "1.0";
private final ObjectMapper objectMapper;
/** In-memory async job store: jobId to jobType ("TEXT_DETECTION" or "DOCUMENT_ANALYSIS"). */
private final ConcurrentHashMap<String, String> asyncJobs = new ConcurrentHashMap<>();
@Inject
public TextractService(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
/**
* DetectDocumentText — returns a stub PAGE + LINE + WORD block hierarchy.
* Response shape: https://docs.aws.amazon.com/textract/latest/dg/API_DetectDocumentText.html
*/
public Response detectDocumentText() {
ObjectNode root = objectMapper.createObjectNode();
root.set("DocumentMetadata", buildDocumentMetadata(1));
root.set("Blocks", buildStubBlocks());
root.put("DetectDocumentTextModelVersion", MODEL_VERSION);
return Response.ok(root).build();
}
/**
* AnalyzeDocument — returns the same stub blocks; FeatureTypes are accepted but ignored.
* Response shape: https://docs.aws.amazon.com/textract/latest/dg/API_AnalyzeDocument.html
*/
public Response analyzeDocument() {
ObjectNode root = objectMapper.createObjectNode();
root.set("DocumentMetadata", buildDocumentMetadata(1));
root.set("Blocks", buildStubBlocks());
root.put("AnalyzeDocumentModelVersion", MODEL_VERSION);
return Response.ok(root).build();
}
/**
* StartDocumentTextDetection — enqueues a fake async job and immediately marks it SUCCEEDED.
* Response shape: https://docs.aws.amazon.com/textract/latest/dg/API_StartDocumentTextDetection.html
*/
public Response startDocumentTextDetection() {
String jobId = UUID.randomUUID().toString();
asyncJobs.put(jobId, "TEXT_DETECTION");
ObjectNode root = objectMapper.createObjectNode();
root.put("JobId", jobId);
return Response.ok(root).build();
}
/**
* GetDocumentTextDetection — returns SUCCEEDED + stub blocks for any known JobId.
* Response shape: https://docs.aws.amazon.com/textract/latest/dg/API_GetDocumentTextDetection.html
*/
public Response getDocumentTextDetection(String jobId) {
requireKnownJob(jobId, "TEXT_DETECTION");
ObjectNode root = objectMapper.createObjectNode();
root.put("JobStatus", "SUCCEEDED");
root.set("DocumentMetadata", buildDocumentMetadata(1));
root.set("Blocks", buildStubBlocks());
root.put("DetectDocumentTextModelVersion", MODEL_VERSION);
//after a successful job, we remove it to avoid memory growth
asyncJobs.remove(jobId);
return Response.ok(root).build();
}
/**
* StartDocumentAnalysis — enqueues a fake async job and immediately marks it SUCCEEDED.
* Response shape: https://docs.aws.amazon.com/textract/latest/dg/API_StartDocumentAnalysis.html
*/
public Response startDocumentAnalysis() {
String jobId = UUID.randomUUID().toString();
asyncJobs.put(jobId, "DOCUMENT_ANALYSIS");
ObjectNode root = objectMapper.createObjectNode();
root.put("JobId", jobId);
return Response.ok(root).build();
}
/**
* GetDocumentAnalysis — returns SUCCEEDED + stub blocks for any known JobId.
* Response shape: https://docs.aws.amazon.com/textract/latest/dg/API_GetDocumentAnalysis.html
*/
public Response getDocumentAnalysis(String jobId) {
requireKnownJob(jobId, "DOCUMENT_ANALYSIS");
ObjectNode root = objectMapper.createObjectNode();
root.put("JobStatus", "SUCCEEDED");
root.set("DocumentMetadata", buildDocumentMetadata(1));
root.set("Blocks", buildStubBlocks());
root.put("AnalyzeDocumentModelVersion", MODEL_VERSION);
//after a successful job, we remove it to avoid memory growth
asyncJobs.remove(jobId);
return Response.ok(root).build();
}
// Private helpers
private void requireKnownJob(String jobId, String expectedType) {
if (jobId == null || jobId.isBlank()) {
throw new AwsException("ValidationException", "JobId is required.", 400);
}
String type = asyncJobs.get(jobId);
if (type == null) {
throw new AwsException("InvalidJobIdException",
"An invalid job identifier was passed to an Amazon Textract operation.", 400);
}
if (!expectedType.equals(type)) {
throw new AwsException("InvalidJobIdException",
"Job was not started by the correct operation.", 400);
}
}
private ObjectNode buildDocumentMetadata(int pages) {
ObjectNode meta = objectMapper.createObjectNode();
meta.put("Pages", pages);
return meta;
}
/**
* Builds a minimal AWS-shaped Block hierarchy: PAGE to LINE to WORD.
* Each Block follows https://docs.aws.amazon.com/textract/latest/dg/API_Block.html
*/
private ArrayNode buildStubBlocks() {
ArrayNode blocks = objectMapper.createArrayNode();
String wordId = UUID.randomUUID().toString();
String lineId = UUID.randomUUID().toString();
String pageId = UUID.randomUUID().toString();
// WORD block
ObjectNode word = objectMapper.createObjectNode();
word.put("BlockType", "WORD");
word.put("Id", wordId);
word.put("Confidence", 99.9);
word.put("Text", "Floci");
word.set("Geometry", buildGeometry(0.1, 0.1, 0.15, 0.05));
word.put("Page", 1);
blocks.add(word);
// LINE block (child: WORD)
ObjectNode line = objectMapper.createObjectNode();
line.put("BlockType", "LINE");
line.put("Id", lineId);
line.put("Confidence", 99.9);
line.put("Text", "Floci");
line.set("Geometry", buildGeometry(0.1, 0.1, 0.15, 0.05));
line.set("Relationships", buildRelationships("CHILD", wordId));
line.put("Page", 1);
blocks.add(line);
// PAGE block (child: LINE)
ObjectNode page = objectMapper.createObjectNode();
page.put("BlockType", "PAGE");
page.put("Id", pageId);
page.put("Confidence", 99.9);
page.set("Geometry", buildGeometry(0.0, 0.0, 1.0, 1.0));
page.set("Relationships", buildRelationships("CHILD", lineId));
page.put("Page", 1);
blocks.add(page);
return blocks;
}
/**
* Builds a Geometry object with BoundingBox and a 4-point Polygon.
* @see <a href="https://docs.aws.amazon.com/textract/latest/dg/API_Geometry.html">Geometry</a>
*/
private ObjectNode buildGeometry(double left, double top, double width, double height) {
ObjectNode geometry = objectMapper.createObjectNode();
ObjectNode bbox = geometry.putObject("BoundingBox");
bbox.put("Width", width);
bbox.put("Height", height);
bbox.put("Left", left);
bbox.put("Top", top);
ArrayNode polygon = geometry.putArray("Polygon");
addPoint(polygon, left, top);
addPoint(polygon, left + width, top);
addPoint(polygon, left + width, top + height);
addPoint(polygon, left, top + height);
return geometry;
}
private void addPoint(ArrayNode polygon, double x, double y) {
ObjectNode point = polygon.addObject();
point.put("X", x);
point.put("Y", y);
}
/**
* Builds a single Relationship entry.
* @see <a href="https://docs.aws.amazon.com/textract/latest/dg/API_Relationship.html">Relationship</a>
*/
private ArrayNode buildRelationships(String type, String... childIds) {
ArrayNode relationships = objectMapper.createArrayNode();
ObjectNode rel = relationships.addObject();
rel.put("Type", type);
ArrayNode ids = rel.putArray("Ids");
for (String id : childIds) {
ids.add(id);
}
return relationships;
}
}