Skip to content

Commit 87b83a1

Browse files
committed
Solves issue #188 setting the wrong HTTP header.
- also solves Maven relativePath problem using a clean build env
1 parent 0fa8730 commit 87b83a1

File tree

10 files changed

+62
-5
lines changed

10 files changed

+62
-5
lines changed

archetypes/jersey/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<groupId>com.amazonaws.serverless</groupId>
66
<artifactId>aws-serverless-java-container</artifactId>
77
<version>1.2-SNAPSHOT</version>
8+
<relativePath>../..</relativePath>
89
</parent>
910

1011
<groupId>com.amazonaws.serverless.archetypes</groupId>

archetypes/spark/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<groupId>com.amazonaws.serverless</groupId>
66
<artifactId>aws-serverless-java-container</artifactId>
77
<version>1.2-SNAPSHOT</version>
8+
<relativePath>../..</relativePath>
89
</parent>
910

1011
<groupId>com.amazonaws.serverless.archetypes</groupId>

archetypes/spring/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<groupId>com.amazonaws.serverless</groupId>
66
<artifactId>aws-serverless-java-container</artifactId>
77
<version>1.2-SNAPSHOT</version>
8+
<relativePath>../..</relativePath>
89
</parent>
910

1011
<groupId>com.amazonaws.serverless.archetypes</groupId>

archetypes/springboot/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<groupId>com.amazonaws.serverless</groupId>
66
<artifactId>aws-serverless-java-container</artifactId>
77
<version>1.2-SNAPSHOT</version>
8+
<relativePath>../..</relativePath>
89
</parent>
910

1011
<groupId>com.amazonaws.serverless.archetypes</groupId>

aws-serverless-java-container-core/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<groupId>com.amazonaws.serverless</groupId>
1313
<artifactId>aws-serverless-java-container</artifactId>
1414
<version>1.2-SNAPSHOT</version>
15+
<relativePath>..</relativePath>
1516
</parent>
1617

1718
<properties>

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/AwsHttpServletResponse.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,17 @@ public Collection<String> getHeaderNames() {
265265

266266
@Override
267267
public String getCharacterEncoding() {
268-
return headers.getFirst(HttpHeaders.CONTENT_ENCODING);
268+
final String contentType = Optional.ofNullable(getContentType()).orElse("");
269+
if (contentType.contains(";")) {
270+
return contentType.split(";")[1].split("=")[1].trim().toLowerCase(Locale.getDefault());
271+
} else {
272+
return "";
273+
}
269274
}
270275

271276

272277
@Override
273-
public String getContentType() {
274-
return headers.getFirst(HttpHeaders.CONTENT_TYPE);
275-
}
278+
public String getContentType() { return getHeader(HttpHeaders.CONTENT_TYPE); }
276279

277280

278281
@Override
@@ -334,7 +337,10 @@ public PrintWriter getWriter() throws IOException {
334337

335338
@Override
336339
public void setCharacterEncoding(String s) {
337-
setHeader(HttpHeaders.CONTENT_ENCODING, s, true);
340+
final String characterEncoding = Optional.ofNullable(s).orElse("").toLowerCase(Locale.getDefault());
341+
final String oldValue = Optional.ofNullable(getHeader(HttpHeaders.CONTENT_TYPE)).orElse("");
342+
String contentType = oldValue.contains(";") ? oldValue.split(";")[0].trim(): oldValue;
343+
setHeader(HttpHeaders.CONTENT_TYPE, String.format("%s; charset=%s", contentType, characterEncoding), true);
338344
}
339345

340346

aws-serverless-java-container-core/src/test/java/com/amazonaws/serverless/proxy/internal/servlet/AwsHttpServletResponseTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,49 @@ public void cookie_addCookieWithoutMaxAge_expectNoExpires() {
140140
assertFalse(cookieHeader.contains("Expires"));
141141
}
142142

143+
@Test
144+
public void characterEncoding_setCharacterEncoding() {
145+
AwsHttpServletResponse resp = new AwsHttpServletResponse(null, null);
146+
resp.setContentType("application/json");
147+
resp.setCharacterEncoding("UTF-8");
148+
assertNotEquals("UTF-8", resp.getHeader("Content-Encoding"));
149+
assertEquals("application/json; charset=utf-8", resp.getContentType());
150+
assertEquals("application/json; charset=utf-8", resp.getHeader("Content-Type"));
151+
}
152+
153+
@Test
154+
public void characterEncoding_setContentType() {
155+
AwsHttpServletResponse resp = new AwsHttpServletResponse(null, null);
156+
resp.setContentType("application/json; charset=utf-8");
157+
resp.setCharacterEncoding("UTF-8");
158+
159+
assertEquals("application/json; charset=utf-8", resp.getContentType());
160+
assertEquals("application/json; charset=utf-8", resp.getHeader("Content-Type"));
161+
assertEquals("utf-8", resp.getCharacterEncoding());
162+
}
163+
164+
@Test
165+
public void characterEncoding_setContentTypeAndsetCharacterEncoding() {
166+
AwsHttpServletResponse resp = new AwsHttpServletResponse(null, null);
167+
resp.setContentType("application/json");
168+
resp.setCharacterEncoding("UTF-8");
169+
170+
assertEquals("application/json; charset=utf-8", resp.getContentType());
171+
assertEquals("application/json; charset=utf-8", resp.getHeader("Content-Type"));
172+
assertEquals("utf-8", resp.getCharacterEncoding());
173+
}
174+
175+
@Test
176+
public void characterEncoding_setCharacterEncodingAndsetContentType() {
177+
AwsHttpServletResponse resp = new AwsHttpServletResponse(null, null);
178+
resp.setCharacterEncoding("UTF-8");
179+
resp.setContentType("application/json");
180+
181+
assertEquals("application/json", resp.getContentType());
182+
assertEquals("application/json", resp.getHeader("Content-Type"));
183+
assertEquals("", resp.getCharacterEncoding());
184+
}
185+
143186
private int getMaxAge(String header) {
144187
Matcher ageMatcher = MAX_AGE_PATTERN.matcher(header);
145188
assertTrue(ageMatcher.find());

aws-serverless-java-container-jersey/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<groupId>com.amazonaws.serverless</groupId>
1313
<artifactId>aws-serverless-java-container</artifactId>
1414
<version>1.2-SNAPSHOT</version>
15+
<relativePath>..</relativePath>
1516
</parent>
1617

1718
<properties>

aws-serverless-java-container-spark/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<groupId>com.amazonaws.serverless</groupId>
1313
<artifactId>aws-serverless-java-container</artifactId>
1414
<version>1.2-SNAPSHOT</version>
15+
<relativePath>..</relativePath>
1516
</parent>
1617

1718
<properties>

aws-serverless-java-container-spring/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<groupId>com.amazonaws.serverless</groupId>
1313
<artifactId>aws-serverless-java-container</artifactId>
1414
<version>1.2-SNAPSHOT</version>
15+
<relativePath>..</relativePath>
1516
</parent>
1617

1718
<properties>

0 commit comments

Comments
 (0)