Skip to content

Commit

Permalink
Added aliases to FormattableWith annotation, added annotation process…
Browse files Browse the repository at this point in the history
…ing into the ContentTypeAliasMap

Caching maps, since they should only be created once.
Closing up the scope of the alias map API
  • Loading branch information
RyanM-RMA committed Jun 4, 2024
1 parent 93d080a commit 2547b96
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -100,7 +93,7 @@ public void getAll(@NotNull Context ctx) {
CountyDao dao = new CountyDao(dsl);
List<County> 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");
}
Expand Down
3 changes: 2 additions & 1 deletion cwms-data-api/src/main/java/cwms/cda/data/dto/County.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, ContentType> _contentTypeMap = new HashMap<>();
private static final Map<Class<? extends CwmsDTOBase>, 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<? extends CwmsDTOBase> 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<? extends CwmsDTOBase> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContentType> contentTypes = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
public @interface FormattableWith {
public String contentType();
public Class<? extends OutputFormatter> formatter();
String[] aliases() default {};
}

0 comments on commit 2547b96

Please sign in to comment.