1616
1717import  java .util .*;
1818
19+ import  com .ericsson .eiffel .remrem .protocol .ValidationResult ;
1920import  com .ericsson .eiffel .remrem .publish .service .*;
2021import  com .google .gson .*;
2122import  org .slf4j .LoggerFactory ;
@@ -392,6 +393,8 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String
392393
393394            responseStatus  = response .getStatusCode ();
394395            String  responseBody  = null ;
396+             // TODO We should not rely on bodyJson (an input string), but rather on given 
397+             //      result, i.e. response.getBody() and check this for type (object or array). 
395398            if  (bodyJson .isJsonObject ()) {
396399                responseBody  = "["  + response .getBody () + "]" ;
397400            } else  if  (bodyJson .isJsonArray ()) {
@@ -407,8 +410,8 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String
407410                if  (msgService  != null  && msgProtocol  != null ) {
408411                    rmqHelper .rabbitMqPropertiesInit (msgProtocol );
409412                }
410-                 responseEvents  = processingValidEvent (responseBody , msgProtocol , userDomain ,
411-                         tag , routingKey );
413+                 responseEvents  = processingValidEvent (responseBody , msgProtocol , msgType ,  userDomain ,
414+                         tag , routingKey ,  okToLeaveOutInvalidOptionalFields );
412415            } else  {
413416                return  response ;
414417            }
@@ -423,7 +426,8 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String
423426            } else  if  (bodyJson .isJsonArray ()) {
424427                responseBody  = responseMessage ;
425428            }
426-             responseEvents  = processingValidEvent (responseBody , msgProtocol , userDomain , tag , routingKey );
429+             responseEvents  = processingValidEvent (responseBody , msgProtocol , msgType , userDomain ,
430+                     tag , routingKey , okToLeaveOutInvalidOptionalFields );
427431            return  new  ResponseEntity <>(responseEvents , HttpStatus .BAD_REQUEST );
428432        }
429433        //Status here is the status returned from generate service, except BAD_REQUEST which already handled above 
@@ -477,8 +481,8 @@ public void initializeResponse(HttpStatus status, String errorMessage, String re
477481     * @return list of responses 
478482     */ 
479483    // TODO Why public? 
480-     public  List <Map <String , Object >> processingValidEvent (String  eventResponseMessage , final  String  msgProtocol , final  String  userDomain ,
481-                                                           final  String  tag , final  String  routingKey ) {
484+     public  List <Map <String , Object >> processingValidEvent (String  eventResponseMessage , final  String  msgProtocol , final  String  msgType ,
485+                                                           final  String  userDomain ,  final   String   tag , final  String  routingKey ,  final   Boolean   okToLeaveOutInvalidOptionalFields ) {
482486        MsgService  msgService  = PublishUtils .getMessageService (msgProtocol , msgServices );
483487        List <Map <String , Object >> responseEvent  = new  ArrayList <>();
484488        Map <String , Object > eventResponse  = null ;
@@ -510,16 +514,15 @@ public List<Map<String, Object>> processingValidEvent(String eventResponseMessag
510514        }
511515
512516        for  (int  i  = 0 ; i  < eventArray .size (); i ++) {
517+             // TODO Having hash map here is very chaotic: key are sometimes predefined values, 
518+             //      e.g. JSON_STATUS_RESULT, JSON_EVENT_MESSAGE_FIELD, but also a general string 
519+             //      as in case of reporting a bad request. 
513520            eventResponse  = new  HashMap <>();
514-             JsonElement  jsonResponseEvent  = eventArray .get (i );
515-             if  (isValid (jsonResponseEvent )) {
516-                 JsonObject  validResponse  = jsonResponseEvent .getAsJsonObject ();
517-                 if  (validResponse  == null ) {
518-                     String  errorMessage  = "Invalid, array item expected to be in the form of JSON object" ;
519-                     log .error (errorMessage );
520-                     eventResponse .put (errorMessage , HttpStatus .BAD_REQUEST );
521-                 }
522-                 String  validResponseBody  = validResponse .toString ();
521+             // TODO Could it be null? 
522+             JsonObject  jsonResponseEvent  = eventArray .get (i ).getAsJsonObject ();
523+             ValidationResult  validationResult  = msgService .validateMsg (msgType , jsonResponseEvent , okToLeaveOutInvalidOptionalFields );
524+             if  (validationResult .isValid ()) {
525+                 String  validResponseBody  = jsonResponseEvent .toString ();
523526                SendResult  result  = messageService .send (validResponseBody , msgService , userDomain , tag , routingKey );
524527                eventResponse .put (JSON_STATUS_RESULT , result );
525528            } else  {
@@ -540,18 +543,6 @@ public JsonObject getAsJsonObject(JsonElement jsonElement) {
540543        return  jsonObject ;
541544    }
542545
543-     /** 
544-      * This helper method check the json event is valid or not 
545-      * @param jsonElement An event which need to check 
546-      * @return boolean type value like it is valid or not 
547-      */ 
548-     private  boolean  isValid (JsonElement  jsonElement ) {
549-         JsonObject  jsonObject  = getAsJsonObject (jsonElement );
550-         return  jsonObject  != null  &&
551-             jsonObject .has (META ) && jsonObject .has (DATA ) && jsonObject .has (LINKS ) &&
552-             !jsonObject .has (JSON_STATUS_CODE );
553-     }
554- 
555546    /** 
556547     * @return this method returns the current version of publish and all loaded 
557548     *         protocols. 
@@ -563,5 +554,4 @@ public JsonElement getVersions() {
563554        Map <String , Map <String , String >> versions  = new  VersionService ().getMessagingVersions ();
564555        return  parser .parse (versions .toString ());
565556    }
566- 
567557}
0 commit comments