This repository was archived by the owner on Jul 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApiGatewayResponse.java
More file actions
129 lines (107 loc) · 3.27 KB
/
Copy pathApiGatewayResponse.java
File metadata and controls
129 lines (107 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.serverless;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Collections;
import java.util.Map;
import org.apache.log4j.Logger;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ApiGatewayResponse {
private final int statusCode;
private final String body;
private final Map<String, String> headers;
private final boolean isBase64Encoded;
public ApiGatewayResponse(int statusCode, String body, Map<String, String> headers, boolean isBase64Encoded) {
this.statusCode = statusCode;
this.body = body;
this.headers = headers;
this.isBase64Encoded = isBase64Encoded;
}
public int getStatusCode() {
return statusCode;
}
public String getBody() {
return body;
}
public Map<String, String> getHeaders() {
return headers;
}
// API Gateway expects the property to be called "isBase64Encoded" => isIs
public boolean isIsBase64Encoded() {
return isBase64Encoded;
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private static final Logger LOG = Logger.getLogger(ApiGatewayResponse.Builder.class);
private static final ObjectMapper objectMapper = new ObjectMapper();
private int statusCode = 200;
private Map<String, String> headers = Collections.emptyMap();
private String rawBody;
private Object objectBody;
private byte[] binaryBody;
private boolean base64Encoded;
public Builder setStatusCode(int statusCode) {
this.statusCode = statusCode;
return this;
}
public Builder setHeaders(Map<String, String> headers) {
this.headers = headers;
return this;
}
/**
* Builds the {@link ApiGatewayResponse} using the passed raw body string.
*/
public Builder setRawBody(String rawBody) {
this.rawBody = rawBody;
return this;
}
/**
* Builds the {@link ApiGatewayResponse} using the passed object body
* converted to JSON.
*/
public Builder setObjectBody(Object objectBody) {
this.objectBody = objectBody;
return this;
}
/**
* Builds the {@link ApiGatewayResponse} using the passed binary body
* encoded as base64. {@link #setBase64Encoded(boolean)
* setBase64Encoded(true)} will be in invoked automatically.
*/
public Builder setBinaryBody(byte[] binaryBody) {
this.binaryBody = binaryBody;
setBase64Encoded(true);
return this;
}
/**
* A binary or rather a base64encoded responses requires
* <ol>
* <li>"Binary Media Types" to be configured in API Gateway
* <li>a request with an "Accept" header set to one of the "Binary Media
* Types"
* </ol>
*/
public Builder setBase64Encoded(boolean base64Encoded) {
this.base64Encoded = base64Encoded;
return this;
}
public ApiGatewayResponse build() {
String body = null;
if (rawBody != null) {
body = rawBody;
} else if (objectBody != null) {
try {
body = objectMapper.writeValueAsString(objectBody);
} catch (JsonProcessingException e) {
LOG.error("failed to serialize object", e);
throw new RuntimeException(e);
}
} else if (binaryBody != null) {
body = new String(Base64.getEncoder().encode(binaryBody), StandardCharsets.UTF_8);
}
return new ApiGatewayResponse(statusCode, body, headers, base64Encoded);
}
}
}