@@ -77,10 +77,10 @@ public class RemremGenerateController {
7777 private boolean lenientValidationEnabledToUsers ;
7878
7979 public void setLenientValidationEnabledToUsers (boolean lenientValidationEnabledToUsers ) {
80- this .lenientValidationEnabledToUsers = lenientValidationEnabledToUsers ;
81- }
80+ this .lenientValidationEnabledToUsers = lenientValidationEnabledToUsers ;
81+ }
8282
83- private static RestTemplate restTemplate = new RestTemplate ();
83+ private static RestTemplate restTemplate = new RestTemplate ();
8484
8585 public void setRestTemplate (RestTemplate restTemplate ) {
8686 this .restTemplate = restTemplate ;
@@ -114,9 +114,7 @@ public ResponseEntity<?> generate(@ApiParam(value = "message protocol", required
114114 @ ApiParam (value = RemremGenerateServiceConstants .LOOKUP_LIMIT ) @ RequestParam (value = "lookupLimit" , required = false , defaultValue = "1" ) final int lookupLimit ,
115115 @ ApiParam (value = RemremGenerateServiceConstants .LenientValidation ) @ RequestParam (value = "okToLeaveOutInvalidOptionalFields" , required = false , defaultValue = "false" ) final Boolean okToLeaveOutInvalidOptionalFields ,
116116 @ ApiParam (value = "JSON message" , required = true ) @ RequestBody String body ) {
117- JsonObject errorResponse = null ;
118117 try {
119- errorResponse = new JsonObject ();
120118 JsonFactory jsonFactory = JsonFactory .builder ().build ().enable (com .fasterxml .jackson .core .JsonParser .Feature .STRICT_DUPLICATE_DETECTION );
121119 ObjectMapper mapper = new ObjectMapper (jsonFactory );
122120 JsonNode node = mapper .readTree (body );
@@ -127,8 +125,8 @@ public ResponseEntity<?> generate(@ApiParam(value = "message protocol", required
127125 } catch (JsonSyntaxException | JsonProcessingException e ) {
128126 String exceptionMessage = e .getMessage ();
129127 log .error ("Invalid JSON parse data format due to" , e .getMessage ());
130- return createResponseEntity (HttpStatus .BAD_REQUEST , "Invalid JSON parse data format due to: " + exceptionMessage , "fatal" ,
131- errorResponse );
128+ return createResponseEntity (HttpStatus .BAD_REQUEST , "Invalid JSON parse data format due to: "
129+ + exceptionMessage , "fatal" );
132130 }
133131 }
134132
@@ -149,14 +147,13 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
149147 final Boolean okToLeaveOutInvalidOptionalFields , JsonElement inputData ) {
150148
151149 JsonArray generatedEventResults = new JsonArray ();
152- JsonObject errorResponse = new JsonObject ();
153150 try {
154151 if (lookupLimit <= 0 ) {
155152 return new ResponseEntity <>("Parameter 'lookupLimit' must be > 0" , HttpStatus .BAD_REQUEST );
156153 }
157154 if (inputData == null ) {
158155 return createResponseEntity (HttpStatus .BAD_REQUEST , JSON_ERROR_STATUS ,
159- "Parameter 'inputData' must not be null" , errorResponse );
156+ "Parameter 'inputData' must not be null" );
160157 }
161158
162159 if (inputData .isJsonArray ()) {
@@ -178,25 +175,29 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
178175 JsonObject inputJsonObject = inputData .getAsJsonObject ();
179176 JsonObject processedJson = processEvent (msgProtocol , msgType , failIfMultipleFound , failIfNoneFound ,
180177 lookupInExternalERs , lookupLimit , okToLeaveOutInvalidOptionalFields , inputJsonObject );
181-
182178 HttpStatus status = null ;
183179 if (processedJson .has (META )) {
184180 status = HttpStatus .OK ;
185181 return new ResponseEntity <>(processedJson , status );
186182 } else if (processedJson .has (JSON_STATUS_CODE )) {
187183 String statusValue = processedJson .get (JSON_STATUS_CODE ).toString ();
188- status = HttpStatus .resolve (Integer .parseInt (statusValue ));
189- return new ResponseEntity <>(processedJson , status );
184+ status = HttpStatus .resolve (Integer .parseInt (statusValue ));
185+ return new ResponseEntity <>(processedJson , status );
186+
190187 } else {
191188 return new ResponseEntity <>(processedJson , HttpStatus .SERVICE_UNAVAILABLE );
192189 }
193190
194191 } else {
195192 return createResponseEntity (HttpStatus .BAD_REQUEST ,
196193 "Invalid JSON format,expected either single template or array of templates" ,
197- JSON_ERROR_STATUS , errorResponse );
194+ JSON_ERROR_STATUS );
198195 }
199- } catch (REMGenerateException | JsonSyntaxException | NumberFormatException e ) {
196+ } catch (NumberFormatException e ){
197+ String exceptionMessage = e .getMessage ();
198+ log .error ("Invalid number format" , exceptionMessage );
199+ return new ResponseEntity <>(exceptionMessage , HttpStatus .BAD_REQUEST );
200+ } catch (REMGenerateException | JsonSyntaxException e ) {
200201 return handleException (e );
201202 }
202203 }
@@ -215,6 +216,9 @@ public ResponseEntity<JsonObject> createResponseEntity(HttpStatus status, String
215216 initializeResponse (status , responseMessage , resultMessage , errorResponse );
216217 return new ResponseEntity <>(errorResponse , status );
217218 }
219+ public ResponseEntity <JsonObject > createResponseEntity (HttpStatus status , String responseMessage , String resultMessage ) {
220+ return createResponseEntity (status , responseMessage , resultMessage , new JsonObject ());
221+ }
218222
219223 public void initializeResponse (HttpStatus status , String errorMessage , String resultMessage ,
220224 JsonObject errorResponse ) {
@@ -229,7 +233,6 @@ public void initializeResponse(HttpStatus status, String errorMessage, String re
229233 * @return ResponseEntity
230234 */
231235 private ResponseEntity <JsonObject > handleException (Exception e ) {
232- JsonObject errorResponse = new JsonObject ();
233236 String exceptionMessage = e .getMessage ();
234237 if (e instanceof REMGenerateException ) {
235238 List <HttpStatus > statuses = List .of (
@@ -238,19 +241,19 @@ private ResponseEntity<JsonObject> handleException(Exception e) {
238241 for (HttpStatus status : statuses ) {
239242 if (exceptionMessage .contains (Integer .toString (status .value ()))) {
240243 return createResponseEntity (
241- status , e .getMessage (), JSON_ERROR_STATUS , errorResponse );
244+ status , e .getMessage (), JSON_ERROR_STATUS );
242245 }
243246 }
244- return createResponseEntity (HttpStatus .BAD_REQUEST , exceptionMessage , JSON_ERROR_STATUS , errorResponse );
247+ return createResponseEntity (HttpStatus .BAD_REQUEST , exceptionMessage , JSON_ERROR_STATUS );
245248 } else if (e instanceof JsonSyntaxException ) {
246249 log .error ("Failed to parse JSON" , exceptionMessage );
247- return createResponseEntity (HttpStatus .BAD_REQUEST , exceptionMessage , JSON_ERROR_STATUS , errorResponse );
248- } else if (e instanceof NumberFormatException ) {
250+ return createResponseEntity (HttpStatus .BAD_REQUEST , exceptionMessage , JSON_ERROR_STATUS );
251+ } /* else if (e instanceof NumberFormatException) {
249252 log.error("Invalid number format", exceptionMessage);
250253 return createResponseEntity(HttpStatus.BAD_REQUEST, exceptionMessage, JSON_ERROR_STATUS, errorResponse);
251- } else {
254+ }*/ else {
252255 log .error ("Unexpected exception caught" , exceptionMessage );
253- return createResponseEntity (HttpStatus .INTERNAL_SERVER_ERROR , exceptionMessage , JSON_ERROR_STATUS , errorResponse );
256+ return createResponseEntity (HttpStatus .INTERNAL_SERVER_ERROR , exceptionMessage , JSON_ERROR_STATUS );
254257 }
255258 }
256259
@@ -270,21 +273,21 @@ private ResponseEntity<JsonObject> handleException(Exception e) {
270273 public JsonObject processEvent (String msgProtocol , String msgType , Boolean failIfMultipleFound ,
271274 Boolean failIfNoneFound , Boolean lookupInExternalERs , int lookupLimit ,
272275 Boolean okToLeaveOutInvalidOptionalFields , JsonObject jsonObject ) throws REMGenerateException , JsonSyntaxException {
273- JsonObject eventResponse = new JsonObject ();
274276 JsonElement parsedResponse ;
275277
276278 JsonObject event = erLookup (jsonObject , failIfMultipleFound , failIfNoneFound , lookupInExternalERs , lookupLimit );
277279 MsgService msgService = getMessageService (msgProtocol );
278280
279281 if (msgService == null ) {
280282 return createResponseEntity (HttpStatus .SERVICE_UNAVAILABLE , JSON_ERROR_STATUS ,
281- "No protocol service has been found registered" , eventResponse ).getBody ();
283+ "No protocol service has been found registered" ).getBody ();
282284 }
283285 String response = msgService .generateMsg (msgType , event , isLenientEnabled (okToLeaveOutInvalidOptionalFields ));
284286 parsedResponse = JsonParser .parseString (response );
285287 JsonObject parsedJson = parsedResponse .getAsJsonObject ();
286288
287289 if (parsedJson .has (JSON_ERROR_MESSAGE_FIELD )) {
290+ JsonObject eventResponse = new JsonObject ();
288291 createResponseEntity (HttpStatus .BAD_REQUEST , JSON_ERROR_STATUS , TEMPLATE_ERROR , eventResponse );
289292 return eventResponse ;
290293 } else {
0 commit comments