Skip to content

Commit 4650b8a

Browse files
authored
Merge pull request #51 from vinscom/develop
dev-to-master
2 parents f4885f1 + b5a813a commit 4650b8a

File tree

8 files changed

+485
-81
lines changed

8 files changed

+485
-81
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>in.erail</groupId>
66
<artifactId>api-framework</artifactId>
7-
<version>2.4</version>
7+
<version>2.4.1</version>
88
<packaging>jar</packaging>
99
<developers>
1010
<developer>
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package in.erail.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.annotation.JsonInclude.Include;
5+
import com.google.common.io.BaseEncoding;
6+
import io.vertx.core.http.HttpMethod;
7+
import java.util.Map;
8+
9+
/**
10+
*
11+
* @author vinay
12+
*/
13+
@JsonInclude(Include.NON_NULL)
14+
public class ReqestEvent {
15+
16+
private String mResource;
17+
private String mPath;
18+
private HttpMethod mHttpMethod;
19+
private Map<String, String> mHeaders;
20+
private Map<String, String[]> mMultiValueHeaders;
21+
private Map<String, String> mQueryStringParameters;
22+
private Map<String, String[]> mMultiValueQueryStringParameters;
23+
private Map<String, String> mPathParameters;
24+
private Map<String, String> mStageVariables;
25+
private Map mRequestContext;
26+
private byte[] mBody = new byte[0];
27+
private boolean mIsBase64Encoded = false;
28+
29+
public String getResource() {
30+
return mResource;
31+
}
32+
33+
public void setResource(String pResource) {
34+
this.mResource = pResource;
35+
}
36+
37+
public String getPath() {
38+
return mPath;
39+
}
40+
41+
public void setPath(String pPath) {
42+
this.mPath = pPath;
43+
}
44+
45+
public HttpMethod getHttpMethod() {
46+
return mHttpMethod;
47+
}
48+
49+
public void setHttpMethod(HttpMethod pHttpMethod) {
50+
this.mHttpMethod = pHttpMethod;
51+
}
52+
53+
public Map<String, String> getHeaders() {
54+
return mHeaders;
55+
}
56+
57+
public void setHeaders(Map<String, String> pHeaders) {
58+
this.mHeaders = pHeaders;
59+
}
60+
61+
public Map<String, String[]> getMultiValueHeaders() {
62+
return mMultiValueHeaders;
63+
}
64+
65+
public void setMultiValueHeaders(Map<String, String[]> pMultiValueHeaders) {
66+
this.mMultiValueHeaders = pMultiValueHeaders;
67+
}
68+
69+
public Map<String, String> getQueryStringParameters() {
70+
return mQueryStringParameters;
71+
}
72+
73+
public void setQueryStringParameters(Map<String, String> pQueryStringParameters) {
74+
this.mQueryStringParameters = pQueryStringParameters;
75+
}
76+
77+
public Map<String, String[]> getMultiValueQueryStringParameters() {
78+
return mMultiValueQueryStringParameters;
79+
}
80+
81+
public void setMultiValueQueryStringParameters(Map<String, String[]> pMultiValueQueryStringParameters) {
82+
this.mMultiValueQueryStringParameters = pMultiValueQueryStringParameters;
83+
}
84+
85+
public Map<String, String> getPathParameters() {
86+
return mPathParameters;
87+
}
88+
89+
public void setPathParameters(Map<String, String> pPathParameters) {
90+
this.mPathParameters = pPathParameters;
91+
}
92+
93+
public Map<String, String> getStageVariables() {
94+
return mStageVariables;
95+
}
96+
97+
public void setStageVariables(Map<String, String> pStageVariables) {
98+
this.mStageVariables = pStageVariables;
99+
}
100+
101+
public Map getRequestContext() {
102+
return mRequestContext;
103+
}
104+
105+
public void setRequestContext(Map pRequestContext) {
106+
this.mRequestContext = pRequestContext;
107+
}
108+
109+
public boolean isIsBase64Encoded() {
110+
return mIsBase64Encoded;
111+
}
112+
113+
public void setIsBase64Encoded(boolean pIsBase64Encoded) {
114+
this.mIsBase64Encoded = pIsBase64Encoded;
115+
}
116+
117+
public byte[] getBody() {
118+
return mBody;
119+
}
120+
121+
public void setBody(byte[] pBody) {
122+
this.mBody = pBody;
123+
}
124+
125+
public String bodyAsString(){
126+
if(isIsBase64Encoded()){
127+
return new String(BaseEncoding.base64().decode(new String(getBody())));
128+
}
129+
return new String(getBody());
130+
}
131+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package in.erail.model;
2+
3+
import com.google.common.net.HttpHeaders;
4+
import com.google.common.net.MediaType;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
/**
9+
*
10+
* @author vinay
11+
*/
12+
public class ResponseEvent {
13+
14+
private Map<String, String>[] mCookies;
15+
private boolean mIsBase64Encoded = true;
16+
private int mStatusCode = 200;
17+
private Map<String, String> mHeaders;
18+
private Map<String, String[]> mMultiValueHeaders;
19+
private byte[] mBody = new byte[0];
20+
21+
public boolean isIsBase64Encoded() {
22+
return mIsBase64Encoded;
23+
}
24+
25+
public void setIsBase64Encoded(boolean pIsBase64Encoded) {
26+
this.mIsBase64Encoded = pIsBase64Encoded;
27+
}
28+
29+
public int getStatusCode() {
30+
return mStatusCode;
31+
}
32+
33+
public void setStatusCode(int pStatusCode) {
34+
this.mStatusCode = pStatusCode;
35+
}
36+
37+
public Map<String, String> getHeaders() {
38+
if (mHeaders == null) {
39+
mHeaders = new HashMap<>();
40+
}
41+
return mHeaders;
42+
}
43+
44+
public void setHeaders(Map<String, String> pHeaders) {
45+
this.mHeaders = pHeaders;
46+
}
47+
48+
public byte[] getBody() {
49+
return mBody;
50+
}
51+
52+
public void setBody(byte[] pBody) {
53+
this.mBody = pBody;
54+
}
55+
56+
public Map<String, String[]> getMultiValueHeaders() {
57+
if (mMultiValueHeaders == null) {
58+
mMultiValueHeaders = new HashMap<>();
59+
}
60+
return mMultiValueHeaders;
61+
}
62+
63+
public void setMultiValueHeaders(Map<String, String[]> pMultiValueHeaders) {
64+
this.mMultiValueHeaders = pMultiValueHeaders;
65+
}
66+
67+
public Map<String, String>[] getCookies() {
68+
return mCookies;
69+
}
70+
71+
public void setCookies(Map<String, String>[] pCookies) {
72+
this.mCookies = pCookies;
73+
}
74+
75+
public void addHeader(String pHeaderName, String pMediaType) {
76+
getHeaders().put(HttpHeaders.CONTENT_TYPE, pMediaType);
77+
}
78+
79+
public void addHeader(String pHeaderName, MediaType pMediaType) {
80+
getHeaders().put(HttpHeaders.CONTENT_TYPE, pMediaType.toString());
81+
}
82+
}

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

Lines changed: 51 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
import static in.erail.common.FrameworkConstants.RoutingContext.Json;
1515
import in.erail.glue.annotation.StartService;
16+
import in.erail.model.ReqestEvent;
17+
import in.erail.model.ResponseEvent;
1618
import io.netty.handler.codec.http.HttpHeaderNames;
1719
import io.netty.handler.codec.http.HttpResponseStatus;
1820
import io.vertx.core.eventbus.DeliveryOptions;
@@ -113,80 +115,74 @@ public void process(RoutingContext pRequestContext, String pServiceUniqueId) {
113115
*/
114116
public JsonObject serialiseRoutingContext(RoutingContext pContext) {
115117

116-
JsonObject result = new JsonObject();
118+
ReqestEvent request = new ReqestEvent();
119+
request.setHttpMethod(pContext.request().method());
117120

118-
if (pContext.request().method() == HttpMethod.POST) {
119-
result.put(Json.BODY, pContext.getBody().getDelegate().getBytes());
120-
} else {
121-
result.put(Json.BODY, new byte[]{});
121+
if (request.getHttpMethod() == HttpMethod.POST
122+
|| request.getHttpMethod() == HttpMethod.PUT
123+
|| request.getHttpMethod() == HttpMethod.PATCH) {
124+
request.setBody(pContext.getBody().getDelegate().getBytes());
122125
}
123126

124-
JsonObject headers = new JsonObject(convertMultiMapIntoMap(pContext.request().headers()));
125-
result.put(Json.HEADERS, headers);
127+
request.setHeaders(convertMultiMapIntoMap(pContext.request().headers()));
128+
request.setQueryStringParameters(convertMultiMapIntoMap(pContext.queryParams()));
129+
request.setPathParameters(convertMultiMapIntoMap(pContext.request().params()));
126130

127-
JsonObject query = new JsonObject(convertMultiMapIntoMap(pContext.queryParams()));
128-
result.put(Json.QUERY_STRING_PARAM, query);
129-
130-
JsonObject params = new JsonObject(convertMultiMapIntoMap(pContext.request().params()));
131-
result.put(Json.PATH_PARAM, params);
131+
JsonObject result = JsonObject.mapFrom(request);
132132

133133
getLog().debug(() -> "Context to JSON:" + result.toString());
134134

135135
return result;
136136
}
137137

138138
/**
139-
* All response content is written in binary. If Content type is not provided then application/octet-stream content type is set.
139+
* All response content is written in binary. If Content type is not provided
140+
* then application/octet-stream content type is set.
140141
*
141142
* @param pReplyResponse Service Body
142143
* @param pContext Routing Context
143144
* @return HttpServerResponse
144145
*/
145146
public HttpServerResponse buildResponseFromReply(JsonObject pReplyResponse, RoutingContext pContext) {
146147

147-
JsonObject headers = pReplyResponse.getJsonObject(Json.HEADERS, new JsonObject());
148-
String statusCode = pReplyResponse.getString(Json.STATUS_CODE, HttpResponseStatus.OK.codeAsText().toString());
148+
ResponseEvent response = pReplyResponse.mapTo(ResponseEvent.class);
149149

150-
if (!headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
151-
headers.put(HttpHeaders.CONTENT_TYPE, MediaType.OCTET_STREAM.toString());
150+
if (!response.getHeaders().containsKey(HttpHeaders.CONTENT_TYPE)) {
151+
response.getHeaders().put(HttpHeaders.CONTENT_TYPE, MediaType.OCTET_STREAM.toString());
152152
}
153153

154-
headers
155-
.fieldNames()
154+
response
155+
.getHeaders()
156+
.entrySet()
156157
.stream()
157-
.forEach((field) -> {
158-
pContext.response().putHeader(field, headers.getString(field, ""));
158+
.forEach((kv) -> {
159+
pContext.response().putHeader(kv.getKey(), kv.getValue());
159160
});
160161

161-
pContext.response().setStatusCode(HttpResponseStatus.parseLine(statusCode).code());
162-
163-
Optional<JsonArray> cookies = Optional.ofNullable(pReplyResponse.getJsonArray(Json.COOKIES));
164-
165-
cookies.ifPresent((cooky) -> {
166-
for (Iterator<Object> iterator = cooky.iterator(); iterator.hasNext();) {
167-
JsonObject next = (JsonObject) iterator.next();
168-
Optional cookieName = Optional.ofNullable(next.getString(Json.Cookie.NAME));
169-
if (cookieName.isPresent()) {
170-
Cookie c = Cookie.cookie((String) cookieName.get(), "");
171-
Optional.ofNullable(next.getString(Json.Cookie.VALUE)).ifPresent(t -> c.setValue(t));
172-
Optional.ofNullable(next.getString(Json.Cookie.PATH)).ifPresent(t -> c.setPath(t));
173-
Optional.ofNullable(next.getDouble(Json.Cookie.MAX_AGE)).ifPresent(t -> c.setMaxAge(t.longValue()));
174-
Optional.ofNullable(next.getString(Json.Cookie.DOMAIN)).ifPresent(t -> c.setDomain(t));
175-
Optional.ofNullable(next.getBoolean(Json.Cookie.SECURE)).ifPresent(t -> c.setSecure(t));
176-
Optional.ofNullable(next.getBoolean(Json.Cookie.HTTP_ONLY)).ifPresent(t -> c.setHttpOnly(t));
177-
pContext.addCookie(c);
178-
}
179-
}
180-
});
162+
pContext.response().setStatusCode(response.getStatusCode());
181163

182-
Optional<byte[]> body;
164+
Map<String,String>[] cookies = Optional.ofNullable(response.getCookies()).orElse(new Map[0]);
183165

184-
try {
185-
body = Optional.ofNullable(pReplyResponse.getBinary(Json.BODY));
186-
} catch (IllegalArgumentException e) {
187-
getLog().error(() -> "Could not get message body as binary. Please check if service is sending body in binary." + pContext.request().absoluteURI() + ":" + e.toString());
188-
body = Optional.empty();
189-
}
166+
Arrays
167+
.stream(cookies)
168+
.map((t) -> {
169+
Optional cookieName = Optional.ofNullable(t.get(Json.Cookie.NAME));
170+
if (cookieName.isPresent()) {
171+
Cookie c = Cookie.cookie((String) cookieName.get(), "");
172+
Optional.ofNullable(t.get(Json.Cookie.VALUE)).ifPresent(v -> c.setValue(v));
173+
Optional.ofNullable(t.get(Json.Cookie.PATH)).ifPresent(v -> c.setPath(v));
174+
Optional.ofNullable(t.get(Json.Cookie.MAX_AGE)).ifPresent(v -> c.setMaxAge(Long.parseLong(v)));
175+
Optional.ofNullable(t.get(Json.Cookie.DOMAIN)).ifPresent(v -> c.setDomain(v));
176+
Optional.ofNullable(t.get(Json.Cookie.SECURE)).ifPresent(v -> c.setSecure(Boolean.parseBoolean(v)));
177+
Optional.ofNullable(t.get(Json.Cookie.HTTP_ONLY)).ifPresent(v -> c.setHttpOnly(Boolean.parseBoolean(v)));
178+
return Optional.of(c);
179+
}
180+
return Optional.<Cookie>empty();
181+
})
182+
.filter(t -> t.isPresent())
183+
.forEach(t -> pContext.addCookie(t.get()));
184+
185+
Optional<byte[]> body = Optional.ofNullable(response.getBody());
190186

191187
body.ifPresent((t) -> {
192188
pContext.response().putHeader(HttpHeaderNames.CONTENT_LENGTH.toString(), Integer.toString(t.length));
@@ -196,7 +192,7 @@ public HttpServerResponse buildResponseFromReply(JsonObject pReplyResponse, Rout
196192
return pContext.response();
197193
}
198194

199-
public Map<String, Object> convertMultiMapIntoMap(MultiMap pMultiMap) {
195+
public Map<String, String> convertMultiMapIntoMap(MultiMap pMultiMap) {
200196
return pMultiMap
201197
.getDelegate()
202198
.entries()
@@ -247,12 +243,12 @@ public Router getRouter(Router pRouter) {
247243
process(routingContext, service.getServiceUniqueId());
248244
}
249245
});
250-
251-
apiFactory.addFailureHandlerByOperationId(service.getOperationId(),(routingContext) -> {
252-
routingContext
253-
.response()
254-
.setStatusCode(400)
255-
.end(routingContext.failure().toString());
246+
247+
apiFactory.addFailureHandlerByOperationId(service.getOperationId(), (routingContext) -> {
248+
routingContext
249+
.response()
250+
.setStatusCode(400)
251+
.end(routingContext.failure().toString());
256252
});
257253
});
258254
});

0 commit comments

Comments
 (0)