Skip to content

Commit 80ff0a8

Browse files
authored
Merge pull request #54 from vinscom/develop
Release 2.4.2
2 parents 5661cad + 3529441 commit 80ff0a8

File tree

14 files changed

+136
-136
lines changed

14 files changed

+136
-136
lines changed

config-layers/common/io/vertx/core/VertxOptions.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ $class=io.vertx.core.VertxOptions
44
clustered=true
55
clusterManager=/io/vertx/spi/cluster/ClusterManager
66
metricsOptions=/io/vertx/ext/dropwizard/DropwizardMetricsOptions
7-
preferNativeTransport=true
7+
preferNativeTransport=false

pom.xml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
</profile>
9797
</profiles>
9898
<properties>
99-
<vertx.version>3.5.4</vertx.version>
99+
<vertx.version>3.6.2</vertx.version>
100100
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
101101
<maven.compiler.source>1.8</maven.compiler.source>
102102
<maven.compiler.target>1.8</maven.compiler.target>
@@ -166,7 +166,7 @@
166166
<dependency>
167167
<groupId>io.netty</groupId>
168168
<artifactId>netty-transport-native-epoll</artifactId>
169-
<version>4.1.19.Final</version>
169+
<version>4.1.30.Final</version>
170170
<classifier>linux-x86_64</classifier>
171171
</dependency>
172172
<dependency>
@@ -177,12 +177,12 @@
177177
<dependency>
178178
<groupId>in.erail</groupId>
179179
<artifactId>glue</artifactId>
180-
<version>2.4</version>
180+
<version>2.4.2</version>
181181
</dependency>
182182
<dependency>
183183
<groupId>org.mockito</groupId>
184184
<artifactId>mockito-core</artifactId>
185-
<version>2.22.0</version>
185+
<version>2.23.4</version>
186186
<scope>test</scope>
187187
</dependency>
188188
<dependency>
@@ -194,7 +194,7 @@
194194
<dependency>
195195
<groupId>org.hamcrest</groupId>
196196
<artifactId>hamcrest-core</artifactId>
197-
<version>1.3</version>
197+
<version>2.1</version>
198198
<scope>test</scope>
199199
</dependency>
200200
</dependencies>
@@ -203,7 +203,7 @@
203203
<plugin>
204204
<groupId>org.apache.maven.plugins</groupId>
205205
<artifactId>maven-surefire-plugin</artifactId>
206-
<version>2.22.0</version>
206+
<version>2.22.1</version>
207207
<configuration>
208208
<argLine>-Dhazelcast.jmx=true -Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.Log4j2LogDelegateFactory -Dglue.layers=${project.basedir}/config-layers/common,${project.basedir}/config-layers/test</argLine>
209209
</configuration>
@@ -229,7 +229,7 @@
229229
<plugin>
230230
<groupId>org.jacoco</groupId>
231231
<artifactId>jacoco-maven-plugin</artifactId>
232-
<version>0.8.1</version>
232+
<version>0.8.2</version>
233233
<executions>
234234
<execution>
235235
<id>prepare-agent</id>

