Skip to content

Commit 99097e3

Browse files
committed
Added proper body format.
1 parent f5cd1f5 commit 99097e3

File tree

6 files changed

+80
-61
lines changed

6 files changed

+80
-61
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
<artifactId>commons-lang3</artifactId>
2424
<version>3.8</version>
2525
</dependency>
26+
<dependency>
27+
<groupId>com.google.code.gson</groupId>
28+
<artifactId>gson</artifactId>
29+
<version>2.8.5</version>
30+
</dependency>
2631
</dependencies>
2732
<build>
2833
<plugins>

serverless/serverless.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package:
1919
functions:
2020
executor:
2121
handler: Handler::handleRequest
22-
2322
events:
2423
- http:
2524
path: executor
Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import java.util.List;
22
import java.util.Map;
33

4-
public class Request {
4+
public class Body {
55

66
private String executable;
77
private List<String> args;
@@ -10,51 +10,52 @@ public class Request {
1010
private List<Map<String, String>> outputs;
1111
private Map<String, String> options;
1212

13-
public String getExecutable() {
14-
return executable;
15-
}
16-
1713
public void setExecutable(String executable) {
1814
this.executable = executable;
1915
}
2016

21-
public List<String> getArgs() {
22-
return args;
23-
}
24-
2517
public void setArgs(List<String> args) {
2618
this.args = args;
2719
}
2820

29-
public Map<String, String> getEnv() {
30-
return env;
31-
}
32-
3321
public void setEnv(Map<String, String> env) {
3422
this.env = env;
3523
}
3624

37-
public List<Map<String, Object>> getInputs() {
38-
return inputs;
39-
}
40-
4125
public void setInputs(List<Map<String, Object>> inputs) {
4226
this.inputs = inputs;
4327
}
4428

45-
public List<Map<String, String>> getOutputs() {
46-
return outputs;
47-
}
48-
4929
public void setOutputs(List<Map<String, String>> outputs) {
5030
this.outputs = outputs;
5131
}
5232

33+
public void setOptions(Map<String, String> options) {
34+
this.options = options;
35+
}
36+
37+
public String getExecutable() {
38+
return executable;
39+
}
40+
41+
public List<String> getArgs() {
42+
return args;
43+
}
44+
45+
public Map<String, String> getEnv() {
46+
return env;
47+
}
48+
49+
public List<Map<String, Object>> getInputs() {
50+
return inputs;
51+
}
52+
53+
public List<Map<String, String>> getOutputs() {
54+
return outputs;
55+
}
56+
5357
public Map<String, String> getOptions() {
5458
return options;
55-
}
5659

57-
public void setOptions(Map<String, String> options) {
58-
this.options = options;
5960
}
6061
}

src/main/java/Event.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class Event {
2+
3+
private String body;
4+
5+
public String getBody() {
6+
return body;
7+
}
8+
9+
public void setBody(String body) {
10+
this.body = body;
11+
}
12+
}

src/main/java/Handler.java

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import com.amazonaws.services.lambda.runtime.LambdaLogger;
33
import com.amazonaws.services.s3.model.S3Object;
44
import com.amazonaws.util.IOUtils;
5+
import com.google.gson.Gson;
56

67
import java.io.IOException;
78
import java.net.HttpURLConnection;
@@ -15,34 +16,37 @@ public class Handler {
1516
private static final String BUCKET_KEY = "bucket";
1617
private static final String PREFIX_KEY = "prefix";
1718

18-
public Response handleRequest(Request request, Context context) {
19+
public Response handleRequest(Event event, Context context) {
1920

2021
Instant instant1 = Instant.now();
2122
LambdaLogger logger = context.getLogger();
23+
logger.log("body: " + event.getBody());
24+
25+
Gson gson = new Gson();
26+
Body body = gson.fromJson(event.getBody(), Body.class);
2227

2328
try {
24-
logger.log("executable: " + request.getExecutable());
25-
logger.log("args: " + request.getArgs());
26-
logger.log("inputs: " + request.getInputs());
27-
logger.log("outputs: " + request.getOutputs());
28-
logger.log("bucket: " + request.getOptions().get(BUCKET_KEY));
29-
logger.log("prefix: " + request.getOptions().get(PREFIX_KEY));
30-
31-
downloadData(request, logger);
32-
downloadExecutable(request, logger);
33-
execute(request.getExecutable(), String.join(" ", request.getArgs()), logger);
34-
uploadData(request, logger);
29+
logger.log("executable: " + body.getExecutable());
30+
logger.log("args: " + body.getArgs());
31+
logger.log("inputs: " + body.getInputs());
32+
logger.log("outputs: " + body.getOutputs());
33+
logger.log("bucket: " + body.getOptions().get(BUCKET_KEY));
34+
logger.log("prefix: " + body.getOptions().get(PREFIX_KEY));
35+
36+
downloadData(body, logger);
37+
downloadExecutable(body, logger);
38+
execute(body.getExecutable(), String.join(" ", body.getArgs()), logger);
39+
uploadData(body, logger);
3540
} catch (Exception e) {
36-
return handleException(e, request);
41+
return handleException(e, body);
3742
}
3843
Response response = new Response();
3944
response.setStatusCode(HttpURLConnection.HTTP_OK);
40-
response.setMessage("Execution of " + request.getExecutable() + " successful, duration: " + Duration.between(instant1, Instant.now()).getSeconds() + " seconds");
41-
response.setArgs(request.getArgs().toString());
45+
response.setMessage("Execution of " + body.getExecutable() + " successful, duration: " + Duration.between(instant1, Instant.now()).getSeconds() + " seconds");
4246
return response;
4347
}
4448

45-
private void downloadData(Request request, LambdaLogger logger) throws IOException {
49+
private void downloadData(Body request, LambdaLogger logger) throws IOException {
4650
for (Map<String, Object> input : request.getInputs()) {
4751
String fileName = input.get("name").toString();
4852
String key = request.getOptions().get(PREFIX_KEY) + "/" + fileName;
@@ -67,17 +71,17 @@ private void execute(String executable, String args, LambdaLogger logger) throws
6771
logger.log("Stderr: " + errorMsg);
6872
}
6973

70-
private void uploadData(Request request, LambdaLogger logger) {
71-
for (Map<String, String> input : request.getOutputs()) {
74+
private void uploadData(Body body, LambdaLogger logger) {
75+
for (Map<String, String> input : body.getOutputs()) {
7276
String fileName = input.get("name");
7377
String filePath = FOLDER_PATH + fileName;
74-
String key = request.getOptions().get(PREFIX_KEY) + "/" + fileName;
75-
logger.log("Uploading " + request.getOptions().get(BUCKET_KEY) + "/" + key);
76-
S3Utils.putObject(request.getOptions().get(BUCKET_KEY), key, filePath);
78+
String key = body.getOptions().get(PREFIX_KEY) + "/" + fileName;
79+
logger.log("Uploading " + body.getOptions().get(BUCKET_KEY) + "/" + key);
80+
S3Utils.putObject(body.getOptions().get(BUCKET_KEY), key, filePath);
7781
}
7882
}
7983

80-
private Response handleException(Exception e, Request request) {
84+
private Response handleException(Exception e, Body body) {
8185
Throwable cause;
8286
if (e.getCause() != null) {
8387
cause = e.getCause();
@@ -89,16 +93,15 @@ private Response handleException(Exception e, Request request) {
8993
}
9094
Response response = new Response();
9195
response.setStatusCode(HttpURLConnection.HTTP_INTERNAL_ERROR);
92-
response.setMessage("Execution of " + request.getExecutable() + " failed, cause: " + cause.getMessage());
93-
response.setArgs(request.getArgs().toString());
96+
response.setMessage("Execution of " + body.getExecutable() + " failed, cause: " + cause.getMessage());
9497
return response;
9598
}
9699

97-
private void downloadExecutable(Request request, LambdaLogger logger) throws IOException {
98-
logger.log("Downloading executable" + request.getExecutable());
99-
String key = request.getOptions().get(PREFIX_KEY) + "/" + request.getExecutable();
100-
S3Object s3Object = S3Utils.getObject(request.getOptions().get(BUCKET_KEY), key);
101-
S3Utils.saveToFile(s3Object, FOLDER_PATH + request.getExecutable());
100+
private void downloadExecutable(Body body, LambdaLogger logger) throws IOException {
101+
logger.log("Downloading executable" + body.getExecutable());
102+
String key = body.getOptions().get(PREFIX_KEY) + "/" + body.getExecutable();
103+
S3Object s3Object = S3Utils.getObject(body.getOptions().get(BUCKET_KEY), key);
104+
S3Utils.saveToFile(s3Object, FOLDER_PATH + body.getExecutable());
102105
}
103106

104107
}

src/main/java/Response.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ public class Response {
22

33
private int statusCode;
44
private String message;
5-
private String args;
65

76
public int getStatusCode() {
87
return statusCode;
@@ -20,11 +19,11 @@ public void setMessage(String message) {
2019
this.message = message;
2120
}
2221

23-
public String getArgs() {
24-
return args;
25-
}
26-
27-
public void setArgs(String args) {
28-
this.args = args;
22+
@Override
23+
public String toString() {
24+
return "Response{" +
25+
"statusCode=" + statusCode +
26+
", message='" + message + '\'' +
27+
'}';
2928
}
3029
}

0 commit comments

Comments
 (0)