mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
Recreate the reviewed Rekognition changes on the existing upstream main base using a verified commit. The complete file tree is identical to PR head 19f259b43e1ec8a3ce5fd2a50eb9c21e4ed604df, preserving the collection, pagination, persistence and model-validation fixes while removing the unsigned merge. Generate constraints for every modeled operation with constraints=True. Select the served operation in the handler, validate its request once, then dispatch. Preserve UnknownOperationException for unserved actions. Move constraint tests to the handler boundary and verify newly modeled operations are detected without maintaining an allowlist. Verified 189 Java tests, 55 generator tests, Checkstyle, generated-data, documentation and partition checks. Addresses review 5865133468.
This commit is contained in:
1 parent
e0f04b7323
commit
a27670c4bb
27 files changed
+3297
-47
No files matched your search
@@ -357,6 +357,7 @@ public interface EmulatorConfig {
|
||||
}
|
||||
|
||||
interface ServiceStorageOverrides {
|
||||
RekognitionStorageConfig rekognition();
|
||||
SsmStorageConfig ssm();
|
||||
SqsStorageConfig sqs();
|
||||
S3StorageConfig s3();
|
||||
@@ -409,6 +410,13 @@ public interface EmulatorConfig {
|
||||
SageMakerStorageConfig sagemaker();
|
||||
}
|
||||
|
||||
interface RekognitionStorageConfig {
|
||||
Optional<String> mode();
|
||||
|
||||
@WithDefault("5000")
|
||||
long flushIntervalMs();
|
||||
}
|
||||
|
||||
interface ApsStorageConfig {
|
||||
Optional<String> mode();
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ public final class RequestConstraintCheck {
|
||||
AwsException type(String path, String type);
|
||||
|
||||
/** {@code size} (a length, a count or a number's value) is outside {@code min}..{@code max}. */
|
||||
AwsException bounds(String path, String type, Long min, Long max, long size);
|
||||
AwsException bounds(String path, String type, Long min, Long max, Number size);
|
||||
|
||||
/** A string does not match the shape's {@code pattern}. */
|
||||
AwsException pattern(String path, String pattern);
|
||||
@@ -51,9 +51,11 @@ public final class RequestConstraintCheck {
|
||||
}
|
||||
|
||||
@Override
|
||||
public AwsException bounds(String path, String type, Long min, Long max, long size) {
|
||||
String measure = "integer".equals(type) || "long".equals(type) ? "value" : "length";
|
||||
return min != null && size < min
|
||||
public AwsException bounds(String path, String type, Long min, Long max, Number size) {
|
||||
boolean fractional = "float".equals(type) || "double".equals(type);
|
||||
String measure = fractional || "integer".equals(type) || "long".equals(type) ? "value" : "length";
|
||||
boolean below = min != null && (fractional ? size.doubleValue() < min : size.longValue() < min);
|
||||
return below
|
||||
? invalid(path, "Member must have " + measure + " greater than or equal to " + min)
|
||||
: invalid(path, "Member must have " + measure + " less than or equal to " + max);
|
||||
}
|
||||
@@ -140,15 +142,18 @@ public final class RequestConstraintCheck {
|
||||
case "list" -> value.isArray();
|
||||
case "string" -> value.isTextual();
|
||||
case "boolean" -> value.isBoolean();
|
||||
case "integer", "long" -> value.isIntegralNumber() && value.canConvertToLong();
|
||||
case "integer" -> value.isIntegralNumber() && value.canConvertToInt();
|
||||
case "long" -> value.isIntegralNumber() && value.canConvertToLong();
|
||||
case "float", "double" -> value.isNumber();
|
||||
default -> false;
|
||||
};
|
||||
if (!validType) {
|
||||
throw messages.type(path, type);
|
||||
}
|
||||
long size = switch (type) {
|
||||
Number size = switch (type) {
|
||||
case "string" -> value.textValue().codePointCount(0, value.textValue().length());
|
||||
case "integer", "long" -> value.longValue();
|
||||
case "float", "double" -> value.doubleValue();
|
||||
default -> value.size();
|
||||
};
|
||||
checkBounds(shape, path, type, size);
|
||||
@@ -196,10 +201,17 @@ public final class RequestConstraintCheck {
|
||||
}
|
||||
}
|
||||
|
||||
private void checkBounds(JsonNode shape, String path, String type, long size) {
|
||||
private void checkBounds(JsonNode shape, String path, String type, Number size) {
|
||||
Long min = shape.has("min") ? shape.get("min").asLong() : null;
|
||||
Long max = shape.has("max") ? shape.get("max").asLong() : null;
|
||||
if (min != null && size < min || max != null && size > max) {
|
||||
boolean outside;
|
||||
if ("float".equals(type) || "double".equals(type)) {
|
||||
double number = size.doubleValue();
|
||||
outside = !Double.isFinite(number) || min != null && number < min || max != null && number > max;
|
||||
} else {
|
||||
outside = min != null && size.longValue() < min || max != null && size.longValue() > max;
|
||||
}
|
||||
if (outside) {
|
||||
throw messages.bounds(path, type, min, max, size);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -667,7 +667,8 @@ public class ResolvedServiceCatalog {
|
||||
protocols(ServiceProtocol.JSON),
|
||||
Set.of("Comprehend_20171127."), Set.of("comprehend"), Set.of(), Set.of()),
|
||||
descriptor("rekognition", "rekognition", config.services().rekognition().enabled(), true,
|
||||
null, null, 5000L, null, ServiceProtocol.JSON,
|
||||
"rekognition", storageMode(config.storage().services().rekognition().mode(), config.storage().mode()),
|
||||
config.storage().services().rekognition().flushIntervalMs(), null, ServiceProtocol.JSON,
|
||||
protocols(ServiceProtocol.JSON),
|
||||
Set.of("RekognitionService."), Set.of("rekognition"), Set.of(), Set.of()),
|
||||
descriptor("pricing", "pricing", config.services().pricing().enabled(), true,
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ final class ApiGatewayDocumentationRequestShapes {
|
||||
}
|
||||
|
||||
@Override
|
||||
public AwsException bounds(String path, String type, Long min, Long max, long size) {
|
||||
public AwsException bounds(String path, String type, Long min, Long max, Number size) {
|
||||
return bad(path + " is outside the modeled bounds.");
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -33,9 +33,9 @@ final class CloudWatchLogsRequestShapes {
|
||||
}
|
||||
|
||||
@Override
|
||||
public AwsException bounds(String path, String type, Long min, Long max, long size) {
|
||||
public AwsException bounds(String path, String type, Long min, Long max, Number size) {
|
||||
String measure = "integer".equals(type) || "long".equals(type) ? "value" : "length";
|
||||
return max != null && size > max
|
||||
return max != null && size.longValue() > max
|
||||
? violation(path, "Member must have " + measure + " less than or equal to " + max)
|
||||
: violation(path, "Member must have " + measure + " greater than or equal to " + min);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,324 @@
|
||||
package io.floci.aws.services.rekognition;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
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.floci.aws.core.common.AiMockConfigLoader;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.Digests;
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.Resettable;
|
||||
import io.floci.aws.core.common.SignedMarkers;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.rekognition.model.FaceCollection;
|
||||
import io.floci.aws.services.rekognition.model.FaceCollection.IndexedFace;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Function;
|
||||
|
||||
/** Collection metadata is real state; face detection and matching use deterministic mock data. */
|
||||
@ApplicationScoped
|
||||
public class RekognitionCollectionService implements Resettable {
|
||||
static final String MODEL_VERSION = "7.0";
|
||||
private final StorageBackend<String, FaceCollection> collections;
|
||||
private final AccountAwareStorageBackend<Map<String, IndexedFace>> indexedFaces;
|
||||
private final SignedMarkers markers = new SignedMarkers(255);
|
||||
private final ObjectMapper mapper;
|
||||
private final RegionResolver regionResolver;
|
||||
private final AiMockConfigLoader mocks;
|
||||
|
||||
@Inject
|
||||
public RekognitionCollectionService(StorageFactory storageFactory, ObjectMapper mapper,
|
||||
RegionResolver regionResolver, AiMockConfigLoader mocks) {
|
||||
this.collections = storageFactory.create("rekognition", "rekognition-collections.json", new TypeReference<>() {});
|
||||
this.indexedFaces = storageFactory.createPartitioned("rekognition", "rekognition-faces.json",
|
||||
new TypeReference<Map<String, Map<String, IndexedFace>>>() {}, IndexedFace.class, 32);
|
||||
this.mapper = mapper;
|
||||
this.regionResolver = regionResolver;
|
||||
this.mocks = mocks;
|
||||
}
|
||||
|
||||
public synchronized ObjectNode createCollection(JsonNode request, String region) {
|
||||
String id = request.path("CollectionId").asText();
|
||||
Map<String, String> tags = tags(request);
|
||||
String key = key(region, id);
|
||||
if (collections.get(key).isPresent()) {
|
||||
throw new AwsException("ResourceAlreadyExistsException", "Collection already exists: " + id, 400);
|
||||
}
|
||||
Instant now = Instant.now();
|
||||
FaceCollection collection = new FaceCollection(id,
|
||||
regionResolver.buildArn("rekognition", region, "collection/" + id), MODEL_VERSION,
|
||||
now.getEpochSecond() + now.getNano() / 1_000_000_000.0, tags);
|
||||
collections.put(key, collection);
|
||||
return mapper.createObjectNode().put("StatusCode", 200).put("CollectionArn", collection.arn())
|
||||
.put("FaceModelVersion", collection.modelVersion());
|
||||
}
|
||||
|
||||
public synchronized ObjectNode describeCollection(JsonNode request, String region) {
|
||||
FaceCollection collection = requireCollection(request, region);
|
||||
return mapper.createObjectNode().put("CollectionARN", collection.arn())
|
||||
.put("CreationTimestamp", collection.creationTimestamp())
|
||||
.put("FaceCount", faces(region, collection).size()).put("UserCount", 0)
|
||||
.put("FaceModelVersion", collection.modelVersion());
|
||||
}
|
||||
|
||||
public synchronized ObjectNode deleteCollection(JsonNode request, String region) {
|
||||
FaceCollection collection = requireCollection(request, region);
|
||||
indexedFaces.delete(key(region, collection.id()));
|
||||
collections.delete(key(region, collection.id()));
|
||||
return mapper.createObjectNode().put("StatusCode", 200);
|
||||
}
|
||||
|
||||
public synchronized ObjectNode listCollections(JsonNode request, String region) {
|
||||
List<FaceCollection> all = collections.scan(key -> key.startsWith(region + ":"));
|
||||
PaginatedResult<FaceCollection> page = page(all, FaceCollection::id, request,
|
||||
"collections:" + regionResolver.getAccountId() + ":" + region);
|
||||
ObjectNode result = mapper.createObjectNode();
|
||||
ArrayNode ids = result.putArray("CollectionIds");
|
||||
ArrayNode versions = result.putArray("FaceModelVersions");
|
||||
page.items().forEach(collection -> {
|
||||
ids.add(collection.id());
|
||||
versions.add(collection.modelVersion());
|
||||
});
|
||||
putToken(result, page.nextToken());
|
||||
return result;
|
||||
}
|
||||
|
||||
public synchronized ObjectNode indexFaces(JsonNode request, String region, String mockKey) {
|
||||
FaceCollection collection = requireCollection(request, region);
|
||||
String externalId = request.path("ExternalImageId").asText(null);
|
||||
int maxFaces = request.path("MaxFaces").asInt(100);
|
||||
String imageKey = imageKey(request.get("Image"));
|
||||
List<ObjectNode> details = faceDetails(mockKey);
|
||||
details.sort(Comparator.comparingDouble(RekognitionCollectionService::area).reversed());
|
||||
String collectionKey = key(region, collection.id());
|
||||
Map<String, IndexedFace> faces = new HashMap<>(faces(region, collection));
|
||||
Map<FaceIdentity, IndexedFace> existing = new HashMap<>();
|
||||
faces.values().forEach(face -> existing.put(new FaceIdentity(face.imageKey(), face.position(),
|
||||
face.face().path("ExternalImageId").asText(null)), face));
|
||||
String imageId = faces.values().stream().filter(face -> face.imageKey().equals(imageKey))
|
||||
.map(face -> face.face().path("ImageId").asText()).findFirst().orElseGet(() -> UUID.randomUUID().toString());
|
||||
Map<String, IndexedFace> additions = new HashMap<>();
|
||||
ObjectNode result = mapper.createObjectNode().put("FaceModelVersion", collection.modelVersion());
|
||||
ArrayNode records = result.putArray("FaceRecords");
|
||||
ArrayNode unindexed = result.putArray("UnindexedFaces");
|
||||
for (int i = 0; i < details.size(); i++) {
|
||||
ObjectNode detail = details.get(i);
|
||||
if (i >= Math.min(maxFaces, 100)) {
|
||||
ObjectNode rejected = unindexed.addObject();
|
||||
rejected.set("FaceDetail", detail.deepCopy());
|
||||
rejected.putArray("Reasons").add("EXCEEDS_MAX_FACES");
|
||||
continue;
|
||||
}
|
||||
String position = position(detail);
|
||||
FaceIdentity identity = new FaceIdentity(imageKey, position, externalId);
|
||||
IndexedFace indexed = existing.get(identity);
|
||||
if (indexed == null) {
|
||||
ObjectNode face = mapper.createObjectNode().put("FaceId", UUID.randomUUID().toString())
|
||||
.put("ImageId", imageId).put("Confidence", detail.path("Confidence").asDouble(99.0))
|
||||
.put("IndexFacesModelVersion", collection.modelVersion());
|
||||
face.set("BoundingBox", detail.path("BoundingBox").deepCopy());
|
||||
if (externalId != null) {
|
||||
face.put("ExternalImageId", externalId);
|
||||
}
|
||||
indexed = new IndexedFace(imageKey, position, face, detail.deepCopy());
|
||||
String id = face.path("FaceId").asText();
|
||||
faces.put(id, indexed);
|
||||
additions.put(id, indexed);
|
||||
existing.put(identity, indexed);
|
||||
}
|
||||
ObjectNode record = records.addObject();
|
||||
record.set("Face", indexed.face().deepCopy());
|
||||
record.set("FaceDetail", indexed.detail().deepCopy());
|
||||
}
|
||||
if (indexedFaces.writesEntries()) {
|
||||
additions.forEach((id, face) -> indexedFaces.putEntry(collectionKey, id, face));
|
||||
} else if (!additions.isEmpty()) {
|
||||
indexedFaces.put(collectionKey, Map.copyOf(faces));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public synchronized ObjectNode listFaces(JsonNode request, String region) {
|
||||
FaceCollection collection = requireCollection(request, region);
|
||||
List<String> ids = faceIds(request);
|
||||
String userId = request.path("UserId").asText(null);
|
||||
List<IndexedFace> faces = faces(region, collection).values().stream()
|
||||
.filter(face -> ids == null || ids.contains(face.face().path("FaceId").asText()))
|
||||
.filter(face -> userId == null || userId.equals(face.face().path("UserId").asText(null))).toList();
|
||||
PaginatedResult<IndexedFace> page = page(faces, face -> face.face().path("FaceId").asText(), request,
|
||||
"faces:" + collection.arn() + ":" + Digests.sha256Hex(String.valueOf(ids) + ":" + userId));
|
||||
ObjectNode result = mapper.createObjectNode().put("FaceModelVersion", collection.modelVersion());
|
||||
ArrayNode entries = result.putArray("Faces");
|
||||
page.items().forEach(face -> entries.add(face.face().deepCopy()));
|
||||
putToken(result, page.nextToken());
|
||||
return result;
|
||||
}
|
||||
|
||||
public synchronized ObjectNode deleteFaces(JsonNode request, String region) {
|
||||
FaceCollection collection = requireCollection(request, region);
|
||||
List<String> ids = faceIds(request);
|
||||
ObjectNode result = mapper.createObjectNode();
|
||||
ArrayNode deleted = result.putArray("DeletedFaces");
|
||||
ArrayNode unsuccessful = result.putArray("UnsuccessfulFaceDeletions");
|
||||
String collectionKey = key(region, collection.id());
|
||||
Map<String, IndexedFace> remaining = new HashMap<>(faces(region, collection));
|
||||
for (String id : new LinkedHashSet<>(ids)) {
|
||||
if (remaining.remove(id) != null) {
|
||||
deleted.add(id);
|
||||
if (indexedFaces.writesEntries()) {
|
||||
indexedFaces.removeEntry(collectionKey, id);
|
||||
}
|
||||
} else {
|
||||
unsuccessful.addObject().put("FaceId", id).putArray("Reasons").add("FACE_NOT_FOUND");
|
||||
}
|
||||
}
|
||||
if (!indexedFaces.writesEntries() && !deleted.isEmpty()) {
|
||||
indexedFaces.put(collectionKey, Map.copyOf(remaining));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public synchronized ObjectNode searchFaces(JsonNode request, String region) {
|
||||
FaceCollection collection = requireCollection(request, region);
|
||||
String id = request.path("FaceId").asText();
|
||||
Map<String, IndexedFace> faces = faces(region, collection);
|
||||
IndexedFace source = faces.get(id);
|
||||
if (source == null) {
|
||||
throw invalid("FaceId is not indexed in this collection.");
|
||||
}
|
||||
ObjectNode result = matches(collection, faces, source.imageKey(), source.position(), id, request);
|
||||
return result.put("SearchedFaceId", id);
|
||||
}
|
||||
|
||||
public synchronized ObjectNode searchFacesByImage(JsonNode request, String region, String mockKey) {
|
||||
FaceCollection collection = requireCollection(request, region);
|
||||
ObjectNode detail = faceDetails(mockKey).stream().max(Comparator.comparingDouble(RekognitionCollectionService::area))
|
||||
.orElseThrow(() -> invalid("There are no faces in the image. Should be at least 1."));
|
||||
ObjectNode result = matches(collection, faces(region, collection), imageKey(request.get("Image")),
|
||||
position(detail), null, request);
|
||||
result.set("SearchedFaceBoundingBox", detail.path("BoundingBox").deepCopy());
|
||||
return result.put("SearchedFaceConfidence", detail.path("Confidence").asDouble(99.0));
|
||||
}
|
||||
|
||||
private ObjectNode matches(FaceCollection collection, Map<String, IndexedFace> faces, String imageKey,
|
||||
String position, String excludedId, JsonNode request) {
|
||||
int limit = request.path("MaxFaces").asInt(4096);
|
||||
ObjectNode result = mapper.createObjectNode().put("FaceModelVersion", collection.modelVersion());
|
||||
ArrayNode matches = result.putArray("FaceMatches");
|
||||
faces.values().stream().filter(face -> face.imageKey().equals(imageKey) && face.position().equals(position))
|
||||
.filter(face -> !face.face().path("FaceId").asText().equals(excludedId))
|
||||
.sorted(Comparator.comparing(face -> face.face().path("FaceId").asText())).limit(limit)
|
||||
.forEach(face -> {
|
||||
ObjectNode match = matches.addObject().put("Similarity", 100.0);
|
||||
match.set("Face", face.face().deepCopy());
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<ObjectNode> faceDetails(String mockKey) {
|
||||
JsonNode details = mocks.lookup("rekognition", mockKey, "DetectFaces")
|
||||
.map(response -> response.path("FaceDetails")).orElse(mapper.createArrayNode());
|
||||
List<ObjectNode> result = new ArrayList<>();
|
||||
for (JsonNode detail : details) {
|
||||
if (detail instanceof ObjectNode object && object.path("BoundingBox").isObject()) {
|
||||
result.add(object.deepCopy());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static double area(JsonNode detail) {
|
||||
return detail.path("BoundingBox").path("Width").asDouble() * detail.path("BoundingBox").path("Height").asDouble();
|
||||
}
|
||||
|
||||
private static String position(JsonNode detail) {
|
||||
JsonNode box = detail.path("BoundingBox");
|
||||
return box.path("Left").asDouble() + ":" + box.path("Top").asDouble() + ":"
|
||||
+ box.path("Width").asDouble() + ":" + box.path("Height").asDouble();
|
||||
}
|
||||
|
||||
private static String imageKey(JsonNode image) {
|
||||
if (image.hasNonNull("Bytes")) {
|
||||
return Digests.sha256Hex(Base64.getEncoder().encodeToString(Base64.getDecoder().decode(image.get("Bytes").asText())));
|
||||
}
|
||||
JsonNode s3 = image.path("S3Object");
|
||||
return Digests.sha256Hex(s3.path("Bucket").asText() + "\n" + s3.path("Name").asText() + "\n" + s3.path("Version").asText());
|
||||
}
|
||||
|
||||
private <T> PaginatedResult<T> page(List<T> all, Function<T, String> cursor, JsonNode request, String scope) {
|
||||
int limit = request.path("MaxResults").asInt(1000);
|
||||
String token = request.path("NextToken").asText(null);
|
||||
PaginatedResult<T> page = Pagination.slice(all, cursor, Math.max(1, limit), token, markers, scope,
|
||||
ignored -> new AwsException("InvalidPaginationTokenException", "Invalid NextToken.", 400));
|
||||
if (limit == 0) {
|
||||
String continuation = page.items().isEmpty() ? null : token == null ? markers.issue(scope, "") : token;
|
||||
return new PaginatedResult<>(List.of(), continuation);
|
||||
}
|
||||
return page;
|
||||
}
|
||||
|
||||
private static void putToken(ObjectNode result, String token) {
|
||||
if (token != null) {
|
||||
result.put("NextToken", token);
|
||||
}
|
||||
}
|
||||
|
||||
private FaceCollection requireCollection(JsonNode request, String region) {
|
||||
String id = request.path("CollectionId").asText();
|
||||
return collections.get(key(region, id)).orElseThrow(() ->
|
||||
new AwsException("ResourceNotFoundException", "Collection not found: " + id, 400));
|
||||
}
|
||||
|
||||
private Map<String, IndexedFace> faces(String region, FaceCollection collection) {
|
||||
return indexedFaces.get(key(region, collection.id())).orElse(Map.of());
|
||||
}
|
||||
|
||||
private record FaceIdentity(String imageKey, String position, String externalId) {}
|
||||
|
||||
private static String key(String region, String id) {
|
||||
return region + ":" + id;
|
||||
}
|
||||
|
||||
private static List<String> faceIds(JsonNode request) {
|
||||
JsonNode ids = request.get("FaceIds");
|
||||
if (ids == null || ids.isNull()) {
|
||||
return null;
|
||||
}
|
||||
List<String> result = new ArrayList<>();
|
||||
ids.forEach(id -> result.add(id.asText()));
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Map<String, String> tags(JsonNode request) {
|
||||
Map<String, String> result = new HashMap<>();
|
||||
request.path("Tags").properties().forEach(tag -> result.put(tag.getKey(), tag.getValue().asText()));
|
||||
return Map.copyOf(result);
|
||||
}
|
||||
|
||||
private static AwsException invalid(String message) {
|
||||
return new AwsException("InvalidParameterException", message, 400);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void clear() {
|
||||
indexedFaces.clear();
|
||||
collections.clear();
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import jakarta.ws.rs.core.Response;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
/**
|
||||
* JSON 1.1 handler for Amazon Rekognition API operations.
|
||||
* Dispatches X-Amz-Target: RekognitionService.* actions to {@link RekognitionService}.
|
||||
@@ -19,37 +20,55 @@ public class RekognitionJsonHandler {
|
||||
private static final Logger LOG = Logger.getLogger(RekognitionJsonHandler.class);
|
||||
private final RekognitionService rekognitionService;
|
||||
private final RekognitionImageValidator imageValidator;
|
||||
private final RekognitionCollectionService collectionService;
|
||||
@Inject
|
||||
public RekognitionJsonHandler(RekognitionService rekognitionService, RekognitionImageValidator imageValidator) {
|
||||
public RekognitionJsonHandler(RekognitionService rekognitionService, RekognitionImageValidator imageValidator,
|
||||
RekognitionCollectionService collectionService) {
|
||||
this.rekognitionService = rekognitionService;
|
||||
this.imageValidator = imageValidator;
|
||||
this.collectionService = collectionService;
|
||||
}
|
||||
/**
|
||||
* Dispatches Rekognition actions received via the AwsJson11Controller.
|
||||
* Only sync image-analysis actions are implemented; face-collection persistence
|
||||
* (CreateCollection/IndexFaces/SearchFaces), custom-model training (Projects/
|
||||
* Sync image-analysis and face collections are implemented; custom-model training (Projects/
|
||||
* Datasets/ProjectVersions), live video (StreamProcessor), async video jobs
|
||||
* (the Start and Get pairs for Celebrity/Content/Face/Label/Person/Segment/Text
|
||||
* detection), and Face Liveness sessions are out of scope.
|
||||
*/
|
||||
public Response handle(String action, JsonNode request, String region) {
|
||||
LOG.debugv("Rekognition action: {0}", action);
|
||||
return switch (action) {
|
||||
case "DetectLabels" -> rekognitionService.detectLabels(validImage(action, request, region));
|
||||
case "DetectFaces" -> rekognitionService.detectFaces(validImage(action, request, region));
|
||||
case "DetectText" -> rekognitionService.detectText(validImage(action, request, region));
|
||||
case "CompareFaces" -> {
|
||||
Supplier<Response> operation = switch (action) {
|
||||
case "CreateCollection" -> () -> Response.ok(collectionService.createCollection(request, region)).build();
|
||||
case "DeleteCollection" -> () -> Response.ok(collectionService.deleteCollection(request, region)).build();
|
||||
case "DescribeCollection" -> () -> Response.ok(collectionService.describeCollection(request, region)).build();
|
||||
case "ListCollections" -> () -> Response.ok(collectionService.listCollections(request, region)).build();
|
||||
case "IndexFaces" -> () -> Response.ok(collectionService.indexFaces(request, region,
|
||||
validImage(action, request, region))).build();
|
||||
case "ListFaces" -> () -> Response.ok(collectionService.listFaces(request, region)).build();
|
||||
case "DeleteFaces" -> () -> Response.ok(collectionService.deleteFaces(request, region)).build();
|
||||
case "SearchFaces" -> () -> Response.ok(collectionService.searchFaces(request, region)).build();
|
||||
case "SearchFacesByImage" -> () -> Response.ok(collectionService.searchFacesByImage(request, region,
|
||||
validImage(action, request, region))).build();
|
||||
case "DetectLabels" -> () -> rekognitionService.detectLabels(validImage(action, request, region));
|
||||
case "DetectFaces" -> () -> rekognitionService.detectFaces(validImage(action, request, region));
|
||||
case "DetectText" -> () -> rekognitionService.detectText(validImage(action, request, region));
|
||||
case "CompareFaces" -> () -> {
|
||||
String sourceKey = requireImage(request, "SourceImage");
|
||||
requireImage(request, "TargetImage");
|
||||
imageValidator.validate(action, request, region, "SourceImage", "TargetImage");
|
||||
yield rekognitionService.compareFaces(sourceKey);
|
||||
}
|
||||
case "DetectModerationLabels" -> rekognitionService.detectModerationLabels(validImage(action, request, region));
|
||||
default -> Response.status(400)
|
||||
return rekognitionService.compareFaces(sourceKey);
|
||||
};
|
||||
case "DetectModerationLabels" -> () -> rekognitionService.detectModerationLabels(validImage(action, request, region));
|
||||
default -> null;
|
||||
};
|
||||
if (operation == null) {
|
||||
return Response.status(400)
|
||||
.entity(new AwsErrorResponse("UnknownOperationException",
|
||||
"Unknown operation: RekognitionService." + action))
|
||||
.build();
|
||||
};
|
||||
}
|
||||
RekognitionRequestShapes.check(action, request);
|
||||
return operation.get();
|
||||
}
|
||||
/** The image's mock-response key, once the image has the shape and content Rekognition accepts. */
|
||||
private String validImage(String action, JsonNode request, String region) {
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package io.floci.aws.services.rekognition;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RequestConstraintCheck;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** Request constraints vendored from botocore by regen_rekognition_shapes.py. */
|
||||
final class RekognitionRequestShapes {
|
||||
|
||||
private static final RequestConstraintCheck CHECK = RequestConstraintCheck.load(
|
||||
"aws/rekognition-request-shapes.json", "Rekognition request shapes",
|
||||
new RequestConstraintCheck.Messages() {
|
||||
@Override
|
||||
public AwsException type(String path, String type) {
|
||||
return new AwsException("SerializationException", path + " must be a " + type + ".", 400);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AwsException bounds(String path, String type, Long min, Long max, Number size) {
|
||||
return invalid(path + " is outside the modeled bounds.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public AwsException pattern(String path, String pattern) {
|
||||
return invalid(path + " does not match the modeled pattern.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public AwsException enumValue(String path, List<String> values) {
|
||||
return invalid(path + " must be one of " + values + ".");
|
||||
}
|
||||
|
||||
@Override
|
||||
public AwsException required(String path) {
|
||||
return invalid(path + " is required.");
|
||||
}
|
||||
});
|
||||
|
||||
private RekognitionRequestShapes() {
|
||||
}
|
||||
|
||||
static void check(String operation, JsonNode request) {
|
||||
CHECK.check(operation, request);
|
||||
}
|
||||
|
||||
private static AwsException invalid(String message) {
|
||||
return new AwsException("InvalidParameterException", message, 400);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package io.floci.aws.services.rekognition.model;
|
||||
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RegisterForReflection
|
||||
public record FaceCollection(String id, String arn, String modelVersion, double creationTimestamp,
|
||||
Map<String, String> tags) {
|
||||
|
||||
@RegisterForReflection
|
||||
public record IndexedFace(String imageKey, String position, ObjectNode face, ObjectNode detail) {}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ final class ResourceGroupsRequestShapes {
|
||||
}
|
||||
|
||||
@Override
|
||||
public AwsException bounds(String path, String type, Long min, Long max, long size) {
|
||||
public AwsException bounds(String path, String type, Long min, Long max, Number size) {
|
||||
return bad(path + " is outside the modeled bounds.");
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ final class SecurityHubRequestShapes {
|
||||
}
|
||||
|
||||
@Override
|
||||
public AwsException bounds(String path, String type, Long min, Long max, long size) {
|
||||
public AwsException bounds(String path, String type, Long min, Long max, Number size) {
|
||||
if ("BatchImportFindings.Findings".equals(path)) {
|
||||
return invalid("Findings must contain between " + min + " and " + max + " findings.");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user