Skip to content

Commit 6161424

Browse files
Update generate endpoint for gen1 template (#231)
* Update generate endpoint for gen1 template
1 parent 243dd06 commit 6161424

File tree

4 files changed

+42
-31
lines changed

4 files changed

+42
-31
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 2.1.13
2+
- Made changes to /generate end-point to work for Gen1 templates
3+
14
## 2.1.11
25
- Made changes to /message_protocols end-point to work for old REMRem-Semantics library.
36

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111

1212
<properties>
13-
<eiffel-remrem-generate.version>2.1.12</eiffel-remrem-generate.version>
13+
<eiffel-remrem-generate.version>2.1.13</eiffel-remrem-generate.version>
1414
<eiffel-remrem-semantics.version>2.2.7</eiffel-remrem-semantics.version>
1515
</properties>
1616
<artifactId>eiffel-remrem-generate</artifactId>

service/src/main/java/com/ericsson/eiffel/remrem/generate/controller/RemremGenerateController.java

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import com.ericsson.eiffel.remrem.generate.config.ErLookUpConfig;
1818
import com.ericsson.eiffel.remrem.generate.constants.RemremGenerateServiceConstants;
19+
import com.ericsson.eiffel.remrem.generate.exception.ProtocolHandlerNotFoundException;
1920
import com.ericsson.eiffel.remrem.generate.exception.REMGenerateException;
2021
import com.ericsson.eiffel.remrem.protocol.MsgService;
2122
import com.fasterxml.jackson.core.JsonFactory;
@@ -168,46 +169,48 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
168169
"The number of events in the input array is too high: " + inputEventJsonArray.size() + " > "
169170
+ maxSizeOfInputArray + "; you can modify the property 'maxSizeOfInputArray' to increase it.");
170171
}
172+
int successCount = 0;
173+
int failedCount = 0;
171174
for (JsonElement element : inputEventJsonArray) {
172-
JsonObject generatedEvent = (processEvent(msgProtocol, msgType,
173-
failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit,
174-
okToLeaveOutInvalidOptionalFields, element.getAsJsonObject()));
175-
generatedEventResults.add(generatedEvent);
176-
}
177-
boolean allSuccess = true;
178-
boolean partialSuccess = false;
179-
for (JsonElement result : generatedEventResults) {
180-
JsonObject jsonObject = result.getAsJsonObject();
181-
allSuccess &= jsonObject.has(META);
182-
partialSuccess |= jsonObject.has(META);
175+
try {
176+
JsonObject generatedEvent = (processEvent(msgProtocol, msgType,
177+
failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit,
178+
okToLeaveOutInvalidOptionalFields, element.getAsJsonObject()));
179+
generatedEventResults.add(generatedEvent);
180+
successCount++;
181+
} catch (ProtocolHandlerNotFoundException e) {
182+
// Rethrow the exception. All events in array use the same protocol. If it's handler
183+
// cannot be found for one event, the others will fail, too.
184+
throw e;
185+
} catch (REMGenerateException e) {
186+
// Something went wrong. Add failure description to array of results.
187+
failedCount++;
188+
JsonObject response = new JsonObject();
189+
createResponseEntity(HttpStatus.BAD_REQUEST, e.getMessage(), JSON_ERROR_STATUS, response);
190+
generatedEventResults.add(response);
191+
}
183192
}
184-
HttpStatus eventStatus = HttpStatus.BAD_REQUEST;
185-
if (allSuccess){
193+
HttpStatus eventStatus;
194+
if (failedCount == 0) {
186195
eventStatus = HttpStatus.OK;
187-
}
188-
else if (partialSuccess){
189-
eventStatus = HttpStatus.MULTI_STATUS;
196+
} else if (successCount > 0 && failedCount > 0) {
197+
eventStatus = HttpStatus.MULTI_STATUS;
198+
} else {
199+
eventStatus = HttpStatus.BAD_REQUEST;
190200
}
191201
return new ResponseEntity<>(generatedEventResults, eventStatus);
192202

193203
} else if (inputData.isJsonObject()) {
194204
JsonObject inputJsonObject = inputData.getAsJsonObject();
195205
JsonObject processedJson = processEvent(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound,
196206
lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields, inputJsonObject);
197-
HttpStatus status;
198-
if (processedJson.has(META)) {
199-
status = HttpStatus.OK;
207+
if (!processedJson.has(JSON_STATUS_CODE)) {
208+
HttpStatus status = HttpStatus.OK;
200209
return new ResponseEntity<>(processedJson, status);
201210
} else if (processedJson.has(JSON_STATUS_CODE)) {
202211
String statusValue = processedJson.get(JSON_STATUS_CODE).toString();
203-
try {
204-
status = HttpStatus.resolve(Integer.parseInt(statusValue));
205-
return new ResponseEntity<>(processedJson, status);
206-
} catch (NumberFormatException e) {
207-
String errorMessage = "Invalid status value: '" + statusValue + "' of response " + processedJson;
208-
log.error(errorMessage);
209-
return createResponseEntity(HttpStatus.BAD_REQUEST, errorMessage, JSON_ERROR_STATUS);
210-
}
212+
HttpStatus status = HttpStatus.resolve(Integer.parseInt(statusValue));
213+
return new ResponseEntity<>(processedJson, status);
211214
} else {
212215
String errorMessage = "There is no status value in the response " + processedJson;
213216
log.error(errorMessage);
@@ -316,9 +319,7 @@ public JsonObject processEvent(String msgProtocol, String msgType, Boolean failI
316319
JsonObject parsedJson = parsedResponse.getAsJsonObject();
317320

318321
if (parsedJson.has(JSON_ERROR_MESSAGE_FIELD)) {
319-
JsonObject eventResponse = new JsonObject();
320-
createResponseEntity(HttpStatus.BAD_REQUEST, parsedJson.toString(), JSON_ERROR_STATUS, eventResponse);
321-
return eventResponse;
322+
throw new REMGenerateException(response);
322323
} else {
323324
return parsedJson;
324325
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.ericsson.eiffel.remrem.generate.exception;
2+
3+
public class ProtocolHandlerNotFoundException extends REMGenerateException {
4+
public ProtocolHandlerNotFoundException(String message) {
5+
super(message);
6+
}
7+
}

0 commit comments

Comments
 (0)