Skip to content

Commit 46ec9fa

Browse files
committed
fix issue #220, using treemap
1 parent 8c76130 commit 46ec9fa

File tree

3 files changed

+93
-19
lines changed

3 files changed

+93
-19
lines changed

aws-lambda-java-events/src/main/java/com/amazonaws/services/lambda/runtime/events/APIGatewayProxyRequestEvent.java

Lines changed: 87 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package com.amazonaws.services.lambda.runtime.events;
22

33
import java.io.Serializable;
4-
import java.util.List;
5-
import java.util.Map;
4+
import java.util.*;
65

76
/**
87
* Class that represents an APIGatewayProxyRequestEvent
@@ -17,9 +16,9 @@ public class APIGatewayProxyRequestEvent implements Serializable, Cloneable {
1716

1817
private String httpMethod;
1918

20-
private Map<String, String> headers;
19+
private Headers<String> headers = new Headers<>();
2120

22-
private Map<String, List<String>> multiValueHeaders;
21+
private Headers<List<String>> multiValueHeaders = new Headers<>();
2322

2423
private Map<String, String> queryStringParameters;
2524

@@ -35,6 +34,77 @@ public class APIGatewayProxyRequestEvent implements Serializable, Cloneable {
3534

3635
private Boolean isBase64Encoded;
3736

37+
/**
38+
* Class that represents Http Headers.
39+
*
40+
* Not using a standard map, because we need insensitive case.
41+
*/
42+
public static class Headers<T> implements Map<String, T> {
43+
44+
// Headers are case insensitive (https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2)
45+
private Map<String, T> map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
46+
47+
@Override
48+
public int size() {
49+
return map.size();
50+
}
51+
52+
@Override
53+
public boolean isEmpty() {
54+
return map.isEmpty();
55+
}
56+
57+
@Override
58+
public boolean containsKey(Object key) {
59+
return map.containsKey(key);
60+
}
61+
62+
@Override
63+
public boolean containsValue(Object value) {
64+
return map.containsValue(value);
65+
}
66+
67+
@Override
68+
public T get(Object key) {
69+
return map.get(key);
70+
}
71+
72+
@Override
73+
public T put(String key, T value) {
74+
return map.put(key, value);
75+
}
76+
77+
@Override
78+
public T remove(Object key) {
79+
return map.remove(key);
80+
}
81+
82+
@Override
83+
public void putAll(Map<? extends String, ? extends T> m) {
84+
map.putAll(m);
85+
}
86+
87+
@Override
88+
public void clear() {
89+
map.clear();
90+
}
91+
92+
@Override
93+
public Set<String> keySet() {
94+
return map.keySet();
95+
}
96+
97+
@Override
98+
public Collection<T> values() {
99+
return map.values();
100+
}
101+
102+
@Override
103+
public Set<Entry<String, T>> entrySet() {
104+
return map.entrySet();
105+
}
106+
}
107+
38108
/**
39109
* class that represents proxy request context
40110
*/
@@ -67,7 +137,8 @@ public static class ProxyRequestContext implements Serializable, Cloneable {
67137
/**
68138
* default constructor
69139
*/
70-
public ProxyRequestContext() {}
140+
public ProxyRequestContext() {
141+
}
71142

72143
/**
73144
* @return account id that owns Lambda function
@@ -101,7 +172,7 @@ public void setAuthorizer(final Map<String, Object> authorizer) {
101172
}
102173

103174
/**
104-
* @return API Gateway stage name
175+
* @return API Gateway stage name
105176
*/
106177
public String getStage() {
107178
return stage;
@@ -286,14 +357,14 @@ public ProxyRequestContext withPath(String path) {
286357

287358
/**
288359
* @return The name of the operation being performed
289-
* */
360+
*/
290361
public String getOperationName() {
291362
return operationName;
292363
}
293364

294365
/**
295366
* @param operationName The name of the operation being performed
296-
* */
367+
*/
297368
public void setOperationName(String operationName) {
298369
this.operationName = operationName;
299370
}
@@ -307,7 +378,6 @@ public ProxyRequestContext withOperationName(String operationName) {
307378
* Returns a string representation of this object; useful for testing and debugging.
308379
*
309380
* @return A string representation of this object.
310-
*
311381
* @see Object#toString()
312382
*/
313383
@Override
@@ -412,7 +482,7 @@ public int hashCode() {
412482
hashCode = prime * hashCode + ((getApiId() == null) ? 0 : getApiId().hashCode());
413483
hashCode = prime * hashCode + ((getPath() == null) ? 0 : getPath().hashCode());
414484
hashCode = prime * hashCode + ((getAuthorizer() == null) ? 0 : getAuthorizer().hashCode());
415-
hashCode = prime * hashCode + ((getOperationName() == null) ? 0: getOperationName().hashCode());
485+
hashCode = prime * hashCode + ((getOperationName() == null) ? 0 : getOperationName().hashCode());
416486
return hashCode;
417487
}
418488

@@ -457,7 +527,8 @@ public static class RequestIdentity implements Serializable, Cloneable {
457527
/**
458528
* default constructor
459529
*/
460-
public RequestIdentity() {}
530+
public RequestIdentity() {
531+
}
461532

462533
/**
463534
* @return The Cognito identity pool id.
@@ -739,7 +810,6 @@ public RequestIdentity withAccessKey(String accessKey) {
739810
* Returns a string representation of this object; useful for testing and debugging.
740811
*
741812
* @return A string representation of this object.
742-
*
743813
* @see Object#toString()
744814
*/
745815
@Override
@@ -869,7 +939,8 @@ public RequestIdentity clone() {
869939
/**
870940
* default constructor
871941
*/
872-
public APIGatewayProxyRequestEvent() {}
942+
public APIGatewayProxyRequestEvent() {
943+
}
873944

874945
/**
875946
* @return The resource path defined in API Gateway
@@ -944,14 +1015,14 @@ public APIGatewayProxyRequestEvent withHttpMethod(String httpMethod) {
9441015
* @return The headers sent with the request
9451016
*/
9461017
public Map<String, String> getHeaders() {
947-
return headers;
1018+
return headers.map;
9481019
}
9491020

9501021
/**
9511022
* @param headers The headers sent with the request
9521023
*/
9531024
public void setHeaders(Map<String, String> headers) {
954-
this.headers = headers;
1025+
this.headers.putAll(headers);
9551026
}
9561027

9571028
/**
@@ -974,7 +1045,7 @@ public Map<String, List<String>> getMultiValueHeaders() {
9741045
* @param multiValueHeaders The multi value headers sent with the request
9751046
*/
9761047
public void setMultiValueHeaders(Map<String, List<String>> multiValueHeaders) {
977-
this.multiValueHeaders = multiValueHeaders;
1048+
this.multiValueHeaders.putAll(multiValueHeaders);
9781049
}
9791050

9801051
/**
@@ -1167,7 +1238,6 @@ public APIGatewayProxyRequestEvent withIsBase64Encoded(Boolean isBase64Encoded)
11671238
* Returns a string representation of this object; useful for testing and debugging.
11681239
*
11691240
* @return A string representation of this object.
1170-
*
11711241
* @see Object#toString()
11721242
*/
11731243
@Override

aws-lambda-java-tests/src/test/java/com/amazonaws/services/lambda/runtime/tests/EventLoaderTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public void testLoadApiGatewayRestEvent() {
2626

2727
assertThat(event).isNotNull();
2828
assertThat(event.getBody()).isEqualTo("Hello from Lambda!");
29+
30+
assertThat(event.getHeaders().get("Header1")).isEqualTo("value1");
31+
assertThat(event.getHeaders().get("header1")).isEqualTo("value1");
2932
assertThat(event.getHeaders()).hasSize(2);
3033
}
3134

aws-lambda-java-tests/src/test/resources/apigw_rest_event.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
"path": "/my/path",
55
"httpMethod": "GET",
66
"headers": {
7-
"Header1": "value1",
8-
"Header2": "value2"
7+
"Header1": "Value1",
8+
"Header2": "value2",
9+
"header1": "value1"
910
},
1011
"multiValueHeaders": {
1112
"Header1": [

0 commit comments

Comments
 (0)