From 64e51ee0cadf714804e0c300d73c69fe8aa4767e Mon Sep 17 00:00:00 2001 From: Shudhansu Shekhar Date: Tue, 29 Oct 2024 13:47:43 +0530 Subject: [PATCH] Update generate endpoint for gen1 template --- .../controller/RemremGenerateController.java | 69 +++++++++++-------- .../ProtocolHandlerNotFoundException.java | 7 ++ 2 files changed, 46 insertions(+), 30 deletions(-) create mode 100644 service/src/main/java/com/ericsson/eiffel/remrem/generate/exception/ProtocolHandlerNotFoundException.java diff --git a/service/src/main/java/com/ericsson/eiffel/remrem/generate/controller/RemremGenerateController.java b/service/src/main/java/com/ericsson/eiffel/remrem/generate/controller/RemremGenerateController.java index 23eeede1..3c42c8b9 100644 --- a/service/src/main/java/com/ericsson/eiffel/remrem/generate/controller/RemremGenerateController.java +++ b/service/src/main/java/com/ericsson/eiffel/remrem/generate/controller/RemremGenerateController.java @@ -16,6 +16,7 @@ import com.ericsson.eiffel.remrem.generate.config.ErLookUpConfig; import com.ericsson.eiffel.remrem.generate.constants.RemremGenerateServiceConstants; +import com.ericsson.eiffel.remrem.generate.exception.ProtocolHandlerNotFoundException; import com.ericsson.eiffel.remrem.generate.exception.REMGenerateException; import com.ericsson.eiffel.remrem.protocol.MsgService; import com.fasterxml.jackson.core.JsonFactory; @@ -168,25 +169,36 @@ public ResponseEntity generate(final String msgProtocol, final String msgType "The number of events in the input array is too high: " + inputEventJsonArray.size() + " > " + maxSizeOfInputArray + "; you can modify the property 'maxSizeOfInputArray' to increase it."); } + int successCount = 0; + int failedCount = 0; for (JsonElement element : inputEventJsonArray) { - JsonObject generatedEvent = (processEvent(msgProtocol, msgType, - failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit, - okToLeaveOutInvalidOptionalFields, element.getAsJsonObject())); - generatedEventResults.add(generatedEvent); - } - boolean allSuccess = true; - boolean partialSuccess = false; - for (JsonElement result : generatedEventResults) { - JsonObject jsonObject = result.getAsJsonObject(); - allSuccess &= jsonObject.has(META); - partialSuccess |= jsonObject.has(META); + try { + JsonObject generatedEvent = (processEvent(msgProtocol, msgType, + failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit, + okToLeaveOutInvalidOptionalFields, element.getAsJsonObject())); + generatedEventResults.add(generatedEvent); + successCount++; + } catch (ProtocolHandlerNotFoundException e) { + // Rethrow the exception. All events in array use the same protocol. If it's handler + // cannot be found for one event, the others will fail, too. + throw e; + } catch (REMGenerateException e) { + // Something went wrong. Add failure description to array of results. + /* partialSuccess = true; + allSuccess=false;*/ + failedCount++; + JsonObject response = new JsonObject(); + createResponseEntity(HttpStatus.BAD_REQUEST, e.getMessage(), JSON_ERROR_STATUS, response); + generatedEventResults.add(response); + } } - HttpStatus eventStatus = HttpStatus.BAD_REQUEST; - if (allSuccess){ + HttpStatus eventStatus; + if (failedCount == 0) { eventStatus = HttpStatus.OK; - } - else if (partialSuccess){ - eventStatus = HttpStatus.MULTI_STATUS; + } else if (successCount > 0 && failedCount > 0) { + eventStatus = HttpStatus.MULTI_STATUS; + } else { + eventStatus = HttpStatus.BAD_REQUEST; } return new ResponseEntity<>(generatedEventResults, eventStatus); @@ -194,20 +206,13 @@ else if (partialSuccess){ JsonObject inputJsonObject = inputData.getAsJsonObject(); JsonObject processedJson = processEvent(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields, inputJsonObject); - HttpStatus status; - if (processedJson.has(META)) { - status = HttpStatus.OK; + if (!processedJson.has(JSON_STATUS_CODE)) { + HttpStatus status = HttpStatus.OK; return new ResponseEntity<>(processedJson, status); } else if (processedJson.has(JSON_STATUS_CODE)) { String statusValue = processedJson.get(JSON_STATUS_CODE).toString(); - try { - status = HttpStatus.resolve(Integer.parseInt(statusValue)); - return new ResponseEntity<>(processedJson, status); - } catch (NumberFormatException e) { - String errorMessage = "Invalid status value: '" + statusValue + "' of response " + processedJson; - log.error(errorMessage); - return createResponseEntity(HttpStatus.BAD_REQUEST, errorMessage, JSON_ERROR_STATUS); - } + HttpStatus status = HttpStatus.resolve(Integer.parseInt(statusValue)); + return new ResponseEntity<>(processedJson, status); } else { String errorMessage = "There is no status value in the response " + processedJson; log.error(errorMessage); @@ -223,6 +228,7 @@ else if (partialSuccess){ } } + /** * To display response in browser or application * @param status response code for the HTTP request @@ -316,10 +322,13 @@ public JsonObject processEvent(String msgProtocol, String msgType, Boolean failI JsonObject parsedJson = parsedResponse.getAsJsonObject(); if (parsedJson.has(JSON_ERROR_MESSAGE_FIELD)) { - JsonObject eventResponse = new JsonObject(); - createResponseEntity(HttpStatus.BAD_REQUEST, parsedJson.toString(), JSON_ERROR_STATUS, eventResponse); - return eventResponse; +// JsonObject eventResponse = new JsonObject(); +// createResponseEntity(HttpStatus.BAD_REQUEST, parsedJson.toString(), JSON_ERROR_STATUS, eventResponse); +// return eventResponse; + throw new REMGenerateException(response); } else { + /* JsonObject eventResponse = new JsonObject(); + createResponseEntity(HttpStatus.OK, parsedJson.toString(), "SUCCESS", eventResponse);*/ return parsedJson; } } diff --git a/service/src/main/java/com/ericsson/eiffel/remrem/generate/exception/ProtocolHandlerNotFoundException.java b/service/src/main/java/com/ericsson/eiffel/remrem/generate/exception/ProtocolHandlerNotFoundException.java new file mode 100644 index 00000000..7c3e640b --- /dev/null +++ b/service/src/main/java/com/ericsson/eiffel/remrem/generate/exception/ProtocolHandlerNotFoundException.java @@ -0,0 +1,7 @@ +package com.ericsson.eiffel.remrem.generate.exception; + +public class ProtocolHandlerNotFoundException extends REMGenerateException { + public ProtocolHandlerNotFoundException(String message) { + super(message); + } +}