Skip to content

Commit 9e38ce7

Browse files
authored
CDA-71: Pump accounting path changes (#1602)
Resolves: #1216 Adds Base64 encoding to contract name ID. Updated test cases.
1 parent ae89946 commit 9e38ce7

File tree

7 files changed

+439
-48
lines changed

7 files changed

+439
-48
lines changed

cwms-data-api/src/main/java/cwms/cda/api/watersupply/AccountingCatalogController.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
package cwms.cda.api.watersupply;
2828

29+
import static cwms.cda.api.Controllers.ACCEPT;
2930
import static cwms.cda.api.Controllers.BEGIN;
3031
import static cwms.cda.api.Controllers.CONTRACT_NAME;
3132
import static cwms.cda.api.Controllers.END;
@@ -42,6 +43,7 @@
4243
import static cwms.cda.api.Controllers.TIME_FORMAT_DESC;
4344
import static cwms.cda.api.Controllers.UNIT;
4445
import static cwms.cda.api.Controllers.WATER_USER;
46+
import static cwms.cda.api.Controllers.queryParamAsClass;
4547
import static cwms.cda.api.Controllers.requiredInstant;
4648
import static cwms.cda.data.dao.JooqDao.getDslContext;
4749

@@ -70,6 +72,7 @@
7072
import java.time.Instant;
7173
import java.util.List;
7274
import javax.servlet.http.HttpServletResponse;
75+
import org.apache.commons.codec.binary.Base64;
7376
import org.jetbrains.annotations.NotNull;
7477
import org.jooq.DSLContext;
7578

@@ -137,6 +140,12 @@ protected WaterSupplyAccountingDao getWaterSupplyAccountingDao(DSLContext dsl) {
137140
+ "provided input parameters."),
138141
@OpenApiResponse(status = STATUS_501, description = "Requested format is not implemented")
139142
},
143+
headers = {
144+
@OpenApiParam(name = ACCEPT, description = "The requested response format. Supported values are "
145+
+ Formats.JSONV1 + ", " + Formats.JSON + ", and " + Formats.JSONV2 + ". " + Formats.JSONV2
146+
+ " should be used only when providing a URL-safe base64 encoded contract ID. If not provided, "
147+
+ Formats.JSONV1 + " will be used as the default.")
148+
},
140149
description = "Get pump accounting entries associated with a water supply contract.",
141150
path = "/projects/{office}/water-user/{water-user}/contracts/{contract-name}/accounting",
142151
method = HttpMethod.GET,
@@ -148,7 +157,7 @@ public void handle(Context ctx) {
148157
try (Timer.Context ignored = markAndTime(GET_ALL)) {
149158
final String office = ctx.pathParam(OFFICE);
150159
final String waterUserName = ctx.pathParam(WATER_USER);
151-
final String contractId = ctx.pathParam(CONTRACT_NAME);
160+
String contractId = ctx.pathParam(CONTRACT_NAME);
152161
final String locationId = ctx.pathParam(PROJECT_ID);
153162
final Instant startTime = requiredInstant(ctx, START);
154163
final Instant endTime = requiredInstant(ctx, END);
@@ -161,6 +170,10 @@ public void handle(Context ctx) {
161170
DSLContext dsl = getDslContext(ctx);
162171

163172
String formatHeader = ctx.headerAsClass(Header.ACCEPT, String.class).getOrDefault(Formats.JSONV1);
173+
if (formatHeader != null && formatHeader.equals(Formats.JSONV2)) {
174+
byte[] decoded = Base64.decodeBase64(contractId);
175+
contractId = new String(decoded);
176+
}
164177
ContentType contentType = Formats.parseHeader(formatHeader, WaterSupplyAccounting.class);
165178
ctx.contentType(contentType.toString());
166179
CwmsId projectLocation = new CwmsId.Builder().withOfficeId(office).withName(locationId).build();

cwms-data-api/src/main/java/cwms/cda/api/watersupply/AccountingCreateController.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
package cwms.cda.api.watersupply;
2828

29+
import static cwms.cda.api.Controllers.ACCEPT;
2930
import static cwms.cda.api.Controllers.CONTRACT_NAME;
3031
import static cwms.cda.api.Controllers.CREATE;
3132
import static cwms.cda.api.Controllers.OFFICE;
@@ -37,30 +38,31 @@
3738
import com.codahale.metrics.MetricRegistry;
3839
import com.codahale.metrics.Timer;
3940
import cwms.cda.api.BaseHandler;
40-
import cwms.cda.api.Controllers;
4141
import cwms.cda.data.dao.LookupTypeDao;
4242
import cwms.cda.data.dao.watersupply.WaterSupplyAccountingDao;
43+
import cwms.cda.data.dao.watersupply.WaterSupplyUtils;
4344
import cwms.cda.data.dto.LookupType;
4445
import cwms.cda.data.dto.StatusResponse;
4546
import cwms.cda.data.dto.watersupply.PumpTransfer;
4647
import cwms.cda.data.dto.watersupply.WaterSupplyAccounting;
4748
import cwms.cda.formatters.ContentType;
4849
import cwms.cda.formatters.Formats;
49-
import cwms.cda.data.dao.watersupply.WaterSupplyUtils;
50-
import mil.army.usace.hec.metadata.DataSetIllegalArgumentException;
5150
import io.javalin.core.util.Header;
5251
import io.javalin.http.Context;
53-
import io.javalin.http.Handler;
5452
import io.javalin.plugin.openapi.annotations.HttpMethod;
5553
import io.javalin.plugin.openapi.annotations.OpenApi;
5654
import io.javalin.plugin.openapi.annotations.OpenApiContent;
5755
import io.javalin.plugin.openapi.annotations.OpenApiParam;
5856
import io.javalin.plugin.openapi.annotations.OpenApiRequestBody;
5957
import io.javalin.plugin.openapi.annotations.OpenApiResponse;
58+
import java.nio.charset.StandardCharsets;
6059
import java.time.Instant;
60+
import java.util.Arrays;
6161
import java.util.List;
6262
import java.util.Map;
6363
import javax.servlet.http.HttpServletResponse;
64+
import mil.army.usace.hec.metadata.DataSetIllegalArgumentException;
65+
import org.apache.commons.codec.binary.Base64;
6466
import org.jetbrains.annotations.NotNull;
6567
import org.jooq.DSLContext;
6668

@@ -89,12 +91,18 @@ protected WaterSupplyAccountingDao getWaterSupplyAccountingDao(DSLContext dsl) {
8991
@OpenApiParam(name = WATER_USER, description = "The water user the accounting is associated with.",
9092
required = true),
9193
@OpenApiParam(name = CONTRACT_NAME, description = "The name of the contract associated with the "
92-
+ "accounting.", required = true),
94+
+ "accounting. For names with special characters (such as '/'), use the JSONV2 accept header "
95+
+ "with a name encoded using URL-safe BASE64.", required = true),
9396
},
9497
responses = {
9598
@OpenApiResponse(status = STATUS_201, description = "The pump accounting entry was created."),
9699
@OpenApiResponse(status = STATUS_501, description = "Requested format is not implemented")
97100
},
101+
headers = {
102+
@OpenApiParam(name = ACCEPT, description = "The format of the request body. Accepts JSONV1 and JSONV2. "
103+
+ "Note: JSONV2 should be used if the contract name contains special characters such as '/'. "
104+
+ "In this case, the contract name should be encoded using URL-safe BASE64 encoding.")
105+
},
98106
description = "Create a new pump accounting entry associated with a water supply contract.",
99107
path = "/projects/{office}/water-user/{water-user}/contracts/{contract-name}/accounting",
100108
method = HttpMethod.POST,
@@ -106,10 +114,17 @@ public void handle(@NotNull Context ctx) {
106114
logUnusedPathParameter(ctx, WATER_USER, "Body contains required information.");
107115

108116
try (Timer.Context ignored = markAndTime(CREATE)) {
109-
final String contractId = ctx.pathParam(CONTRACT_NAME);
117+
String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : Formats.JSONV1;
118+
String contractId;
119+
if (formatHeader != null && formatHeader.equals(Formats.JSONV2)) {
120+
byte[] decoded = Base64.decodeBase64(ctx.pathParam(CONTRACT_NAME));
121+
contractId = new String(decoded);
122+
} else {
123+
contractId = ctx.pathParam(CONTRACT_NAME);
124+
}
125+
110126
final String office = ctx.pathParam(OFFICE);
111127
DSLContext dsl = getDslContext(ctx);
112-
String formatHeader = ctx.header(Header.ACCEPT) != null ? ctx.header(Header.ACCEPT) : Formats.JSONV1;
113128
ContentType contentType = Formats.parseHeader(formatHeader, WaterSupplyAccounting.class);
114129
ctx.contentType(contentType.toString());
115130
WaterSupplyAccounting accounting = Formats.parseContent(contentType, ctx.body(),

cwms-data-api/src/main/java/cwms/cda/data/dto/watersupply/WaterSupplyAccounting.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import java.util.Map;
4141

4242
@FormattableWith(contentType = Formats.JSONV1, formatter = JsonV1.class,
43-
aliases = {Formats.DEFAULT, Formats.JSON})
43+
aliases = {Formats.DEFAULT, Formats.JSON, Formats.JSONV2})
4444
@JsonDeserialize(builder = WaterSupplyAccounting.Builder.class)
4545
@JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class)
4646
public final class WaterSupplyAccounting extends CwmsDTOBase {

0 commit comments

Comments
 (0)