From 861be28f8508e861ddac479ba840410464947978 Mon Sep 17 00:00:00 2001 From: Adam Korynta <47677856+adamkorynta@users.noreply.github.com> Date: Wed, 4 Sep 2024 15:52:58 -0700 Subject: [PATCH] handle null content type headers the same as */* (#861) Also updated DTO's and controllers for consistency, removing deprecated methods. --- .../java/cwms/cda/api/BasinController.java | 31 +++--- .../cda/api/BinaryTimeSeriesController.java | 12 +-- .../java/cwms/cda/api/BlobController.java | 5 +- .../java/cwms/cda/api/CatalogController.java | 2 +- .../java/cwms/cda/api/ClobController.java | 9 +- .../main/java/cwms/cda/api/Controllers.java | 2 +- .../api/DownstreamLocationsGetController.java | 2 +- .../cwms/cda/api/EmbankmentController.java | 9 +- .../cda/api/ForecastInstanceController.java | 15 +-- .../cwms/cda/api/ForecastSpecController.java | 15 +-- .../java/cwms/cda/api/LevelsController.java | 10 +- .../cda/api/LocationCategoryController.java | 9 +- .../java/cwms/cda/api/LocationController.java | 13 +-- .../cwms/cda/api/LocationGroupController.java | 23 ++-- .../cwms/cda/api/LookupTypeController.java | 14 ++- .../java/cwms/cda/api/PoolController.java | 4 +- .../java/cwms/cda/api/ProjectController.java | 5 +- .../java/cwms/cda/api/PropertyController.java | 20 ++-- .../java/cwms/cda/api/RatingController.java | 28 ++--- .../cda/api/RatingMetadataController.java | 2 +- .../cwms/cda/api/RatingSpecController.java | 4 +- .../cda/api/RatingTemplateController.java | 4 +- .../cda/api/SpecifiedLevelController.java | 7 +- .../cwms/cda/api/StandardTextController.java | 9 +- .../java/cwms/cda/api/StreamController.java | 9 +- .../cda/api/StreamLocationController.java | 12 +-- .../cwms/cda/api/StreamReachController.java | 5 +- .../cda/api/TextTimeSeriesController.java | 15 ++- .../cda/api/TimeSeriesCategoryController.java | 9 +- .../cda/api/TimeSeriesGroupController.java | 17 ++- ...eSeriesIdentifierDescriptorController.java | 9 +- .../cda/api/TimeSeriesRecentController.java | 2 +- .../cda/api/TurbineChangesGetController.java | 3 +- .../cda/api/TurbineChangesPostController.java | 3 +- .../java/cwms/cda/api/TurbineController.java | 9 +- .../api/UpstreamLocationsGetController.java | 5 +- .../api/location/kind/OutletController.java | 9 +- .../kind/VirtualOutletController.java | 6 +- .../kind/VirtualOutletCreateController.java | 3 +- .../cda/api/project/ProjectLockRequest.java | 3 +- .../WaterContractCatalogController.java | 2 +- .../watersupply/WaterContractController.java | 2 +- .../WaterContractCreateController.java | 2 +- .../WaterContractTypeCatalogController.java | 2 +- .../WaterContractTypeCreateController.java | 2 +- .../WaterContractUpdateController.java | 2 +- .../WaterUserCatalogController.java | 2 +- .../api/watersupply/WaterUserController.java | 2 +- .../WaterUserCreateController.java | 2 +- .../WaterUserUpdateController.java | 2 +- .../src/main/java/cwms/cda/data/dto/Pool.java | 2 +- .../main/java/cwms/cda/data/dto/Pools.java | 2 +- .../cwms/cda/data/dto/SpecifiedLevel.java | 2 +- .../dto/TimeSeriesIdentifierDescriptor.java | 2 +- .../cda/data/dto/basinconnectivity/Basin.java | 3 +- .../binarytimeseries/BinaryTimeSeries.java | 2 +- .../data/dto/forecast/ForecastInstance.java | 2 +- .../cda/data/dto/forecast/ForecastSpec.java | 2 +- .../dto/project/ProjectChildLocations.java | 2 +- .../data/dto/rating/RatingMetadataList.java | 2 +- .../cwms/cda/data/dto/rating/RatingSpec.java | 2 +- .../cwms/cda/data/dto/rating/RatingSpecs.java | 2 +- .../cda/data/dto/rating/RatingTemplate.java | 2 +- .../cda/data/dto/rating/RatingTemplates.java | 2 +- .../texttimeseries/StandardTextCatalog.java | 2 +- .../dto/texttimeseries/StandardTextValue.java | 2 +- .../dto/texttimeseries/TextTimeSeries.java | 2 +- .../dto/watersupply/WaterUserContract.java | 2 +- .../java/cwms/cda/formatters/Formats.java | 101 +++++++----------- .../java/cwms/cda/api/ClobControllerTest.java | 2 +- .../cwms/cda/api/LevelControllerTest.java | 8 +- .../api/LocationCategoryControllerTestIT.java | 5 +- .../cwms/cda/api/LocationControllerTest.java | 6 +- .../cda/api/LocationControllerTestIT.java | 3 +- .../api/LocationGroupControllerTestIT.java | 8 +- .../TimeSeriesCategoryControllerTestIT.java | 2 +- .../api/TimeSeriesGroupControllerTestIT.java | 6 +- .../cda/api/auth/ApiKeyControllerTestIT.java | 3 +- .../test/java/cwms/cda/data/dto/ClobTest.java | 8 +- .../cwms/cda/data/dto/LocationLevelTest.java | 2 +- .../java/cwms/cda/data/dto/LocationTest.java | 5 +- .../cda/data/dto/TimeSeriesGroupTest.java | 2 +- .../TimeSeriesIdentifierDescriptorTest.java | 2 +- .../dto/catalog/LocationCatalogEntryTest.java | 4 +- .../catalog/TimeseriesCatalogEntryTest.java | 8 +- .../dto/forecast/ForecastInstanceTest.java | 2 +- .../data/dto/forecast/ForecastSpecTest.java | 2 +- .../java/cwms/cda/formatters/FormatsTest.java | 62 ++++++----- .../java/cwms/cda/formatters/JsonV2Test.java | 4 +- .../cwms/cda/data/sql/load_testusers.sql | 6 +- 90 files changed, 302 insertions(+), 379 deletions(-) diff --git a/cwms-data-api/src/main/java/cwms/cda/api/BasinController.java b/cwms-data-api/src/main/java/cwms/cda/api/BasinController.java index 946bbdbdb..215bb2ab6 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/BasinController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/BasinController.java @@ -119,26 +119,26 @@ public void getAll(@NotNull Context ctx) { String units = ctx.queryParamAsClass(UNIT, String.class).getOrDefault(UnitSystem.EN.value()); String office = ctx.queryParam(OFFICE); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : - Formats.JSONV1; - - ContentType contentType = Formats.parseHeader(formatHeader, Basin.class); + String formatHeader = ctx.header(Header.ACCEPT); String result; - - ctx.contentType(contentType.toString()); - if (contentType.getType().equals(Formats.NAMED_PGJSON)) { + ContentType contentType; + if (formatHeader != null && formatHeader.contains(Formats.NAMED_PGJSON)) { + contentType = Formats.parseHeader(formatHeader, Basin.class); + ctx.contentType(contentType.toString()); BasinDao basinDao = new BasinDao(dsl); List basins = basinDao.getAllBasins(units, office); result = Formats.format(contentType, basins, Basin.class); ctx.result(result); ctx.status(HttpServletResponse.SC_OK); } else { + contentType = Formats.parseHeader(formatHeader, cwms.cda.data.dto.basin.Basin.class); + ctx.contentType(contentType.toString()); cwms.cda.data.dao.basin.BasinDao basinDao = new cwms.cda.data.dao.basin.BasinDao(dsl); List basins = basinDao.getAllBasins(office, units); result = Formats.format(contentType, basins, cwms.cda.data.dto.basin.Basin.class); - ctx.result(result); - ctx.status(HttpServletResponse.SC_OK); } + ctx.result(result); + ctx.status(HttpServletResponse.SC_OK); } catch (SQLException ex) { CdaError error = new CdaError("Error retrieving all basins"); LOGGER.log(Level.SEVERE, "Error retrieving all basins", ex); @@ -189,18 +189,14 @@ public void getOne(@NotNull Context ctx, @NotNull String name) { String units = ctx.queryParamAsClass(UNIT, String.class).getOrDefault(UnitSystem.EN.value()); String office = ctx.queryParam(OFFICE); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : - Formats.NAMED_PGJSON; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, Basin.class); ctx.contentType(contentType.toString()); String result; - if (contentType.getType().equals(Formats.NAMED_PGJSON)) { BasinDao basinDao = new BasinDao(dsl); Basin basin = basinDao.getBasin(name, units, office); result = Formats.format(contentType, Collections.singletonList(basin), Basin.class); - ctx.result(result); - ctx.status(HttpServletResponse.SC_OK); } else { cwms.cda.data.dao.basin.BasinDao basinDao = new cwms.cda.data.dao.basin.BasinDao(dsl); CwmsId basinId = new CwmsId.Builder() @@ -209,9 +205,9 @@ public void getOne(@NotNull Context ctx, @NotNull String name) { .build(); cwms.cda.data.dto.basin.Basin basin = basinDao.getBasin(basinId, units); result = Formats.format(contentType, basin); - ctx.result(result); - ctx.status(HttpServletResponse.SC_OK); } + ctx.result(result); + ctx.status(HttpServletResponse.SC_OK); } catch (SQLException ex) { CdaError error = new CdaError("Error retrieving " + name); String errorMsg = "Error retrieving " + name; @@ -270,8 +266,7 @@ public void update(@NotNull Context ctx, @NotNull String name) { @Override public void create(@NotNull Context ctx) { DSLContext dsl = getDslContext(ctx); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : - Formats.JSONV1; + String formatHeader = ctx.req.getContentType(); ContentType contentType = Formats.parseHeader(formatHeader, cwms.cda.data.dto.basin.Basin.class); ctx.contentType(contentType.toString()); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/BinaryTimeSeriesController.java b/cwms-data-api/src/main/java/cwms/cda/api/BinaryTimeSeriesController.java index f81bb64b2..a1b3c46b5 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/BinaryTimeSeriesController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/BinaryTimeSeriesController.java @@ -137,7 +137,7 @@ public void getAll(@NotNull Context ctx) { int kiloByteLimit = Integer.parseInt(System.getProperty("cda.api.ts.bin.max.length.kB", "64")); String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, ""); + ContentType contentType = Formats.parseHeader(formatHeader, BinaryTimeSeries.class); try (Timer.Context ignored = markAndTime(GET_ALL)) { String dateToken = "{date_token}"; String path = ctx.path(); @@ -200,9 +200,8 @@ public void create(@NotNull Context ctx) { try (Timer.Context ignored = markAndTime(CREATE)) { DSLContext dsl = getDslContext(ctx); - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSONV2; - ContentType contentType = Formats.parseHeader(formatHeader); + String formatHeader = ctx.req.getContentType(); + ContentType contentType = Formats.parseHeader(formatHeader, BinaryTimeSeries.class); BinaryTimeSeries tts = deserializeBody(ctx, contentType); TimeSeriesBinaryDao dao = getDao(dsl); @@ -238,9 +237,8 @@ public void update(@NotNull Context ctx, @NotNull String oldBinaryTimeSeriesId) try (Timer.Context ignored = markAndTime(UPDATE)) { boolean maxVersion = true; boolean replaceAll = ctx.queryParamAsClass(REPLACE_ALL, Boolean.class).getOrDefault(false); - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSONV2; - ContentType contentType = Formats.parseHeader(formatHeader); + String formatHeader = ctx.req.getContentType(); + ContentType contentType = Formats.parseHeader(formatHeader, BinaryTimeSeries.class); BinaryTimeSeries tts = deserializeBody(ctx, contentType); DSLContext dsl = getDslContext(ctx); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/BlobController.java b/cwms-data-api/src/main/java/cwms/cda/api/BlobController.java index 323d1bf73..e026d922c 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/BlobController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/BlobController.java @@ -183,10 +183,7 @@ public void getOne(@NotNull Context ctx, @NotNull String blobId) { public void create(@NotNull Context ctx) { try (final Timer.Context ignored = markAndTime(CREATE)) { DSLContext dsl = getDslContext(ctx); - - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSON; - + String formatHeader = ctx.req.getContentType(); boolean failIfExists = ctx.queryParamAsClass(FAIL_IF_EXISTS, Boolean.class).getOrDefault(true); ContentType contentType = Formats.parseHeader(formatHeader, Blob.class); Blob blob = Formats.parseContent(contentType, ctx.bodyAsInputStream(), Blob.class); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/CatalogController.java b/cwms-data-api/src/main/java/cwms/cda/api/CatalogController.java index f3db4bbe3..fe6f715ae 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/CatalogController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/CatalogController.java @@ -232,7 +232,7 @@ public void getOne(@NotNull Context ctx, @NotNull String dataSet) { String.class, null, metrics, name(CatalogController.class.getName(), GET_ONE)); String acceptHeader = ctx.header(ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(acceptHeader, null); + ContentType contentType = Formats.parseHeader(acceptHeader, Catalog.class); Catalog cat = null; if (TIMESERIES.equalsIgnoreCase(valDataSet)) { TimeSeriesDao tsDao = new TimeSeriesDaoImpl(dsl, metrics); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/ClobController.java b/cwms-data-api/src/main/java/cwms/cda/api/ClobController.java index 5a3ec78de..3c8eef6bf 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/ClobController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/ClobController.java @@ -221,12 +221,8 @@ public void getOne(@NotNull Context ctx, @NotNull String clobId) { public void create(@NotNull Context ctx) { try (final Timer.Context ignored = markAndTime(CREATE)) { DSLContext dsl = getDslContext(ctx); - - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSONV2; - + String formatHeader = ctx.req.getContentType(); boolean failIfExists = ctx.queryParamAsClass(FAIL_IF_EXISTS, Boolean.class).getOrDefault(true); - ContentType contentType = Formats.parseHeader(formatHeader, Clob.class); Clob clob = Formats.parseContent(contentType, ctx.bodyAsInputStream(), Clob.class); ClobDao dao = new ClobDao(dsl); @@ -264,8 +260,7 @@ public void update(@NotNull Context ctx, @NotNull String clobId) { try (final Timer.Context ignored = markAndTime(UPDATE)) { DSLContext dsl = getDslContext(ctx); - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSON; + String formatHeader = ctx.req.getContentType(); ClobDao dao = new ClobDao(dsl); ContentType contentType = Formats.parseHeader(formatHeader, Clob.class); Clob clob = Formats.parseContent(contentType, ctx.bodyAsInputStream(), Clob.class); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/Controllers.java b/cwms-data-api/src/main/java/cwms/cda/api/Controllers.java index 43bf7be5d..fb36e3dd4 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/Controllers.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/Controllers.java @@ -114,7 +114,7 @@ public final class Controllers { public static final String INTERVAL = "interval"; public static final String CATEGORY_ID = "category-id"; public static final String CATEGORY_ID_MASK = "category-id-mask"; - public static final String EXAMPLE_DATE = "2021-06-10T13:00:00-0700[PST8PDT]"; + public static final String EXAMPLE_DATE = "2021-06-10T13:00:00-07:00"; public static final String VERSION_DATE = "version-date"; public static final String CREATE_AS_LRTS = "create-as-lrts"; diff --git a/cwms-data-api/src/main/java/cwms/cda/api/DownstreamLocationsGetController.java b/cwms-data-api/src/main/java/cwms/cda/api/DownstreamLocationsGetController.java index b72ac1a92..13979179a 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/DownstreamLocationsGetController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/DownstreamLocationsGetController.java @@ -100,7 +100,7 @@ public void handle(@NotNull Context ctx) throws Exception { StreamLocationDao dao = new StreamLocationDao(dsl); List downstreamLocations = dao.retrieveDownstreamLocations(office, locationId, allDownstream, sameStreamOnly, stationUnits, stageUnits, areaUnits); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : Formats.JSONV1; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, StreamLocation.class); ctx.contentType(contentType.toString()); String serialized = Formats.format(contentType, downstreamLocations, StreamLocation.class); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/EmbankmentController.java b/cwms-data-api/src/main/java/cwms/cda/api/EmbankmentController.java index 5dd1094b2..a73527762 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/EmbankmentController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/EmbankmentController.java @@ -94,8 +94,7 @@ public void getAll(Context ctx) { DSLContext dsl = getDslContext(ctx); EmbankmentDao dao = new EmbankmentDao(dsl); List embankments = dao.retrieveEmbankments(projectId, office); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : - Formats.JSONV1; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, Embankment.class); ctx.contentType(contentType.toString()); String serialized = Formats.format(contentType, embankments, Embankment.class); @@ -131,8 +130,7 @@ public void getOne(@NotNull Context ctx, @NotNull String name) { DSLContext dsl = getDslContext(ctx); EmbankmentDao dao = new EmbankmentDao(dsl); Embankment embankment = dao.retrieveEmbankment(name, office); - String header = ctx.header(Header.ACCEPT); - String formatHeader = header != null ? header : Formats.JSONV1; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, Embankment.class); ctx.contentType(contentType.toString()); String serialized = Formats.format(contentType, embankment); @@ -162,8 +160,7 @@ public void getOne(@NotNull Context ctx, @NotNull String name) { @Override public void create(Context ctx) { try (Timer.Context ignored = markAndTime(CREATE)) { - String acceptHeader = ctx.req.getContentType(); - String formatHeader = acceptHeader != null ? acceptHeader : Formats.JSONV1; + String formatHeader = ctx.req.getContentType(); ContentType contentType = Formats.parseHeader(formatHeader, Embankment.class); Embankment embankment = Formats.parseContent(contentType, ctx.body(), Embankment.class); boolean failIfExists = ctx.queryParamAsClass(FAIL_IF_EXISTS, Boolean.class).getOrDefault(true); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/ForecastInstanceController.java b/cwms-data-api/src/main/java/cwms/cda/api/ForecastInstanceController.java index 7ea93175c..cbda9866e 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/ForecastInstanceController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/ForecastInstanceController.java @@ -74,8 +74,6 @@ public void create(@NotNull Context ctx) { ForecastInstance forecastInstance = deserializeForecastInstance(ctx); dao.create(forecastInstance); ctx.status(HttpServletResponse.SC_CREATED); - } catch (IOException ex) { - throw new IllegalArgumentException("Unable to deserialize forecast instance from content body", ex); } } @@ -170,7 +168,7 @@ public void getAll(@NotNull Context ctx) { List instances = dao.getForecastInstances(KILO_BYTE_LIMIT, urlBuilder, office, name, desionatorMask); String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, null); + ContentType contentType = Formats.parseHeader(formatHeader, ForecastInstance.class); String result = Formats.format(contentType, instances, ForecastInstance.class); ctx.result(result).contentType(contentType.toString()); @@ -240,7 +238,7 @@ public void getOne(@NotNull Context ctx, @NotNull String name) { ForecastInstance instance = dao.getForecastInstance(KILO_BYTE_LIMIT, urlBuilder, office, name, designator, forecastInstant, issueInstant); String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, null); + ContentType contentType = Formats.parseHeader(formatHeader, ForecastInstance.class); String result = Formats.format(contentType, instance); ctx.result(result).contentType(contentType.toString()); @@ -278,15 +276,12 @@ public void update(@NotNull Context ctx, @NotNull String name) { ForecastInstanceDao dao = new ForecastInstanceDao(getDslContext(ctx)); dao.update(forecastInstance); ctx.status(HttpServletResponse.SC_OK); - } catch (IOException ex) { - throw new IllegalArgumentException("Unable to deserialize forecast instance from content body", ex); } } - private ForecastInstance deserializeForecastInstance(Context ctx) throws IOException { - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSONV2; - ContentType contentType = Formats.parseHeader(formatHeader); + private ForecastInstance deserializeForecastInstance(Context ctx) { + String formatHeader = ctx.req.getContentType(); + ContentType contentType = Formats.parseHeader(formatHeader, ForecastInstance.class); return Formats.parseContent(contentType, ctx.body(), ForecastInstance.class); } diff --git a/cwms-data-api/src/main/java/cwms/cda/api/ForecastSpecController.java b/cwms-data-api/src/main/java/cwms/cda/api/ForecastSpecController.java index 34fbb665a..b4b220f18 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/ForecastSpecController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/ForecastSpecController.java @@ -69,8 +69,6 @@ public void create(@NotNull Context ctx) { dao.create(forecastSpec); ctx.status(HttpServletResponse.SC_CREATED); - } catch (IOException ex) { - throw new IllegalArgumentException("Unable to deserialize forecast spec from content body", ex); } } @@ -169,7 +167,7 @@ public void getAll(@NotNull Context ctx) { sourceEntity); String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, null); + ContentType contentType = Formats.parseHeader(formatHeader, ForecastSpec.class); String result = Formats.format(contentType, specs, ForecastSpec.class); ctx.result(result).contentType(contentType.toString()); @@ -218,7 +216,7 @@ public void getOne(@NotNull Context ctx, @NotNull String name) { ForecastSpec spec = dao.getForecastSpec(office, name, designator); String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, null); + ContentType contentType = Formats.parseHeader(formatHeader, ForecastSpec.class); String result = Formats.format(contentType, spec); ctx.result(result).contentType(contentType.toString()); @@ -253,15 +251,12 @@ public void update(@NotNull Context ctx, @NotNull String name) { ForecastSpecDao dao = new ForecastSpecDao(dsl); dao.update(forecastSpec); ctx.status(HttpServletResponse.SC_OK); - } catch (IOException ex) { - throw new IllegalArgumentException("Unable to deserialize forecast spec from content body", ex); } } - private ForecastSpec deserializeForecastSpec(Context ctx) throws IOException { - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSONV2; - ContentType contentType = Formats.parseHeader(formatHeader); + private ForecastSpec deserializeForecastSpec(Context ctx) { + String formatHeader = ctx.req.getContentType(); + ContentType contentType = Formats.parseHeader(formatHeader, ForecastSpec.class); return Formats.parseContent(contentType, ctx.body(), ForecastSpec.class); } diff --git a/cwms-data-api/src/main/java/cwms/cda/api/LevelsController.java b/cwms-data-api/src/main/java/cwms/cda/api/LevelsController.java index 06ab6a8e1..d51bd0c87 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/LevelsController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/LevelsController.java @@ -133,9 +133,8 @@ private Timer.Context markAndTime(String subject) { public void create(@NotNull Context ctx) { try (final Timer.Context ignored = markAndTime(CREATE)) { - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSONV2; - ContentType contentType = Formats.parseHeader(formatHeader); + String formatHeader = ctx.req.getContentType(); + ContentType contentType = Formats.parseHeader(formatHeader, LocationLevel.class); LocationLevel level = Formats.parseContent(contentType, ctx.body(), LocationLevel.class); level.validate(); @@ -403,9 +402,8 @@ public void update(@NotNull Context ctx, @NotNull String oldLevelId) { try (final Timer.Context ignored = markAndTime(UPDATE)) { DSLContext dsl = getDslContext(ctx); - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSON; - ContentType contentType = Formats.parseHeader(formatHeader); + String formatHeader = ctx.req.getContentType(); + ContentType contentType = Formats.parseHeader(formatHeader, LocationLevel.class); LocationLevel levelFromBody = Formats.parseContent(contentType, ctx.body(), LocationLevel.class); String officeId = levelFromBody.getOfficeId(); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/LocationCategoryController.java b/cwms-data-api/src/main/java/cwms/cda/api/LocationCategoryController.java index ebc2bb18b..bd1e269ac 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/LocationCategoryController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/LocationCategoryController.java @@ -99,7 +99,7 @@ public void getAll(Context ctx) { if (!cats.isEmpty()) { String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, null); + ContentType contentType = Formats.parseHeader(formatHeader, LocationCategory.class); String result = Formats.format(contentType, cats, LocationCategory.class); @@ -155,7 +155,7 @@ public void getOne(Context ctx, @NotNull String categoryId) { Optional grp = dao.getLocationCategory(office, categoryId); if (grp.isPresent()) { String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, null); + ContentType contentType = Formats.parseHeader(formatHeader, LocationCategory.class); String result = Formats.format(contentType, grp.get()); @@ -194,10 +194,9 @@ public void create(Context ctx) { try (Timer.Context ignored = markAndTime(CREATE)){ DSLContext dsl = getDslContext(ctx); - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSON; + String formatHeader = ctx.req.getContentType(); String body = ctx.body(); - ContentType contentType = Formats.parseHeader(formatHeader); + ContentType contentType = Formats.parseHeader(formatHeader, LocationCategory.class); LocationCategory deserialize = Formats.parseContent(contentType, body, LocationCategory.class); LocationCategoryDao dao = new LocationCategoryDao(dsl); dao.create(deserialize); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/LocationController.java b/cwms-data-api/src/main/java/cwms/cda/api/LocationController.java index 5d4031ca6..cf759dda5 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/LocationController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/LocationController.java @@ -265,8 +265,7 @@ public void getOne(@NotNull Context ctx, @NotNull String name) { String units = ctx.queryParamAsClass(UNIT, String.class).getOrDefault(UnitSystem.EN.value()); String office = ctx.queryParam(OFFICE); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : - Formats.JSONV2; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, Location.class); ctx.contentType(contentType.toString()); LocationsDao locationDao = getLocationsDao(dsl); @@ -307,9 +306,8 @@ public void create(@NotNull Context ctx) { LocationsDao locationsDao = getLocationsDao(dsl); - String acceptHeader = ctx.req.getContentType(); - String formatHeader = acceptHeader != null ? acceptHeader : Formats.JSON; - ContentType contentType = Formats.parseHeader(formatHeader); + String formatHeader = ctx.req.getContentType(); + ContentType contentType = Formats.parseHeader(formatHeader, Location.class); Location locationFromBody = Formats.parseContent(contentType, ctx.body(), Location.class); locationsDao.storeLocation(locationFromBody); ctx.status(HttpServletResponse.SC_OK).json("Created Location"); @@ -344,9 +342,8 @@ public void update(@NotNull Context ctx, @NotNull String locationId) { LocationsDao locationsDao = getLocationsDao(dsl); - String acceptHeader = ctx.req.getContentType(); - String formatHeader = acceptHeader != null ? acceptHeader : Formats.JSON; - ContentType contentType = Formats.parseHeader(formatHeader); + String formatHeader = ctx.req.getContentType(); + ContentType contentType = Formats.parseHeader(formatHeader, Location.class); Location locationFromBody = Formats.parseContent(contentType, ctx.body(), Location.class); //getLocation will throw an error if location does not exist Location existingLocation = locationsDao.getLocation(locationId, diff --git a/cwms-data-api/src/main/java/cwms/cda/api/LocationGroupController.java b/cwms-data-api/src/main/java/cwms/cda/api/LocationGroupController.java index db8f4aedb..d9d91a081 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/LocationGroupController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/LocationGroupController.java @@ -113,7 +113,7 @@ Boolean.class, false, metrics, name(LocationGroupController.class.getName(), if (!grps.isEmpty()) { String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, ""); + ContentType contentType = Formats.parseHeader(formatHeader, LocationGroup.class); String result = Formats.format(contentType, grps, LocationGroup.class); @@ -159,19 +159,20 @@ public void getOne(@NotNull Context ctx, @NotNull String groupId) { try (final Timer.Context ignored = markAndTime(GET_ONE)) { DSLContext dsl = getDslContext(ctx); LocationGroupDao cdm = new LocationGroupDao(dsl); - String office = ctx.queryParam(OFFICE); - String categoryId = ctx.queryParam(CATEGORY_ID); + String office = requiredParam(ctx, OFFICE); + String categoryId = requiredParam(ctx, CATEGORY_ID); String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, ""); - String result; - if (Formats.GEOJSON.equals(contentType.getType())) { + ContentType contentType; + if (formatHeader != null && formatHeader.contains(Formats.GEOJSON)) { + contentType = new ContentType(Formats.GEOJSON); FeatureCollection fc = cdm.buildFeatureCollectionForLocationGroup(office, categoryId, groupId, "EN"); ObjectMapper mapper = ctx.appAttribute("ObjectMapper"); result = mapper.writeValueAsString(fc); } else { + contentType = Formats.parseHeader(formatHeader, LocationGroup.class); Optional grp = cdm.getLocationGroup(office, categoryId, groupId); if (grp.isPresent()) { result = Formats.format(contentType, grp.get()); @@ -212,10 +213,9 @@ public void create(@NotNull Context ctx) { try (Timer.Context ignored = markAndTime(CREATE)) { DSLContext dsl = getDslContext(ctx); - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSON; + String formatHeader = ctx.req.getContentType(); String body = ctx.body(); - ContentType contentType = Formats.parseHeader(formatHeader); + ContentType contentType = Formats.parseHeader(formatHeader, LocationGroup.class); LocationGroup deserialize = Formats.parseContent(contentType, body, LocationGroup.class); LocationGroupDao dao = new LocationGroupDao(dsl); dao.create(deserialize); @@ -246,10 +246,9 @@ public void update(@NotNull Context ctx, String oldGroupId) { try (Timer.Context ignored = markAndTime(CREATE)) { DSLContext dsl = getDslContext(ctx); - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSON; + String formatHeader = ctx.req.getContentType(); String body = ctx.body(); - ContentType contentType = Formats.parseHeader(formatHeader); + ContentType contentType = Formats.parseHeader(formatHeader, LocationGroup.class); LocationGroup deserialize = Formats.parseContent(contentType, body, LocationGroup.class); boolean replaceAssignedLocs = ctx.queryParamAsClass(REPLACE_ASSIGNED_LOCS, Boolean.class).getOrDefault(false); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/LookupTypeController.java b/cwms-data-api/src/main/java/cwms/cda/api/LookupTypeController.java index c376dcb6b..e3b70700f 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/LookupTypeController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/LookupTypeController.java @@ -92,8 +92,8 @@ public void getAll(Context ctx) { DSLContext dsl = getDslContext(ctx); LookupTypeDao dao = new LookupTypeDao(dsl); List lookupTypes = dao.retrieveLookupTypes(category, prefix, officeId); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : Formats.JSON; - ContentType contentType = Formats.parseHeader(formatHeader); + String formatHeader = ctx.header(Header.ACCEPT); + ContentType contentType = Formats.parseHeader(formatHeader, LookupType.class); ctx.contentType(contentType.toString()); String serialized = Formats.format(contentType, lookupTypes, LookupType.class); ctx.result(serialized); @@ -132,9 +132,8 @@ public void create(Context ctx) { String category = requiredParam(ctx, CATEGORY); String prefix = requiredParam(ctx, PREFIX); try (Timer.Context ignored = markAndTime(CREATE)) { - String acceptHeader = ctx.req.getContentType(); - String formatHeader = acceptHeader != null ? acceptHeader : Formats.JSON; - ContentType contentType = Formats.parseHeader(formatHeader); + String formatHeader = ctx.req.getContentType(); + ContentType contentType = Formats.parseHeader(formatHeader, LookupType.class); LookupType lookupType = Formats.parseContent(contentType, ctx.body(), LookupType.class); DSLContext dsl = getDslContext(ctx); LookupTypeDao dao = new LookupTypeDao(dsl); @@ -165,9 +164,8 @@ public void update(Context ctx, String name) { String category = requiredParam(ctx, CATEGORY); String prefix = requiredParam(ctx, PREFIX); try (Timer.Context ignored = markAndTime(UPDATE)) { - String acceptHeader = ctx.req.getContentType(); - String formatHeader = acceptHeader != null ? acceptHeader : Formats.JSON; - ContentType contentType = Formats.parseHeader(formatHeader); + String formatHeader = ctx.req.getContentType(); + ContentType contentType = Formats.parseHeader(formatHeader, LookupType.class); LookupType lookupType = Formats.parseContent(contentType, ctx.body(), LookupType.class); DSLContext dsl = getDslContext(ctx); LookupTypeDao dao = new LookupTypeDao(dsl); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/PoolController.java b/cwms-data-api/src/main/java/cwms/cda/api/PoolController.java index 7bdf4b8f4..73a5a1b93 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/PoolController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/PoolController.java @@ -133,7 +133,7 @@ public void getAll(@NotNull Context ctx) { topMask, isExplicit, isImplicit, office); String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, ""); + ContentType contentType = Formats.parseHeader(formatHeader, Pools.class); String result = Formats.format(contentType, pools); @@ -214,7 +214,7 @@ public void getOne(@NotNull Context ctx, @NotNull String poolId) { ctx.status(HttpServletResponse.SC_NOT_FOUND).json(re); } else { String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, ""); + ContentType contentType = Formats.parseHeader(formatHeader, Pool.class); ctx.contentType(contentType.toString()); String result = Formats.format(contentType, pool); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/ProjectController.java b/cwms-data-api/src/main/java/cwms/cda/api/ProjectController.java index e88e30708..7b42139c9 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/ProjectController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/ProjectController.java @@ -132,7 +132,7 @@ public void getAll(@NotNull Context ctx) { Projects projects = dao.retrieveProjects(cursor, office, projectIdMask, pageSize); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : Formats.JSON; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, Projects.class); ctx.contentType(contentType.toString()); String serialized = Formats.format(contentType, projects); @@ -218,8 +218,7 @@ public void create(@NotNull Context ctx) { DSLContext dsl = getDslContext(ctx); boolean failIfExists = ctx.queryParamAsClass(FAIL_IF_EXISTS, Boolean.class).getOrDefault(true); - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSON; + String formatHeader = ctx.req.getContentType(); ContentType contentType = Formats.parseHeader(formatHeader, Project.class); Project project = Formats.parseContent(contentType, ctx.body(), Project.class); ProjectDao dao = new ProjectDao(dsl); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/PropertyController.java b/cwms-data-api/src/main/java/cwms/cda/api/PropertyController.java index c441cdca8..9067bd3c8 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/PropertyController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/PropertyController.java @@ -93,9 +93,8 @@ public void getAll(Context ctx) { DSLContext dsl = getDslContext(ctx); PropertyDao dao = new PropertyDao(dsl); List properties = dao.retrieveProperties(officeMask, categoryMask, nameMask); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : - Formats.JSON; - ContentType contentType = Formats.parseHeader(formatHeader); + String formatHeader = ctx.header(Header.ACCEPT); + ContentType contentType = Formats.parseHeader(formatHeader, Property.class); ctx.contentType(contentType.toString()); String serialized = Formats.format(contentType, properties, Property.class); ctx.result(serialized); @@ -135,9 +134,8 @@ public void getOne(Context ctx, String name) { DSLContext dsl = getDslContext(ctx); PropertyDao dao = new PropertyDao(dsl); Property property = dao.retrieveProperty(office, category, name, defaultValue); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : - Formats.JSON; - ContentType contentType = Formats.parseHeader(formatHeader); + String formatHeader = ctx.header(Header.ACCEPT); + ContentType contentType = Formats.parseHeader(formatHeader, Property.class); ctx.contentType(contentType.toString()); String serialized = Formats.format(contentType, property); ctx.result(serialized); @@ -163,9 +161,8 @@ public void getOne(Context ctx, String name) { @Override public void create(Context ctx) { try (Timer.Context ignored = markAndTime(CREATE)) { - String acceptHeader = ctx.req.getContentType(); - String formatHeader = acceptHeader != null ? acceptHeader : Formats.JSON; - ContentType contentType = Formats.parseHeader(formatHeader); + String formatHeader = ctx.req.getContentType(); + ContentType contentType = Formats.parseHeader(formatHeader, Property.class); Property property = Formats.parseContent(contentType, ctx.body(), Property.class); DSLContext dsl = getDslContext(ctx); PropertyDao dao = new PropertyDao(dsl); @@ -191,9 +188,8 @@ public void create(Context ctx) { @Override public void update(Context ctx, String name) { try (Timer.Context ignored = markAndTime(UPDATE)) { - String acceptHeader = ctx.req.getContentType(); - String formatHeader = acceptHeader != null ? acceptHeader : Formats.JSON; - ContentType contentType = Formats.parseHeader(formatHeader); + String formatHeader = ctx.req.getContentType(); + ContentType contentType = Formats.parseHeader(formatHeader, Property.class); Property property = Formats.parseContent(contentType, ctx.body(), Property.class); DSLContext dsl = getDslContext(ctx); PropertyDao dao = new PropertyDao(dsl); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/RatingController.java b/cwms-data-api/src/main/java/cwms/cda/api/RatingController.java index bb5bc685a..edc330ced 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/RatingController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/RatingController.java @@ -156,24 +156,21 @@ private Timer.Context markAndTime(String subject) { return Controllers.markAndTime(metrics, getClass().getName(), subject); } - @NotNull - private ContentType getContentType(Context ctx) { - String requestContent = ctx.req.getContentType(); - String formatHeader = requestContent != null ? requestContent : Formats.JSON; - return Formats.parseHeader(formatHeader); - } - private String deserializeRatingSet(Context ctx) throws IOException, RatingException { - return deserializeRatingSet(ctx.body(), getContentType(ctx).getType()); + String formatHeader = ctx.req.getContentType(); + //Using placeholder CwmsDTOBase.class since we do not have a RatingSet DTO + //The contentType will match against the standard listing of Formats constants + ContentType contentType = Formats.parseHeader(formatHeader, CwmsDTOBase.class); + String body = ctx.body(); + return deserializeRatingSet(body, contentType.getType()); } - String deserializeRatingSet(String body, String contentType) throws IOException, - RatingException { + //Package private for unit testing + String deserializeRatingSet(String body, String contentType) throws IOException, RatingException { String retval; - - if ((Formats.XML).equals(contentType)) { + if (Formats.XML.equals(contentType)) { retval = body; - } else if ((Formats.JSON).equals(contentType)) { + } else if (Formats.JSON.equals(contentType)) { retval = RatingXmlFactory.toXml(JsonRatingUtils.fromJson(body), ""); } else { throw new IOException("Unexpected format:" + contentType); @@ -182,11 +179,6 @@ String deserializeRatingSet(String body, String contentType) throws IOException, return retval; } - @NotNull - public RatingSet deserializeFromXml(String body) throws RatingException { - return RatingXmlFactory.ratingSet(body); - } - @OpenApi( pathParams = { @OpenApiParam(name = RATING_ID, required = true, description = "The rating-id of the " diff --git a/cwms-data-api/src/main/java/cwms/cda/api/RatingMetadataController.java b/cwms-data-api/src/main/java/cwms/cda/api/RatingMetadataController.java index f9211022e..8ad49a525 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/RatingMetadataController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/RatingMetadataController.java @@ -126,7 +126,7 @@ public void getAll(Context ctx) { } String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, ""); + ContentType contentType = Formats.parseHeader(formatHeader, RatingMetadataList.class); try (final Timer.Context timeContext = markAndTime(GET_ALL)){ DSLContext dsl = getDslContext(ctx); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/RatingSpecController.java b/cwms-data-api/src/main/java/cwms/cda/api/RatingSpecController.java index 2ff620edf..c11d05ba9 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/RatingSpecController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/RatingSpecController.java @@ -121,7 +121,7 @@ public void getAll(Context ctx) { String ratingIdMask = ctx.queryParam(RATING_ID_MASK); String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, ""); + ContentType contentType = Formats.parseHeader(formatHeader, RatingSpecs.class); try (final Timer.Context timeContext = markAndTime(GET_ALL)){ DSLContext dsl = getDslContext(ctx); @@ -167,7 +167,7 @@ public void getAll(Context ctx) { @Override public void getOne(Context ctx, String ratingId) { String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, ""); + ContentType contentType = Formats.parseHeader(formatHeader, RatingSpec.class); String office = ctx.queryParam(OFFICE); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/RatingTemplateController.java b/cwms-data-api/src/main/java/cwms/cda/api/RatingTemplateController.java index 6b3f9e4a4..57eafc6d8 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/RatingTemplateController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/RatingTemplateController.java @@ -120,7 +120,7 @@ public void getAll(Context ctx) { String templateIdMask = ctx.queryParam(TEMPLATE_ID_MASK); String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, ""); + ContentType contentType = Formats.parseHeader(formatHeader, RatingTemplates.class); try (final Timer.Context timeContext = markAndTime(GET_ALL)){ DSLContext dsl = getDslContext(ctx); @@ -171,7 +171,7 @@ private RatingTemplateDao getRatingTemplateDao(DSLContext dsl) { @Override public void getOne(Context ctx, String templateId) { String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, ""); + ContentType contentType = Formats.parseHeader(formatHeader, RatingTemplate.class); String office = ctx.queryParam(OFFICE); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/SpecifiedLevelController.java b/cwms-data-api/src/main/java/cwms/cda/api/SpecifiedLevelController.java index 463a78677..309502d26 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/SpecifiedLevelController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/SpecifiedLevelController.java @@ -98,7 +98,7 @@ public void getAll(Context ctx) { String templateIdMask = ctx.queryParam(TEMPLATE_ID_MASK); String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, ""); + ContentType contentType = Formats.parseHeader(formatHeader, SpecifiedLevel.class); try (Timer.Context timeContext = markAndTime(GET_ALL)){ DSLContext dsl = getDslContext(ctx); @@ -146,10 +146,9 @@ public void create(Context ctx) { try (Timer.Context ignored = markAndTime(CREATE)){ DSLContext dsl = getDslContext(ctx); - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSONV2; + String formatHeader = ctx.req.getContentType(); String body = ctx.body(); - ContentType contentType = Formats.parseHeader(formatHeader); + ContentType contentType = Formats.parseHeader(formatHeader, SpecifiedLevel.class); SpecifiedLevel deserialize = Formats.parseContent(contentType, body, SpecifiedLevel.class); SpecifiedLevelDao dao = getDao(dsl); boolean failIfExists = ctx.queryParamAsClass(FAIL_IF_EXISTS, Boolean.class).getOrDefault(true); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/StandardTextController.java b/cwms-data-api/src/main/java/cwms/cda/api/StandardTextController.java index a0d3f1b6d..79e06dadf 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/StandardTextController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/StandardTextController.java @@ -99,7 +99,7 @@ public void getAll(Context ctx) { idMask = "*"; } String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, ""); + ContentType contentType = Formats.parseHeader(formatHeader, StandardTextCatalog.class); DSLContext dsl = getDslContext(ctx); StandardTextCatalog catalog = getDao(dsl).retreiveStandardTextCatalog(idMask, officeMask); @@ -141,7 +141,7 @@ public void getOne(@NotNull Context ctx, @NotNull String stdTextId) { StandardTextValue standardTextValue = getDao(dsl).retrieveStandardText(stdTextId, office); String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, ""); + ContentType contentType = Formats.parseHeader(formatHeader, StandardTextValue.class); ctx.contentType(contentType.toString()); String result = Formats.format(contentType, standardTextValue); ctx.result(result); @@ -166,12 +166,11 @@ public void getOne(@NotNull Context ctx, @NotNull String stdTextId) { @Override public void create(@NotNull Context ctx) { try (Timer.Context ignored = markAndTime(CREATE)) { - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSONV2; + String formatHeader = ctx.req.getContentType(); String body = ctx.body(); boolean failIfExists = ctx.queryParamAsClass(FAIL_IF_EXISTS, Boolean.class).getOrDefault(false); - ContentType contentType = Formats.parseHeader(formatHeader); + ContentType contentType = Formats.parseHeader(formatHeader, StandardTextValue.class); StandardTextValue tts = Formats.parseContent(contentType, body, StandardTextValue.class); DSLContext dsl = getDslContext(ctx); getDao(dsl).storeStandardText(tts.getId().getId(), tts.getStandardText(), tts.getId().getOfficeId(), diff --git a/cwms-data-api/src/main/java/cwms/cda/api/StreamController.java b/cwms-data-api/src/main/java/cwms/cda/api/StreamController.java index 11a613343..d24660e7a 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/StreamController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/StreamController.java @@ -121,8 +121,7 @@ public void getAll(@NotNull Context ctx) { StreamDao dao = new StreamDao(dsl); String stationUnits = ctx.queryParamAsClass(STATION_UNIT, String.class).getOrDefault("mi"); List streams = dao.retrieveStreams(office, streamId, divertsFromStream, flowsIntoStream, stationUnits); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : - Formats.JSONV1; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, Stream.class); ctx.contentType(contentType.toString()); String serialized = Formats.format(contentType, streams, Stream.class); @@ -161,8 +160,7 @@ public void getOne(@NotNull Context ctx, @NotNull String streamId) { StreamDao dao = new StreamDao(dsl); String stationUnits = ctx.queryParamAsClass(STATION_UNIT, String.class).getOrDefault("mi"); Stream stream = dao.retrieveStream(office, streamId, stationUnits); - String header = ctx.header(Header.ACCEPT); - String formatHeader = header != null ? header : Formats.JSONV1; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, Stream.class); ctx.contentType(contentType.toString()); String serialized = Formats.format(contentType, stream); @@ -192,8 +190,7 @@ public void getOne(@NotNull Context ctx, @NotNull String streamId) { @Override public void create(Context ctx) { try (Timer.Context ignored = markAndTime(CREATE)) { - String acceptHeader = ctx.req.getContentType(); - String formatHeader = acceptHeader != null ? acceptHeader : Formats.JSONV1; + String formatHeader = ctx.req.getContentType(); ContentType contentType = Formats.parseHeader(formatHeader, Stream.class); Stream stream = Formats.parseContent(contentType, ctx.body(), Stream.class); boolean failIfExists = ctx.queryParamAsClass(FAIL_IF_EXISTS, Boolean.class).getOrDefault(true); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/StreamLocationController.java b/cwms-data-api/src/main/java/cwms/cda/api/StreamLocationController.java index f50ce9674..b611f6560 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/StreamLocationController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/StreamLocationController.java @@ -121,8 +121,7 @@ public void getAll(Context ctx) { String areaUnits = ctx.queryParamAsClass(AREA_UNIT, String.class).getOrDefault("mi2"); String stageUnits = ctx.queryParamAsClass(STAGE_UNIT, String.class).getOrDefault("ft"); List streamLocations = dao.retrieveStreamLocations(office, streamId, locationId, stationUnits, stageUnits, areaUnits); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : - Formats.JSONV1; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, StreamLocation.class); ctx.contentType(contentType.toString()); String serialized = Formats.format(contentType, streamLocations, StreamLocation.class); @@ -168,8 +167,7 @@ public void getOne(@NotNull Context ctx, @NotNull String locationId) { String areaUnits = ctx.queryParamAsClass(AREA_UNIT, String.class).getOrDefault("mi2"); String stageUnits = ctx.queryParamAsClass(STAGE_UNIT, String.class).getOrDefault("ft"); StreamLocation streamLocation = dao.retrieveStreamLocation(office, streamId, locationId, stationUnits, stageUnits, areaUnits); - String header = ctx.header(Header.ACCEPT); - String formatHeader = header != null ? header : Formats.JSONV1; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, StreamLocation.class); ctx.contentType(contentType.toString()); String serialized = Formats.format(contentType, streamLocation); @@ -199,8 +197,7 @@ public void getOne(@NotNull Context ctx, @NotNull String locationId) { @Override public void create(Context ctx) { try (Timer.Context ignored = markAndTime(CREATE)) { - String acceptHeader = ctx.req.getContentType(); - String formatHeader = acceptHeader != null ? acceptHeader : Formats.JSONV1; + String formatHeader = ctx.req.getContentType(); ContentType contentType = Formats.parseHeader(formatHeader, StreamLocation.class); StreamLocation streamLocation = Formats.parseContent(contentType, ctx.body(), StreamLocation.class); boolean failIfExists = ctx.queryParamAsClass(FAIL_IF_EXISTS, Boolean.class).getOrDefault(true); @@ -227,8 +224,7 @@ public void create(Context ctx) { @Override public void update(Context ctx, @NotNull String locationId) { try (Timer.Context ignored = markAndTime(METHOD + "update")) { - String acceptHeader = ctx.req.getContentType(); - String formatHeader = acceptHeader != null ? acceptHeader : Formats.JSONV1; + String formatHeader = ctx.req.getContentType(); ContentType contentType = Formats.parseHeader(formatHeader, StreamLocation.class); StreamLocation streamLocation = Formats.parseContent(contentType, ctx.body(), StreamLocation.class); DSLContext dsl = getDslContext(ctx); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/StreamReachController.java b/cwms-data-api/src/main/java/cwms/cda/api/StreamReachController.java index 387ba7840..689395531 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/StreamReachController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/StreamReachController.java @@ -115,7 +115,7 @@ public void getAll(@NotNull Context ctx) { StreamReachDao dao = new StreamReachDao(dsl); String stationUnits = ctx.queryParamAsClass(STATION_UNIT, String.class).getOrDefault("mi"); List streamReaches = dao.retrieveStreamReaches(office, streamIdMask, reachIdMask, configurationIdMask, stationUnits); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : Formats.JSONV1; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, StreamReach.class); ctx.contentType(contentType.toString()); String serialized = Formats.format(contentType, streamReaches, StreamReach.class); @@ -188,8 +188,7 @@ public void getOne(@NotNull Context ctx, @NotNull String reachId) { @Override public void create(@NotNull Context ctx) { try (Timer.Context ignored = markAndTime(CREATE)) { - String acceptHeader = ctx.req.getContentType(); - String formatHeader = acceptHeader != null ? acceptHeader : Formats.JSONV1; + String formatHeader = ctx.req.getContentType(); ContentType contentType = Formats.parseHeader(formatHeader, StreamReach.class); StreamReach streamReach = Formats.parseContent(contentType, ctx.body(), StreamReach.class); boolean failIfExists = ctx.queryParamAsClass(FAIL_IF_EXISTS, Boolean.class).getOrDefault(true); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/TextTimeSeriesController.java b/cwms-data-api/src/main/java/cwms/cda/api/TextTimeSeriesController.java index d26d25c76..6638d2650 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/TextTimeSeriesController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/TextTimeSeriesController.java @@ -117,7 +117,7 @@ public void getAll(@NotNull Context ctx) { Instant version = queryParamAsInstant(ctx, VERSION_DATE); int kiloByteLimit = Integer.parseInt(System.getProperty("cda.api.ts.text.max.length.kB", "64")); String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, ""); + ContentType contentType = Formats.parseHeader(formatHeader, TextTimeSeries.class); try (Timer.Context ignored = markAndTime(GET_ALL)) { DSLContext dsl = getDslContext(ctx); TimeSeriesTextDao dao = getDao(dsl); @@ -185,10 +185,9 @@ public void create(@NotNull Context ctx) { try (Timer.Context ignored = markAndTime(CREATE)) { DSLContext dsl = getDslContext(ctx); - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSONV2; + String formatHeader = ctx.req.getContentType(); - ContentType contentType = Formats.parseHeader(formatHeader); + ContentType contentType = Formats.parseHeader(formatHeader, TextTimeSeries.class); TextTimeSeries tts = Formats.parseContent(contentType, ctx.bodyAsInputStream(), TextTimeSeries.class); TimeSeriesTextDao dao = getDao(dsl); @@ -221,10 +220,10 @@ public void create(@NotNull Context ctx) { public void update(@NotNull Context ctx, @NotNull String oldTextTimeSeriesId) { try (Timer.Context ignored = markAndTime(UPDATE)) { - boolean replaceAll = ctx.queryParamAsClass(REPLACE_ALL, Boolean.class).getOrDefault(DEFAULT_UPDATE_REPLACE_ALL); - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSONV2; - ContentType contentType = Formats.parseHeader(formatHeader); + boolean replaceAll = ctx.queryParamAsClass(REPLACE_ALL, Boolean.class) + .getOrDefault(DEFAULT_UPDATE_REPLACE_ALL); + String formatHeader = ctx.req.getContentType(); + ContentType contentType = Formats.parseHeader(formatHeader, TextTimeSeries.class); TextTimeSeries tts = Formats.parseContent(contentType, ctx.bodyAsInputStream(), TextTimeSeries.class); DSLContext dsl = getDslContext(ctx); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/TimeSeriesCategoryController.java b/cwms-data-api/src/main/java/cwms/cda/api/TimeSeriesCategoryController.java index ba24ac65f..4de269573 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/TimeSeriesCategoryController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/TimeSeriesCategoryController.java @@ -97,7 +97,7 @@ public void getAll(Context ctx) { List cats = dao.getTimeSeriesCategories(office); String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, null); + ContentType contentType = Formats.parseHeader(formatHeader, TimeSeriesCategory.class); String result = Formats.format(contentType, cats, TimeSeriesCategory.class); @@ -140,7 +140,7 @@ public void getOne(Context ctx, @NotNull String categoryId) { String office = requiredParam(ctx, OFFICE); String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, null); + ContentType contentType = Formats.parseHeader(formatHeader, TimeSeriesCategory.class); Optional grp = dao.getTimeSeriesCategory(office, categoryId); if (grp.isPresent()) { @@ -179,11 +179,10 @@ public void create(Context ctx) { try (Timer.Context ignored = markAndTime(CREATE)){ DSLContext dsl = getDslContext(ctx); - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSON; + String formatHeader = ctx.req.getContentType(); String body = ctx.body(); - ContentType contentType = Formats.parseHeader(formatHeader); + ContentType contentType = Formats.parseHeader(formatHeader, TimeSeriesCategory.class); TimeSeriesCategory deserialize = Formats.parseContent(contentType, body, TimeSeriesCategory.class); boolean failIfExists = ctx.queryParamAsClass(FAIL_IF_EXISTS, Boolean.class).getOrDefault(true); TimeSeriesCategoryDao dao = new TimeSeriesCategoryDao(dsl); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/TimeSeriesGroupController.java b/cwms-data-api/src/main/java/cwms/cda/api/TimeSeriesGroupController.java index 9d59c2f1f..f838babd0 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/TimeSeriesGroupController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/TimeSeriesGroupController.java @@ -118,7 +118,7 @@ Boolean.class, true, metrics, name(TimeSeriesGroupController.class.getName(), ctx.status(HttpCode.NOT_FOUND).json(re); } else { String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, null); + ContentType contentType = Formats.parseHeader(formatHeader, TimeSeriesGroup.class); String result = Formats.format(contentType, grps, TimeSeriesGroup.class); @@ -160,7 +160,7 @@ public void getOne(@NotNull Context ctx, @NotNull String groupId) { String categoryId = ctx.queryParam(CATEGORY_ID); String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, null); + ContentType contentType = Formats.parseHeader(formatHeader, TimeSeriesGroup.class); TimeSeriesGroup group = null; List timeSeriesGroups = dao.getTimeSeriesGroups(office, categoryId, @@ -216,10 +216,9 @@ public void create(@NotNull Context ctx) { try (Timer.Context ignored = markAndTime(CREATE)) { DSLContext dsl = getDslContext(ctx); - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSON; + String formatHeader = ctx.req.getContentType(); String body = ctx.body(); - ContentType contentType = Formats.parseHeader(formatHeader); + ContentType contentType = Formats.parseHeader(formatHeader, TimeSeriesGroup.class); TimeSeriesGroup deserialize = Formats.parseContent(contentType, body, TimeSeriesGroup.class); boolean failIfExists = ctx.queryParamAsClass(FAIL_IF_EXISTS, Boolean.class).getOrDefault(true); TimeSeriesGroupDao dao = new TimeSeriesGroupDao(dsl); @@ -251,12 +250,12 @@ public void update(@NotNull Context ctx, String oldGroupId) { try (Timer.Context ignored = markAndTime(CREATE)) { DSLContext dsl = getDslContext(ctx); - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSON; + String formatHeader = ctx.req.getContentType(); String body = ctx.body(); - ContentType contentType = Formats.parseHeader(formatHeader); + ContentType contentType = Formats.parseHeader(formatHeader, TimeSeriesGroup.class); TimeSeriesGroup deserialize = Formats.parseContent(contentType, body, TimeSeriesGroup.class); - boolean replaceAssignedTs = ctx.queryParamAsClass(REPLACE_ASSIGNED_TS, Boolean.class).getOrDefault(false); + boolean replaceAssignedTs = ctx.queryParamAsClass(REPLACE_ASSIGNED_TS, Boolean.class) + .getOrDefault(false); TimeSeriesGroupDao timeSeriesGroupDao = new TimeSeriesGroupDao(dsl); if (!oldGroupId.equals(deserialize.getId())) { timeSeriesGroupDao.renameTimeSeriesGroup(oldGroupId, deserialize); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/TimeSeriesIdentifierDescriptorController.java b/cwms-data-api/src/main/java/cwms/cda/api/TimeSeriesIdentifierDescriptorController.java index 86c8a2b72..f4c183ec8 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/TimeSeriesIdentifierDescriptorController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/TimeSeriesIdentifierDescriptorController.java @@ -154,7 +154,7 @@ public void getAll(Context ctx) { // parseHeaderAndQueryParm normally defaults to JSONV1 when the input is */* formatHeader = Formats.JSONV2; } - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, null); + ContentType contentType = Formats.parseHeader(formatHeader, TimeSeriesIdentifierDescriptors.class); String result = Formats.format(contentType, descriptors); @@ -203,7 +203,7 @@ public void getOne(Context ctx, @NotNull String timeseriesId) { formatHeader = Formats.JSONV2; } - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, null); + ContentType contentType = Formats.parseHeader(formatHeader, TimeSeriesIdentifierDescriptor.class); Optional grp = dao.getTimeSeriesIdentifier(office, timeseriesId); if (grp.isPresent()) { @@ -242,11 +242,10 @@ public void create(@NotNull Context ctx) { try (final Timer.Context ignored = markAndTime(CREATE)) { DSLContext dsl = getDslContext(ctx); - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSONV2; + String formatHeader = ctx.req.getContentType(); String body = ctx.body(); - ContentType contentType = Formats.parseHeader(formatHeader); + ContentType contentType = Formats.parseHeader(formatHeader, TimeSeriesIdentifierDescriptor.class); TimeSeriesIdentifierDescriptor tsid = Formats.parseContent(contentType, body, TimeSeriesIdentifierDescriptor.class); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/TimeSeriesRecentController.java b/cwms-data-api/src/main/java/cwms/cda/api/TimeSeriesRecentController.java index 44c00da69..da762699d 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/TimeSeriesRecentController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/TimeSeriesRecentController.java @@ -183,7 +183,7 @@ public void handle(@NotNull Context ctx) { } String formatHeader = ctx.header(Header.ACCEPT); - ContentType contentType = Formats.parseHeaderAndQueryParm(formatHeader, null); + ContentType contentType = Formats.parseHeader(formatHeader, RecentValue.class); String result = Formats.format(contentType, latestValues, RecentValue.class); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/TurbineChangesGetController.java b/cwms-data-api/src/main/java/cwms/cda/api/TurbineChangesGetController.java index 6ca5887c5..d9d5a9837 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/TurbineChangesGetController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/TurbineChangesGetController.java @@ -152,8 +152,7 @@ public void handle(@NotNull Context ctx) throws Exception { TurbineDao dao = new TurbineDao(dsl); List turbineChanges = dao.retrieveOperationalChanges(cwmsId, begin, end, startTimeInclusive, endTimeInclusive, unitSystem.getValue(), rowLimit); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : - Formats.JSONV1; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, TurbineChange.class); ctx.contentType(contentType.toString()); String serialized = Formats.format(contentType, turbineChanges, TurbineChange.class); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/TurbineChangesPostController.java b/cwms-data-api/src/main/java/cwms/cda/api/TurbineChangesPostController.java index 5a99a7998..fd25b9121 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/TurbineChangesPostController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/TurbineChangesPostController.java @@ -114,8 +114,7 @@ private Timer.Context markAndTime(String subject) { @Override public void handle(@NotNull Context ctx) throws Exception { try (Timer.Context ignored = markAndTime(CREATE)) { - String acceptHeader = ctx.req.getContentType(); - String formatHeader = acceptHeader != null ? acceptHeader : Formats.JSONV1; + String formatHeader = ctx.req.getContentType(); ContentType contentType = Formats.parseHeader(formatHeader, TurbineChange.class); List turbine = Formats.parseContentList(contentType, ctx.body(), TurbineChange.class); boolean overrideProtection = ctx.queryParamAsClass(OVERRIDE_PROTECTION, Boolean.class) diff --git a/cwms-data-api/src/main/java/cwms/cda/api/TurbineController.java b/cwms-data-api/src/main/java/cwms/cda/api/TurbineController.java index 5c2d7afa9..f6d409d58 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/TurbineController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/TurbineController.java @@ -94,8 +94,7 @@ public void getAll(Context ctx) { DSLContext dsl = getDslContext(ctx); TurbineDao dao = new TurbineDao(dsl); List turbines = dao.retrieveTurbines(projectId, office); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : - Formats.JSONV1; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, Turbine.class); ctx.contentType(contentType.toString()); String serialized = Formats.format(contentType, turbines, Turbine.class); @@ -131,8 +130,7 @@ public void getOne(@NotNull Context ctx, @NotNull String name) { DSLContext dsl = getDslContext(ctx); TurbineDao dao = new TurbineDao(dsl); Turbine turbine = dao.retrieveTurbine(name, office); - String header = ctx.header(Header.ACCEPT); - String formatHeader = header != null ? header : Formats.JSONV1; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, Turbine.class); ctx.contentType(contentType.toString()); String serialized = Formats.format(contentType, turbine); @@ -163,8 +161,7 @@ public void getOne(@NotNull Context ctx, @NotNull String name) { @Override public void create(Context ctx) { try (Timer.Context ignored = markAndTime(CREATE)) { - String acceptHeader = ctx.req.getContentType(); - String formatHeader = acceptHeader != null ? acceptHeader : Formats.JSONV1; + String formatHeader = ctx.req.getContentType(); ContentType contentType = Formats.parseHeader(formatHeader, Turbine.class); Turbine turbine = Formats.parseContent(contentType, ctx.body(), Turbine.class); turbine.validate(); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/UpstreamLocationsGetController.java b/cwms-data-api/src/main/java/cwms/cda/api/UpstreamLocationsGetController.java index f0a380b11..d23fba3e8 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/UpstreamLocationsGetController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/UpstreamLocationsGetController.java @@ -98,9 +98,10 @@ public void handle(@NotNull Context ctx) throws Exception { try (Timer.Context ignored = markAndTime(GET_ALL)) { DSLContext dsl = getDslContext(ctx); StreamLocationDao dao = new StreamLocationDao(dsl); - List upstreamLocations = dao.retrieveUpstreamLocations(office, locationId, allUpstream, sameStreamOnly, stationUnits, stageUnits, areaUnits); + List upstreamLocations = dao.retrieveUpstreamLocations(office, locationId, allUpstream, + sameStreamOnly, stationUnits, stageUnits, areaUnits); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : Formats.JSONV1; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, StreamLocation.class); ctx.contentType(contentType.toString()); String serialized = Formats.format(contentType, upstreamLocations, StreamLocation.class); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/location/kind/OutletController.java b/cwms-data-api/src/main/java/cwms/cda/api/location/kind/OutletController.java index ab09b4d89..7a4878773 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/location/kind/OutletController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/location/kind/OutletController.java @@ -85,8 +85,7 @@ private Timer.Context markAndTime(String subject) { @Override public void create(@NotNull Context ctx) { try (Timer.Context ignored = markAndTime(CREATE)) { - String acceptHeader = ctx.req.getContentType(); - String formatHeader = acceptHeader != null ? acceptHeader : Formats.JSONV1; + String formatHeader = ctx.req.getContentType(); ContentType contentType = Formats.parseHeader(formatHeader, Outlet.class); Outlet outlet = Formats.parseContent(contentType, ctx.body(), Outlet.class); outlet.validate(); @@ -122,8 +121,7 @@ public void getAll(@NotNull Context ctx) { DSLContext dsl = getDslContext(ctx); OutletDao dao = new OutletDao(dsl); List outlets = dao.retrieveOutletsForProject(office, projectId); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : - Formats.JSONV1; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, Outlet.class); ctx.contentType(contentType.toString()); String serialized = Formats.format(contentType, outlets, Outlet.class); @@ -159,8 +157,7 @@ public void getOne(@NotNull Context ctx, @NotNull String name) { DSLContext dsl = getDslContext(ctx); OutletDao dao = new OutletDao(dsl); Outlet outlet = dao.retrieveOutlet(office, name); - String header = ctx.header(Header.ACCEPT); - String formatHeader = header != null ? header : Formats.JSONV1; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, Outlet.class); ctx.contentType(contentType.toString()); String serialized = Formats.format(contentType, outlet); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/location/kind/VirtualOutletController.java b/cwms-data-api/src/main/java/cwms/cda/api/location/kind/VirtualOutletController.java index ad0ce8ff8..1a2964d4c 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/location/kind/VirtualOutletController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/location/kind/VirtualOutletController.java @@ -94,8 +94,7 @@ public void getAll(@NotNull Context ctx) { DSLContext dsl = getDslContext(ctx); OutletDao dao = new OutletDao(dsl); List outlets = dao.retrieveVirtualOutletsForProject(office, projectId); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : - Formats.JSONV1; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, VirtualOutlet.class); ctx.contentType(contentType.toString()); String serialized = Formats.format(contentType, outlets, VirtualOutlet.class); @@ -132,8 +131,7 @@ public void getOne(@NotNull Context ctx, @NotNull String name) { DSLContext dsl = getDslContext(ctx); OutletDao dao = new OutletDao(dsl); VirtualOutlet outlet = dao.retrieveVirtualOutlet(office, projectId, name); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : - Formats.JSONV1; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, VirtualOutlet.class); ctx.contentType(contentType.toString()); String serialized = Formats.format(contentType, outlet); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/location/kind/VirtualOutletCreateController.java b/cwms-data-api/src/main/java/cwms/cda/api/location/kind/VirtualOutletCreateController.java index 665f9c293..07aa10225 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/location/kind/VirtualOutletCreateController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/location/kind/VirtualOutletCreateController.java @@ -66,8 +66,7 @@ public VirtualOutletCreateController(MetricRegistry metrics) { @Override public void handle(@NotNull Context ctx) throws Exception { try (Timer.Context ignored = markAndTime(CREATE)) { - String acceptHeader = ctx.req.getContentType(); - String formatHeader = acceptHeader != null ? acceptHeader : Formats.JSONV1; + String formatHeader = ctx.req.getContentType(); ContentType contentType = Formats.parseHeader(formatHeader, VirtualOutlet.class); VirtualOutlet virtualOutlet = Formats.parseContent(contentType, ctx.body(), VirtualOutlet.class); boolean failIfExists = queryParamAsClass(ctx, Boolean.class, true, FAIL_IF_EXISTS); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/project/ProjectLockRequest.java b/cwms-data-api/src/main/java/cwms/cda/api/project/ProjectLockRequest.java index 0a2be177e..a094fb7d0 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/project/ProjectLockRequest.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/project/ProjectLockRequest.java @@ -130,8 +130,7 @@ public void handle(@NotNull Context ctx) throws Exception { } private static ProjectLock getProjectLock(@NotNull Context ctx) { - String reqContentType = ctx.req.getContentType(); - String formatHeader = reqContentType != null ? reqContentType : Formats.JSON; + String formatHeader = ctx.req.getContentType(); ContentType contentType = Formats.parseHeader(formatHeader, ProjectLock.class); ProjectLock lock = Formats.parseContent(contentType, ctx.bodyAsInputStream(), ProjectLock.class); if(lock.getSessionUser() == null) { diff --git a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractCatalogController.java b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractCatalogController.java index a0bb87738..8e31f1dbf 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractCatalogController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractCatalogController.java @@ -91,7 +91,7 @@ public void handle(@NotNull Context ctx) { String office = ctx.pathParam(OFFICE); String locationId = ctx.pathParam(PROJECT_ID); CwmsId projectLocation = CwmsId.buildCwmsId(office, locationId); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : Formats.JSONV1; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, WaterUserContract.class); ctx.contentType(contentType.toString()); WaterContractDao contractDao = getContractDao(dsl); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractController.java b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractController.java index 7bb959bd7..d251086d7 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractController.java @@ -95,7 +95,7 @@ public void handle(@NotNull Context ctx) { String contractName = ctx.pathParam(CONTRACT_NAME); String waterUser = ctx.pathParam(WATER_USER); CwmsId projectLocation = CwmsId.buildCwmsId(office, locationId); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : Formats.JSONV1; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, WaterUserContract.class); ctx.contentType(contentType.toString()); WaterContractDao contractDao = getContractDao(dsl); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractCreateController.java b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractCreateController.java index c0e90d5d6..c1f190598 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractCreateController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractCreateController.java @@ -96,7 +96,7 @@ public WaterContractCreateController(MetricRegistry metrics) { public void handle(@NotNull Context ctx) { try (Timer.Context ignored = markAndTime(CREATE)) { DSLContext dsl = getDslContext(ctx); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : Formats.JSONV1; + String formatHeader = ctx.req.getContentType(); ContentType contentType = Formats.parseHeader(formatHeader, WaterUserContract.class); ctx.contentType(contentType.toString()); WaterUserContract waterContract = Formats.parseContent(contentType, ctx.body(), WaterUserContract.class); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractTypeCatalogController.java b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractTypeCatalogController.java index 6e0dff3ca..4b8af1208 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractTypeCatalogController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractTypeCatalogController.java @@ -82,7 +82,7 @@ public void handle(@NotNull Context ctx) { String officeId = ctx.pathParam(OFFICE); DSLContext dsl = getDslContext(ctx); String result; - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : Formats.JSONV1; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, LookupType.class); ctx.contentType(contentType.toString()); WaterContractDao dao = getContractDao(dsl); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractTypeCreateController.java b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractTypeCreateController.java index 51491a785..3b79c3696 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractTypeCreateController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractTypeCreateController.java @@ -81,7 +81,7 @@ public void handle(@NotNull Context ctx) { try (Timer.Context ignored = markAndTime(CREATE)) { DSLContext dsl = getDslContext(ctx); boolean failIfExists = ctx.queryParamAsClass(FAIL_IF_EXISTS, Boolean.class).getOrDefault(true); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : Formats.JSONV1; + String formatHeader = ctx.req.getContentType(); ContentType contentType = Formats.parseHeader(formatHeader, LookupType.class); ctx.contentType(contentType.toString()); LookupType contractType = Formats.parseContent(contentType, ctx.body(), LookupType.class); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractUpdateController.java b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractUpdateController.java index c02fbc7c7..e9c773f75 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractUpdateController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterContractUpdateController.java @@ -97,7 +97,7 @@ public void handle(@NotNull Context ctx) { try (Timer.Context ignored = markAndTime(UPDATE)) { DSLContext dsl = getDslContext(ctx); String contractName = ctx.pathParam(CONTRACT_NAME); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : Formats.JSONV1; + String formatHeader = ctx.req.getContentType(); ContentType contentType = Formats.parseHeader(formatHeader, WaterUserContract.class); ctx.contentType(contentType.toString()); String newName = requiredParam(ctx, CONTRACT_NAME); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterUserCatalogController.java b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterUserCatalogController.java index d3e5d13e9..ddd66862c 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterUserCatalogController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterUserCatalogController.java @@ -86,7 +86,7 @@ public void handle(@NotNull Context ctx) { String office = ctx.pathParam(OFFICE); String locationId = ctx.pathParam(PROJECT_ID); CwmsId projectLocation = CwmsId.buildCwmsId(office, locationId); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : Formats.JSONV1; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, WaterUserContract.class); ctx.contentType(contentType.toString()); WaterContractDao contractDao = getContractDao(dsl); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterUserController.java b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterUserController.java index 0087f12e8..57bf93241 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterUserController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterUserController.java @@ -89,7 +89,7 @@ public void handle(@NotNull Context ctx) { CwmsId projectLocation = CwmsId.buildCwmsId(office, location); DSLContext dsl = getDslContext(ctx); String entityName = ctx.pathParam(WATER_USER); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : Formats.JSONV1; + String formatHeader = ctx.header(Header.ACCEPT); ContentType contentType = Formats.parseHeader(formatHeader, WaterUserContract.class); ctx.contentType(contentType.toString()); WaterContractDao contractDao = getContractDao(dsl); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterUserCreateController.java b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterUserCreateController.java index 887762e79..6b0e252e3 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterUserCreateController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterUserCreateController.java @@ -78,7 +78,7 @@ public WaterUserCreateController(MetricRegistry metrics) { public void handle(@NotNull Context ctx) { try (Timer.Context ignored = markAndTime(CREATE)) { DSLContext dsl = getDslContext(ctx); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : Formats.JSONV1; + String formatHeader = ctx.req.getContentType(); ContentType contentType = Formats.parseHeader(formatHeader, WaterUser.class); ctx.contentType(contentType.toString()); WaterUser user = Formats.parseContent(contentType, ctx.body(), WaterUser.class); diff --git a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterUserUpdateController.java b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterUserUpdateController.java index 510840ced..ddfc2227e 100644 --- a/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterUserUpdateController.java +++ b/cwms-data-api/src/main/java/cwms/cda/api/watersupply/WaterUserUpdateController.java @@ -94,7 +94,7 @@ public WaterUserUpdateController(MetricRegistry metrics) { public void handle(@NotNull Context ctx) { try (Timer.Context ignored = markAndTime(UPDATE)) { DSLContext dsl = getDslContext(ctx); - String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : Formats.JSONV1; + String formatHeader = ctx.req.getContentType(); ContentType contentType = Formats.parseHeader(formatHeader, WaterUser.class); ctx.contentType(contentType.toString()); String newName = requiredParam(ctx, NAME); diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/Pool.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/Pool.java index cc28a8e3e..542f0ebfd 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/Pool.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/Pool.java @@ -7,7 +7,7 @@ import java.util.regex.Pattern; import usace.cwms.db.dao.ifc.pool.PoolNameType; -@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class) +@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class, aliases = {Formats.DEFAULT, Formats.JSON}) public class Pool extends CwmsDTOBase { private final PoolNameType poolName; diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/Pools.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/Pools.java index e8216c122..82dfaaa2c 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/Pools.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/Pools.java @@ -16,7 +16,7 @@ import java.util.List; @JsonRootName("pools") -@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class) +@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class, aliases = {Formats.DEFAULT, Formats.JSON}) @JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) public class Pools extends CwmsDTOPaginated { @JacksonXmlElementWrapper diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/SpecifiedLevel.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/SpecifiedLevel.java index 8dddd4699..47bfd251c 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/SpecifiedLevel.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/SpecifiedLevel.java @@ -4,7 +4,7 @@ import cwms.cda.formatters.json.JsonV2; import cwms.cda.formatters.Formats; -@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class) +@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class, aliases = {Formats.DEFAULT, Formats.JSON}) public class SpecifiedLevel extends CwmsDTO { private String id; diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/TimeSeriesIdentifierDescriptor.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/TimeSeriesIdentifierDescriptor.java index 043680421..74ad29600 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/TimeSeriesIdentifierDescriptor.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/TimeSeriesIdentifierDescriptor.java @@ -16,7 +16,7 @@ @JsonDeserialize(builder = cwms.cda.data.dto.TimeSeriesIdentifierDescriptor.Builder.class) @JsonInclude(JsonInclude.Include.NON_NULL) @JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) -@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class) +@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class, aliases = {Formats.DEFAULT, Formats.JSON}) public class TimeSeriesIdentifierDescriptor extends CwmsDTO { private final String timeSeriesId; private final String timezoneName; diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/basinconnectivity/Basin.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/basinconnectivity/Basin.java index 4836bae0a..fd5442079 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/basinconnectivity/Basin.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/basinconnectivity/Basin.java @@ -6,7 +6,8 @@ import cwms.cda.formatters.Formats; import cwms.cda.formatters.json.PgJsonFormatter; -@FormattableWith(contentType = Formats.NAMED_PGJSON, formatter = NamedPgJsonFormatter.class, aliases = {Formats.DEFAULT, Formats.JSON}) +@FormattableWith(contentType = Formats.NAMED_PGJSON, formatter = NamedPgJsonFormatter.class, + aliases = {Formats.DEFAULT, Formats.JSON}) @FormattableWith(contentType = Formats.PGJSON, formatter = PgJsonFormatter.class) public final class Basin extends CwmsDTO { diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/binarytimeseries/BinaryTimeSeries.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/binarytimeseries/BinaryTimeSeries.java index 68babd682..28168ff5b 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/binarytimeseries/BinaryTimeSeries.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/binarytimeseries/BinaryTimeSeries.java @@ -25,7 +25,7 @@ @JsonDeserialize(builder = BinaryTimeSeries.Builder.class) @JsonInclude(JsonInclude.Include.NON_NULL) @JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) -@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class) +@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class, aliases = {Formats.DEFAULT, Formats.JSON}) public class BinaryTimeSeries extends CwmsDTO { private final String name; private final Long intervalOffset; diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/forecast/ForecastInstance.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/forecast/ForecastInstance.java index bca6a59c8..9e6ec3633 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/forecast/ForecastInstance.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/forecast/ForecastInstance.java @@ -17,7 +17,7 @@ import java.util.Map; @JsonRootName("forecast-instance") -@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class) +@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class, aliases = {Formats.DEFAULT, Formats.JSON}) @JsonDeserialize(builder = ForecastInstance.Builder.class) @JsonInclude(JsonInclude.Include.NON_NULL) @JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/forecast/ForecastSpec.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/forecast/ForecastSpec.java index 131faf5ee..98a546d6d 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/forecast/ForecastSpec.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/forecast/ForecastSpec.java @@ -18,7 +18,7 @@ import java.util.List; @JsonRootName("forecast-spec") -@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class) +@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class, aliases = {Formats.DEFAULT, Formats.JSON}) @JsonDeserialize(builder = ForecastSpec.Builder.class) @JsonInclude(JsonInclude.Include.NON_NULL) @JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/project/ProjectChildLocations.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/project/ProjectChildLocations.java index acffa808d..b4e76c4cb 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/project/ProjectChildLocations.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/project/ProjectChildLocations.java @@ -49,7 +49,7 @@ @JsonDeserialize(builder = ProjectChildLocations.Builder.class) @JsonInclude(JsonInclude.Include.NON_NULL) @JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) -@FormattableWith(contentType = Formats.JSONV1, aliases = {Formats.JSON}, formatter = JsonV1.class) +@FormattableWith(contentType = Formats.JSONV1, aliases = {Formats.DEFAULT, Formats.JSON}, formatter = JsonV1.class) public class ProjectChildLocations extends CwmsDTOBase { private final CwmsId projectId; diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/rating/RatingMetadataList.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/rating/RatingMetadataList.java index c25139a67..f74e3bdec 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/rating/RatingMetadataList.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/rating/RatingMetadataList.java @@ -42,7 +42,7 @@ @JsonDeserialize(builder = RatingMetadataList.Builder.class) @JsonInclude(JsonInclude.Include.NON_NULL) @JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) -@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class) +@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class, aliases = {Formats.DEFAULT, Formats.JSON}) public class RatingMetadataList extends CwmsDTOPaginated { private List metadata; diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/rating/RatingSpec.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/rating/RatingSpec.java index de8dc86b1..8d4654220 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/rating/RatingSpec.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/rating/RatingSpec.java @@ -21,7 +21,7 @@ @JsonDeserialize(builder = RatingSpec.Builder.class) @JsonInclude(JsonInclude.Include.NON_NULL) @JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) -@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class) +@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class, aliases = {Formats.DEFAULT, Formats.JSON}) public class RatingSpec extends CwmsDTO { private final String ratingId; private final String templateId; diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/rating/RatingSpecs.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/rating/RatingSpecs.java index 59c0efa08..5a568e0a4 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/rating/RatingSpecs.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/rating/RatingSpecs.java @@ -16,7 +16,7 @@ @JsonDeserialize(builder = RatingSpecs.Builder.class) @JsonInclude(JsonInclude.Include.NON_NULL) @JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) -@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class) +@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class, aliases = {Formats.DEFAULT, Formats.JSON}) public class RatingSpecs extends CwmsDTOPaginated { private List specs; diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/rating/RatingTemplate.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/rating/RatingTemplate.java index 06611fa65..acdee07a7 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/rating/RatingTemplate.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/rating/RatingTemplate.java @@ -20,7 +20,7 @@ @JsonDeserialize(builder = RatingTemplate.Builder.class) @JsonInclude(JsonInclude.Include.NON_NULL) @JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) -@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class) +@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class, aliases = {Formats.DEFAULT, Formats.JSON}) public class RatingTemplate extends CwmsDTO { private final String id; private final String version; diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/rating/RatingTemplates.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/rating/RatingTemplates.java index 3f072cd49..ae1413117 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/rating/RatingTemplates.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/rating/RatingTemplates.java @@ -16,7 +16,7 @@ @JsonDeserialize(builder = RatingTemplates.Builder.class) @JsonInclude(JsonInclude.Include.NON_NULL) @JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) -@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class) +@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class, aliases = {Formats.DEFAULT, Formats.JSON}) public class RatingTemplates extends CwmsDTOPaginated { private List templates; diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/texttimeseries/StandardTextCatalog.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/texttimeseries/StandardTextCatalog.java index 6044cb95f..0f54610d9 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/texttimeseries/StandardTextCatalog.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/texttimeseries/StandardTextCatalog.java @@ -23,7 +23,7 @@ @JsonInclude(JsonInclude.Include.NON_NULL) @JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) @JsonIgnoreProperties("office-id") // This blocks office-id from the super! -@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class) +@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class, aliases = {Formats.DEFAULT, Formats.JSON}) public class StandardTextCatalog extends CwmsDTO { private final NavigableMap values; diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/texttimeseries/StandardTextValue.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/texttimeseries/StandardTextValue.java index 115698d6a..fb42be7e4 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/texttimeseries/StandardTextValue.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/texttimeseries/StandardTextValue.java @@ -19,7 +19,7 @@ @JsonInclude(JsonInclude.Include.NON_NULL) @JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) @JsonIgnoreProperties("office-id") // This does work to block office-id from the super! -@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class) +@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class, aliases = {Formats.DEFAULT, Formats.JSON}) public class StandardTextValue extends CwmsDTO { private final StandardTextId id; diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/texttimeseries/TextTimeSeries.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/texttimeseries/TextTimeSeries.java index a3c2a986d..bedcf7906 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/texttimeseries/TextTimeSeries.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/texttimeseries/TextTimeSeries.java @@ -29,7 +29,7 @@ @JsonDeserialize(builder = TextTimeSeries.Builder.class) @JsonInclude(JsonInclude.Include.NON_NULL) @JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class) -@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class) +@FormattableWith(contentType = Formats.JSONV2, formatter = JsonV2.class, aliases = {Formats.DEFAULT, Formats.JSON}) public class TextTimeSeries extends CwmsDTO { diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/watersupply/WaterUserContract.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/watersupply/WaterUserContract.java index 7a5035a00..4d2360f4e 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/watersupply/WaterUserContract.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/watersupply/WaterUserContract.java @@ -37,7 +37,7 @@ import java.time.Instant; -@FormattableWith(contentType = Formats.JSONV1, formatter = JsonV1.class) +@FormattableWith(contentType = Formats.JSONV1, formatter = JsonV1.class, aliases = {Formats.DEFAULT, Formats.JSON}) @JsonDeserialize(builder = WaterUserContract.Builder.class) public final class WaterUserContract extends CwmsDTO { 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 536dd373b..a47a9a6d7 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 @@ -27,10 +27,10 @@ import cwms.cda.data.dto.CwmsDTOBase; import cwms.cda.formatters.annotations.FormattableWith; -import javax.validation.constraints.NotNull; +import java.util.SortedSet; +import java.util.TreeSet; import java.io.InputStream; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; @@ -40,6 +40,8 @@ import java.util.logging.Logger; import java.util.stream.Collectors; import java.util.stream.Stream; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public class Formats { @@ -74,7 +76,8 @@ public class Formats { static { contentTypeList.addAll( - Stream.of(JSON, JSONV1, XML, XMLV1, XMLV2, WML2, JSONV2, TAB, CSV, GEOJSON, PGJSON, NAMED_PGJSON) + Stream.of(DEFAULT, JSON, JSONV1, XML, XMLV1, XMLV2, WML2, JSONV2, + TAB, CSV, GEOJSON, PGJSON, NAMED_PGJSON) .map(ContentType::new) .collect(Collectors.toList())); } @@ -240,21 +243,6 @@ public static List parseContentList(ContentType type, return formats.parseContentListFromType(type, content, rootType); } - /** - * Parses the supplied header param or queryParam to determine the content type. - * If both are supplied an exception is thrown. If neither are supplied an exception is thrown. - * - * @param header Accept header value - * @param queryParam format query parameter value - * @return an appropriate standard mimetype for lookup - * @throws FormattingException if the header and queryParam are both supplied or neither are - * @deprecated Use overloaded parseHeaderAndQueryParm that takes in a class to utilize the format aliasing. - */ - @Deprecated - public static ContentType parseHeaderAndQueryParm(String header, String queryParam) { - return parseHeaderAndQueryParm(header, queryParam, null); - } - /** * Parses the supplied header param or queryParam to determine the content type. * If both are supplied an exception is thrown. If neither are supplied an exception is thrown. @@ -266,7 +254,8 @@ public static ContentType parseHeaderAndQueryParm(String header, String queryPar * @return an appropriate standard mimetype for lookup * @throws FormattingException if the header and queryParam are both supplied or neither are */ - public static ContentType parseHeaderAndQueryParm(String header, String queryParam, Class klass) { + public static ContentType parseHeaderAndQueryParm(String header, String queryParam, + Class klass) { if (queryParam != null && !queryParam.isEmpty()) { if (header != null && !header.isEmpty() && !DEFAULT.equals(header.trim())) { // If the user supplies an accept header and also a format= parameter, which @@ -287,12 +276,8 @@ public static ContentType parseHeaderAndQueryParm(String header, String queryPar } else if (header == null) { throw new UnsupportedFormatException("no content type or format specified"); } else { - ContentType ct = parseHeader(header, klass); - if (ct != null) { - return ct; - } + return parseHeader(header, klass); } - throw new UnsupportedFormatException("Content-Type " + header + " is not available"); } public static ContentType parseQueryParam(String queryParam, Class klass) @@ -322,58 +307,50 @@ public static ContentType parseQueryParam(String queryParam, ClassFormattableWith annotations. + * @param header Accept header value. If null, will assume */* content type + * @param klass DTO object class, used for identifying content type aliases from the DTO's + * {@link cwms.cda.formatters.annotations.FormattableWith} annotations. * @return an appropriate standard mimetype for lookup * @throws FormattingException if the header can't be identified as a mimetype */ - public static @NotNull ContentType parseHeader(String header, Class klass) { - ContentTypeAliasMap aliasMap = ContentTypeAliasMap.empty(); - if (klass != null) { - aliasMap = ContentTypeAliasMap.forDtoClass(klass); + public static @NotNull ContentType parseHeader(@Nullable String header, + @NotNull Class klass) { + Objects.requireNonNull(klass, "Cannot determine content type without a DTO class definition"); + ContentTypeAliasMap aliasMap = ContentTypeAliasMap.forDtoClass(klass); + //Swap out null content type with */* for flexibility. + //This routine will match DTO's when the DEFAULT alias specified by the format annotations. + if(header == null || header.trim().isEmpty()) { + header = DEFAULT; } - ArrayList contentTypes = new ArrayList<>(); - if (header != null && !header.isEmpty()) { - String[] all = header.split(","); - logger.log(Level.FINEST, "Finding handlers {0}", all.length); - for (String ct : all) { - ContentType aliasType = aliasMap.getContentType(ct); - if (aliasType != null) { - logger.finest(() -> ct + " converted to " + aliasType); - contentTypes.add(aliasType); - } - else { - logger.finest(ct); - contentTypes.add(new ContentType(ct)); + //TreeSet will sort based on prioritized content type + //if multiple valid content types are specified in the header. + SortedSet contentTypes = new TreeSet<>(); + String[] all = header.split(","); + logger.log(Level.FINEST, "Finding handlers {0}", all.length); + for (String ct : all) { + ContentType aliasType = aliasMap.getContentType(ct); + //Found type defined in annotations, add to the priority list. + if (aliasType != null) { + logger.finest(() -> ct + " converted to " + aliasType); + contentTypes.add(aliasType); + } else { + //If the DTO parameter is null, alias map is empty. Compare against well-known types + //Only use the ContentType classes initialized in contentTypeList rather than + //the client headers itself + ContentType type = new ContentType(ct); + if(contentTypeList.contains(type)) { + contentTypes.add(type); } } - Collections.sort(contentTypes); } logger.finest(() -> "have " + contentTypes.size()); + //Look through known content types to match using priority sorted TreeSet for (ContentType ct : contentTypes) { logger.finest(() -> "checking " + ct.toString()); if (contentTypeList.contains(ct)) { return ct; } } - for (ContentType ct : contentTypes) { - if (ct.getType().equals(DEFAULT)) { - return new ContentType(Formats.JSON); - } - } throw new UnsupportedFormatException("Format header " + header + " could not be parsed"); } } diff --git a/cwms-data-api/src/test/java/cwms/cda/api/ClobControllerTest.java b/cwms-data-api/src/test/java/cwms/cda/api/ClobControllerTest.java index 344048b70..239aab655 100644 --- a/cwms-data-api/src/test/java/cwms/cda/api/ClobControllerTest.java +++ b/cwms-data-api/src/test/java/cwms/cda/api/ClobControllerTest.java @@ -63,7 +63,7 @@ void testDeserialize() throws JsonProcessingException { String input = "{\"office-id\":\"MYOFFICE\",\"id\":\"MYID\",\"description\":\"MYDESC\"," + "\"value\":\"MYVALUE\"}"; - Clob clob = Formats.parseContent(Formats.parseHeader(Formats.JSONV2),input, Clob.class); + Clob clob = Formats.parseContent(Formats.parseHeader(Formats.JSONV2, Clob.class),input, Clob.class); assertNotNull(clob); assertEquals("MYOFFICE", clob.getOfficeId()); assertEquals("MYID", clob.getId()); diff --git a/cwms-data-api/src/test/java/cwms/cda/api/LevelControllerTest.java b/cwms-data-api/src/test/java/cwms/cda/api/LevelControllerTest.java index f3ce9e0cf..5f02ac28c 100644 --- a/cwms-data-api/src/test/java/cwms/cda/api/LevelControllerTest.java +++ b/cwms-data-api/src/test/java/cwms/cda/api/LevelControllerTest.java @@ -48,7 +48,7 @@ void testDeserializeConstantLevelXml() throws Exception ZonedDateTimeAdapter dateTimeAdapter = new ZonedDateTimeAdapter(); String xml = loadResourceAsString("cwms/cda/api/levels_constant_create.xml"); assertNotNull(xml); - LocationLevel level = Formats.parseContent(Formats.parseHeader(Formats.XML), xml, LocationLevel.class); + LocationLevel level = Formats.parseContent(Formats.parseHeader(Formats.XML, LocationLevel.class), xml, LocationLevel.class); assertNotNull(level); assertEquals("LOC_TEST.Elev.Inst.0.Bottom of Inlet", level.getLocationLevelId()); assertEquals(OFFICE_ID, level.getOfficeId()); @@ -78,12 +78,14 @@ void testDeserializeTimeSeriesLevelXml() throws Exception ZonedDateTimeAdapter dateTimeAdapter = new ZonedDateTimeAdapter(); String xml = loadResourceAsString("cwms/cda/api/levels_timeseries_create.xml"); assertNotNull(xml); - LocationLevel level = Formats.parseContent(Formats.parseHeader(Formats.XML), xml, LocationLevel.class); + LocationLevel level = Formats.parseContent(Formats.parseHeader(Formats.XML, LocationLevel.class), + xml, LocationLevel.class); assertNotNull(level); assertEquals("LOC_TEST.Elev.Inst.0.Bottom of Inlet", level.getLocationLevelId()); assertEquals(OFFICE_ID, level.getOfficeId()); assertEquals("ft", level.getLevelUnitsId()); - assertEquals(dateTimeAdapter.unmarshal("2008-12-03T10:15:30+01:00[Z]").toInstant(), level.getLevelDate().toInstant()); + assertEquals(dateTimeAdapter.unmarshal("2008-12-03T10:15:30+01:00[Z]").toInstant(), + level.getLevelDate().toInstant()); assertEquals("RYAN3.Stage.Inst.5Minutes.0.ZSTORE_TS_TEST630", level.getSeasonalTimeSeriesId()); } diff --git a/cwms-data-api/src/test/java/cwms/cda/api/LocationCategoryControllerTestIT.java b/cwms-data-api/src/test/java/cwms/cda/api/LocationCategoryControllerTestIT.java index ae57c28ca..a5848cc44 100644 --- a/cwms-data-api/src/test/java/cwms/cda/api/LocationCategoryControllerTestIT.java +++ b/cwms-data-api/src/test/java/cwms/cda/api/LocationCategoryControllerTestIT.java @@ -50,8 +50,9 @@ class LocationCategoryControllerTestIT extends DataApiTestIT { void test_create_read_delete() throws Exception { String officeId = "SPK"; TestAccounts.KeyUser user = TestAccounts.KeyUser.SPK_NORMAL; - LocationCategory cat = new LocationCategory(officeId, LocationCategoryControllerTestIT.class.getSimpleName(), "IntegrationTesting"); - ContentType contentType = Formats.parseHeaderAndQueryParm(Formats.JSON, null); + LocationCategory cat = new LocationCategory(officeId, LocationCategoryControllerTestIT.class.getSimpleName(), + "IntegrationTesting"); + ContentType contentType = Formats.parseHeader(Formats.JSON, LocationCategory.class); String xml = Formats.format(contentType, cat); registerCategory(cat); //Create Category diff --git a/cwms-data-api/src/test/java/cwms/cda/api/LocationControllerTest.java b/cwms-data-api/src/test/java/cwms/cda/api/LocationControllerTest.java index c1771ca5b..364412ecb 100644 --- a/cwms-data-api/src/test/java/cwms/cda/api/LocationControllerTest.java +++ b/cwms-data-api/src/test/java/cwms/cda/api/LocationControllerTest.java @@ -36,7 +36,8 @@ void testDeserializeLocationXml() throws IOException { String xml = loadResourceAsString("cwms/cda/api/location_create.xml"); assertNotNull(xml); - Location location = Formats.parseContent(Formats.parseHeader(Formats.XML), xml, Location.class); + Location location = Formats.parseContent(Formats.parseHeader(Formats.XML, Location.class), + xml, Location.class); assertNotNull(location); assertEquals("LOC_TEST", location.getName()); assertEquals("LRL", location.getOfficeId()); @@ -52,7 +53,8 @@ void testDeserializeLocationJSON() throws Exception final String json = loadResourceAsString("cwms/cda/api/location_create_spk.json"); assertNotNull(json); - Location location = Formats.parseContent(Formats.parseHeader(Formats.JSON), json, Location.class); + Location location = Formats.parseContent(Formats.parseHeader(Formats.JSON, Location.class), + json, Location.class); assertNotNull(location); assertEquals("LOC_TEST", location.getName()); assertEquals(OFFICE, location.getOfficeId()); diff --git a/cwms-data-api/src/test/java/cwms/cda/api/LocationControllerTestIT.java b/cwms-data-api/src/test/java/cwms/cda/api/LocationControllerTestIT.java index 5d7255a94..52fe321fa 100644 --- a/cwms-data-api/src/test/java/cwms/cda/api/LocationControllerTestIT.java +++ b/cwms-data-api/src/test/java/cwms/cda/api/LocationControllerTestIT.java @@ -51,7 +51,8 @@ public class LocationControllerTestIT extends DataApiTestIT { void test_location_create_get_delete() throws Exception { String officeId = "SPK"; String json = loadResourceAsString("cwms/cda/api/location_create_spk.json"); - Location location = new Location.Builder(Formats.parseContent(Formats.parseHeader(Formats.JSON), json, Location.class)) + Location location = new Location.Builder(Formats.parseContent(Formats.parseHeader(Formats.JSON, Location.class), + json, Location.class)) .withOfficeId(officeId) //withName(getClass().getSimpleName()) .build(); diff --git a/cwms-data-api/src/test/java/cwms/cda/api/LocationGroupControllerTestIT.java b/cwms-data-api/src/test/java/cwms/cda/api/LocationGroupControllerTestIT.java index a04bff626..1c6bb002a 100644 --- a/cwms-data-api/src/test/java/cwms/cda/api/LocationGroupControllerTestIT.java +++ b/cwms-data-api/src/test/java/cwms/cda/api/LocationGroupControllerTestIT.java @@ -59,7 +59,7 @@ void test_getall() throws Exception { "sharedLocAliasId", locationId, 123); List assignedLocations = group.getAssignedLocations(); assignedLocations.add(new AssignedLocation(locationId, officeId, "AliasId", 1, locationId)); - ContentType contentType = Formats.parseHeaderAndQueryParm(Formats.JSON, null); + ContentType contentType = Formats.parseHeader(Formats.JSON, LocationCategory.class); String categoryXml = Formats.format(contentType, cat); String groupXml = Formats.format(contentType, group); //Create Category @@ -139,7 +139,7 @@ void test_create_read_delete() throws Exception { "sharedLocAliasId", locationId, 123); List assignedLocations = group.getAssignedLocations(); assignedLocations.add(new AssignedLocation(locationId, officeId, "AliasId", 1, locationId)); - ContentType contentType = Formats.parseHeaderAndQueryParm(Formats.JSON, null); + ContentType contentType = Formats.parseHeader(Formats.JSON, LocationCategory.class); String categoryXml = Formats.format(contentType, cat); String groupXml = Formats.format(contentType, group); //Create Category @@ -257,7 +257,7 @@ void test_rename_group() throws Exception { registerGroup(group); List assignedLocations = group.getAssignedLocations(); assignedLocations.add(new AssignedLocation(locationId, officeId, "AliasId", 1, locationId)); - ContentType contentType = Formats.parseHeaderAndQueryParm(Formats.JSON, null); + ContentType contentType = Formats.parseHeader(Formats.JSON, LocationCategory.class); String categoryXml = Formats.format(contentType, cat); String groupXml = Formats.format(contentType, group); //Create Category @@ -363,7 +363,7 @@ void test_add_assigned_locs() throws Exception { "sharedLocAliasId", locationId, 123); List assignedLocations = group.getAssignedLocations(); assignedLocations.add(new AssignedLocation(locationId, officeId, "AliasId", 1, locationId)); - ContentType contentType = Formats.parseHeaderAndQueryParm(Formats.JSON, null); + ContentType contentType = Formats.parseHeader(Formats.JSON, LocationCategory.class); String categoryXml = Formats.format(contentType, cat); String groupXml = Formats.format(contentType, group); //Create Category diff --git a/cwms-data-api/src/test/java/cwms/cda/api/TimeSeriesCategoryControllerTestIT.java b/cwms-data-api/src/test/java/cwms/cda/api/TimeSeriesCategoryControllerTestIT.java index 334847623..c681cb668 100644 --- a/cwms-data-api/src/test/java/cwms/cda/api/TimeSeriesCategoryControllerTestIT.java +++ b/cwms-data-api/src/test/java/cwms/cda/api/TimeSeriesCategoryControllerTestIT.java @@ -53,7 +53,7 @@ void test_create_read_delete() throws Exception { String officeId = "SPK"; TestAccounts.KeyUser user = TestAccounts.KeyUser.SPK_NORMAL; TimeSeriesCategory cat = new TimeSeriesCategory(officeId, "test_create_read_delete", "IntegrationTesting"); - ContentType contentType = Formats.parseHeaderAndQueryParm(Formats.JSON, null); + ContentType contentType = Formats.parseHeader(Formats.JSON, TimeSeriesCategory.class); String xml = Formats.format(contentType, cat); //Create Category given() diff --git a/cwms-data-api/src/test/java/cwms/cda/api/TimeSeriesGroupControllerTestIT.java b/cwms-data-api/src/test/java/cwms/cda/api/TimeSeriesGroupControllerTestIT.java index 46a07b967..3e584fea9 100644 --- a/cwms-data-api/src/test/java/cwms/cda/api/TimeSeriesGroupControllerTestIT.java +++ b/cwms-data-api/src/test/java/cwms/cda/api/TimeSeriesGroupControllerTestIT.java @@ -161,7 +161,7 @@ void test_create_read_delete() throws Exception { BigDecimal tsCode = getTsCode(officeId, timeSeriesId); assignedTimeSeries.add(new AssignedTimeSeries(officeId,timeSeriesId, tsCode, "AliasId", timeSeriesId, 1)); - ContentType contentType = Formats.parseHeaderAndQueryParm(Formats.JSON, null); + ContentType contentType = Formats.parseHeader(Formats.JSON, TimeSeriesCategory.class); String categoryXml = Formats.format(contentType, cat); String groupXml = Formats.format(contentType, group); //Create Category @@ -309,7 +309,7 @@ void test_rename_group() throws Exception { BigDecimal tsCode = getTsCode(officeId, timeSeriesId); assignedTimeSeries.add(new AssignedTimeSeries(officeId,timeSeriesId, tsCode, "AliasId", timeSeriesId, 1)); - ContentType contentType = Formats.parseHeaderAndQueryParm(Formats.JSON, null); + ContentType contentType = Formats.parseHeader(Formats.JSON, TimeSeriesCategory.class); String categoryXml = Formats.format(contentType, cat); String groupXml = Formats.format(contentType, group); //Create Category @@ -451,7 +451,7 @@ void test_add_assigned_locs() throws Exception { BigDecimal tsCode = getTsCode(officeId, timeSeriesId); assignedTimeSeries.add(new AssignedTimeSeries(officeId, timeSeriesId, tsCode, "AliasId", timeSeriesId, 1)); - ContentType contentType = Formats.parseHeaderAndQueryParm(Formats.JSON, null); + ContentType contentType = Formats.parseHeader(Formats.JSON, TimeSeriesCategory.class); String categoryXml = Formats.format(contentType, cat); String groupXml = Formats.format(contentType, group); //Create Category diff --git a/cwms-data-api/src/test/java/cwms/cda/api/auth/ApiKeyControllerTestIT.java b/cwms-data-api/src/test/java/cwms/cda/api/auth/ApiKeyControllerTestIT.java index 5d631bb8d..ebe72f6f4 100644 --- a/cwms-data-api/src/test/java/cwms/cda/api/auth/ApiKeyControllerTestIT.java +++ b/cwms-data-api/src/test/java/cwms/cda/api/auth/ApiKeyControllerTestIT.java @@ -220,7 +220,8 @@ public void test_api_key_listing(String authType, TestAccounts.KeyUser theUser, public void test_key_usage() throws Exception { createLocation("ApiKey-Test Location",true,"SPK"); String json = loadResourceAsString("cwms/cda/api/location_create.json"); - Location location = new Location.Builder(Formats.parseContent(Formats.parseHeader(Formats.JSON), json, Location.class)) + Location location = new Location.Builder(Formats.parseContent(Formats.parseHeader(Formats.JSON, Location.class), + json, Location.class)) .withOfficeId("SPK") .withName(getClass().getSimpleName()) .build(); diff --git a/cwms-data-api/src/test/java/cwms/cda/data/dto/ClobTest.java b/cwms-data-api/src/test/java/cwms/cda/data/dto/ClobTest.java index 99448e2f5..b8443bd97 100644 --- a/cwms-data-api/src/test/java/cwms/cda/data/dto/ClobTest.java +++ b/cwms-data-api/src/test/java/cwms/cda/data/dto/ClobTest.java @@ -49,7 +49,7 @@ void testRoundtripXML() throws JAXBException { assertNotNull(output); - Clob clob2 = Formats.parseContent(Formats.parseHeader(Formats.XMLV2), output, Clob.class); + Clob clob2 = Formats.parseContent(Formats.parseHeader(Formats.XMLV2, Clob.class), output, Clob.class); assertNotNull(clob2); @@ -76,7 +76,7 @@ void testRoundtripXML2() throws JsonProcessingException { // MYVALUE // - Clob clob2 = Formats.parseContent(Formats.parseHeader(Formats.XMLV2), output, Clob.class); + Clob clob2 = Formats.parseContent(Formats.parseHeader(Formats.XMLV2, Clob.class), output, Clob.class); assertNotNull(clob2); @@ -93,9 +93,9 @@ void testRoundtripXML2Clobs(String format) { Clobs clobs = new Clobs.Builder("cursor", 1, 1) .addClob(clob) .build(); - String output = Formats.format(Formats.parseHeader(format), clobs); + String output = Formats.format(Formats.parseHeader(format, Clobs.class), clobs); - Clobs clobs2 = Formats.parseContent(Formats.parseHeader(format), output, Clobs.class); + Clobs clobs2 = Formats.parseContent(Formats.parseHeader(format, Clobs.class), output, Clobs.class); assertNotNull(clobs2); diff --git a/cwms-data-api/src/test/java/cwms/cda/data/dto/LocationLevelTest.java b/cwms-data-api/src/test/java/cwms/cda/data/dto/LocationLevelTest.java index 22665a2ee..92e9d8555 100644 --- a/cwms-data-api/src/test/java/cwms/cda/data/dto/LocationLevelTest.java +++ b/cwms-data-api/src/test/java/cwms/cda/data/dto/LocationLevelTest.java @@ -66,7 +66,7 @@ void test_serialization_formats() ZonedDateTime zdt = ZonedDateTime.parse("2021-06-21T08:00:00-07:00[PST8PDT]"); final LocationLevel level = new LocationLevel.Builder("Test", zdt).build(); - ContentType contentType = Formats.parseHeader(Formats.JSONV2); + ContentType contentType = Formats.parseHeader(Formats.JSONV2, LocationLevel.class); String jsonStr = Formats.format(contentType, level); // If JSONv2 isn't setup correctly it will serialize the level like: diff --git a/cwms-data-api/src/test/java/cwms/cda/data/dto/LocationTest.java b/cwms-data-api/src/test/java/cwms/cda/data/dto/LocationTest.java index 7145a2836..c6800c9f6 100644 --- a/cwms-data-api/src/test/java/cwms/cda/data/dto/LocationTest.java +++ b/cwms-data-api/src/test/java/cwms/cda/data/dto/LocationTest.java @@ -57,10 +57,11 @@ void testSerializationRoundTrip(String format) throws JsonProcessingException Location location = buildTestLocation(); assertNotNull(location); - String serialized = Formats.format(Formats.parseHeader(format), location); + String serialized = Formats.format(Formats.parseHeader(format, Location.class), location); assertNotNull(serialized); - Location deserialized = Formats.parseContent(Formats.parseHeader(format), serialized, Location.class); + Location deserialized = Formats.parseContent(Formats.parseHeader(format, Location.class), + serialized, Location.class); assertEquals(location, deserialized); } diff --git a/cwms-data-api/src/test/java/cwms/cda/data/dto/TimeSeriesGroupTest.java b/cwms-data-api/src/test/java/cwms/cda/data/dto/TimeSeriesGroupTest.java index 29b7b9ea3..014e06d50 100644 --- a/cwms-data-api/src/test/java/cwms/cda/data/dto/TimeSeriesGroupTest.java +++ b/cwms-data-api/src/test/java/cwms/cda/data/dto/TimeSeriesGroupTest.java @@ -15,7 +15,7 @@ class TimeSeriesGroupTest void test_serialize_json(){ TimeSeriesGroup group = buildTimeSeriesGroup(); - ContentType contentType = Formats.parseHeader(Formats.JSON); + ContentType contentType = Formats.parseHeader(Formats.JSON, TimeSeriesGroup.class); String result = Formats.format(contentType, group); assertNotNull(result); diff --git a/cwms-data-api/src/test/java/cwms/cda/data/dto/TimeSeriesIdentifierDescriptorTest.java b/cwms-data-api/src/test/java/cwms/cda/data/dto/TimeSeriesIdentifierDescriptorTest.java index 511b6a577..a84a98431 100644 --- a/cwms-data-api/src/test/java/cwms/cda/data/dto/TimeSeriesIdentifierDescriptorTest.java +++ b/cwms-data-api/src/test/java/cwms/cda/data/dto/TimeSeriesIdentifierDescriptorTest.java @@ -96,7 +96,7 @@ void test_serialization_with_formats() // If JsonV2 does not contain TimeSeriesIdentifierDescriptor in its list of classes TimeSeriesIdentifierDescriptor tsID = buildTsId(); - ContentType contentType = Formats.parseHeader(Formats.JSONV2); + ContentType contentType = Formats.parseHeader(Formats.JSONV2, TimeSeriesIdentifierDescriptor.class); String jsonStr = Formats.format(contentType, tsID); assertNotNull(jsonStr); } diff --git a/cwms-data-api/src/test/java/cwms/cda/data/dto/catalog/LocationCatalogEntryTest.java b/cwms-data-api/src/test/java/cwms/cda/data/dto/catalog/LocationCatalogEntryTest.java index 0b4f9befd..e76ec0ece 100644 --- a/cwms-data-api/src/test/java/cwms/cda/data/dto/catalog/LocationCatalogEntryTest.java +++ b/cwms-data-api/src/test/java/cwms/cda/data/dto/catalog/LocationCatalogEntryTest.java @@ -18,7 +18,7 @@ void test_json_serialization_no_cursor(){ Catalog cat = new Catalog(null, 1, 10, new ArrayList(){{add(entry);}}); - ContentType contentType = Formats.parseHeader(Formats.JSONV2); + ContentType contentType = Formats.parseHeader(Formats.JSONV2, Catalog.class); String json = Formats.format(contentType, cat); assertNotNull(json); @@ -35,7 +35,7 @@ void test_xml_serialization_no_cursor() { add(entry); }}); - ContentType contentType = Formats.parseHeader(Formats.XML); + ContentType contentType = Formats.parseHeader(Formats.XML, Catalog.class); String xml = Formats.format(contentType, cat); assertNotNull(xml); diff --git a/cwms-data-api/src/test/java/cwms/cda/data/dto/catalog/TimeseriesCatalogEntryTest.java b/cwms-data-api/src/test/java/cwms/cda/data/dto/catalog/TimeseriesCatalogEntryTest.java index 085084605..4f265e47c 100644 --- a/cwms-data-api/src/test/java/cwms/cda/data/dto/catalog/TimeseriesCatalogEntryTest.java +++ b/cwms-data-api/src/test/java/cwms/cda/data/dto/catalog/TimeseriesCatalogEntryTest.java @@ -26,7 +26,7 @@ void test_xml_serialization_earliest(){ Catalog cat = new Catalog(null, 1, 10, new ArrayList(){{add(entry);}}); - ContentType contentType = Formats.parseHeader(Formats.XML); + ContentType contentType = Formats.parseHeader(Formats.XML, Catalog.class); String xml = Formats.format(contentType, cat); assertNotNull(xml); @@ -51,7 +51,7 @@ void test_json_serialization_earliest(){ Catalog cat = new Catalog(null, 1, 10, new ArrayList(){{add(entry);}}); - ContentType contentType = Formats.parseHeader(Formats.JSONV2); + ContentType contentType = Formats.parseHeader(Formats.JSONV2, Catalog.class); String json = Formats.format(contentType, cat); assertNotNull(json); @@ -75,7 +75,7 @@ void test_json_serialization_no_cursor(){ Catalog cat = new Catalog(null, 1, 10, new ArrayList(){{add(entry);}}); - ContentType contentType = Formats.parseHeader(Formats.JSONV2); + ContentType contentType = Formats.parseHeader(Formats.JSONV2, Catalog.class); String json = Formats.format(contentType, cat); assertNotNull(json); @@ -92,7 +92,7 @@ void test_xml_serialization_no_cursor() { add(entry); }}); - ContentType contentType = Formats.parseHeader(Formats.XML); + ContentType contentType = Formats.parseHeader(Formats.XML, Catalog.class); String xml = Formats.format(contentType, cat); assertNotNull(xml); diff --git a/cwms-data-api/src/test/java/cwms/cda/data/dto/forecast/ForecastInstanceTest.java b/cwms-data-api/src/test/java/cwms/cda/data/dto/forecast/ForecastInstanceTest.java index bb4323b4c..71bfe516c 100644 --- a/cwms-data-api/src/test/java/cwms/cda/data/dto/forecast/ForecastInstanceTest.java +++ b/cwms-data-api/src/test/java/cwms/cda/data/dto/forecast/ForecastInstanceTest.java @@ -41,7 +41,7 @@ void testRoundTripJson() throws JsonProcessingException { @Test void testFormatsSerialization() { ForecastInstance i1 = buildForecastInstance(); - ContentType contentType = Formats.parseHeader(Formats.JSONV2); + ContentType contentType = Formats.parseHeader(Formats.JSONV2, ForecastInstance.class); String jsonStr = Formats.format(contentType, i1); assertNotNull(jsonStr); } diff --git a/cwms-data-api/src/test/java/cwms/cda/data/dto/forecast/ForecastSpecTest.java b/cwms-data-api/src/test/java/cwms/cda/data/dto/forecast/ForecastSpecTest.java index efda5c6af..771bcd5b5 100644 --- a/cwms-data-api/src/test/java/cwms/cda/data/dto/forecast/ForecastSpecTest.java +++ b/cwms-data-api/src/test/java/cwms/cda/data/dto/forecast/ForecastSpecTest.java @@ -39,7 +39,7 @@ void testRoundTripJson() throws JsonProcessingException { @Test void testFormatsSerialization() { ForecastSpec s1 = buildForecastSpec(); - ContentType contentType = Formats.parseHeader(Formats.JSONV2); + ContentType contentType = Formats.parseHeader(Formats.JSONV2, ForecastSpec.class); String jsonStr = Formats.format(contentType, s1); assertNotNull(jsonStr); } diff --git a/cwms-data-api/src/test/java/cwms/cda/formatters/FormatsTest.java b/cwms-data-api/src/test/java/cwms/cda/formatters/FormatsTest.java index feb73e1c4..1b44bdb9e 100644 --- a/cwms-data-api/src/test/java/cwms/cda/formatters/FormatsTest.java +++ b/cwms-data-api/src/test/java/cwms/cda/formatters/FormatsTest.java @@ -5,10 +5,12 @@ import cwms.cda.data.dto.Blob; import cwms.cda.data.dto.Blobs; +import cwms.cda.data.dto.Catalog; import cwms.cda.data.dto.Clob; import cwms.cda.data.dto.Clobs; import cwms.cda.data.dto.County; import cwms.cda.data.dto.CwmsDTOBase; +import cwms.cda.data.dto.LocationLevels; import cwms.cda.data.dto.Office; import cwms.cda.data.dto.State; import cwms.cda.data.dto.basinconnectivity.Basin; @@ -26,18 +28,17 @@ class FormatsTest { @Test void testParseHeaderAndQueryParmJson() { - ContentType contentType = Formats.parseHeaderAndQueryParm("application/json", null); - + ContentType contentType = Formats.parseHeaderAndQueryParm("application/json", null, LocationLevels.class); assertNotNull(contentType); assertEquals("application/json", contentType.getType()); Map parameters = contentType.getParameters(); - assertTrue(parameters == null || parameters.isEmpty()); - + assertEquals("2", parameters.get("version")); } @Test void testParseHeaderAndQueryParmJsonV2() { - ContentType contentType = Formats.parseHeaderAndQueryParm("application/json;version=2", null); + ContentType contentType = Formats.parseHeaderAndQueryParm("application/json;version=2", + null, LocationLevels.class); assertNotNull(contentType); assertEquals("application/json", contentType.getType()); @@ -50,39 +51,39 @@ void testParseHeaderAndQueryParmJsonV2() { @Test void testParseNullNull() { - assertThrows(FormattingException.class, () -> Formats.parseHeaderAndQueryParm(null, null)); + assertThrows(FormattingException.class, () -> Formats.parseHeaderAndQueryParm(null, null, Catalog.class)); } @Test void testParseEmptyHeader() { - ContentType contentType = Formats.parseHeaderAndQueryParm("", "json"); + ContentType contentType = Formats.parseHeaderAndQueryParm("", "json", LocationLevels.class); assertNotNull(contentType); assertEquals("application/json", contentType.getType()); Map parameters = contentType.getParameters(); - assertTrue(parameters == null || parameters.isEmpty()); + assertEquals("2", parameters.get("version")); } @Test void testParseNullHeader() { - ContentType contentType = Formats.parseHeaderAndQueryParm(null, "json"); + ContentType contentType = Formats.parseHeaderAndQueryParm(null, "json", Catalog.class); assertNotNull(contentType); assertEquals("application/json", contentType.getType()); Map parameters = contentType.getParameters(); - assertTrue(parameters == null || parameters.isEmpty()); + assertEquals("1", parameters.get("version")); } @Test void testParseHeaderAndQueryParmXml() { assertThrows(FormattingException.class, () -> { - Formats.parseHeaderAndQueryParm(null, null); + Formats.parseHeaderAndQueryParm(null, null, Catalog.class); }); - ContentType contentType = Formats.parseHeaderAndQueryParm("application/xml", null); + ContentType contentType = Formats.parseHeaderAndQueryParm("application/xml", null, Catalog.class); assertNotNull(contentType); assertEquals("application/xml", contentType.getType()); @@ -90,7 +91,7 @@ void testParseHeaderAndQueryParmXml() { assertTrue(parameters == null || parameters.isEmpty()); - contentType = Formats.parseHeaderAndQueryParm("application/xml;version=2", null); + contentType = Formats.parseHeaderAndQueryParm("application/xml;version=2", null, Catalog.class); assertNotNull(contentType); assertEquals("application/xml", contentType.getType()); @@ -105,14 +106,14 @@ void testParseHeaderAndQueryParmXml() { @Test void testParseBoth() { assertThrows(FormattingException.class, () -> { - Formats.parseHeaderAndQueryParm("application/json", "json"); + Formats.parseHeaderAndQueryParm("application/json", "json", LocationLevels.class); }); } @Test void testParseBothv2() { assertThrows(FormattingException.class, () -> { - Formats.parseHeaderAndQueryParm("application/json;version=2", "json"); + Formats.parseHeaderAndQueryParm("application/json;version=2", "json", LocationLevels.class); }); } @@ -121,16 +122,26 @@ void testParseBothv2() { void testParseHeader() { ContentType contentType; - contentType = Formats.parseHeader("application/json"); + contentType = Formats.parseHeader("application/json", Catalog.class); assertNotNull(contentType); assertEquals("application/json", contentType.getType()); - contentType = Formats.parseHeader("application/json;version=2"); + contentType = Formats.parseHeader("application/json;version=2", Catalog.class); assertNotNull(contentType); assertEquals("application/json", contentType.getType()); - assertThrows(FormattingException.class, () -> Formats.parseHeader(null)); - assertThrows(FormattingException.class, () -> Formats.parseHeader("")); + assertEquals(new ContentType("application/json;version=1"), + Formats.parseHeader(null, Catalog.class)); + assertEquals(new ContentType("application/json;version=1"), + Formats.parseHeader("", Catalog.class)); + assertEquals(new ContentType("application/json;version=1"), + Formats.parseHeader(" ", Catalog.class)); + assertEquals(new ContentType("application/json;version=2"), + Formats.parseHeader("application/json;version=2,hello=world", LocationLevels.class)); + + assertThrows(FormattingException.class, () -> Formats.parseHeader("abc", CwmsDTOBase.class)); + assertThrows(FormattingException.class, () -> Formats.parseHeader("abc", Catalog.class)); + assertThrows(FormattingException.class, () -> Formats.parseHeader("abc,def", Catalog.class)); } @@ -143,7 +154,8 @@ void testParseQueryParam(ParseQueryParamTest test) { @Test void testParseHeaderAndQueryParmJsonV2WithCharset() { - ContentType contentType = Formats.parseHeaderAndQueryParm("application/json;version=2; charset=utf-8", null); + ContentType contentType = Formats.parseHeaderAndQueryParm("application/json;version=2; charset=utf-8", null, + LocationLevels.class); assertNotNull(contentType); assertEquals("application/json", contentType.getType()); @@ -151,7 +163,7 @@ void testParseHeaderAndQueryParmJsonV2WithCharset() { @Test void testParseHeaderJsonV2WithCharset() { - ContentType contentType = Formats.parseHeader("application/json;version=2; charset=utf-8"); + ContentType contentType = Formats.parseHeader("application/json;version=2; charset=utf-8", Catalog.class); assertNotNull(contentType); assertEquals("application/json", contentType.getType()); @@ -160,9 +172,9 @@ void testParseHeaderJsonV2WithCharset() { @Test void testParseHeaderFromFirefox() { //The following header comes from firefox - ContentType contentType = Formats.parseHeader(FIREFOX_HEADER); + ContentType contentType = Formats.parseHeader(FIREFOX_HEADER, Catalog.class); assertNotNull(contentType); - assertEquals(Formats.XML, contentType.toString()); + assertEquals(Formats.DEFAULT, contentType.toString()); } @EnumSource(ParseHeaderClassAliasTest.class) @@ -206,8 +218,8 @@ enum ParseHeaderClassAliasTest { PROJECT_JSONV1(Project.class, Formats.JSONV1, Formats.JSONV1), PROJECT_JSON(Project.class, Formats.JSON, Formats.JSONV1), LOCK_REVOKER_RIGHTS_JSON(LockRevokerRights.class, Formats.JSON, Formats.JSONV1), - LOCK_REVOKER_RIGHTS_JSONV1(LockRevokerRights.class, Formats.JSONV1, Formats.JSONV1), - LOCK_REVOKER_RIGHTS_DEFAULT(LockRevokerRights.class, Formats.DEFAULT, Formats.JSONV1) + LOCK_REVOKER_RIGHTS_JSONV1(LockRevokerRights.class, Formats.JSONV1, Formats.JSONV1), + LOCK_REVOKER_RIGHTS_DEFAULT(LockRevokerRights.class, Formats.DEFAULT, Formats.JSONV1) ; final Class klass; diff --git a/cwms-data-api/src/test/java/cwms/cda/formatters/JsonV2Test.java b/cwms-data-api/src/test/java/cwms/cda/formatters/JsonV2Test.java index a5b5961eb..498e7c040 100644 --- a/cwms-data-api/src/test/java/cwms/cda/formatters/JsonV2Test.java +++ b/cwms-data-api/src/test/java/cwms/cda/formatters/JsonV2Test.java @@ -34,7 +34,7 @@ void canSerializeLocationLevel(){ // Make sure that Formats can format it when asked to use JSONV2 // This will fail if JSONv2 is missing the class in the @FormatService annotation. - ContentType contentType = Formats.parseHeader(Formats.JSONV2); + ContentType contentType = new ContentType(Formats.JSONV2); String formatted2 = Formats.format(contentType, level); assertNotNull(formatted2); assertTrue(formatted2.contains(crazyName)); @@ -59,7 +59,7 @@ void canSerializeLocationLevels(){ // Make sure that Formats can format it when asked to use JSONV2 // This will fail if JSONv2 is missing the class in the @FormatService annotation. - ContentType contentType = Formats.parseHeader(Formats.JSONV2); + ContentType contentType = new ContentType(Formats.JSONV2); String formatted2 = Formats.format(contentType, levels); assertNotNull(formatted2); assertTrue(formatted2.contains(crazyName)); diff --git a/cwms-data-api/src/test/resources/cwms/cda/data/sql/load_testusers.sql b/cwms-data-api/src/test/resources/cwms/cda/data/sql/load_testusers.sql index befe23116..41d28e854 100644 --- a/cwms-data-api/src/test/resources/cwms/cda/data/sql/load_testusers.sql +++ b/cwms-data-api/src/test/resources/cwms/cda/data/sql/load_testusers.sql @@ -2,7 +2,7 @@ declare group_list cwms_20.char_32_array_type; begin group_list := cwms_20.CHAR_32_ARRAY_TYPE('CWMS Users'); - cwms_20.cwms_sec.create_user('user','blah',group_list, 'HQ'); - cwms_20.cwms_sec.create_user('user2','blah',NULL,'SPK'); - cwms_20.cwms_sec.create_user('s0webtest','&password.',group_list,'HQ'); + cwms_20.cwms_sec.create_user('user','blah',group_list, 'HQ'); + cwms_20.cwms_sec.create_user('user2','blah',NULL,'SPK'); + cwms_20.cwms_sec.create_user('s0webtest','&password.',group_list,'HQ'); end; \ No newline at end of file