-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.java
43 lines (32 loc) · 1.43 KB
/
lambda.java
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
public class MyHandler implements RequestHandler<Map<String,String>, String> {
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
throws IOException {
JSONParser parser = new JSONParser();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
JSONObject responseJson = new JSONObject();
try {
JSONObject event = (JSONObject) parser.parse(reader);
if (event.get("body") != null) {
MyRemoteService svrs = new MyRemoteService((String) event.get("body"));
Client client = ClientBuilder.newClient();
WebTarget resource = client.target(svrs.getURL());
Builder request = resource.request();
request.accept(MediaType.APPLICATION_JSON);
Response response = request.get();
JSONObject responseBody = new JSONObject();
responseBody.put("message", response.getEntity());
JSONObject headerJson = new JSONObject();
headerJson.put("x-custom-header");
responseJson.put("statusCode", response.getStatus());
responseJson.put("headers", headerJson);
responseJson.put("body", responseBody.toString());
}
} catch (ParseException pex) {
responseJson.put("statusCode", 400);
responseJson.put("exception", pex);
}
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
writer.write(responseJson.toString());
writer.close();
}
}