Skip to content

Commit

Permalink
Merge pull request #51 from vinscom/develop
Browse files Browse the repository at this point in the history
dev-to-master
  • Loading branch information
vinscom authored Dec 20, 2018
2 parents f4885f1 + b5a813a commit 4650b8a
Show file tree
Hide file tree
Showing 8 changed files with 485 additions and 81 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>in.erail</groupId>
<artifactId>api-framework</artifactId>
<version>2.4</version>
<version>2.4.1</version>
<packaging>jar</packaging>
<developers>
<developer>
Expand Down
131 changes: 131 additions & 0 deletions src/main/java/in/erail/model/ReqestEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package in.erail.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.google.common.io.BaseEncoding;
import io.vertx.core.http.HttpMethod;
import java.util.Map;

/**
*
* @author vinay
*/
@JsonInclude(Include.NON_NULL)
public class ReqestEvent {

private String mResource;
private String mPath;
private HttpMethod mHttpMethod;
private Map<String, String> mHeaders;
private Map<String, String[]> mMultiValueHeaders;
private Map<String, String> mQueryStringParameters;
private Map<String, String[]> mMultiValueQueryStringParameters;
private Map<String, String> mPathParameters;
private Map<String, String> mStageVariables;
private Map mRequestContext;
private byte[] mBody = new byte[0];
private boolean mIsBase64Encoded = false;

public String getResource() {
return mResource;
}

public void setResource(String pResource) {
this.mResource = pResource;
}

public String getPath() {
return mPath;
}

public void setPath(String pPath) {
this.mPath = pPath;
}

public HttpMethod getHttpMethod() {
return mHttpMethod;
}

public void setHttpMethod(HttpMethod pHttpMethod) {
this.mHttpMethod = pHttpMethod;
}

public Map<String, String> getHeaders() {
return mHeaders;
}

public void setHeaders(Map<String, String> pHeaders) {
this.mHeaders = pHeaders;
}

public Map<String, String[]> getMultiValueHeaders() {
return mMultiValueHeaders;
}

public void setMultiValueHeaders(Map<String, String[]> pMultiValueHeaders) {
this.mMultiValueHeaders = pMultiValueHeaders;
}

public Map<String, String> getQueryStringParameters() {
return mQueryStringParameters;
}

public void setQueryStringParameters(Map<String, String> pQueryStringParameters) {
this.mQueryStringParameters = pQueryStringParameters;
}

public Map<String, String[]> getMultiValueQueryStringParameters() {
return mMultiValueQueryStringParameters;
}

public void setMultiValueQueryStringParameters(Map<String, String[]> pMultiValueQueryStringParameters) {
this.mMultiValueQueryStringParameters = pMultiValueQueryStringParameters;
}

public Map<String, String> getPathParameters() {
return mPathParameters;
}

public void setPathParameters(Map<String, String> pPathParameters) {
this.mPathParameters = pPathParameters;
}

public Map<String, String> getStageVariables() {
return mStageVariables;
}

public void setStageVariables(Map<String, String> pStageVariables) {
this.mStageVariables = pStageVariables;
}

public Map getRequestContext() {
return mRequestContext;
}

public void setRequestContext(Map pRequestContext) {
this.mRequestContext = pRequestContext;
}

public boolean isIsBase64Encoded() {
return mIsBase64Encoded;
}

public void setIsBase64Encoded(boolean pIsBase64Encoded) {
this.mIsBase64Encoded = pIsBase64Encoded;
}

public byte[] getBody() {
return mBody;
}

public void setBody(byte[] pBody) {
this.mBody = pBody;
}

public String bodyAsString(){
if(isIsBase64Encoded()){
return new String(BaseEncoding.base64().decode(new String(getBody())));
}
return new String(getBody());
}
}
82 changes: 82 additions & 0 deletions src/main/java/in/erail/model/ResponseEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package in.erail.model;

