Skip to content
This repository was archived by the owner on Oct 8, 2023. It is now read-only.

Commit 814e2fc

Browse files
committed
Allow returning binary response data
1 parent e8ee8e3 commit 814e2fc

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

src/main/java/com/github/kaklakariada/aws/lambda/RequestHandlingService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ private ApiGatewayRequest parseRequest(InputStream input) {
9393
private ApiGatewayResponse handleRequest(final ApiGatewayRequest request, Context context) {
9494
try {
9595
final Object result = handler.handleRequest(request, context);
96-
return new ApiGatewayResponse(serializeResult(result));
96+
final String responseBody = serializeResult(result);
97+
return ApiGatewayResponse.ok(responseBody);
9798
} catch (final LambdaException e) {
9899
LOG.error("Error processing request: " + e.getMessage());
99100
return buildErrorResponse(e, context);

src/main/java/com/github/kaklakariada/aws/lambda/model/response/ApiGatewayResponse.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,39 @@
1717
*/
1818
package com.github.kaklakariada.aws.lambda.model.response;
1919

20+
import java.util.Base64;
2021
import java.util.Collections;
2122
import java.util.Map;
2223

24+
import com.fasterxml.jackson.annotation.JsonProperty;
25+
2326
public class ApiGatewayResponse {
2427
private final int statusCode;
2528
private final Map<String, String> headers;
2629
private final String body;
30+
private final boolean base64Encoded;
2731

2832
public ApiGatewayResponse(int statusCode, Map<String, String> headers, String body) {
33+
this(statusCode, headers, body, false);
34+
}
35+
36+
private ApiGatewayResponse(int statusCode, Map<String, String> headers, String body, boolean base64Encoded) {
2937
this.statusCode = statusCode;
3038
this.headers = headers;
3139
this.body = body;
40+
this.base64Encoded = base64Encoded;
41+
}
42+
43+
public static ApiGatewayResponse ok(String body) {
44+
return new ApiGatewayResponse(200, Collections.emptyMap(), body);
3245
}
3346

34-
public ApiGatewayResponse(String body) {
35-
this(200, Collections.emptyMap(), body);
47+
public static ApiGatewayResponse ok(byte[] body) {
48+
return new ApiGatewayResponse(200, Collections.emptyMap(), base64Encode(body), true);
49+
}
50+
51+
private static String base64Encode(byte[] body) {
52+
return Base64.getEncoder().encodeToString(body);
3653
}
3754

3855
public int getStatusCode() {
@@ -46,4 +63,9 @@ public Map<String, String> getHeaders() {
4663
public String getBody() {
4764
return body;
4865
}
66+
67+
@JsonProperty("isBase64Encoded")
68+
public boolean isBase64Encoded() {
69+
return base64Encoded;
70+
}
4971
}

src/test/java/com/github/kaklakariada/aws/lambda/RequestHandlingServiceTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ private ObjectNode response(int status, String body) {
9191
response.set("statusCode", jsonFactory.numberNode(status));
9292
response.set("headers", headers);
9393
response.set("body", jsonFactory.textNode(body));
94+
response.set("isBase64Encoded", jsonFactory.booleanNode(false));
9495
return response;
9596
}
9697

src/test/java/com/github/kaklakariada/aws/lambda/model/response/ApiGatewayResponseTest.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,34 @@
2121
import static org.hamcrest.core.IsEqual.equalTo;
2222
import static org.hamcrest.core.IsNull.nullValue;
2323

24+
import java.util.Base64;
25+
2426
import org.junit.jupiter.api.Test;
2527

2628
class ApiGatewayResponseTest {
2729

2830
@Test
29-
void testConstructorWithBody() {
30-
final ApiGatewayResponse response = new ApiGatewayResponse("body");
31+
void testConstructorWithStringBody() {
32+
final ApiGatewayResponse response = ApiGatewayResponse.ok("body");
3133
assertThat(response.getBody(), equalTo("body"));
3234
assertThat(response.getStatusCode(), equalTo(200));
3335
assertThat(response.getHeaders().size(), equalTo(0));
36+
assertThat(response.isBase64Encoded(), equalTo(false));
37+
}
38+
39+
@Test
40+
void testNullStringBody() {
41+
assertThat(ApiGatewayResponse.ok((String) null).getBody(), nullValue());
3442
}
3543

3644
@Test
37-
void testNullBody() {
38-
assertThat(new ApiGatewayResponse(null).getBody(), nullValue());
45+
void testConstructorWithBinaryBody() {
46+
final byte[] body = "body".getBytes();
47+
final ApiGatewayResponse response = ApiGatewayResponse.ok(body);
48+
assertThat(response.getBody(), equalTo("Ym9keQ=="));
49+
assertThat(Base64.getDecoder().decode(response.getBody()), equalTo(body));
50+
assertThat(response.getStatusCode(), equalTo(200));
51+
assertThat(response.getHeaders().size(), equalTo(0));
52+
assertThat(response.isBase64Encoded(), equalTo(true));
3953
}
4054
}

0 commit comments

Comments
 (0)