Skip to content

Commit

Permalink
Improve efficiency how result of event generation is handled (#233)
Browse files Browse the repository at this point in the history
* Modify /generate end-point to work for Gen1 templates

* Improve efficiency how result of event generation is handled
  • Loading branch information
shudhansu-shekhar authored Nov 5, 2024
1 parent 6161424 commit 9c2a9c7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 25 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2.1.14
- Made changes to /generate end-point to improve efficiency how result of event generation is handled

## 2.1.13
- Made changes to /generate end-point to work for Gen1 templates

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>

<properties>
<eiffel-remrem-generate.version>2.1.13</eiffel-remrem-generate.version>
<eiffel-remrem-generate.version>2.1.14</eiffel-remrem-generate.version>
<eiffel-remrem-semantics.version>2.2.7</eiffel-remrem-semantics.version>
</properties>
<artifactId>eiffel-remrem-generate</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.ericsson.eiffel.remrem.protocol.MsgService;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.*;
Expand All @@ -33,7 +32,6 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
Expand Down Expand Up @@ -173,9 +171,9 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
int failedCount = 0;
for (JsonElement element : inputEventJsonArray) {
try {
JsonObject generatedEvent = (processEvent(msgProtocol, msgType,
JsonObject generatedEvent = generateEvent(msgProtocol, msgType,
failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit,
okToLeaveOutInvalidOptionalFields, element.getAsJsonObject()));
okToLeaveOutInvalidOptionalFields, element.getAsJsonObject());
generatedEventResults.add(generatedEvent);
successCount++;
} catch (ProtocolHandlerNotFoundException e) {
Expand All @@ -202,20 +200,9 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType

} else if (inputData.isJsonObject()) {
JsonObject inputJsonObject = inputData.getAsJsonObject();
JsonObject processedJson = processEvent(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound,
JsonObject processedJson = generateEvent(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound,
lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields, inputJsonObject);
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();
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);
return createResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR, errorMessage, JSON_ERROR_STATUS);
}
return new ResponseEntity<>(processedJson, HttpStatus.OK);
} else {
return createResponseEntity(HttpStatus.BAD_REQUEST,
"Invalid JSON format,expected either single template or array of templates",
Expand Down Expand Up @@ -270,6 +257,10 @@ public void initializeResponse(HttpStatus status, String resultMessage, String e
*/
private ResponseEntity<JsonObject> handleException(Exception e) {
String exceptionMessage = e.getMessage();
if (e instanceof ProtocolHandlerNotFoundException) {
return createResponseEntity(HttpStatus.SERVICE_UNAVAILABLE, exceptionMessage, JSON_ERROR_STATUS);
}

if (e instanceof REMGenerateException) {
List<HttpStatus> statusList = List.of(
HttpStatus.NOT_ACCEPTABLE, HttpStatus.EXPECTATION_FAILED, HttpStatus.SERVICE_UNAVAILABLE,
Expand Down Expand Up @@ -302,27 +293,25 @@ private ResponseEntity<JsonObject> handleException(Exception e) {
* @param jsonObject The content of the message which is used in creating the event details.
* @return JsonObject generated event
*/
public JsonObject processEvent(String msgProtocol, String msgType, Boolean failIfMultipleFound,
Boolean failIfNoneFound, Boolean lookupInExternalERs, int lookupLimit,
Boolean okToLeaveOutInvalidOptionalFields, JsonObject jsonObject) throws REMGenerateException, JsonSyntaxException {
public JsonObject generateEvent(String msgProtocol, String msgType, Boolean failIfMultipleFound,
Boolean failIfNoneFound, Boolean lookupInExternalERs, int lookupLimit,
Boolean okToLeaveOutInvalidOptionalFields, JsonObject jsonObject) throws REMGenerateException, JsonSyntaxException {
JsonElement parsedResponse;

JsonObject event = erLookup(jsonObject, failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit);
MsgService msgService = getMessageService(msgProtocol);

if (msgService == null) {
return createResponseEntity(HttpStatus.SERVICE_UNAVAILABLE,
"No protocol service has been found registered", JSON_ERROR_STATUS).getBody();
throw new ProtocolHandlerNotFoundException("Handler of Eiffel protocol '" + msgProtocol + "' not found");
}
String response = msgService.generateMsg(msgType, event, isLenientEnabled(okToLeaveOutInvalidOptionalFields));
parsedResponse = JsonParser.parseString(response);
JsonObject parsedJson = parsedResponse.getAsJsonObject();

if (parsedJson.has(JSON_ERROR_MESSAGE_FIELD)) {
throw new REMGenerateException(response);
} else {
return parsedJson;
}
return parsedJson;
}

private JsonObject erLookup(final JsonObject bodyJson, Boolean failIfMultipleFound, Boolean failIfNoneFound,
Expand Down

0 comments on commit 9c2a9c7

Please sign in to comment.