Skip to content

Commit

Permalink
handle null content type headers the same as */* (#861)
Browse files Browse the repository at this point in the history
Also updated DTO's and controllers for consistency, removing deprecated
methods.
  • Loading branch information
adamkorynta authored Sep 4, 2024
1 parent 67fa0fb commit 861be28
Show file tree
Hide file tree
Showing 90 changed files with 302 additions and 379 deletions.
31 changes: 13 additions & 18 deletions cwms-data-api/src/main/java/cwms/cda/api/BasinController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Basin> 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<cwms.cda.data.dto.basin.Basin> 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);
Expand Down Expand Up @@ -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()
Expand All @@ -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;
Expand Down Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
5 changes: 1 addition & 4 deletions cwms-data-api/src/main/java/cwms/cda/api/BlobController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 2 additions & 7 deletions cwms-data-api/src/main/java/cwms/cda/api/ClobController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion cwms-data-api/src/main/java/cwms/cda/api/Controllers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void handle(@NotNull Context ctx) throws Exception {
StreamLocationDao dao = new StreamLocationDao(dsl);
List<StreamLocation> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ public void getAll(Context ctx) {
DSLContext dsl = getDslContext(ctx);
EmbankmentDao dao = new EmbankmentDao(dsl);
List<Embankment> 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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -170,7 +168,7 @@ public void getAll(@NotNull Context ctx) {
List<ForecastInstance> 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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
}

Expand Down
10 changes: 4 additions & 6 deletions cwms-data-api/src/main/java/cwms/cda/api/LevelsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit 861be28

Please sign in to comment.