@@ -115,7 +115,8 @@ public ResponseEntity<?> generate(@ApiParam(value = "message protocol", required
115
115
@ ApiParam (value = RemremGenerateServiceConstants .LenientValidation ) @ RequestParam (value = "okToLeaveOutInvalidOptionalFields" , required = false , defaultValue = "false" ) final Boolean okToLeaveOutInvalidOptionalFields ,
116
116
@ ApiParam (value = "JSON message" , required = true ) @ RequestBody String body ) {
117
117
try {
118
- JsonFactory jsonFactory = JsonFactory .builder ().build ().enable (com .fasterxml .jackson .core .JsonParser .Feature .STRICT_DUPLICATE_DETECTION );
118
+ JsonFactory jsonFactory = JsonFactory .builder ().build ().enable (com .fasterxml .jackson .core .JsonParser
119
+ .Feature .STRICT_DUPLICATE_DETECTION );
119
120
ObjectMapper mapper = new ObjectMapper (jsonFactory );
120
121
JsonNode node = mapper .readTree (body );
121
122
Gson gson = new Gson ();
@@ -126,7 +127,7 @@ public ResponseEntity<?> generate(@ApiParam(value = "message protocol", required
126
127
String exceptionMessage = e .getMessage ();
127
128
log .error ("Invalid JSON parse data format due to" , e .getMessage ());
128
129
return createResponseEntity (HttpStatus .BAD_REQUEST , "Invalid JSON parse data format due to: "
129
- + exceptionMessage , "fatal" );
130
+ + exceptionMessage , JSON_FATAL_STATUS );
130
131
}
131
132
}
132
133
@@ -152,8 +153,8 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
152
153
return new ResponseEntity <>("Parameter 'lookupLimit' must be > 0" , HttpStatus .BAD_REQUEST );
153
154
}
154
155
if (inputData == null ) {
155
- return createResponseEntity (HttpStatus .BAD_REQUEST , JSON_ERROR_STATUS ,
156
- "Parameter 'inputData' must not be null" );
156
+ return createResponseEntity (HttpStatus .BAD_REQUEST , "Parameter 'inputData' must not be null" ,
157
+ JSON_ERROR_STATUS );
157
158
}
158
159
159
160
if (inputData .isJsonArray ()) {
@@ -175,28 +176,31 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
175
176
JsonObject inputJsonObject = inputData .getAsJsonObject ();
176
177
JsonObject processedJson = processEvent (msgProtocol , msgType , failIfMultipleFound , failIfNoneFound ,
177
178
lookupInExternalERs , lookupLimit , okToLeaveOutInvalidOptionalFields , inputJsonObject );
178
- HttpStatus status = null ;
179
+ HttpStatus status ;
180
+ String statusValue = null ;
179
181
if (processedJson .has (META )) {
180
182
status = HttpStatus .OK ;
181
183
return new ResponseEntity <>(processedJson , status );
182
184
} else if (processedJson .has (JSON_STATUS_CODE )) {
183
- String statusValue = processedJson .get (JSON_STATUS_CODE ).toString ();
185
+ statusValue = processedJson .get (JSON_STATUS_CODE ).toString ();
186
+ try {
184
187
status = HttpStatus .resolve (Integer .parseInt (statusValue ));
185
188
return new ResponseEntity <>(processedJson , status );
186
-
189
+ } catch (NumberFormatException e ) {
190
+ log .error ("Invalid status value: '" + statusValue + "' of response " + processedJson );
191
+ return createResponseEntity (HttpStatus .BAD_REQUEST , "Invalid status value: '"
192
+ + statusValue + "' of response " + processedJson , JSON_ERROR_STATUS );
193
+ }
187
194
} else {
188
- return new ResponseEntity <>(processedJson , HttpStatus .SERVICE_UNAVAILABLE );
195
+ log .error ("There is no status value: '" + statusValue + "' in the response" + processedJson );
196
+ return createResponseEntity (HttpStatus .INTERNAL_SERVER_ERROR , "There is no status value: '"
197
+ + statusValue + "' in the response " + processedJson , JSON_ERROR_STATUS );
189
198
}
190
-
191
199
} else {
192
200
return createResponseEntity (HttpStatus .BAD_REQUEST ,
193
201
"Invalid JSON format,expected either single template or array of templates" ,
194
202
JSON_ERROR_STATUS );
195
203
}
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
204
} catch (REMGenerateException | JsonSyntaxException e ) {
201
205
return handleException (e );
202
206
}
@@ -220,8 +224,7 @@ public ResponseEntity<JsonObject> createResponseEntity(HttpStatus status, String
220
224
return createResponseEntity (status , responseMessage , resultMessage , new JsonObject ());
221
225
}
222
226
223
- public void initializeResponse (HttpStatus status , String errorMessage , String resultMessage ,
224
- JsonObject errorResponse ) {
227
+ public void initializeResponse (HttpStatus status , String errorMessage , String resultMessage , JsonObject errorResponse ) {
225
228
errorResponse .addProperty (JSON_STATUS_CODE , status .value ());
226
229
errorResponse .addProperty (JSON_STATUS_RESULT , resultMessage );
227
230
errorResponse .addProperty (JSON_ERROR_MESSAGE_FIELD , errorMessage );
@@ -235,23 +238,20 @@ public void initializeResponse(HttpStatus status, String errorMessage, String re
235
238
private ResponseEntity <JsonObject > handleException (Exception e ) {
236
239
String exceptionMessage = e .getMessage ();
237
240
if (e instanceof REMGenerateException ) {
238
- List <HttpStatus > statuses = List .of (
239
- HttpStatus .NOT_ACCEPTABLE , HttpStatus .EXPECTATION_FAILED , HttpStatus .SERVICE_UNAVAILABLE , HttpStatus .UNPROCESSABLE_ENTITY
241
+ List <HttpStatus > statuseList = List .of (
242
+ HttpStatus .NOT_ACCEPTABLE , HttpStatus .EXPECTATION_FAILED , HttpStatus .SERVICE_UNAVAILABLE ,
243
+ HttpStatus .UNPROCESSABLE_ENTITY
240
244
);
241
- for (HttpStatus status : statuses ) {
245
+ for (HttpStatus status : statuseList ) {
242
246
if (exceptionMessage .contains (Integer .toString (status .value ()))) {
243
- return createResponseEntity (
244
- status , e .getMessage (), JSON_ERROR_STATUS );
247
+ return createResponseEntity (status , exceptionMessage , JSON_ERROR_STATUS );
245
248
}
246
249
}
247
250
return createResponseEntity (HttpStatus .BAD_REQUEST , exceptionMessage , JSON_ERROR_STATUS );
248
251
} else if (e instanceof JsonSyntaxException ) {
249
252
log .error ("Failed to parse JSON" , exceptionMessage );
250
253
return createResponseEntity (HttpStatus .BAD_REQUEST , exceptionMessage , JSON_ERROR_STATUS );
251
- } /*else if (e instanceof NumberFormatException) {
252
- log.error("Invalid number format", exceptionMessage);
253
- return createResponseEntity(HttpStatus.BAD_REQUEST, exceptionMessage, JSON_ERROR_STATUS, errorResponse);
254
- }*/ else {
254
+ } else {
255
255
log .error ("Unexpected exception caught" , exceptionMessage );
256
256
return createResponseEntity (HttpStatus .INTERNAL_SERVER_ERROR , exceptionMessage , JSON_ERROR_STATUS );
257
257
}
@@ -279,8 +279,8 @@ public JsonObject processEvent(String msgProtocol, String msgType, Boolean failI
279
279
MsgService msgService = getMessageService (msgProtocol );
280
280
281
281
if (msgService == null ) {
282
- return createResponseEntity (HttpStatus .SERVICE_UNAVAILABLE , JSON_ERROR_STATUS ,
283
- "No protocol service has been found registered" ).getBody ();
282
+ return createResponseEntity (HttpStatus .SERVICE_UNAVAILABLE ,
283
+ "No protocol service has been found registered" , JSON_ERROR_STATUS ).getBody ();
284
284
}
285
285
String response = msgService .generateMsg (msgType , event , isLenientEnabled (okToLeaveOutInvalidOptionalFields ));
286
286
parsedResponse = JsonParser .parseString (response );
0 commit comments