import com.google.common.net.HttpHeaders;
import com.google.common.net.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
*
* @author vinay
*/
public class ResponseEvent {

private Map<String, String>[] mCookies;
private boolean mIsBase64Encoded = true;
private int mStatusCode = 200;
private Map<String, String> mHeaders;
private Map<String, String[]> mMultiValueHeaders;
private byte[] mBody = new byte[0];

public boolean isIsBase64Encoded() {
return mIsBase64Encoded;
}

public void setIsBase64Encoded(boolean pIsBase64Encoded) {
this.mIsBase64Encoded = pIsBase64Encoded;
}

public int getStatusCode() {
return mStatusCode;
}

public void setStatusCode(int pStatusCode) {
this.mStatusCode = pStatusCode;
}

public Map<String, String> getHeaders() {
if (mHeaders == null) {
mHeaders = new HashMap<>();
}
return mHeaders;
}

public void setHeaders(Map<String, String> pHeaders) {
this.mHeaders = pHeaders;
}

public byte[] getBody() {
return mBody;
}

public void setBody(byte[] pBody) {
this.mBody = pBody;
}

public Map<String, String[]> getMultiValueHeaders() {
if (mMultiValueHeaders == null) {
mMultiValueHeaders = new HashMap<>();
}
return mMultiValueHeaders;
}

public void setMultiValueHeaders(Map<String, String[]> pMultiValueHeaders) {
this.mMultiValueHeaders = pMultiValueHeaders;
}

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

public void setCookies(Map<String, String>[] pCookies) {
this.mCookies = pCookies;
}

public void addHeader(String pHeaderName, String pMediaType) {
getHeaders().put(HttpHeaders.CONTENT_TYPE, pMediaType);
}

public void addHeader(String pHeaderName, MediaType pMediaType) {
getHeaders().put(HttpHeaders.CONTENT_TYPE, pMediaType.toString());
}
}
106 changes: 51 additions & 55 deletions src/main/java/in/erail/route/OpenAPI3RouteBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import static in.erail.common.FrameworkConstants.RoutingContext.Json;
import in.erail.glue.annotation.StartService;
import in.erail.model.ReqestEvent;
import in.erail.model.ResponseEvent;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.vertx.core.eventbus.DeliveryOptions;
Expand Down Expand Up @@ -113,80 +115,74 @@ public void process(RoutingContext pRequestContext, String pServiceUniqueId) {
*/
public JsonObject serialiseRoutingContext(RoutingContext pContext) {

JsonObject result = new JsonObject();
ReqestEvent request = new ReqestEvent();
request.setHttpMethod(pContext.request().method());

if (pContext.request().method() == HttpMethod.POST) {
result.put(Json.BODY, pContext.getBody().getDelegate().getBytes());
} else {
result.put(Json.BODY, new byte[]{});
if (request.getHttpMethod() == HttpMethod.POST
|| request.getHttpMethod() == HttpMethod.PUT
|| request.getHttpMethod() == HttpMethod.PATCH) {
request.setBody(pContext.getBody().getDelegate().getBytes());
}

JsonObject headers = new JsonObject(convertMultiMapIntoMap(pContext.request().headers()));
result.put(Json.HEADERS, headers);
request.setHeaders(convertMultiMapIntoMap(pContext.request().headers()));
request.setQueryStringParameters(convertMultiMapIntoMap(pContext.queryParams()));
request.setPathParameters(convertMultiMapIntoMap(pContext.request().params()));

JsonObject query = new JsonObject(convertMultiMapIntoMap(pContext.queryParams()));
result.put(Json.QUERY_STRING_PARAM, query);

JsonObject params = new JsonObject(convertMultiMapIntoMap(pContext.request().params()));
result.put(Json.PATH_PARAM, params);
JsonObject result = JsonObject.mapFrom(request);

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

return result;
}

/**
* All response content is written in binary. If Content type is not provided then application/octet-stream content type is set.
* All response content is written in binary. If Content type is not provided
* then application/octet-stream content type is set.
*
* @param pReplyResponse Service Body
* @param pContext Routing Context
* @return HttpServerResponse
*/
public HttpServerResponse buildResponseFromReply(JsonObject pReplyResponse, RoutingContext pContext) {

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

if (!headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
headers.put(HttpHeaders.CONTENT_TYPE, MediaType.OCTET_STREAM.toString());
if (!response.getHeaders().containsKey(HttpHeaders.CONTENT_TYPE)) {
response.getHeaders().put(HttpHeaders.CONTENT_TYPE, MediaType.OCTET_STREAM.toString());
}

headers
.fieldNames()
response
.getHeaders()
.entrySet()
.stream()
.forEach((field) -> {
pContext.response().putHeader(field, headers.getString(field, ""));
.forEach((kv) -> {
pContext.response().putHeader(kv.getKey(), kv.getValue());
});

pContext.response().setStatusCode(HttpResponseStatus.parseLine(statusCode).code());

Optional<JsonArray> cookies = Optional.ofNullable(pReplyResponse.getJsonArray(Json.COOKIES));

cookies.ifPresent((cooky) -> {
for (Iterator<Object> iterator = cooky.iterator(); iterator.hasNext();) {
JsonObject next = (JsonObject) iterator.next();
Optional cookieName = Optional.ofNullable(next.getString(Json.Cookie.NAME));
if (cookieName.isPresent()) {
Cookie c = Cookie.cookie((String) cookieName.get(), "");
Optional.ofNullable(next.getString(Json.Cookie.VALUE)).ifPresent(t -> c.setValue(t));
Optional.ofNullable(next.getString(Json.Cookie.PATH)).ifPresent(t -> c.setPath(t));
Optional.ofNullable(next.getDouble(Json.Cookie.MAX_AGE)).ifPresent(t -> c.setMaxAge(t.longValue()));
Optional.ofNullable(next.getString(Json.Cookie.DOMAIN)).ifPresent(t -> c.setDomain(t));
Optional.ofNullable(next.getBoolean(Json.Cookie.SECURE)).ifPresent(t -> c.setSecure(t));
Optional.ofNullable(next.getBoolean(Json.Cookie.HTTP_ONLY)).ifPresent(t -> c.setHttpOnly(t));
pContext.addCookie(c);
}
}
});
pContext.response().setStatusCode(response.getStatusCode());

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

try {
body = Optional.ofNullable(pReplyResponse.getBinary(Json.BODY));
} catch (IllegalArgumentException e) {
getLog().error(() -> "Could not get message body as binary. Please check if service is sending body in binary." + pContext.request().absoluteURI() + ":" + e.toString());
body = Optional.empty();
}
Arrays
.stream(cookies)
.map((t) -> {
Optional cookieName = Optional.ofNullable(t.get(Json.Cookie.NAME));
if (cookieName.isPresent()) {
Cookie c = Cookie.cookie((String) cookieName.get(), "");
Optional.ofNullable(t.get(Json.Cookie.VALUE)).ifPresent(v -> c.setValue(v));
Optional.ofNullable(t.get(Json.Cookie.PATH)).ifPresent(v -> c.setPath(v));
Optional.ofNullable(t.get(Json.Cookie.MAX_AGE)).ifPresent(v -> c.setMaxAge(Long.parseLong(v)));
Optional.ofNullable(t.get(Json.Cookie.DOMAIN)).ifPresent(v -> c.setDomain(v));
Optional.ofNullable(t.get(Json.Cookie.SECURE)).ifPresent(v -> c.setSecure(Boolean.parseBoolean(v)));
Optional.ofNullable(t.get(Json.Cookie.HTTP_ONLY)).ifPresent(v -> c.setHttpOnly(Boolean.parseBoolean(v)));
return Optional.of(c);
}
return Optional.<Cookie>empty();
})
.filter(t -> t.isPresent())
.forEach(t -> pContext.addCookie(t.get()));

Optional<byte[]> body = Optional.ofNullable(response.getBody());

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

public Map<String, Object> convertMultiMapIntoMap(MultiMap pMultiMap) {
public Map<String, String> convertMultiMapIntoMap(MultiMap pMultiMap) {
return pMultiMap
.getDelegate()
.entries()
Expand Down Expand Up @@ -247,12 +243,12 @@ public Router getRouter(Router pRouter) {
process(routingContext, service.getServiceUniqueId());
}
});
apiFactory.addFailureHandlerByOperationId(service.getOperationId(),(routingContext) -> {
routingContext
.response()
.setStatusCode(400)
.end(routingContext.failure().toString());

apiFactory.addFailureHandlerByOperationId(service.getOperationId(), (routingContext) -> {
routingContext
.response()
.setStatusCode(400)
.end(routingContext.failure().toString());
});
});
});
Expand Down
Loading

0 comments on commit 4650b8a

Please sign in to comment.