44import com .codahale .metrics .Metered ;
55import com .codahale .metrics .MetricRegistry ;
66import com .codahale .metrics .Timer ;
7- import com .google .common .base .Strings ;
87import com .google .common .net .HttpHeaders ;
98import com .google .common .net .MediaType ;
109import java .io .File ;
2524import io .vertx .reactivex .ext .web .RoutingContext ;
2625import io .vertx .reactivex .ext .web .api .contract .openapi3 .OpenAPI3RouterFactory ;
2726import in .erail .service .RESTService ;
27+ import io .vertx .reactivex .core .buffer .Buffer ;
2828import java .util .Arrays ;
2929import java .util .HashMap ;
3030import java .util .Optional ;
@@ -102,28 +102,20 @@ public void process(RoutingContext pRequestContext, String pServiceUniqueId) {
102102
103103 }
104104
105+ /**
106+ * In case of post request. Body is sent in binary
107+ *
108+ * @param pContext Routing Context
109+ * @return JsonObject representing RoutingContext
110+ */
105111 public JsonObject serialiseRoutingContext (RoutingContext pContext ) {
106112
107113 JsonObject result = new JsonObject ();
108114
109115 if (pContext .request ().method () == HttpMethod .POST ) {
110- boolean bodyAsJson = pContext .<Boolean >get (FrameworkConstants .RoutingContext .Attribute .BODY_AS_JSON );
111- if (bodyAsJson ) {
112- String mediaTypeHeader = pContext .request ().headers ().get (HttpHeaders .CONTENT_TYPE );
113- MediaType contentType ;
114- if (Strings .isNullOrEmpty (mediaTypeHeader )) {
115- contentType = MediaType .JSON_UTF_8 ;
116- } else {
117- contentType = MediaType .parse (mediaTypeHeader );
118- }
119- if (MediaType .JSON_UTF_8 .type ().equals (contentType .type ()) && MediaType .JSON_UTF_8 .subtype ().equals (contentType .subtype ())) {
120- result .put (FrameworkConstants .RoutingContext .Json .BODY , pContext .getBodyAsJson ());
121- }
122- } else {
123- result .put (FrameworkConstants .RoutingContext .Json .BODY , pContext .getBody ().getDelegate ().getBytes ());
124- }
116+ result .put (FrameworkConstants .RoutingContext .Json .BODY , pContext .getBody ().getDelegate ().getBytes ());
125117 } else {
126- result .put (FrameworkConstants .RoutingContext .Json .BODY , new JsonObject () );
118+ result .put (FrameworkConstants .RoutingContext .Json .BODY , new byte []{} );
127119 }
128120
129121 JsonObject headers = new JsonObject (convertMultiMapIntoMap (pContext .request ().headers ()));
@@ -140,13 +132,20 @@ public JsonObject serialiseRoutingContext(RoutingContext pContext) {
140132 return result ;
141133 }
142134
135+ /**
136+ * All response content is written in binary. If Content type is not provided then application/octet-stream content type is set.
137+ *
138+ * @param pReplyResponse Service Body
139+ * @param pContext Routing Context
140+ * @return HttpServerResponse
141+ */
143142 public HttpServerResponse buildResponseFromReply (JsonObject pReplyResponse , RoutingContext pContext ) {
144143
145144 JsonObject headers = pReplyResponse .getJsonObject (FrameworkConstants .RoutingContext .Json .HEADERS , new JsonObject ());
146145 String statusCode = pReplyResponse .getString (FrameworkConstants .RoutingContext .Json .STATUS_CODE , HttpResponseStatus .OK .codeAsText ().toString ());
147146
148147 if (!headers .containsKey (HttpHeaders .CONTENT_TYPE )) {
149- headers .put (HttpHeaders .CONTENT_TYPE , MediaType .JSON_UTF_8 .toString ());
148+ headers .put (HttpHeaders .CONTENT_TYPE , MediaType .OCTET_STREAM .toString ());
150149 }
151150
152151 headers
@@ -158,13 +157,12 @@ public HttpServerResponse buildResponseFromReply(JsonObject pReplyResponse, Rout
158157
159158 pContext .response ().setStatusCode (HttpResponseStatus .parseLine (statusCode ).code ());
160159
161- Object body = pReplyResponse . getMap (). get (FrameworkConstants .RoutingContext .Json .BODY );
160+ Optional < byte []> body = Optional . ofNullable ( pReplyResponse . getBinary (FrameworkConstants .RoutingContext .Json .BODY ) );
162161
163- if (body != null ) {
164- String bodyStr = body .toString ();
165- pContext .response ().putHeader (HttpHeaderNames .CONTENT_LENGTH .toString (), Integer .toString (bodyStr .length ()));
166- pContext .response ().write (bodyStr );
167- }
162+ body .ifPresent ((t ) -> {
163+ pContext .response ().putHeader (HttpHeaderNames .CONTENT_LENGTH .toString (), Integer .toString (t .length ));
164+ pContext .response ().write (Buffer .newInstance (io .vertx .core .buffer .Buffer .buffer (t )));
165+ });
168166
169167 return pContext .response ();
170168 }
@@ -200,9 +198,6 @@ public Router getRouter(Router pRouter) {
200198 .stream ()
201199 .forEach ((service ) -> {
202200 apiFactory .addHandlerByOperationId (service .getOperationId (), (routingContext ) -> {
203-
204- routingContext .put (FrameworkConstants .RoutingContext .Attribute .BODY_AS_JSON , service .isBodyAsJson ());
205-
206201 if (isSecurityEnable ()) {
207202
208203 if (routingContext .user () == null ) {
0 commit comments