diff --git a/pom.xml b/pom.xml index 77c4f5d..d95b055 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 in.erail api-framework - 2.4 + 2.4.1 jar diff --git a/src/main/java/in/erail/model/ReqestEvent.java b/src/main/java/in/erail/model/ReqestEvent.java new file mode 100644 index 0000000..aeea867 --- /dev/null +++ b/src/main/java/in/erail/model/ReqestEvent.java @@ -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 mHeaders; + private Map mMultiValueHeaders; + private Map mQueryStringParameters; + private Map mMultiValueQueryStringParameters; + private Map mPathParameters; + private Map 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 getHeaders() { + return mHeaders; + } + + public void setHeaders(Map pHeaders) { + this.mHeaders = pHeaders; + } + + public Map getMultiValueHeaders() { + return mMultiValueHeaders; + } + + public void setMultiValueHeaders(Map pMultiValueHeaders) { + this.mMultiValueHeaders = pMultiValueHeaders; + } + + public Map getQueryStringParameters() { + return mQueryStringParameters; + } + + public void setQueryStringParameters(Map pQueryStringParameters) { + this.mQueryStringParameters = pQueryStringParameters; + } + + public Map getMultiValueQueryStringParameters() { + return mMultiValueQueryStringParameters; + } + + public void setMultiValueQueryStringParameters(Map pMultiValueQueryStringParameters) { + this.mMultiValueQueryStringParameters = pMultiValueQueryStringParameters; + } + + public Map getPathParameters() { + return mPathParameters; + } + + public void setPathParameters(Map pPathParameters) { + this.mPathParameters = pPathParameters; + } + + public Map getStageVariables() { + return mStageVariables; + } + + public void setStageVariables(Map 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()); + } +} diff --git a/src/main/java/in/erail/model/ResponseEvent.java b/src/main/java/in/erail/model/ResponseEvent.java new file mode 100644 index 0000000..b42f3c5 --- /dev/null +++ b/src/main/java/in/erail/model/ResponseEvent.java @@ -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[] mCookies; + private boolean mIsBase64Encoded = true; + private int mStatusCode = 200; + private Map mHeaders; + private Map 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 getHeaders() { + if (mHeaders == null) { + mHeaders = new HashMap<>(); + } + return mHeaders; + } + + public void setHeaders(Map pHeaders) { + this.mHeaders = pHeaders; + } + + public byte[] getBody() { + return mBody; + } + + public void setBody(byte[] pBody) { + this.mBody = pBody; + } + + public Map getMultiValueHeaders() { + if (mMultiValueHeaders == null) { + mMultiValueHeaders = new HashMap<>(); + } + return mMultiValueHeaders; + } + + public void setMultiValueHeaders(Map pMultiValueHeaders) { + this.mMultiValueHeaders = pMultiValueHeaders; + } + + public Map[] getCookies() { + return mCookies; + } + + public void setCookies(Map[] 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()); + } +} diff --git a/src/main/java/in/erail/route/OpenAPI3RouteBuilder.java b/src/main/java/in/erail/route/OpenAPI3RouteBuilder.java index bc7e890..390504c 100644 --- a/src/main/java/in/erail/route/OpenAPI3RouteBuilder.java +++ b/src/main/java/in/erail/route/OpenAPI3RouteBuilder.java @@ -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; @@ -113,22 +115,20 @@ 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()); @@ -136,7 +136,8 @@ public JsonObject serialiseRoutingContext(RoutingContext pContext) { } /** - * 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 @@ -144,49 +145,44 @@ public JsonObject serialiseRoutingContext(RoutingContext pContext) { */ 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 cookies = Optional.ofNullable(pReplyResponse.getJsonArray(Json.COOKIES)); - - cookies.ifPresent((cooky) -> { - for (Iterator 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 body; + Map[] 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.empty(); + }) + .filter(t -> t.isPresent()) + .forEach(t -> pContext.addCookie(t.get())); + + Optional body = Optional.ofNullable(response.getBody()); body.ifPresent((t) -> { pContext.response().putHeader(HttpHeaderNames.CONTENT_LENGTH.toString(), Integer.toString(t.length)); @@ -196,7 +192,7 @@ public HttpServerResponse buildResponseFromReply(JsonObject pReplyResponse, Rout return pContext.response(); } - public Map convertMultiMapIntoMap(MultiMap pMultiMap) { + public Map convertMultiMapIntoMap(MultiMap pMultiMap) { return pMultiMap .getDelegate() .entries() @@ -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()); }); }); }); diff --git a/src/test/java/in/erail/model/ReqestEventTest.java b/src/test/java/in/erail/model/ReqestEventTest.java new file mode 100644 index 0000000..14d13c2 --- /dev/null +++ b/src/test/java/in/erail/model/ReqestEventTest.java @@ -0,0 +1,154 @@ +package in.erail.model; + +import io.vertx.core.json.Json; +import io.vertx.core.json.JsonObject; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author vinay + */ +public class ReqestEventTest { + + private String stringJson = "{\n" + + " \"resource\": \"/{proxy+}\",\n" + + " \"path\": \"/hello/world\",\n" + + " \"httpMethod\": \"POST\",\n" + + " \"headers\": {\n" + + " \"Accept\": \"*/*\",\n" + + " \"Accept-Encoding\": \"gzip, deflate\",\n" + + " \"cache-control\": \"no-cache\",\n" + + " \"CloudFront-Forwarded-Proto\": \"https\",\n" + + " \"CloudFront-Is-Desktop-Viewer\": \"true\",\n" + + " \"CloudFront-Is-Mobile-Viewer\": \"false\",\n" + + " \"CloudFront-Is-SmartTV-Viewer\": \"false\",\n" + + " \"CloudFront-Is-Tablet-Viewer\": \"false\",\n" + + " \"CloudFront-Viewer-Country\": \"US\",\n" + + " \"Content-Type\": \"application/json\",\n" + + " \"headerName\": \"headerValue\",\n" + + " \"Host\": \"gy415nuibc.execute-api.us-east-1.amazonaws.com\",\n" + + " \"Postman-Token\": \"9f583ef0-ed83-4a38-aef3-eb9ce3f7a57f\",\n" + + " \"User-Agent\": \"PostmanRuntime/2.4.5\",\n" + + " \"Via\": \"1.1 d98420743a69852491bbdea73f7680bd.cloudfront.net (CloudFront)\",\n" + + " \"X-Amz-Cf-Id\": \"pn-PWIJc6thYnZm5P0NMgOUglL1DYtl0gdeJky8tqsg8iS_sgsKD1A==\",\n" + + " \"X-Forwarded-For\": \"54.240.196.186, 54.182.214.83\",\n" + + " \"X-Forwarded-Port\": \"443\",\n" + + " \"X-Forwarded-Proto\": \"https\"\n" + + " },\n" + + " \"multiValueHeaders\":{\n" + + " \"Accept\":[\n" + + " \"*/*\"\n" + + " ],\n" + + " \"Accept-Encoding\":[\n" + + " \"gzip, deflate\"\n" + + " ],\n" + + " \"cache-control\":[\n" + + " \"no-cache\"\n" + + " ],\n" + + " \"CloudFront-Forwarded-Proto\":[\n" + + " \"https\"\n" + + " ],\n" + + " \"CloudFront-Is-Desktop-Viewer\":[\n" + + " \"true\"\n" + + " ],\n" + + " \"CloudFront-Is-Mobile-Viewer\":[\n" + + " \"false\"\n" + + " ],\n" + + " \"CloudFront-Is-SmartTV-Viewer\":[\n" + + " \"false\"\n" + + " ],\n" + + " \"CloudFront-Is-Tablet-Viewer\":[\n" + + " \"false\"\n" + + " ],\n" + + " \"CloudFront-Viewer-Country\":[\n" + + " \"US\"\n" + + " ],\n" + + " \"\":[\n" + + " \"\"\n" + + " ],\n" + + " \"Content-Type\":[\n" + + " \"application/json\"\n" + + " ],\n" + + " \"headerName\":[\n" + + " \"headerValue\"\n" + + " ],\n" + + " \"Host\":[\n" + + " \"gy415nuibc.execute-api.us-east-1.amazonaws.com\"\n" + + " ],\n" + + " \"Postman-Token\":[\n" + + " \"9f583ef0-ed83-4a38-aef3-eb9ce3f7a57f\"\n" + + " ],\n" + + " \"User-Agent\":[\n" + + " \"PostmanRuntime/2.4.5\"\n" + + " ],\n" + + " \"Via\":[\n" + + " \"1.1 d98420743a69852491bbdea73f7680bd.cloudfront.net (CloudFront)\"\n" + + " ],\n" + + " \"X-Amz-Cf-Id\":[\n" + + " \"pn-PWIJc6thYnZm5P0NMgOUglL1DYtl0gdeJky8tqsg8iS_sgsKD1A==\"\n" + + " ],\n" + + " \"X-Forwarded-For\":[\n" + + " \"54.240.196.186, 54.182.214.83\"\n" + + " ],\n" + + " \"X-Forwarded-Port\":[\n" + + " \"443\"\n" + + " ],\n" + + " \"X-Forwarded-Proto\":[\n" + + " \"https\"\n" + + " ]\n" + + " },\n" + + " \"queryStringParameters\": {\n" + + " \"name\": \"me\",\n" + + " \"multivalueName\": \"me\"\n" + + " },\n" + + " \"multiValueQueryStringParameters\":{\n" + + " \"name\":[\n" + + " \"me\"\n" + + " ],\n" + + " \"multivalueName\":[\n" + + " \"you\",\n" + + " \"me\"\n" + + " ]\n" + + " },\n" + + " \"pathParameters\": {\n" + + " \"proxy\": \"hello/world\"\n" + + " },\n" + + " \"stageVariables\": {\n" + + " \"stageVariableName\": \"stageVariableValue\"\n" + + " },\n" + + " \"requestContext\": {\n" + + " \"accountId\": \"12345678912\",\n" + + " \"resourceId\": \"roq9wj\",\n" + + " \"stage\": \"testStage\",\n" + + " \"requestId\": \"deef4878-7910-11e6-8f14-25afc3e9ae33\",\n" + + " \"identity\": {\n" + + " \"cognitoIdentityPoolId\": null,\n" + + " \"accountId\": null,\n" + + " \"cognitoIdentityId\": null,\n" + + " \"caller\": null,\n" + + " \"apiKey\": null,\n" + + " \"sourceIp\": \"192.168.196.186\",\n" + + " \"cognitoAuthenticationType\": null,\n" + + " \"cognitoAuthenticationProvider\": null,\n" + + " \"userArn\": null,\n" + + " \"userAgent\": \"PostmanRuntime/2.4.5\",\n" + + " \"user\": null\n" + + " },\n" + + " \"resourcePath\": \"/{proxy+}\",\n" + + " \"httpMethod\": \"POST\",\n" + + " \"apiId\": \"gy415nuibc\"\n" + + " },\n" + + " \"body\": \"eyJhIjogMX0=\",\n" + + " \"isBase64Encoded\": true\n" + + " }"; + + @Test + public void tesMapping() { + ReqestEvent request = Json.decodeValue(stringJson, ReqestEvent.class); + JsonObject json = JsonObject.mapFrom(request); + JsonObject result = new JsonObject(stringJson); + assertEquals(json, result); + } + +} diff --git a/src/test/java/in/erail/model/ResponseEventTest.java b/src/test/java/in/erail/model/ResponseEventTest.java new file mode 100644 index 0000000..a8e1bd8 --- /dev/null +++ b/src/test/java/in/erail/model/ResponseEventTest.java @@ -0,0 +1,43 @@ +package in.erail.model; + +import io.vertx.core.json.JsonObject; +import org.junit.Assert; +import org.junit.Test; + +/** + * + * @author vinay + */ +public class ResponseEventTest { + + + public void testMapping() { + JsonObject result = new JsonObject("{\n" + + " \"body\" : \"VGVzdGluZw==\",\n" + + " \"isBase64Encoded\" : true,\n" + + " \"statusCode\" : 200,\n" + + " \"multiValueHeaders\":{},\n" + + " \"cookies\" : [{\n" + + " \"name\" : \"yello\"\n" + + " \"value\" : \"yello\"\n" + + " \"httpOnly\" : \"true\"\n" + + " \"secure\" : \"true\"\n" + + " \"path\" : \"/fsdfsd\"\n" + + " \"maxAge\" : \"233\"\n" + + " \"domain\" : \"yahoo.com\"\n" + + " }],\n" + + " \"headers\" : {\n" + + " \"a\" : \"b\"\n" + + " }\n" + + "}"); + + ResponseEvent response = new ResponseEvent(); + + response.setBody("Testing".getBytes()); + response.getHeaders().put("a", "b"); + + JsonObject json = JsonObject.mapFrom(response); + Assert.assertEquals(result, json); + } + +} diff --git a/src/test/java/in/erail/service/BinaryBodyService.java b/src/test/java/in/erail/service/BinaryBodyService.java index 583cf45..9c369fa 100644 --- a/src/test/java/in/erail/service/BinaryBodyService.java +++ b/src/test/java/in/erail/service/BinaryBodyService.java @@ -8,6 +8,8 @@ import io.vertx.core.json.JsonObject; import io.vertx.reactivex.core.eventbus.Message; import static in.erail.common.FrameworkConstants.RoutingContext.*; +import in.erail.model.ReqestEvent; +import in.erail.model.ResponseEvent; /** * @@ -17,26 +19,24 @@ public class BinaryBodyService extends RESTServiceImpl{ @Override public void process(Message pMessage) { - JsonObject param = pMessage.body().getJsonObject(Json.PATH_PARAM); - String topicName = param.getString(TestConstants.Service.Broadcast.APIMessage.PARAM_TOPIC_NAME); - + + ReqestEvent request = pMessage.body().mapTo(ReqestEvent.class); + + String topicName = request.getPathParameters().get(TestConstants.Service.Broadcast.APIMessage.PARAM_TOPIC_NAME); + if(Strings.isNullOrEmpty(topicName)){ pMessage.fail(0, "Empty Topic Name"); } - JsonObject headers = new JsonObject(); - headers.put(HttpHeaders.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8.toString()); + ResponseEvent response = new ResponseEvent(); + response.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8); - byte[] body = pMessage.body().getBinary(Json.BODY); - JsonObject jsonBody = new JsonObject(Buffer.buffer(body)); + JsonObject jsonBody = new JsonObject(Buffer.buffer(request.getBody())); String bodyContent = jsonBody.getString("data"); + response.setBody(bodyContent.getBytes()); - JsonObject resp = new JsonObject(); - resp.put(Json.BODY, bodyContent.getBytes()); - resp.put(Json.HEADERS, headers); - - pMessage.reply(resp); + pMessage.reply(JsonObject.mapFrom(response)); } } diff --git a/src/test/java/in/erail/service/BroadcastService.java b/src/test/java/in/erail/service/BroadcastService.java index ba502b0..fa366ab 100644 --- a/src/test/java/in/erail/service/BroadcastService.java +++ b/src/test/java/in/erail/service/BroadcastService.java @@ -3,12 +3,11 @@ import com.google.common.base.Strings; import com.google.common.net.HttpHeaders; import com.google.common.net.MediaType; -import in.erail.common.FrameworkConstants; import in.erail.test.TestConstants; -import io.vertx.core.buffer.Buffer; import io.vertx.core.json.JsonObject; import io.vertx.reactivex.core.eventbus.Message; -import static in.erail.common.FrameworkConstants.RoutingContext.*; +import in.erail.model.ReqestEvent; +import in.erail.model.ResponseEvent; /** * @@ -18,15 +17,16 @@ public class BroadcastService extends RESTServiceImpl { @Override public void process(Message pMessage) { - JsonObject param = pMessage.body().getJsonObject(Json.PATH_PARAM); - String topicName = param.getString(TestConstants.Service.Broadcast.APIMessage.PARAM_TOPIC_NAME); + + ReqestEvent request = pMessage.body().mapTo(ReqestEvent.class); + + String topicName = request.getPathParameters().get(TestConstants.Service.Broadcast.APIMessage.PARAM_TOPIC_NAME); if (Strings.isNullOrEmpty(topicName)) { pMessage.fail(0, "Empty Topic Name"); } - byte[] body = pMessage.body().getBinary(FrameworkConstants.RoutingContext.Json.BODY); - JsonObject bodyJson = new JsonObject(Buffer.buffer(body)); + JsonObject bodyJson = new JsonObject(request.bodyAsString()); getVertx() .eventBus() @@ -34,13 +34,11 @@ public void process(Message pMessage) { getLog().debug(() -> String.format("Message[%s] published on [%s]", bodyJson.toString(), topicName)); - JsonObject resp = new JsonObject(); - resp.put(Json.BODY, TestConstants.Service.Message.successMessage().toString().getBytes()); - - JsonObject headers = new JsonObject().put(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString()); - resp.put(Json.HEADERS, headers); + ResponseEvent response = new ResponseEvent(); + response.setBody(TestConstants.Service.Message.successMessage().toString().getBytes()); + response.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8); - pMessage.reply(resp); + pMessage.reply(JsonObject.mapFrom(response)); } }