@@ -119,10 +119,10 @@ public ResponseEntity<?> generate(@ApiParam(value = "message protocol", required
119
119
120
120
} catch (JsonSyntaxException e ) {
121
121
JsonObject errorResponse = new JsonObject ();
122
- log .error ("Unexpected exception caught due to parse json data" , e .getMessage ());
123
122
String exceptionMessage = e .getMessage ();
123
+ log .error (exceptionMessage , e .getMessage ());
124
124
errorResponse .addProperty ("Status code" , HttpStatus .BAD_REQUEST .value ());
125
- errorResponse .addProperty ("result" , "Fatal " );
125
+ errorResponse .addProperty ("result" , "fatal " );
126
126
errorResponse .addProperty ("message" , "Invalid JSON parse data format due to: " + exceptionMessage );
127
127
return new ResponseEntity <>(errorResponse , HttpStatus .INTERNAL_SERVER_ERROR );
128
128
}
@@ -133,35 +133,87 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
133
133
JsonArray results = new JsonArray ();
134
134
JsonObject errorResponse = new JsonObject ();
135
135
try {
136
+ if (lookupLimit <= 0 ) {
137
+ return new ResponseEntity <>("LookupLimit must be greater than or equals to 1" , HttpStatus .valueOf (HttpStatus .BAD_REQUEST .value ()));
138
+ }
136
139
137
140
if (bodyJson .isJsonArray ()) {
138
141
139
142
JsonArray jsonArray = bodyJson .getAsJsonArray ();
140
143
for (JsonElement element : jsonArray ) {
141
- results .add (processEvent (element . getAsJsonObject (), msgProtocol , msgType ,
144
+ results .add (processEvent (msgProtocol , msgType ,
142
145
failIfMultipleFound , failIfNoneFound , lookupInExternalERs , lookupLimit ,
143
- okToLeaveOutInvalidOptionalFields ));
146
+ okToLeaveOutInvalidOptionalFields , element .getAsJsonObject ()));
147
+ }
148
+ for (int i = 0 ; i < results .size (); i ++) {
149
+ JsonObject event = results .get (i ).getAsJsonObject ();
150
+ if (event .has ("meta" )) {
151
+ return new ResponseEntity <>(results , HttpStatus .OK );
152
+ } else if (event .has ("status code" ) && "400" .equals (event .get ("status code" ).toString ())) {
153
+ return new ResponseEntity <>(results , HttpStatus .BAD_REQUEST );
154
+ } else {
155
+ return new ResponseEntity <>(results , HttpStatus .SERVICE_UNAVAILABLE );
156
+ }
144
157
}
145
- return new ResponseEntity <>(results , HttpStatus .OK );
158
+ return new ResponseEntity <>(results , HttpStatus .ACCEPTED );
146
159
147
160
} else if (bodyJson .isJsonObject ()) {
148
161
JsonObject jsonObject = bodyJson .getAsJsonObject ();
149
- JsonObject jsonObject1 = processEvent (jsonObject , msgProtocol , msgType , failIfMultipleFound , failIfNoneFound ,
150
- lookupInExternalERs , lookupLimit , okToLeaveOutInvalidOptionalFields );
151
- return new ResponseEntity <>(jsonObject1 , HttpStatus .OK );
162
+ JsonObject processedJson = processEvent (msgProtocol , msgType , failIfMultipleFound , failIfNoneFound ,
163
+ lookupInExternalERs , lookupLimit , okToLeaveOutInvalidOptionalFields , jsonObject );
164
+
165
+ if (processedJson .has ("meta" )) {
166
+ return new ResponseEntity <>(processedJson , HttpStatus .OK );
167
+ }
168
+ if (processedJson .has ("status code" ) && "400" .equals (processedJson .get ("status code" ).toString ())) {
169
+ return new ResponseEntity <>(processedJson , HttpStatus .BAD_REQUEST );
170
+ } else {
171
+ return new ResponseEntity <>(processedJson , HttpStatus .SERVICE_UNAVAILABLE );
172
+ }
152
173
} else {
153
174
errorResponse .addProperty ("Status code" , HttpStatus .BAD_REQUEST .value ());
154
175
errorResponse .addProperty ("result" , "fail" );
155
- errorResponse .addProperty ("message" , "Invalid JSON format" );
176
+ errorResponse .addProperty ("message" , "Invalid JSON format,expected either single template or array of templates " );
156
177
return new ResponseEntity <>(errorResponse , HttpStatus .BAD_REQUEST );
157
178
}
179
+ } catch (REMGenerateException e ) {
180
+ if (e .getMessage ().contains (Integer .toString (HttpStatus .NOT_ACCEPTABLE .value ()))) {
181
+ errorResponse .addProperty ("message" , e .getMessage ());
182
+ return new ResponseEntity <>(errorResponse , HttpStatus .NOT_ACCEPTABLE );
183
+ } else if (e .getMessage ().contains (Integer .toString (HttpStatus .EXPECTATION_FAILED .value ()))) {
184
+ errorResponse .addProperty ("message" , e .getMessage ());
185
+ return new ResponseEntity <>(errorResponse , HttpStatus .EXPECTATION_FAILED );
186
+ } else if (e .getMessage ().contains (Integer .toString (HttpStatus .EXPECTATION_FAILED .value ()))) {
187
+ errorResponse .addProperty ("status code" , HttpStatus .EXPECTATION_FAILED .value ());
188
+
189
+ } else if (e .getMessage ()
190
+ .contains (Integer .toString (HttpStatus .SERVICE_UNAVAILABLE .value ()))) {
191
+ errorResponse .addProperty ("message" , e .getMessage ());
192
+ return new ResponseEntity <>(errorResponse , HttpStatus .SERVICE_UNAVAILABLE );
193
+ } else {
194
+ errorResponse .addProperty ("status code" , HttpStatus .UNPROCESSABLE_ENTITY .value ());
195
+ errorResponse .addProperty ("message" , e .getMessage ());
196
+ return new ResponseEntity <>(errorResponse , HttpStatus .UNPROCESSABLE_ENTITY );
197
+ }
198
+ errorResponse .addProperty ("result" , "fail" );
199
+ errorResponse .add ("message" , parser .parse (e .getMessage ()));
200
+ return new ResponseEntity <>(errorResponse , HttpStatus .BAD_REQUEST );
201
+
202
+ } catch (JsonSyntaxException e ) {
203
+ log .error ("Failed to parse JSON: " , e .getMessage ());
204
+ String exceptionMessage = e .getMessage ();
205
+ errorResponse .addProperty ("status code" , HttpStatus .INTERNAL_SERVER_ERROR .value ());
206
+ errorResponse .addProperty ("result" , "fail" );
207
+ errorResponse .addProperty ("message" , exceptionMessage );
208
+ return new ResponseEntity <>(errorResponse , HttpStatus .INTERNAL_SERVER_ERROR );
209
+
158
210
} catch (Exception e ) {
159
211
log .error ("Unexpected exception caught" , e );
160
- errorResponse .addProperty ("Status code" , HttpStatus .BAD_REQUEST .value ());
161
- errorResponse .addProperty ("Result" , "Fail" );
162
- errorResponse .addProperty ("Message" , "Invalid JSON format" );
212
+ String exceptionMessage = e .getMessage ();
213
+ errorResponse .addProperty ("status code" , HttpStatus .BAD_REQUEST .value ());
214
+ errorResponse .addProperty ("result" , "fail" );
215
+ errorResponse .addProperty ("message" , exceptionMessage );
163
216
return new ResponseEntity <>(errorResponse , HttpStatus .INTERNAL_SERVER_ERROR );
164
-
165
217
}
166
218
}
167
219
@@ -170,57 +222,28 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
170
222
* @return JsonObject generated event
171
223
*/
172
224
173
- public JsonObject processEvent (JsonObject jsonElement , String msgProtocol , String msgType , Boolean failIfMultipleFound ,
174
- Boolean okToLeaveOutInvalidOptionalFields ,
175
- Boolean failIfNoneFound , int lookupLimit , Boolean lookupInExternalERs ) {
225
+ public JsonObject processEvent (String msgProtocol , String msgType , Boolean failIfMultipleFound ,
226
+ Boolean failIfNoneFound , Boolean lookupInExternalERs , int lookupLimit ,
227
+ Boolean okToLeaveOutInvalidOptionalFields , JsonObject jsonObject ) throws REMGenerateException , JsonSyntaxException {
176
228
JsonObject eventResponse = new JsonObject ();
177
229
JsonElement parsedResponse = null ;
178
- try {
179
- JsonObject event = jsonElement .getAsJsonObject ();
180
- event = erLookup (event , failIfMultipleFound , failIfNoneFound , lookupInExternalERs , lookupLimit );
230
+
231
+ JsonObject event = erLookup (jsonObject , failIfMultipleFound , failIfNoneFound , lookupInExternalERs , lookupLimit );
181
232
MsgService msgService = getMessageService (msgProtocol );
182
233
183
234
if (msgService != null ) {
184
235
String response = msgService .generateMsg (msgType , event , isLenientEnabled (okToLeaveOutInvalidOptionalFields ));
185
236
parsedResponse = parser .parse (response );
186
237
187
- if (lookupLimit <= 0 ) {
188
- eventResponse .addProperty ("status code" , HttpStatus .BAD_REQUEST .value ());
189
- } else if (!parsedResponse .getAsJsonObject ().has (RemremGenerateServiceConstants .JSON_ERROR_MESSAGE_FIELD )) {
238
+ if (!parsedResponse .getAsJsonObject ().has (RemremGenerateServiceConstants .JSON_ERROR_MESSAGE_FIELD )) {
190
239
return parsedResponse .getAsJsonObject ();
191
240
} else {
192
- eventResponse .addProperty ("Status code" , HttpStatus .BAD_REQUEST .value ());
193
- eventResponse .addProperty ("Result " , "Fail " );
194
- eventResponse .addProperty ("Message " , RemremGenerateServiceConstants .NOT_ACCEPTABLE );
241
+ eventResponse .addProperty ("status code" , HttpStatus .BAD_REQUEST .value ());
242
+ eventResponse .addProperty ("result " , "fail " );
243
+ eventResponse .addProperty ("message " , RemremGenerateServiceConstants .TEMPLATE_ERROR );
195
244
return eventResponse ;
196
245
}
197
-
198
246
}
199
- } catch (REMGenerateException e1 ) {
200
- if (e1 .getMessage ().contains (Integer .toString (HttpStatus .NOT_ACCEPTABLE .value ()))) {
201
- eventResponse .addProperty ("Status code" , HttpStatus .NOT_ACCEPTABLE .value ());
202
- } else if (e1 .getMessage ().contains (Integer .toString (HttpStatus .EXPECTATION_FAILED .value ()))) {
203
- eventResponse .addProperty ("Status code" , HttpStatus .EXPECTATION_FAILED .value ());
204
- } else if (e1 .getMessage ().contains (Integer .toString (HttpStatus .EXPECTATION_FAILED .value ()))) {
205
- eventResponse .addProperty ("Status code" , HttpStatus .EXPECTATION_FAILED .value ());
206
-
207
- } else if (e1 .getMessage ()
208
- .contains (Integer .toString (HttpStatus .SERVICE_UNAVAILABLE .value ()))) {
209
- eventResponse .addProperty ("Status code" , HttpStatus .SERVICE_UNAVAILABLE .value ());
210
- eventResponse .addProperty ("Message" , RemremGenerateServiceConstants .NO_SERVICE_ERROR );
211
- } else {
212
- eventResponse .addProperty ("Status code" , HttpStatus .UNPROCESSABLE_ENTITY .value ());
213
- }
214
- eventResponse .addProperty ("Result" , "Fail" );
215
- eventResponse .add ("Message" , parser .parse (e1 .getMessage ()));
216
- return eventResponse ;
217
- } catch (Exception e ) {
218
- log .error ("Unexpected error caught" , e );
219
- eventResponse .addProperty ("Status code" , HttpStatus .INTERNAL_SERVER_ERROR .value ());
220
- eventResponse .addProperty ("Result" , "Fail" );
221
- eventResponse .addProperty ("Message" , RemremGenerateServiceConstants .INTERNAL_SERVER_ERROR );
222
- return eventResponse ;
223
- }
224
247
return eventResponse ;
225
248
}
226
249
0 commit comments