From 2547b9627a945fd413b9a02385fc76e75909ac2f Mon Sep 17 00:00:00 2001 From: Ryan Miles Date: Tue, 4 Jun 2024 16:07:20 -0700 Subject: [PATCH] Added aliases to FormattableWith annotation, added annotation processing into the ContentTypeAliasMap Caching maps, since they should only be created once. Closing up the scope of the alias map API --- .../java/cwms/cda/api/CountyController.java | 9 +---- .../main/java/cwms/cda/data/dto/County.java | 3 +- .../cda/formatters/ContentTypeAliasMap.java | 36 ++++++++++++++++--- .../java/cwms/cda/formatters/Formats.java | 2 +- .../annotations/FormattableWith.java | 1 + 5 files changed, 37 insertions(+), 14 deletions(-) diff --git a/cwms-data-api/src/main/java/cwms/cda/api/CountyController.java b/cwms-data-api/src/main/java/cwms/cda/api/CountyController.java index 56af159b7..86b3a6dbb 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/CountyController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/CountyController.java @@ -59,13 +59,6 @@ public class CountyController implements CrudHandler { private final MetricRegistry metrics; private final Histogram requestResultSize; - private static final ContentTypeAliasMap CONTENT_TYPE_ALIAS_MAP = new ContentTypeAliasMap(); - - static - { - CONTENT_TYPE_ALIAS_MAP.addContentType(Formats.JSON, new ContentType(Formats.JSONV2)); - CONTENT_TYPE_ALIAS_MAP.addContentType(Formats.DEFAULT, new ContentType(Formats.JSONV2)); - } /** * Sets up county endpoint metrics for the controller. @@ -100,7 +93,7 @@ public void getAll(@NotNull Context ctx) { CountyDao dao = new CountyDao(dsl); List counties = dao.getCounties(); String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeader(formatHeader, CONTENT_TYPE_ALIAS_MAP); + ContentType contentType = Formats.parseHeader(formatHeader, ContentTypeAliasMap.forDtoClass(County.class)); if (contentType == null) { throw new FormattingException("Format header could not be parsed"); } diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/County.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/County.java index 9bf6859a5..0f9ba3c15 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/County.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/County.java @@ -28,6 +28,7 @@ import cwms.cda.api.errors.RequiredFieldException; import cwms.cda.formatters.Formats; import cwms.cda.formatters.annotations.FormattableWith; +import cwms.cda.formatters.annotations.Formattables; import cwms.cda.formatters.json.JsonV2; import io.swagger.v3.oas.annotations.media.Schema; @@ -40,7 +41,7 @@ @Schema(description = "A representation of a county") @XmlRootElement(name = "county") @XmlAccessorType(XmlAccessType.FIELD) -@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class) +@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class, aliases = {Formats.DEFAULT, Formats.JSON}) public class County implements CwmsDTOBase { @XmlElement(name = "name") diff --git a/cwms-data-api/src/main/java/cwms/cda/formatters/ContentTypeAliasMap.java b/cwms-data-api/src/main/java/cwms/cda/formatters/ContentTypeAliasMap.java index d894742d1..63df35dd6 100644 --- a/cwms-data-api/src/main/java/cwms/cda/formatters/ContentTypeAliasMap.java +++ b/cwms-data-api/src/main/java/cwms/cda/formatters/ContentTypeAliasMap.java @@ -7,20 +7,48 @@ package cwms.cda.formatters; +import cwms.cda.data.dto.CwmsDTOBase; +import cwms.cda.formatters.annotations.FormattableWith; +import org.jetbrains.annotations.NotNull; + import java.util.HashMap; import java.util.Map; public final class ContentTypeAliasMap { private final Map _contentTypeMap = new HashMap<>(); + private static final Map, ContentTypeAliasMap> ALIAS_MAP = new HashMap<>(); - public ContentType getContentType(String alias) + private ContentTypeAliasMap() { - return _contentTypeMap.get(alias); } - public void addContentType(String alias, ContentType contentType) + private ContentTypeAliasMap(Class dtoClass) + { + FormattableWith[] formats = dtoClass.getAnnotationsByType(FormattableWith.class); + for (FormattableWith format : formats) + { + ContentType type = new ContentType(format.contentType()); + + for (String alias : format.aliases()) + { + _contentTypeMap.put(alias, type); + } + } + } + + public static ContentTypeAliasMap forDtoClass(@NotNull Class dtoClass) { - _contentTypeMap.put(alias, contentType); + return ALIAS_MAP.computeIfAbsent(dtoClass, ContentTypeAliasMap::new); + } + + public static ContentTypeAliasMap empty() + { + return new ContentTypeAliasMap(); + } + + public ContentType getContentType(String alias) + { + return _contentTypeMap.get(alias); } } diff --git a/cwms-data-api/src/main/java/cwms/cda/formatters/Formats.java b/cwms-data-api/src/main/java/cwms/cda/formatters/Formats.java index 012f0af5c..2b0a9f401 100644 --- a/cwms-data-api/src/main/java/cwms/cda/formatters/Formats.java +++ b/cwms-data-api/src/main/java/cwms/cda/formatters/Formats.java @@ -227,7 +227,7 @@ public static ContentType parseQueryParam(String queryParam) { public static ContentType parseHeader(String header) { - return parseHeader(header, new ContentTypeAliasMap()); + return parseHeader(header, ContentTypeAliasMap.empty()); } public static ContentType parseHeader(String header, ContentTypeAliasMap aliasMap) { ArrayList contentTypes = new ArrayList<>(); diff --git a/cwms-data-api/src/main/java/cwms/cda/formatters/annotations/FormattableWith.java b/cwms-data-api/src/main/java/cwms/cda/formatters/annotations/FormattableWith.java index efc4a6198..80733b23c 100644 --- a/cwms-data-api/src/main/java/cwms/cda/formatters/annotations/FormattableWith.java +++ b/cwms-data-api/src/main/java/cwms/cda/formatters/annotations/FormattableWith.java @@ -18,4 +18,5 @@ public @interface FormattableWith { public String contentType(); public Class formatter(); + String[] aliases() default {}; } \ No newline at end of file