Skip to content

Commit 9c2a9c7

Browse files
Improve efficiency how result of event generation is handled (#233)
* Modify /generate end-point to work for Gen1 templates * Improve efficiency how result of event generation is handled
1 parent 6161424 commit 9c2a9c7

File tree

3 files changed

+17
-25
lines changed

3 files changed

+17
-25
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.14
2+
- Made changes to /generate end-point to improve efficiency how result of event generation is handled
3+
14
## 2.1.13
25
- Made changes to /generate end-point to work for Gen1 templates
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.13</eiffel-remrem-generate.version>
13+
<eiffel-remrem-generate.version>2.1.14</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: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.ericsson.eiffel.remrem.protocol.MsgService;
2222
import com.fasterxml.jackson.core.JsonFactory;
2323
import com.fasterxml.jackson.core.JsonProcessingException;
24-
import com.fasterxml.jackson.databind.JsonMappingException;
2524
import com.fasterxml.jackson.databind.JsonNode;
2625
import com.fasterxml.jackson.databind.ObjectMapper;
2726
import com.google.gson.*;
@@ -33,7 +32,6 @@
3332
import org.slf4j.LoggerFactory;
3433
import org.springframework.beans.factory.annotation.Autowired;
3534
import org.springframework.beans.factory.annotation.Value;
36-
import org.springframework.context.annotation.PropertySource;
3735
import org.springframework.http.HttpStatus;
3836
import org.springframework.http.MediaType;
3937
import org.springframework.http.RequestEntity;
@@ -173,9 +171,9 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
173171
int failedCount = 0;
174172
for (JsonElement element : inputEventJsonArray) {
175173
try {
176-
JsonObject generatedEvent = (processEvent(msgProtocol, msgType,
174+
JsonObject generatedEvent = generateEvent(msgProtocol, msgType,
177175
failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit,
178-
okToLeaveOutInvalidOptionalFields, element.getAsJsonObject()));
176+
okToLeaveOutInvalidOptionalFields, element.getAsJsonObject());
179177
generatedEventResults.add(generatedEvent);
180178
successCount++;
181179
} catch (ProtocolHandlerNotFoundException e) {
@@ -202,20 +200,9 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
202200

203201
} else if (inputData.isJsonObject()) {
204202
JsonObject inputJsonObject = inputData.getAsJsonObject();
205-
JsonObject processedJson = processEvent(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound,
203+
JsonObject processedJson = generateEvent(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound,
206204
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);
219206
} else {
220207
return createResponseEntity(HttpStatus.BAD_REQUEST,
221208
"Invalid JSON format,expected either single template or array of templates",
@@ -270,6 +257,10 @@ public void initializeResponse(HttpStatus status, String resultMessage, String e
270257
*/
271258
private ResponseEntity<JsonObject> handleException(Exception e) {
272259
String exceptionMessage = e.getMessage();
260+
if (e instanceof ProtocolHandlerNotFoundException) {
261+
return createResponseEntity(HttpStatus.SERVICE_UNAVAILABLE, exceptionMessage, JSON_ERROR_STATUS);
262+
}
263+
273264
if (e instanceof REMGenerateException) {
274265
List<HttpStatus> statusList = List.of(
275266
HttpStatus.NOT_ACCEPTABLE, HttpStatus.EXPECTATION_FAILED, HttpStatus.SERVICE_UNAVAILABLE,
@@ -302,27 +293,25 @@ private ResponseEntity<JsonObject> handleException(Exception e) {
302293
* @param jsonObject The content of the message which is used in creating the event details.
303294
* @return JsonObject generated event
304295
*/
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 {
308299
JsonElement parsedResponse;
309300

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

313304
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");
316306
}
317307
String response = msgService.generateMsg(msgType, event, isLenientEnabled(okToLeaveOutInvalidOptionalFields));
318308
parsedResponse = JsonParser.parseString(response);
319309
JsonObject parsedJson = parsedResponse.getAsJsonObject();
320310

321311
if (parsedJson.has(JSON_ERROR_MESSAGE_FIELD)) {
322312
throw new REMGenerateException(response);
323-
} else {
324-
return parsedJson;
325313
}
314+
return parsedJson;
326315
}
327316

328317
private JsonObject erLookup(final JsonObject bodyJson, Boolean failIfMultipleFound, Boolean failIfNoneFound,

0 commit comments

Comments
 (0)