Skip to content

Commit

Permalink
aws#1084: remove duplicated code with populateContentAndContentType
Browse files Browse the repository at this point in the history
  • Loading branch information
npeters committed Nov 20, 2024
1 parent 3a59eff commit 6fa3f29
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,12 @@ private static HttpServletRequest generateRequest1(String request, Context lambd
MultiValueMapAdapter headers = new MultiValueMapAdapter(v1Request.getMultiValueHeaders());
httpRequest.setHeaders(headers);
}
if (StringUtils.hasText(v1Request.getBody())) {
if (v1Request.getHeaders().get(HttpHeaders.CONTENT_TYPE)==null) {
httpRequest.setContentType("application/json");
}
if (v1Request.isBase64Encoded()) {
httpRequest.setContent(Base64.getMimeDecoder().decode(v1Request.getBody()));
} else {
Charset charseEncoding = parseCharacterEncoding(v1Request.getHeaders().get(HttpHeaders.CONTENT_TYPE));
httpRequest.setContent(v1Request.getBody().getBytes(charseEncoding));
}
}
populateContentAndContentType(
v1Request.getBody(),
v1Request.getHeaders().get(HttpHeaders.CONTENT_TYPE),
v1Request.isBase64Encoded(),
httpRequest
);
if (v1Request.getRequestContext() != null) {
httpRequest.setAttribute(RequestReader.API_GATEWAY_CONTEXT_PROPERTY, v1Request.getRequestContext());
httpRequest.setAttribute(RequestReader.ALB_CONTEXT_PROPERTY, v1Request.getRequestContext().getElb());
Expand All @@ -159,6 +154,12 @@ private static HttpServletRequest generateRequest2(String request, Context lambd

v2Request.getHeaders().forEach(httpRequest::setHeader);

populateContentAndContentType(
v2Request.getBody(),
v2Request.getHeaders().get(HttpHeaders.CONTENT_TYPE),
v2Request.isBase64Encoded(),
httpRequest
);

if (StringUtils.hasText(v2Request.getBody())) {
if (v2Request.getHeaders().get(HttpHeaders.CONTENT_TYPE)==null) {
Expand Down Expand Up @@ -197,6 +198,24 @@ private static <T> T readValue(String json, Class<T> clazz, ObjectMapper mapper)
}
}

private static void populateContentAndContentType(
String body,
String contentType,
boolean base64Encoded,
ServerlessHttpServletRequest httpRequest) {
if (StringUtils.hasText(body)) {
if (contentType == null) {
httpRequest.setContentType("application/json");
}
if (base64Encoded) {
httpRequest.setContent(Base64.getMimeDecoder().decode(body));
} else {
Charset charseEncoding = parseCharacterEncoding(contentType);
httpRequest.setContent(body.getBytes(charseEncoding));
}
}
}

static final String HEADER_KEY_VALUE_SEPARATOR = "=";
static final String HEADER_VALUE_SEPARATOR = ";";
static final String ENCODING_VALUE_KEY = "charset";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ private byte[] generateHttpRequest2(Map requestMap, String method, String path,
if (!CollectionUtils.isEmpty(headers)) {
map.put("headers", headers);
}
System.out.println(map);
return mapper.writeValueAsBytes(map);
}
}

0 comments on commit 6fa3f29

Please sign in to comment.