1313
1414import static in .erail .common .FrameworkConstants .RoutingContext .Json ;
1515import in .erail .glue .annotation .StartService ;
16+ import in .erail .model .ReqestEvent ;
17+ import in .erail .model .ResponseEvent ;
1618import io .netty .handler .codec .http .HttpHeaderNames ;
1719import io .netty .handler .codec .http .HttpResponseStatus ;
1820import 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