Skip to content

Commit 3d3d01f

Browse files
authored
Merge pull request #3 from vinscom/develop
Release 2.4.2
2 parents 5cb4fc0 + b62179f commit 3d3d01f

File tree

9 files changed

+341
-124
lines changed

9 files changed

+341
-124
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
target
22
nbactions.xml
3+
/nbproject/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#/in/erail/route/OpenAPI3RouteBuilder
2+
3+
services+=\
4+
/in/erail/service/SessionGetService
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"title": "tutorial-api",
5+
"version": "1.0.0"
6+
},
7+
"paths": {
8+
"/session": {
9+
"get": {
10+
"summary": "Get all session messages",
11+
"description": "Return all session messages",
12+
"operationId": "API_SESSION_GET",
13+
"responses": {
14+
"200": {
15+
"description": "Return all messages",
16+
"content": {
17+
"application/json": {
18+
"schema": {
19+
"type": "array",
20+
"items": {
21+
"$ref": "#/components/schemas/message"
22+
}
23+
}
24+
}
25+
}
26+
}
27+
}
28+
}
29+
}
30+
},
31+
"components": {
32+
"schemas": {
33+
"message": {
34+
"title": "Root Type for message",
35+
"description": "The root of the message type's schema.",
36+
"type": "object",
37+
"properties": {
38+
"message": {
39+
"type": "string"
40+
},
41+
"time": {
42+
"type": "string"
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#/in/erail/service/SessionGetService
2+
$class=in.erail.service.SessionGetService
3+
4+
operationId=API_SESSION_GET
5+
serviceUniqueId=API_SESSION_GET
6+
vertx=/io/vertx/core/Vertx
7+
enable=true
8+
log=true

pom.xml

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>in.erail</groupId>
55
<artifactId>api-framework-amazon-lambda</artifactId>
6-
<version>2.3</version>
6+
<version>2.4.2</version>
77
<packaging>jar</packaging>
88
<developers>
99
<developer>
@@ -15,7 +15,10 @@
1515
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1616
<maven.compiler.source>1.8</maven.compiler.source>
1717
<maven.compiler.target>1.8</maven.compiler.target>
18-
<api.framework.version>2.3</api.framework.version>
18+
<api.framework.version>2.4.2</api.framework.version>
19+
<layer.api.framework>${settings.localRepository}/in/erail/api-framework/${api.framework.version}/api-framework-${api.framework.version}-common-config.zip</layer.api.framework>
20+
<layer.api.framework.lambda.common>${project.basedir}/config-layers/common</layer.api.framework.lambda.common>
21+
<layer.api.framework.lambda.test>${project.basedir}/config-layers/test</layer.api.framework.lambda.test>
1922
</properties>
2023
<description>
2124
Lib to run API Framework code on Amazon Lambda
@@ -127,9 +130,35 @@
127130
<artifactId>aws-lambda-java-log4j2</artifactId>
128131
<version>1.1.0</version>
129132
</dependency>
133+
<dependency>
134+
<groupId>io.vertx</groupId>
135+
<artifactId>vertx-unit</artifactId>
136+
<version>3.6.2</version>
137+
<scope>test</scope>
138+
</dependency>
139+
<dependency>
140+
<groupId>junit</groupId>
141+
<artifactId>junit</artifactId>
142+
<version>4.12</version>
143+
<scope>test</scope>
144+
</dependency>
145+
<dependency>
146+
<groupId>org.hamcrest</groupId>
147+
<artifactId>hamcrest-core</artifactId>
148+
<version>2.1</version>
149+
<scope>test</scope>
150+
</dependency>
130151
</dependencies>
131152
<build>
132153
<plugins>
154+
<plugin>
155+
<groupId>org.apache.maven.plugins</groupId>
156+
<artifactId>maven-surefire-plugin</artifactId>
157+
<version>2.22.1</version>
158+
<configuration>
159+
<argLine>-Dhazelcast.jmx=true -Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.Log4j2LogDelegateFactory -Dglue.layers=${layer.api.framework},${layer.api.framework.lambda.common},${layer.api.framework.lambda.test}</argLine>
160+
</configuration>
161+
</plugin>
133162
<plugin>
134163
<artifactId>maven-assembly-plugin</artifactId>
135164
<version>3.1.0</version>

src/main/java/in/erail/amazon/lambda/AWSLambda.java

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.amazonaws.services.lambda.runtime.Context;
44
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
5+
import com.google.common.base.Preconditions;
56
import com.google.common.base.Strings;
67
import com.google.common.io.ByteStreams;
78
import in.erail.glue.Glue;
@@ -10,88 +11,99 @@
1011
import io.reactivex.schedulers.Schedulers;
1112
import io.vertx.core.buffer.Buffer;
1213
import io.vertx.core.json.JsonObject;
13-
import io.vertx.reactivex.core.eventbus.Message;
1414
import java.io.IOException;
1515
import java.io.InputStream;
1616
import java.io.OutputStream;
1717
import java.io.OutputStreamWriter;
18-
import java.util.concurrent.CompletableFuture;
19-
import java.util.concurrent.ExecutionException;
20-
import java.util.logging.Level;
21-
import java.util.logging.Logger;
22-
import static in.erail.common.FrameworkConstants.RoutingContext.Json.*;
18+
import in.erail.model.RequestEvent;
19+
import java.util.ArrayList;
20+
import java.util.HashSet;
21+
import java.util.List;
22+
import java.util.Optional;
23+
import java.util.Set;
24+
import org.apache.logging.log4j.LogManager;
25+
import org.apache.logging.log4j.Logger;
2326

2427
/**
2528
*
2629
* @author vinay
2730
*/
2831
public class AWSLambda implements RequestStreamHandler {
2932

33+
protected Logger log = LogManager.getLogger(AWSLambda.class.getCanonicalName());
3034
private static final String SERVICE_ENV = "SERVICE";
3135
private static final String SERVICE_SYS_PROP = "service";
3236
private final RESTService mService;
37+
private final List<String> allowedFields = new ArrayList<>();
3338

3439
public AWSLambda() {
40+
41+
allowedFields.add("isBase64Encoded");
42+
allowedFields.add("statusCode");
43+
allowedFields.add("headers");
44+
allowedFields.add("multiValueHeaders");
45+
allowedFields.add("body");
46+
3547
String component = System.getenv(SERVICE_ENV);
3648
if (Strings.isNullOrEmpty(component)) {
3749
component = System.getProperty(SERVICE_SYS_PROP);
3850
}
51+
3952
mService = Glue.instance().resolve(component);
4053
}
4154

4255
@Override
4356
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
44-
45-
JsonObject requestJson = new JsonObject(Buffer.buffer(ByteStreams.toByteArray(inputStream)));
46-
JsonObject responseJson = handleMessage(requestJson);
47-
4857
try (OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8")) {
49-
writer.write(responseJson.toString());
58+
JsonObject requestJson = new JsonObject(Buffer.buffer(ByteStreams.toByteArray(inputStream)));
59+
log.debug(() -> requestJson.toString());
60+
String resp = handleMessage(requestJson).blockingGet();
61+
log.debug(() -> resp);
62+
writer.write(resp);
5063
}
5164
}
5265

53-
public JsonObject handleMessage(JsonObject pJsonObject) {
54-
55-
JsonObject msg;
66+
public Single<String> handleMessage(JsonObject pRequest) {
67+
return Single
68+
.just(pRequest)
69+
.subscribeOn(Schedulers.computation())
70+
.map(this::convertBodyToBase64)
71+
.map(reqJson -> reqJson.mapTo(RequestEvent.class))
72+
.map(req -> getService().process(req))
73+
.map(resp -> JsonObject.mapFrom(resp))
74+
.map(this::sanatizeResponse)
75+
.map(respJson -> respJson.toString());
76+
}
5677

57-
try {
58-
CompletableFuture<JsonObject> result = new CompletableFuture<>();
59-
MessageAWSLambda<JsonObject> message = new MessageAWSLambda<>(result, pJsonObject);
78+
protected JsonObject sanatizeResponse(JsonObject pResp) {
79+
Preconditions.checkNotNull(pResp);
6080

61-
Single
62-
.just(message)
63-
.map(m -> new Message<JsonObject>(message))
64-
.subscribeOn(Schedulers.computation())
65-
.subscribe(m -> getService().process(m));
81+
Set<String> keys = new HashSet(pResp.fieldNames());
6682

67-
msg = result.get();
68-
} catch (InterruptedException | ExecutionException ex) {
69-
msg = new JsonObject();
70-
Logger.getLogger(AWSLambda.class.getName()).log(Level.SEVERE, null, ex);
71-
}
83+
keys
84+
.stream()
85+
.filter(key -> !allowedFields.contains(key))
86+
.forEach(key -> pResp.remove(key));
7287

73-
return transform(msg);
88+
return pResp;
7489
}
7590

76-
public JsonObject transform(JsonObject pMsg) {
77-
78-
pMsg.put(IS_BASE64_ENCODED, Boolean.TRUE);
91+
protected JsonObject convertBodyToBase64(JsonObject pRequest) {
7992

80-
if (!pMsg.containsKey(STATUS_CODE)) {
81-
pMsg.put(STATUS_CODE, "200");
82-
}
83-
84-
if (!pMsg.containsKey(HEADERS)) {
85-
pMsg.put(HEADERS, new JsonObject());
86-
}
93+
Boolean isBase64Encoded
94+
= Optional
95+
.ofNullable(pRequest.getBoolean("isBase64Encoded"))
96+
.orElse(Boolean.FALSE);
8797

88-
if (pMsg.containsKey(BODY)) {
89-
pMsg.put(BODY, pMsg.getString(BODY));
90-
} else {
91-
pMsg.put(BODY, "");
98+
if (isBase64Encoded == false && pRequest.containsKey("body")) {
99+
Optional<String> body = Optional.ofNullable(pRequest.getString("body"));
100+
body.ifPresent((t) -> {
101+
pRequest.remove("body");
102+
pRequest.put("body", body.get().getBytes());
103+
});
92104
}
93105

94-
return pMsg;
106+
return pRequest;
95107
}
96108

97109
public RESTService getService() {

src/main/java/in/erail/amazon/lambda/MessageAWSLambda.java

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)