src/main/java/in/erail/model/ReqestEvent.java renamed to src/main/java/in/erail/model/RequestEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @author vinay
1212
*/
1313
@JsonInclude(Include.NON_NULL)
14-
public class ReqestEvent {
14+
public class RequestEvent {
1515

1616
private String mResource;
1717
private String mPath;

src/main/java/in/erail/model/ResponseEvent.java

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,49 @@ public class ResponseEvent {
2424

2525
public ResponseEvent() {
2626
mMultiValueHeaders = MultiMap.caseInsensitiveMultiMap();
27-
mMultiValueHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.JAVASCRIPT_UTF_8.toString());
2827
}
2928

3029
public boolean isIsBase64Encoded() {
3130
return mIsBase64Encoded;
3231
}
3332

34-
public void setIsBase64Encoded(boolean pIsBase64Encoded) {
33+
public ResponseEvent setIsBase64Encoded(boolean pIsBase64Encoded) {
3534
this.mIsBase64Encoded = pIsBase64Encoded;
35+
return this;
3636
}
3737

3838
public int getStatusCode() {
3939
return mStatusCode;
4040
}
4141

42-
public void setStatusCode(int pStatusCode) {
42+
public ResponseEvent setStatusCode(int pStatusCode) {
4343
this.mStatusCode = pStatusCode;
44+
return this;
4445
}
4546

4647
public byte[] getBody() {
4748
return mBody;
4849
}
4950

50-
public void setBody(byte[] pBody) {
51+
public ResponseEvent setBody(byte[] pBody) {
5152
this.mBody = pBody;
53+
return this;
5254
}
5355

5456
public Map<String, String>[] getCookies() {
5557
return mCookies;
5658
}
5759

58-
public void setCookies(Map<String, String>[] pCookies) {
60+
public ResponseEvent setCookies(Map<String, String>[] pCookies) {
5961
this.mCookies = pCookies;
62+
return this;
6063
}
6164

62-
public void setMultiValueHeaders(Map<String, String[]> pValue) {
65+
public ResponseEvent setMultiValueHeaders(Map<String, String[]> pValue) {
6366
Preconditions.checkNotNull(pValue);
6467

6568
if (pValue.isEmpty()) {
66-
return;
69+
return this;
6770
}
6871

6972
mMultiValueHeaders
@@ -81,8 +84,14 @@ public void setMultiValueHeaders(Map<String, String[]> pValue) {
8184
a.addAll(b);
8285
return a;
8386
});
87+
88+
return this;
8489
}
8590

91+
/**
92+
* Return copy of headers map
93+
* @return
94+
*/
8695
public Map<String, String[]> getMultiValueHeaders() {
8796

8897
Map<String, String[]> result
@@ -100,11 +109,11 @@ public Map<String, String[]> getMultiValueHeaders() {
100109
return Collections.unmodifiableMap(result);
101110
}
102111

103-
public void setHeaders(Map<String, String> pValue) {
112+
public ResponseEvent setHeaders(Map<String, String> pValue) {
104113
Preconditions.checkNotNull(pValue);
105114

106115
if (pValue.isEmpty()) {
107-
return;
116+
return this;
108117
}
109118

110119
mMultiValueHeaders
@@ -120,8 +129,13 @@ public void setHeaders(Map<String, String> pValue) {
120129
a.addAll(b);
121130
return a;
122131
});
132+
return this;
123133
}
124134

135+
/**
136+
* Return copy of Header Map
137+
* @return
138+
*/
125139
public Map<String, String> getHeaders() {
126140

127141
Map<String, String> result
@@ -139,22 +153,30 @@ public Map<String, String> getHeaders() {
139153
return Collections.unmodifiableMap(result);
140154
}
141155

142-
public void setContentType(String pContentType) {
156+
public ResponseEvent setContentType(String pContentType) {
143157
if (mMultiValueHeaders.contains(HttpHeaders.CONTENT_TYPE)) {
144158
mMultiValueHeaders.remove(HttpHeaders.CONTENT_TYPE);
145159
}
146160
mMultiValueHeaders.add(HttpHeaders.CONTENT_TYPE, pContentType);
161+
return this;
147162
}
148163

149-
public void setContentType(MediaType pMediaType) {
164+
public ResponseEvent setContentType(MediaType pMediaType) {
150165
setContentType(pMediaType.toString());
166+
return this;
151167
}
152168

153-
public void addHeader(String pHeaderName, String pMediaType) {
169+
public ResponseEvent addHeader(String pHeaderName, String pMediaType) {
154170
mMultiValueHeaders.add(HttpHeaders.CONTENT_TYPE, pMediaType);
171+
return this;
155172
}
156173

157-
public void addHeader(String pHeaderName, MediaType pMediaType) {
174+
public ResponseEvent addHeader(String pHeaderName, MediaType pMediaType) {
158175
addHeader(HttpHeaders.CONTENT_TYPE, pMediaType.toString());
176+
return this;
177+
}
178+
179+
public String headerValue(String pHeaderName) {
180+
return mMultiValueHeaders.get(pHeaderName);
159181
}
160182
}

src/main/java/in/erail/route/OpenAPI3RouteBuilder.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import static in.erail.common.FrameworkConstants.RoutingContext.Json;
1515
import in.erail.glue.annotation.StartService;
16-
import in.erail.model.ReqestEvent;
16+
import in.erail.model.RequestEvent;
1717
import in.erail.model.ResponseEvent;
1818
import io.netty.handler.codec.http.HttpHeaderNames;
1919
import io.vertx.core.eventbus.DeliveryOptions;
@@ -90,7 +90,8 @@ public void process(RoutingContext pRequestContext, String pServiceUniqueId) {
9090
(reply) -> {
9191
if (reply.succeeded()) {
9292
JsonObject response = (JsonObject) reply.result().body();
93-
buildResponseFromReply(response, pRequestContext).end();
93+
HttpServerResponse resp = buildResponseFromReply(response, pRequestContext);
94+
resp.end();
9495
} else {
9596
((Meter) getMetrics().get(pServiceUniqueId + FAIL_SUFFIX)).mark();
9697
getLog().error(() -> "Error in reply:" + reply.cause().toString());
@@ -112,7 +113,7 @@ public void process(RoutingContext pRequestContext, String pServiceUniqueId) {
112113
*/
113114
public JsonObject serialiseRoutingContext(RoutingContext pContext) {
114115

115-
ReqestEvent request = new ReqestEvent();
116+
RequestEvent request = new RequestEvent();
116117
request.setHttpMethod(pContext.request().method());
117118

118119
if (request.getHttpMethod() == HttpMethod.POST
@@ -144,8 +145,10 @@ public HttpServerResponse buildResponseFromReply(JsonObject pReplyResponse, Rout
144145

145146
ResponseEvent response = pReplyResponse.mapTo(ResponseEvent.class);
146147

147-
if (!response.getHeaders().containsKey(HttpHeaders.CONTENT_TYPE)) {
148-
response.getHeaders().put(HttpHeaders.CONTENT_TYPE, MediaType.OCTET_STREAM.toString());
148+
Optional<String> contentType = Optional.ofNullable(response.headerValue(HttpHeaders.CONTENT_TYPE));
149+
150+
if (contentType.isPresent()) {
151+
response.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.OCTET_STREAM);
149152
}
150153

151154
response
@@ -159,7 +162,7 @@ public HttpServerResponse buildResponseFromReply(JsonObject pReplyResponse, Rout
159162
pContext.response().setStatusCode(response.getStatusCode());
160163

161164
@SuppressWarnings("unchecked")
162-
Map<String,String>[] cookies = Optional.ofNullable(response.getCookies()).orElse(new Map[0]);
165+
Map<String, String>[] cookies = Optional.ofNullable(response.getCookies()).orElse(new Map[0]);
163166

164167
Arrays
165168
.stream(cookies)
@@ -184,9 +187,8 @@ public HttpServerResponse buildResponseFromReply(JsonObject pReplyResponse, Rout
184187

185188
body.ifPresent((t) -> {
186189
pContext.response().putHeader(HttpHeaderNames.CONTENT_LENGTH.toString(), Integer.toString(t.length));
187-
pContext.response().write(Buffer.newInstance(io.vertx.core.buffer.Buffer.buffer(t)));
190+
pContext.response().write(Buffer.buffer(t));
188191
});
189-
190192
return pContext.response();
191193
}
192194

@@ -210,7 +212,7 @@ public void setDeliveryOptions(DeliveryOptions pDeliveryOptions) {
210212
public Router getRouter(Router pRouter) {
211213

212214
OpenAPI3RouterFactory apiFactory = OpenAPI3RouterFactory
213-
.rxCreateRouterFactoryFromFile(getVertx(), getOpenAPI3File().getAbsolutePath())
215+
.rxCreate(getVertx(), getOpenAPI3File().getAbsolutePath())
214216
.blockingGet();
215217

216218
Optional

src/main/java/in/erail/security/SecurityTools.java

Lines changed: 21 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@
77
import in.erail.glue.annotation.StartService;
88
import io.reactivex.Single;
99
import io.vertx.reactivex.core.Vertx;
10-
import io.vertx.reactivex.core.shareddata.AsyncMap;
11-
import io.vertx.reactivex.core.shareddata.Lock;
1210
import java.security.InvalidAlgorithmParameterException;
1311
import java.security.InvalidKeyException;
1412
import java.security.NoSuchAlgorithmException;
1513
import java.security.SecureRandom;
1614
import java.util.Arrays;
1715
import java.util.Base64;
18-
import java.util.HashMap;
19-
import java.util.Map;
2016
import java.util.concurrent.CompletableFuture;
2117
import java.util.concurrent.ExecutionException;
2218
import javax.crypto.BadPaddingException;
@@ -47,63 +43,8 @@ public void startup() {
4743
return;
4844
}
4945

50-
Map<String, Object> cryptCtx = new HashMap<>();
51-
52-
Single<Lock> lock = getVertx()
53-
.sharedData()
54-
.rxGetLockWithTimeout("_in.erail.security", 5000);
55-
56-
getVertx()
57-
.sharedData()
58-
.<String, byte[]>rxGetClusterWideMap("_in.erail.security")
59-
.flatMap((m) -> {
60-
cryptCtx.put("map", m);
61-
return m.rxGet("key");
62-
})
63-
.map((k) -> {
64-
cryptCtx.put("key", k);
65-
return cryptCtx;
66-
})
67-
.flatMap((ctx) -> {
68-
if (ctx.get("key") == null) {
69-
return lock
70-
.map((l) -> {
71-
ctx.put("lock", l);
72-
return ctx;
73-
});
74-
}
75-
return Single.just(ctx);
76-
})
77-
.flatMap(ctx -> {
78-
if (ctx.get("lock") != null) {
79-
return ((AsyncMap<String, Object>) (ctx.get("map")))
80-
.rxGet("key")
81-
.map((k) -> {
82-
ctx.put("key", k);
83-
return ctx;
84-
});
85-
}
86-
return Single.just(ctx);
87-
})
88-
.flatMap((ctx) -> {
89-
if (ctx.get("key") == null) {
90-
KeyGenerator keygen = KeyGenerator.getInstance("AES");
91-
keygen.init(128);
92-
byte[] key = keygen.generateKey().getEncoded();
93-
return ((AsyncMap<String, Object>) (ctx.get("map")))
94-
.rxPut("key", key)
95-
.doOnComplete(() -> ctx.put("key", key))
96-
.toSingleDefault(ctx);
97-
}
98-
return Single.just(ctx);
99-
})
100-
.map(ctx -> (byte[]) ctx.get("key"))
101-
.doFinally(() -> {
102-
if (cryptCtx.containsKey("lock")) {
103-
Lock l = (Lock) cryptCtx.get("lock");
104-
l.release();
105-
}
106-
})
46+
generateKey()
47+
.flatMap(v -> addValueToClusterMap("key", v))
10748
.subscribe((key) -> {
10849
mKeySpec.complete(new SecretKeySpec(key, "AES"));
10950
String unique = Base64.getEncoder().encodeToString(Arrays.copyOfRange(key, 0, 5));
@@ -112,6 +53,25 @@ public void startup() {
11253
});
11354
}
11455

56+
protected Single<byte[]> addValueToClusterMap(String pKey, byte[] pValue) {
57+
return getVertx()
58+
.sharedData()
59+
.<String, byte[]>rxGetClusterWideMap("_in.erail.security")
60+
.flatMapMaybe(m -> m.rxPutIfAbsent(pKey, pValue))
61+
.toSingle(pValue);
62+
}
63+
64+
protected Single<byte[]> generateKey() {
65+
KeyGenerator keygen;
66+
try {
67+
keygen = KeyGenerator.getInstance("AES");
68+
} catch (NoSuchAlgorithmException ex) {
69+
return Single.error(ex);
70+
}
71+
keygen.init(128);
72+
return Single.just(keygen.generateKey().getEncoded());
73+
}
74+
11575
/**
11676
* Unique string across cluster. Changes on each restart of cluster.
11777
*

0 commit comments

Comments
 (0)