21
21
import com .ericsson .eiffel .remrem .protocol .MsgService ;
22
22
import com .fasterxml .jackson .core .JsonFactory ;
23
23
import com .fasterxml .jackson .core .JsonProcessingException ;
24
- import com .fasterxml .jackson .databind .JsonMappingException ;
25
24
import com .fasterxml .jackson .databind .JsonNode ;
26
25
import com .fasterxml .jackson .databind .ObjectMapper ;
27
26
import com .google .gson .*;
33
32
import org .slf4j .LoggerFactory ;
34
33
import org .springframework .beans .factory .annotation .Autowired ;
35
34
import org .springframework .beans .factory .annotation .Value ;
36
- import org .springframework .context .annotation .PropertySource ;
37
35
import org .springframework .http .HttpStatus ;
38
36
import org .springframework .http .MediaType ;
39
37
import org .springframework .http .RequestEntity ;
@@ -173,9 +171,9 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
173
171
int failedCount = 0 ;
174
172
for (JsonElement element : inputEventJsonArray ) {
175
173
try {
176
- JsonObject generatedEvent = ( processEvent (msgProtocol , msgType ,
174
+ JsonObject generatedEvent = generateEvent (msgProtocol , msgType ,
177
175
failIfMultipleFound , failIfNoneFound , lookupInExternalERs , lookupLimit ,
178
- okToLeaveOutInvalidOptionalFields , element .getAsJsonObject ())) ;
176
+ okToLeaveOutInvalidOptionalFields , element .getAsJsonObject ());
179
177
generatedEventResults .add (generatedEvent );
180
178
successCount ++;
181
179
} catch (ProtocolHandlerNotFoundException e ) {
@@ -202,20 +200,9 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
202
200
203
201
} else if (inputData .isJsonObject ()) {
204
202
JsonObject inputJsonObject = inputData .getAsJsonObject ();
205
- JsonObject processedJson = processEvent (msgProtocol , msgType , failIfMultipleFound , failIfNoneFound ,
203
+ JsonObject processedJson = generateEvent (msgProtocol , msgType , failIfMultipleFound , failIfNoneFound ,
206
204
lookupInExternalERs , lookupLimit , okToLeaveOutInvalidOptionalFields , inputJsonObject );
207
- if (!processedJson .has (JSON_STATUS_CODE )) {
208
- HttpStatus status = HttpStatus .OK ;
209
- return new ResponseEntity <>(processedJson , status );
210
- } else if (processedJson .has (JSON_STATUS_CODE )) {
211
- String statusValue = processedJson .get (JSON_STATUS_CODE ).toString ();
212
- HttpStatus status = HttpStatus .resolve (Integer .parseInt (statusValue ));
213
- return new ResponseEntity <>(processedJson , status );
214
- } else {
215
- String errorMessage = "There is no status value in the response " + processedJson ;
216
- log .error (errorMessage );
217
- return createResponseEntity (HttpStatus .INTERNAL_SERVER_ERROR , errorMessage , JSON_ERROR_STATUS );
218
- }
205
+ return new ResponseEntity <>(processedJson , HttpStatus .OK );
219
206
} else {
220
207
return createResponseEntity (HttpStatus .BAD_REQUEST ,
221
208
"Invalid JSON format,expected either single template or array of templates" ,
@@ -270,6 +257,10 @@ public void initializeResponse(HttpStatus status, String resultMessage, String e
270
257
*/
271
258
private ResponseEntity <JsonObject > handleException (Exception e ) {
272
259
String exceptionMessage = e .getMessage ();
260
+ if (e instanceof ProtocolHandlerNotFoundException ) {
261
+ return createResponseEntity (HttpStatus .SERVICE_UNAVAILABLE , exceptionMessage , JSON_ERROR_STATUS );
262
+ }
263
+
273
264
if (e instanceof REMGenerateException ) {
274
265
List <HttpStatus > statusList = List .of (
275
266
HttpStatus .NOT_ACCEPTABLE , HttpStatus .EXPECTATION_FAILED , HttpStatus .SERVICE_UNAVAILABLE ,
@@ -302,27 +293,25 @@ private ResponseEntity<JsonObject> handleException(Exception e) {
302
293
* @param jsonObject The content of the message which is used in creating the event details.
303
294
* @return JsonObject generated event
304
295
*/
305
- public JsonObject processEvent (String msgProtocol , String msgType , Boolean failIfMultipleFound ,
306
- Boolean failIfNoneFound , Boolean lookupInExternalERs , int lookupLimit ,
307
- Boolean okToLeaveOutInvalidOptionalFields , JsonObject jsonObject ) throws REMGenerateException , JsonSyntaxException {
296
+ public JsonObject generateEvent (String msgProtocol , String msgType , Boolean failIfMultipleFound ,
297
+ Boolean failIfNoneFound , Boolean lookupInExternalERs , int lookupLimit ,
298
+ Boolean okToLeaveOutInvalidOptionalFields , JsonObject jsonObject ) throws REMGenerateException , JsonSyntaxException {
308
299
JsonElement parsedResponse ;
309
300
310
301
JsonObject event = erLookup (jsonObject , failIfMultipleFound , failIfNoneFound , lookupInExternalERs , lookupLimit );
311
302
MsgService msgService = getMessageService (msgProtocol );
312
303
313
304
if (msgService == null ) {
314
- return createResponseEntity (HttpStatus .SERVICE_UNAVAILABLE ,
315
- "No protocol service has been found registered" , JSON_ERROR_STATUS ).getBody ();
305
+ throw new ProtocolHandlerNotFoundException ("Handler of Eiffel protocol '" + msgProtocol + "' not found" );
316
306
}
317
307
String response = msgService .generateMsg (msgType , event , isLenientEnabled (okToLeaveOutInvalidOptionalFields ));
318
308
parsedResponse = JsonParser .parseString (response );
319
309
JsonObject parsedJson = parsedResponse .getAsJsonObject ();
320
310
321
311
if (parsedJson .has (JSON_ERROR_MESSAGE_FIELD )) {
322
312
throw new REMGenerateException (response );
323
- } else {
324
- return parsedJson ;
325
313
}
314
+ return parsedJson ;
326
315
}
327
316
328
317
private JsonObject erLookup (final JsonObject bodyJson , Boolean failIfMultipleFound , Boolean failIfNoneFound ,
0 commit comments