@@ -77,10 +77,10 @@ public class RemremGenerateController {
77
77
private boolean lenientValidationEnabledToUsers ;
78
78
79
79
public void setLenientValidationEnabledToUsers (boolean lenientValidationEnabledToUsers ) {
80
- this .lenientValidationEnabledToUsers = lenientValidationEnabledToUsers ;
81
- }
80
+ this .lenientValidationEnabledToUsers = lenientValidationEnabledToUsers ;
81
+ }
82
82
83
- private static RestTemplate restTemplate = new RestTemplate ();
83
+ private static RestTemplate restTemplate = new RestTemplate ();
84
84
85
85
public void setRestTemplate (RestTemplate restTemplate ) {
86
86
this .restTemplate = restTemplate ;
@@ -114,9 +114,7 @@ public ResponseEntity<?> generate(@ApiParam(value = "message protocol", required
114
114
@ ApiParam (value = RemremGenerateServiceConstants .LOOKUP_LIMIT ) @ RequestParam (value = "lookupLimit" , required = false , defaultValue = "1" ) final int lookupLimit ,
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
- JsonObject errorResponse = null ;
118
117
try {
119
- errorResponse = new JsonObject ();
120
118
JsonFactory jsonFactory = JsonFactory .builder ().build ().enable (com .fasterxml .jackson .core .JsonParser .Feature .STRICT_DUPLICATE_DETECTION );
121
119
ObjectMapper mapper = new ObjectMapper (jsonFactory );
122
120
JsonNode node = mapper .readTree (body );
@@ -127,8 +125,8 @@ public ResponseEntity<?> generate(@ApiParam(value = "message protocol", required
127
125
} catch (JsonSyntaxException | JsonProcessingException e ) {
128
126
String exceptionMessage = e .getMessage ();
129
127
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" );
132
130
}
133
131
}
134
132
@@ -149,14 +147,13 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
149
147
final Boolean okToLeaveOutInvalidOptionalFields , JsonElement inputData ) {
150
148
151
149
JsonArray generatedEventResults = new JsonArray ();
152
- JsonObject errorResponse = new JsonObject ();
153
150
try {
154
151
if (lookupLimit <= 0 ) {
155
152
return new ResponseEntity <>("Parameter 'lookupLimit' must be > 0" , HttpStatus .BAD_REQUEST );
156
153
}
157
154
if (inputData == null ) {
158
155
return createResponseEntity (HttpStatus .BAD_REQUEST , JSON_ERROR_STATUS ,
159
- "Parameter 'inputData' must not be null" , errorResponse );
156
+ "Parameter 'inputData' must not be null" );
160
157
}
161
158
162
159
if (inputData .isJsonArray ()) {
@@ -178,25 +175,29 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
178
175
JsonObject inputJsonObject = inputData .getAsJsonObject ();
179
176
JsonObject processedJson = processEvent (msgProtocol , msgType , failIfMultipleFound , failIfNoneFound ,
180
177
lookupInExternalERs , lookupLimit , okToLeaveOutInvalidOptionalFields , inputJsonObject );
181
-
182
178
HttpStatus status = null ;
183
179
if (processedJson .has (META )) {
184
180
status = HttpStatus .OK ;
185
181
return new ResponseEntity <>(processedJson , status );
186
182
} else if (processedJson .has (JSON_STATUS_CODE )) {
187
183
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
+
190
187
} else {
191
188
return new ResponseEntity <>(processedJson , HttpStatus .SERVICE_UNAVAILABLE );
192
189
}
193
190
194
191
} else {
195
192
return createResponseEntity (HttpStatus .BAD_REQUEST ,
196
193
"Invalid JSON format,expected either single template or array of templates" ,
197
- JSON_ERROR_STATUS , errorResponse );
194
+ JSON_ERROR_STATUS );
198
195
}
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 ) {
200
201
return handleException (e );
201
202
}
202
203
}
@@ -215,6 +216,9 @@ public ResponseEntity<JsonObject> createResponseEntity(HttpStatus status, String
215
216
initializeResponse (status , responseMessage , resultMessage , errorResponse );
216
217
return new ResponseEntity <>(errorResponse , status );
217
218
}
219
+ public ResponseEntity <JsonObject > createResponseEntity (HttpStatus status , String responseMessage , String resultMessage ) {
220
+ return createResponseEntity (status , responseMessage , resultMessage , new JsonObject ());
221
+ }
218
222
219
223
public void initializeResponse (HttpStatus status , String errorMessage , String resultMessage ,
220
224
JsonObject errorResponse ) {
@@ -229,7 +233,6 @@ public void initializeResponse(HttpStatus status, String errorMessage, String re
229
233
* @return ResponseEntity
230
234
*/
231
235
private ResponseEntity <JsonObject > handleException (Exception e ) {
232
- JsonObject errorResponse = new JsonObject ();
233
236
String exceptionMessage = e .getMessage ();
234
237
if (e instanceof REMGenerateException ) {
235
238
List <HttpStatus > statuses = List .of (
@@ -238,19 +241,19 @@ private ResponseEntity<JsonObject> handleException(Exception e) {
238
241
for (HttpStatus status : statuses ) {
239
242
if (exceptionMessage .contains (Integer .toString (status .value ()))) {
240
243
return createResponseEntity (
241
- status , e .getMessage (), JSON_ERROR_STATUS , errorResponse );
244
+ status , e .getMessage (), JSON_ERROR_STATUS );
242
245
}
243
246
}
244
- return createResponseEntity (HttpStatus .BAD_REQUEST , exceptionMessage , JSON_ERROR_STATUS , errorResponse );
247
+ return createResponseEntity (HttpStatus .BAD_REQUEST , exceptionMessage , JSON_ERROR_STATUS );
245
248
} else if (e instanceof JsonSyntaxException ) {
246
249
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) {
249
252
log.error("Invalid number format", exceptionMessage);
250
253
return createResponseEntity(HttpStatus.BAD_REQUEST, exceptionMessage, JSON_ERROR_STATUS, errorResponse);
251
- } else {
254
+ }*/ else {
252
255
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 );
254
257
}
255
258
}
256
259
@@ -270,21 +273,21 @@ private ResponseEntity<JsonObject> handleException(Exception e) {
270
273
public JsonObject processEvent (String msgProtocol , String msgType , Boolean failIfMultipleFound ,
271
274
Boolean failIfNoneFound , Boolean lookupInExternalERs , int lookupLimit ,
272
275
Boolean okToLeaveOutInvalidOptionalFields , JsonObject jsonObject ) throws REMGenerateException , JsonSyntaxException {
273
- JsonObject eventResponse = new JsonObject ();
274
276
JsonElement parsedResponse ;
275
277
276
278
JsonObject event = erLookup (jsonObject , failIfMultipleFound , failIfNoneFound , lookupInExternalERs , lookupLimit );
277
279
MsgService msgService = getMessageService (msgProtocol );
278
280
279
281
if (msgService == null ) {
280
282
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 ();
282
284
}
283
285
String response = msgService .generateMsg (msgType , event , isLenientEnabled (okToLeaveOutInvalidOptionalFields ));
284
286
parsedResponse = JsonParser .parseString (response );
285
287
JsonObject parsedJson = parsedResponse .getAsJsonObject ();
286
288
287
289
if (parsedJson .has (JSON_ERROR_MESSAGE_FIELD )) {
290
+ JsonObject eventResponse = new JsonObject ();
288
291
createResponseEntity (HttpStatus .BAD_REQUEST , JSON_ERROR_STATUS , TEMPLATE_ERROR , eventResponse );
289
292
return eventResponse ;
290
293
} else {
0 commit comments