Skip to content

Commit b53d81c

Browse files
committed
Add static factory methods to ApiGatewayRequestIdentity and AwsProxyRequestContext for issue #1495
1 parent e8b5aa7 commit b53d81c

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/model/ApiGatewayRequestIdentity.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515

1616
import com.amazonaws.serverless.proxy.RequestReader;
17-
1817
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
1918

2019

@@ -28,6 +27,42 @@
2827
@JsonIgnoreProperties(ignoreUnknown = true)
2928
public class ApiGatewayRequestIdentity {
3029

30+
/**
31+
* Creates an ApiGatewayRequestIdentity instance with default values.
32+
*
33+
* @return A pre-configured ApiGatewayRequestIdentity instance
34+
*/
35+
public static ApiGatewayRequestIdentity getApiGatewayRequestIdentity() {
36+
ApiGatewayRequestIdentity identity = new ApiGatewayRequestIdentity();
37+
// Set default values for all fields
38+
identity.setApiKey("");
39+
identity.setApiKeyId("");
40+
identity.setUserArn("");
41+
identity.setCognitoAuthenticationType("");
42+
identity.setCaller("");
43+
identity.setUserAgent("");
44+
identity.setUser("");
45+
identity.setCognitoIdentityPoolId("");
46+
identity.setCognitoIdentityId("");
47+
identity.setCognitoAuthenticationProvider("");
48+
identity.setSourceIp("127.0.0.1"); // Reasonable default
49+
identity.setAccountId("");
50+
identity.setAccessKey("");
51+
return identity;
52+
}
53+
54+
/**
55+
* Creates an ApiGatewayRequestIdentity instance with the specified source IP.
56+
*
57+
* @param sourceIp the source IP to set
58+
* @return A pre-configured ApiGatewayRequestIdentity instance
59+
*/
60+
public static ApiGatewayRequestIdentity getApiGatewayRequestIdentity(String sourceIp) {
61+
ApiGatewayRequestIdentity identity = getApiGatewayRequestIdentity();
62+
identity.setSourceIp(sourceIp);
63+
return identity;
64+
}
65+
3166
//-------------------------------------------------------------
3267
// Variables - Private
3368
//-------------------------------------------------------------

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/model/AwsProxyRequestContext.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515

1616
import com.amazonaws.serverless.proxy.RequestReader;
17-
1817
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
1918

2019

@@ -29,6 +28,48 @@
2928
@JsonIgnoreProperties(ignoreUnknown = true)
3029
public class AwsProxyRequestContext {
3130

31+
/**
32+
* Creates an AwsProxyRequestContext instance with default values.
33+
*
34+
* @return A pre-configured AwsProxyRequestContext instance
35+
*/
36+
public static AwsProxyRequestContext getAwsProxyRequestContext() {
37+
AwsProxyRequestContext context = new AwsProxyRequestContext();
38+
// Set default values for all fields
39+
context.setResourceId("");
40+
context.setApiId("");
41+
context.setResourcePath("");
42+
context.setHttpMethod("GET"); // Reasonable default
43+
context.setRequestId("");
44+
context.setExtendedRequestId("");
45+
context.setAccountId("");
46+
context.setStage("");
47+
context.setPath("");
48+
context.setProtocol("");
49+
context.setRequestTime("");
50+
context.setRequestTimeEpoch(System.currentTimeMillis()); // Current time as reasonable default
51+
52+
// Create and set identity with defaults
53+
context.setIdentity(ApiGatewayRequestIdentity.getApiGatewayRequestIdentity());
54+
55+
// Initialize authorizer
56+
context.setAuthorizer(new ApiGatewayAuthorizerContext());
57+
58+
return context;
59+
}
60+
61+
/**
62+
* Creates an AwsProxyRequestContext instance with the specified HTTP method.
63+
*
64+
* @param httpMethod the HTTP method to set
65+
* @return A pre-configured AwsProxyRequestContext instance
66+
*/
67+
public static AwsProxyRequestContext getAwsProxyRequestContext(String httpMethod) {
68+
AwsProxyRequestContext context = getAwsProxyRequestContext();
69+
context.setHttpMethod(httpMethod);
70+
return context;
71+
}
72+
3273
//-------------------------------------------------------------
3374
// Variables - Private
3475
//-------------------------------------------------------------

0 commit comments

Comments
 (0)