Skip to content

Commit

Permalink
Update generate endpoint for gen1 template (eiffel-community#231)
Browse files Browse the repository at this point in the history
* Update generate endpoint for gen1 template
  • Loading branch information
shudhansu-shekhar authored Oct 30, 2024
1 parent 243dd06 commit 6161424
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 31 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.13
- Made changes to /generate end-point to work for Gen1 templates

## 2.1.11
- Made changes to /message_protocols end-point to work for old REMRem-Semantics library.

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.12</eiffel-remrem-generate.version>
<eiffel-remrem-generate.version>2.1.13</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 @@ -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;
Expand Down Expand Up @@ -168,46 +169,48 @@ 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.
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);

} else if (inputData.isJsonObject()) {
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);
Expand Down Expand Up @@ -316,9 +319,7 @@ 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;
throw new REMGenerateException(response);
} else {
return parsedJson;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.ericsson.eiffel.remrem.generate.exception;

public class ProtocolHandlerNotFoundException extends REMGenerateException {
public ProtocolHandlerNotFoundException(String message) {
super(message);
}
}

0 comments on commit 6161424

Please sign in to comment.