-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLambdaHandler.java
More file actions
43 lines (35 loc) · 1.78 KB
/
Copy pathLambdaHandler.java
File metadata and controls
43 lines (35 loc) · 1.78 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
package com.securitydemo;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import java.util.HashMap;
import java.util.Map;
public class LambdaHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
private static ConfigurableApplicationContext applicationContext;
private static SpringApiGatewayRequestHandler requestHandler;
@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
try {
// Initialize Spring context if not already done
if (applicationContext == null) {
applicationContext = SpringApplication.run(SecurityDemoApplication.class);
requestHandler = new SpringApiGatewayRequestHandler(applicationContext);
}
// Process the request through Spring
return requestHandler.handle(input);
} catch (Exception e) {
e.printStackTrace();
APIGatewayProxyResponseEvent errorResponse = new APIGatewayProxyResponseEvent();
errorResponse.setStatusCode(500);
errorResponse.setBody("{\"error\":\"" + e.getMessage() + "\"}");
// Add only Content-Type header without CORS
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
errorResponse.setHeaders(headers);
return errorResponse;
}
}
}