@@ -88,30 +88,32 @@ public void setRestTemplate(RestTemplate restTemplate) {
88
88
89
89
/**
90
90
* Returns event information as json element based on the message protocol,
91
- * taking message type and json body as input.
91
+ * taking message type and json body of string type as input because just to parse
92
+ * the string in to JsonElement not using JsonElement directly here.
93
+ *
92
94
* <p>
93
95
* <p>
94
96
* Parameters: msgProtocol - The message protocol, which tells us which
95
97
* service to invoke. msgType - The type of message that needs to be
96
- * generated. bodyJson - The content of the message which is used in
98
+ * generated. body - The content of the message which is used in
97
99
* creating the event details.
98
100
* <p>
99
101
* Returns: The event information as a json element
100
102
*/
101
103
102
104
@ ApiOperation (value = "To generate eiffel event based on the message protocol" , response = String .class )
103
- @ ApiResponses (value = { @ ApiResponse (code = 200 , message = "Event sent successfully" ),
105
+ @ ApiResponses (value = {@ ApiResponse (code = 200 , message = "Event sent successfully" ),
104
106
@ ApiResponse (code = 400 , message = "Malformed JSON" ),
105
107
@ ApiResponse (code = 500 , message = "Internal server error" ),
106
- @ ApiResponse (code = 503 , message = "Message protocol is invalid" ) })
108
+ @ ApiResponse (code = 503 , message = "Message protocol is invalid" )})
107
109
@ RequestMapping (value = "/{mp" + REGEX + "}" , method = RequestMethod .POST )
108
110
public ResponseEntity <?> generate (@ ApiParam (value = "message protocol" , required = true ) @ PathVariable ("mp" ) final String msgProtocol ,
109
111
@ ApiParam (value = "message type" , required = true ) @ RequestParam ("msgType" ) final String msgType ,
110
112
@ ApiParam (value = "ER lookup result multiple found, Generate will fail" ) @ RequestParam (value = "failIfMultipleFound" , required = false , defaultValue = "false" ) final Boolean failIfMultipleFound ,
111
113
@ ApiParam (value = "ER lookup result none found, Generate will fail" ) @ RequestParam (value = "failIfNoneFound" , required = false , defaultValue = "false" ) final Boolean failIfNoneFound ,
112
114
@ ApiParam (value = RemremGenerateServiceConstants .LOOKUP_IN_EXTERNAL_ERS ) @ RequestParam (value = "lookupInExternalERs" , required = false , defaultValue = "false" ) final Boolean lookupInExternalERs ,
113
115
@ ApiParam (value = RemremGenerateServiceConstants .LOOKUP_LIMIT ) @ RequestParam (value = "lookupLimit" , required = false , defaultValue = "1" ) final int lookupLimit ,
114
- @ ApiParam (value = RemremGenerateServiceConstants .LenientValidation ) @ RequestParam (value = "okToLeaveOutInvalidOptionalFields" , required = false , defaultValue = "false" ) final Boolean okToLeaveOutInvalidOptionalFields ,
116
+ @ ApiParam (value = RemremGenerateServiceConstants .LenientValidation ) @ RequestParam (value = "okToLeaveOutInvalidOptionalFields" , required = false , defaultValue = "false" ) final Boolean okToLeaveOutInvalidOptionalFields ,
115
117
@ ApiParam (value = "JSON message" , required = true ) @ RequestBody String body ) {
116
118
JsonObject errorResponse = null ;
117
119
try {
@@ -123,32 +125,43 @@ public ResponseEntity<?> generate(@ApiParam(value = "message protocol", required
123
125
JsonElement inputJson = gson .fromJson (node .toString (), JsonElement .class );
124
126
return generate (msgProtocol , msgType , failIfMultipleFound , failIfNoneFound , lookupInExternalERs ,
125
127
lookupLimit , okToLeaveOutInvalidOptionalFields , inputJson );
126
- } catch (JsonSyntaxException e ) {
128
+ } catch (JsonSyntaxException | JsonProcessingException e ) {
127
129
String exceptionMessage = e .getMessage ();
128
130
log .error ("Invalid JSON parse data format due to:" , e .getMessage ());
129
- return createResponseEntity (HttpStatus .BAD_REQUEST ,"Invalid JSON parse data format due to: " + exceptionMessage , "fatal" ,
130
- errorResponse );
131
- } catch (JsonProcessingException e ) {
132
- String exceptionMessage = e .getMessage ();
133
- log .info ("Incorrect Json data" , exceptionMessage );
134
- return createResponseEntity (HttpStatus .BAD_REQUEST ,"Incorrect Json data: " + exceptionMessage , "fatal" ,
131
+ return createResponseEntity (HttpStatus .BAD_REQUEST , "Invalid JSON parse data format due to: " + exceptionMessage , "fatal" ,
135
132
errorResponse );
136
133
}
137
134
}
138
135
139
- public ResponseEntity <?> generate (final String msgProtocol , final String msgType , final Boolean failIfMultipleFound , final Boolean failIfNoneFound , final Boolean lookupInExternalERs , final int lookupLimit , final Boolean okToLeaveOutInvalidOptionalFields , JsonElement inputData ) {
136
+ /**
137
+ * Returns event information as json element based on the message protocol,
138
+ * taking message type and json body as input
139
+ * Here we basically add this to handle if inputData is of jsonArray type as well
140
+ *
141
+ * <p>
142
+ * <p>
143
+ * Parameters: msgProtocol - The message protocol, which tells us which
144
+ * service to invoke. msgType - The type of message that needs to be
145
+ * generated. inputData - The content of the message which is used in
146
+ * creating the event details.
147
+ * <p>
148
+ * Returns: The event information as a json element
149
+ */
150
+ public ResponseEntity <?> generate (final String msgProtocol , final String msgType , final Boolean failIfMultipleFound ,
151
+ final Boolean failIfNoneFound , final Boolean lookupInExternalERs , final int lookupLimit ,
152
+ final Boolean okToLeaveOutInvalidOptionalFields , JsonElement inputData ) {
140
153
141
154
JsonArray generatedEventResults = new JsonArray ();
142
155
JsonObject errorResponse = new JsonObject ();
143
156
try {
144
157
if (lookupLimit <= 0 ) {
145
- return new ResponseEntity <>("LookupLimit must be greater than or equals to 1 " , HttpStatus .BAD_REQUEST );
158
+ return new ResponseEntity <>("Parameter 'lookupLimit' must be > 0 " , HttpStatus .BAD_REQUEST );
146
159
}
147
160
if (inputData == null ) {
148
- log .error ("Json event must not be null" );
149
- errorResponse .addProperty (JSON_ERROR_MESSAGE_FIELD , "inputData must not be null" );
150
- return new ResponseEntity <>(errorResponse , HttpStatus .BAD_REQUEST );
161
+ createResponseEntity (HttpStatus .BAD_REQUEST , JSON_ERROR_STATUS ,
162
+ "Parameter 'inputData' must not be null" , errorResponse );
151
163
}
164
+
152
165
if (inputData .isJsonArray ()) {
153
166
JsonArray inputEventJsonArray = inputData .getAsJsonArray ();
154
167
for (JsonElement element : inputEventJsonArray ) {
@@ -169,27 +182,40 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
169
182
JsonObject processedJson = processEvent (msgProtocol , msgType , failIfMultipleFound , failIfNoneFound ,
170
183
lookupInExternalERs , lookupLimit , okToLeaveOutInvalidOptionalFields , inputJsonObject );
171
184
185
+ HttpStatus status =null ;
172
186
if (processedJson .has (META )) {
173
- return new ResponseEntity <>(processedJson , HttpStatus .OK );
174
- }
175
- if (processedJson .has (JSON_STATUS_CODE ) && "400" .equals (processedJson .get (JSON_STATUS_CODE ).toString ())) {
176
- return new ResponseEntity <>(processedJson , HttpStatus .BAD_REQUEST );
187
+ status = HttpStatus .OK ;
188
+ return new ResponseEntity <>(processedJson , status );
189
+ } else if (processedJson .has (JSON_STATUS_CODE )) {
190
+ String statusValue = processedJson .get (JSON_STATUS_CODE ).toString ();
191
+ status = HttpStatus .resolve (Integer .parseInt (statusValue ));
192
+ return new ResponseEntity <>(processedJson , status );
177
193
} else {
178
194
return new ResponseEntity <>(processedJson , HttpStatus .SERVICE_UNAVAILABLE );
179
195
}
180
196
181
197
} else {
182
- return createResponseEntity (HttpStatus .BAD_REQUEST , "Invalid JSON format,expected either single template or array of templates" ,
183
- "fail" , errorResponse );
198
+ return createResponseEntity (HttpStatus .BAD_REQUEST ,
199
+ "Invalid JSON format,expected either single template or array of templates" ,
200
+ JSON_ERROR_STATUS , errorResponse );
184
201
}
185
202
} catch (REMGenerateException | JsonSyntaxException e ) {
186
203
return handleException (e );
187
204
}
188
205
}
189
206
190
- public ResponseEntity <JsonObject > createResponseEntity (HttpStatus status , String errorMessage , String resultMessage ,
207
+
208
+ /**
209
+ * To display response in browser or application
210
+ * @param status response code for the HTTP request
211
+ * @param responseMessage the message according to response
212
+ * @param resultMessage whatever the result this message gives you idea about that
213
+ * @param errorResponse is to collect all the responses here.
214
+ * @return ResponseEntity
215
+ */
216
+ public ResponseEntity <JsonObject > createResponseEntity (HttpStatus status , String responseMessage , String resultMessage ,
191
217
JsonObject errorResponse ) {
192
- initializeResponse (status , errorMessage , resultMessage , errorResponse );
218
+ initializeResponse (status , responseMessage , resultMessage , errorResponse );
193
219
return new ResponseEntity <>(errorResponse , status );
194
220
}
195
221
@@ -200,6 +226,12 @@ public void initializeResponse(HttpStatus status, String errorMessage, String re
200
226
errorResponse .addProperty (JSON_ERROR_MESSAGE_FIELD , errorMessage );
201
227
}
202
228
229
+ /**
230
+ * To handle the exception in one method
231
+ * @param e taken general exception here
232
+ * @return ResponseEntity
233
+ */
234
+
203
235
private ResponseEntity <JsonObject > handleException (Exception e ) {
204
236
JsonObject errorResponse = new JsonObject ();
205
237
String exceptionMessage = e .getMessage ();
@@ -210,21 +242,29 @@ private ResponseEntity<JsonObject> handleException(Exception e) {
210
242
for (HttpStatus status : statuses ) {
211
243
if (exceptionMessage .contains (Integer .toString (status .value ()))) {
212
244
return createResponseEntity (
213
- status , e .getMessage (), "fail" , errorResponse );
245
+ status , e .getMessage (), JSON_ERROR_STATUS , errorResponse );
214
246
}
215
247
}
216
- return createResponseEntity (HttpStatus .BAD_REQUEST , e .getMessage (), "fail" , errorResponse );
248
+ return createResponseEntity (HttpStatus .BAD_REQUEST , e .getMessage (), JSON_ERROR_STATUS , errorResponse );
217
249
} else if (e instanceof JsonSyntaxException ) {
218
250
log .error ("Failed to parse JSON: " , exceptionMessage );
219
- return createResponseEntity (HttpStatus .BAD_REQUEST , e .getMessage (), "fail" , errorResponse );
251
+ return createResponseEntity (HttpStatus .BAD_REQUEST , e .getMessage (), JSON_ERROR_STATUS , errorResponse );
220
252
} else {
221
253
log .error ("Unexpected exception caught" , exceptionMessage );
222
- return createResponseEntity (HttpStatus .INTERNAL_SERVER_ERROR , exceptionMessage , "fail" , errorResponse );
254
+ return createResponseEntity (HttpStatus .INTERNAL_SERVER_ERROR , exceptionMessage , JSON_ERROR_STATUS , errorResponse );
223
255
}
224
256
}
225
257
226
258
/**
227
- * This helper method basically generate or process one event
259
+ * This helper method basically generate or process single event
260
+ * @param msgProtocol The message protocol, which tells us which service to invoke
261
+ * @param msgType The type of message that needs to be generated. inputData
262
+ * @param failIfMultipleFound
263
+ * @param failIfNoneFound
264
+ * @param lookupInExternalERs
265
+ * @param lookupLimit
266
+ * @param okToLeaveOutInvalidOptionalFields
267
+ * @param jsonObject The content of the message which is used in creating the event details.
228
268
* @return JsonObject generated event
229
269
*/
230
270
@@ -237,19 +277,20 @@ public JsonObject processEvent(String msgProtocol, String msgType, Boolean failI
237
277
JsonObject event = erLookup (jsonObject , failIfMultipleFound , failIfNoneFound , lookupInExternalERs , lookupLimit );
238
278
MsgService msgService = getMessageService (msgProtocol );
239
279
240
- if (msgService != null ) {
241
- String response = msgService .generateMsg (msgType , event , isLenientEnabled (okToLeaveOutInvalidOptionalFields ));
242
- parsedResponse = JsonParser .parseString (response );
243
- JsonObject parsedJson = parsedResponse .getAsJsonObject ();
280
+ if (msgService == null ) {
281
+ return createResponseEntity (HttpStatus .SERVICE_UNAVAILABLE , JSON_ERROR_STATUS ,
282
+ "No protocol service has been found registered" , eventResponse ).getBody ();
283
+ }
284
+ String response = msgService .generateMsg (msgType , event , isLenientEnabled (okToLeaveOutInvalidOptionalFields ));
285
+ parsedResponse = JsonParser .parseString (response );
286
+ JsonObject parsedJson = parsedResponse .getAsJsonObject ();
244
287
245
- if (parsedJson .has (JSON_ERROR_MESSAGE_FIELD )) {
246
- createResponseEntity (HttpStatus .BAD_REQUEST , "fail" , TEMPLATE_ERROR , eventResponse );
247
- return eventResponse ;
248
- } else {
249
- return parsedJson ;
250
- }
288
+ if (parsedJson .has (JSON_ERROR_MESSAGE_FIELD )) {
289
+ createResponseEntity (HttpStatus .BAD_REQUEST , JSON_ERROR_STATUS , TEMPLATE_ERROR , eventResponse );
290
+ return eventResponse ;
291
+ } else {
292
+ return parsedJson ;
251
293
}
252
- return eventResponse ;
253
294
}
254
295
255
296
private JsonObject erLookup (final JsonObject bodyJson , Boolean failIfMultipleFound , Boolean failIfNoneFound ,
0 commit comments