From 65fc7498b3a2c3eb41a27bbc819b75b79d18a11f Mon Sep 17 00:00:00 2001 From: Shudhansu Shekhar Date: Mon, 10 Jun 2024 17:17:51 +0530 Subject: [PATCH 01/13] Modified endpoint generateAndPublish to accept array of events --- .../controller/ProducerController.java | 132 +++++++++++------- 1 file changed, 82 insertions(+), 50 deletions(-) diff --git a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java index 90ff56f9..255788d3 100644 --- a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java +++ b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java @@ -14,8 +14,7 @@ */ package com.ericsson.eiffel.remrem.publish.controller; -import java.util.EnumSet; -import java.util.Map; +import java.util.*; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -193,65 +192,98 @@ public ResponseEntity generateAndPublish(@ApiParam(value = "message protocol", r @ApiParam(value = "okToLeaveOutInvalidOptionalFields true will remove the optional " + "event fields from the input event data that does not validate successfully, " + "and add those removed field information into customData/remremGenerateFailures") @RequestParam(value = "okToLeaveOutInvalidOptionalFields", required = false, defaultValue = "false") final Boolean okToLeaveOutInvalidOptionalFields, - @ApiParam(value = "JSON message", required = true) @RequestBody final JsonObject bodyJson) { + @ApiParam(value = "JSON message", required = true) @RequestBody final JsonElement bodyJson) { if (isAuthenticationEnabled) { logUserName(); } - String bodyJsonOut = null; - if(parseData) { - // -- parse params in incoming request -> body ------------- - EventTemplateHandler eventTemplateHandler = new EventTemplateHandler(); - JsonNode parsedTemplate = eventTemplateHandler.eventTemplateParser(bodyJson.toString(), msgType); - bodyJsonOut = parsedTemplate.toString(); - log.info("Parsed template: " + bodyJsonOut); - }else{ - bodyJsonOut = bodyJson.toString(); + List events = new ArrayList<>(); + if (bodyJson.isJsonObject()) { + events.add(bodyJson.getAsJsonObject()); + } else if (bodyJson.isJsonArray()) { + for (JsonElement element : bodyJson.getAsJsonArray()) { + if (element.isJsonObject()) { + events.add(element.getAsJsonObject()); + } else { + return new ResponseEntity<>("Invalid event content", HttpStatus.BAD_REQUEST); + } + } + } else { + return new ResponseEntity<>("Invalid event content", HttpStatus.BAD_REQUEST); } - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_JSON_UTF8); - HttpEntity entity = new HttpEntity<>(bodyJsonOut, headers); - EnumSet getStatus = EnumSet.of(HttpStatus.SERVICE_UNAVAILABLE, HttpStatus.UNAUTHORIZED, HttpStatus.NOT_ACCEPTABLE, HttpStatus.EXPECTATION_FAILED, HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.UNPROCESSABLE_ENTITY); - - try { - String generateUrl = generateURLTemplate.getUrl() + "&failIfMultipleFound=" + failIfMultipleFound - + "&failIfNoneFound=" + failIfNoneFound + "&lookupInExternalERs=" + lookupInExternalERs - + "&lookupLimit=" + lookupLimit + "&okToLeaveOutInvalidOptionalFields=" + okToLeaveOutInvalidOptionalFields; - ResponseEntity response = restTemplate.postForEntity(generateUrl, - entity, String.class, generateURLTemplate.getMap(msgProtocol, msgType)); - - if (response.getStatusCode() == HttpStatus.OK) { - log.info("The result from REMReM Generate is: " + response.getStatusCodeValue()); - - // publishing requires an array if you want status code - String responseBody = "[" + response.getBody() + "]"; - MsgService msgService = PublishUtils.getMessageService(msgProtocol, msgServices); - - log.debug("mp: " + msgProtocol); - log.debug("body: " + responseBody); - log.debug("user domain suffix: " + userDomain + " tag: " + tag + " routing key: " + routingKey); - if (msgService != null && msgProtocol != null) { - rmqHelper.rabbitMqPropertiesInit(msgProtocol); - } - synchronized(this) { - SendResult result = messageService.send(responseBody, msgService, userDomain, tag, routingKey); - return new ResponseEntity(result, messageService.getHttpStatus()); - } + List> responseEvents = new ArrayList<>(); + for (JsonObject eventJson : events) { + Map eventResponse = new HashMap<>(); + String bodyJsonOut = null; + if (parseData) { + // -- parse params in incoming request -> body ------------- + EventTemplateHandler eventTemplateHandler = new EventTemplateHandler(); + JsonNode parsedTemplate = eventTemplateHandler.eventTemplateParser(eventJson.toString(), msgType); + bodyJsonOut = parsedTemplate.toString(); + log.info("Parsed template: " + bodyJsonOut); } else { - return response; + bodyJsonOut = eventJson.toString(); } - } - catch (RemRemPublishException e) { + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON_UTF8); + HttpEntity entity = new HttpEntity<>(bodyJsonOut, headers); + EnumSet getStatus = EnumSet.of(HttpStatus.SERVICE_UNAVAILABLE, HttpStatus.UNAUTHORIZED, HttpStatus.NOT_ACCEPTABLE, HttpStatus.EXPECTATION_FAILED, HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.UNPROCESSABLE_ENTITY); + + try { + String generateUrl = generateURLTemplate.getUrl() + "&failIfMultipleFound=" + failIfMultipleFound + + "&failIfNoneFound=" + failIfNoneFound + "&lookupInExternalERs=" + lookupInExternalERs + + "&lookupLimit=" + lookupLimit + "&okToLeaveOutInvalidOptionalFields=" + okToLeaveOutInvalidOptionalFields; + ResponseEntity response = restTemplate.postForEntity(generateUrl, + entity, String.class, generateURLTemplate.getMap(msgProtocol, msgType)); + + + //eventResponse.put("ID", UUID.randomUUID().toString()); + //eventResponse.put("Status Code", response.getStatusCodeValue()); + + if (response.getStatusCode() == HttpStatus.OK) { + log.info("The result from REMReM Generate is: " + response.getStatusCodeValue()); + + // publishing requires an array if you want status code + String responseBody = "[" + response.getBody() + "]"; + MsgService msgService = PublishUtils.getMessageService(msgProtocol, msgServices); + + log.debug("mp: " + msgProtocol); + log.debug("body: " + responseBody); + log.debug("user domain suffix: " + userDomain + " tag: " + tag + " routing key: " + routingKey); + if (msgService != null && msgProtocol != null) { + rmqHelper.rabbitMqPropertiesInit(msgProtocol); + } + + synchronized (this) { + SendResult result = messageService.send(responseBody, msgService, userDomain, tag, routingKey); + //return new ResponseEntity(result, messageService.getHttpStatus()); + eventResponse.put("Result", result); + eventResponse.put("message", messageService.getHttpStatus()); + } + + + } else { + return response; + } + responseEvents.add(eventResponse); + } catch (RemRemPublishException e) { + responseEvents.add(eventResponse); return new ResponseEntity(e.getMessage(), HttpStatus.NOT_FOUND); - } - catch (HttpStatusCodeException e) { - HttpStatus result = HttpStatus.BAD_REQUEST; - if (getStatus.contains(e.getStatusCode())) { - result = e.getStatusCode(); + //eventResponse.put("Message", responseEvents) + } catch (HttpStatusCodeException e) { + HttpStatus result = HttpStatus.BAD_REQUEST; + if (getStatus.contains(e.getStatusCode())) { + result = e.getStatusCode(); + responseEvents.add(eventResponse); + } + return new ResponseEntity(parser.parse(e.getResponseBodyAsString()), result); } - return new ResponseEntity(parser.parse(e.getResponseBodyAsString()), result); } + Map responseBody = new HashMap<>(); + responseBody.put("events", responseEvents); + return new ResponseEntity<>(responseBody, HttpStatus.OK); } /** From 0136d8bdbfc5928a2de4649076ec4c01715ebc96 Mon Sep 17 00:00:00 2001 From: Shudhansu Shekhar Date: Wed, 19 Jun 2024 15:11:41 +0530 Subject: [PATCH 02/13] Modified endpoint generate to accept array of events --- .../controller/ProducerController.java | 169 +++++++++++------- 1 file changed, 107 insertions(+), 62 deletions(-) diff --git a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java index 255788d3..329e71d6 100644 --- a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java +++ b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java @@ -16,6 +16,7 @@ import java.util.*; +import com.google.gson.*; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -47,9 +48,6 @@ import com.ericsson.eiffel.remrem.publish.service.MessageService; import com.ericsson.eiffel.remrem.publish.service.SendResult; import com.fasterxml.jackson.databind.JsonNode; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; import ch.qos.logback.classic.Logger; import io.swagger.annotations.Api; @@ -192,7 +190,26 @@ public ResponseEntity generateAndPublish(@ApiParam(value = "message protocol", r @ApiParam(value = "okToLeaveOutInvalidOptionalFields true will remove the optional " + "event fields from the input event data that does not validate successfully, " + "and add those removed field information into customData/remremGenerateFailures") @RequestParam(value = "okToLeaveOutInvalidOptionalFields", required = false, defaultValue = "false") final Boolean okToLeaveOutInvalidOptionalFields, - @ApiParam(value = "JSON message", required = true) @RequestBody final JsonElement bodyJson) { + @ApiParam(value = "JSON message", required = true) @RequestBody final String body){ + + try { + JsonElement bodyJson = JsonParser.parseString(body); + return generateAndPublish(msgProtocol, msgType, userDomain, tag, routingKey, parseData, failIfMultipleFound, + failIfNoneFound, lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields, bodyJson); + } catch (JsonSyntaxException e) { + JsonObject errorResponse = new JsonObject(); + log.error("Unexpected exception caught due to parse json data", e.getMessage()); + String exceptionMessage = e.getMessage(); + errorResponse.addProperty("Status code", HttpStatus.BAD_REQUEST.value()); + errorResponse.addProperty("result", "Fatal"); + errorResponse.addProperty("message", "Invalid JSON parse data format due to: " + exceptionMessage); + return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + public ResponseEntity generateAndPublish(final String msgProtocol, final String msgType, final String userDomain, final String tag, final String routingKey, + final Boolean parseData, final Boolean failIfMultipleFound, final Boolean failIfNoneFound, final Boolean lookupInExternalERs, + final int lookupLimit, final Boolean okToLeaveOutInvalidOptionalFields, final JsonElement bodyJson) { if (isAuthenticationEnabled) { logUserName(); } @@ -205,85 +222,113 @@ public ResponseEntity generateAndPublish(@ApiParam(value = "message protocol", r if (element.isJsonObject()) { events.add(element.getAsJsonObject()); } else { - return new ResponseEntity<>("Invalid event content", HttpStatus.BAD_REQUEST); + return new ResponseEntity<>("Invalid events content or format", HttpStatus.BAD_REQUEST); } } } else { - return new ResponseEntity<>("Invalid event content", HttpStatus.BAD_REQUEST); + return new ResponseEntity<>("Invalid, event is not in the correct format", HttpStatus.BAD_REQUEST); } - List> responseEvents = new ArrayList<>(); - for (JsonObject eventJson : events) { - Map eventResponse = new HashMap<>(); + EnumSet getStatus = EnumSet.of(HttpStatus.SERVICE_UNAVAILABLE, HttpStatus.UNAUTHORIZED, + HttpStatus.NOT_ACCEPTABLE, HttpStatus.EXPECTATION_FAILED, HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.UNPROCESSABLE_ENTITY); + Map eventResponse; + try { String bodyJsonOut = null; if (parseData) { - // -- parse params in incoming request -> body ------------- EventTemplateHandler eventTemplateHandler = new EventTemplateHandler(); - JsonNode parsedTemplate = eventTemplateHandler.eventTemplateParser(eventJson.toString(), msgType); - bodyJsonOut = parsedTemplate.toString(); + StringBuffer parsedTempaltes = new StringBuffer(); + parsedTempaltes.append("["); + for (JsonElement eventJson : events) { + + // -- parse params in incoming request -> body ------------- + JsonNode parsedTemplate = eventTemplateHandler.eventTemplateParser(eventJson.toString(), msgType); + if (parsedTempaltes.length() > 1) { + parsedTempaltes.append(","); + } + parsedTempaltes.append(parsedTemplate.toString()); + } + parsedTempaltes.append("]"); + bodyJsonOut = parsedTempaltes.toString(); log.info("Parsed template: " + bodyJsonOut); } else { - bodyJsonOut = eventJson.toString(); + bodyJsonOut = bodyJson.toString(); } HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON_UTF8); HttpEntity entity = new HttpEntity<>(bodyJsonOut, headers); - EnumSet getStatus = EnumSet.of(HttpStatus.SERVICE_UNAVAILABLE, HttpStatus.UNAUTHORIZED, HttpStatus.NOT_ACCEPTABLE, HttpStatus.EXPECTATION_FAILED, HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.UNPROCESSABLE_ENTITY); - - try { - String generateUrl = generateURLTemplate.getUrl() + "&failIfMultipleFound=" + failIfMultipleFound - + "&failIfNoneFound=" + failIfNoneFound + "&lookupInExternalERs=" + lookupInExternalERs - + "&lookupLimit=" + lookupLimit + "&okToLeaveOutInvalidOptionalFields=" + okToLeaveOutInvalidOptionalFields; - ResponseEntity response = restTemplate.postForEntity(generateUrl, - entity, String.class, generateURLTemplate.getMap(msgProtocol, msgType)); - - //eventResponse.put("ID", UUID.randomUUID().toString()); - //eventResponse.put("Status Code", response.getStatusCodeValue()); - - if (response.getStatusCode() == HttpStatus.OK) { - log.info("The result from REMReM Generate is: " + response.getStatusCodeValue()); - - // publishing requires an array if you want status code - String responseBody = "[" + response.getBody() + "]"; - MsgService msgService = PublishUtils.getMessageService(msgProtocol, msgServices); - - log.debug("mp: " + msgProtocol); - log.debug("body: " + responseBody); - log.debug("user domain suffix: " + userDomain + " tag: " + tag + " routing key: " + routingKey); - if (msgService != null && msgProtocol != null) { - rmqHelper.rabbitMqPropertiesInit(msgProtocol); + String generateUrl = generateURLTemplate.getUrl() + "&failIfMultipleFound=" + failIfMultipleFound + + "&failIfNoneFound=" + failIfNoneFound + "&lookupInExternalERs=" + lookupInExternalERs + + "&lookupLimit=" + lookupLimit + "&okToLeaveOutInvalidOptionalFields=" + okToLeaveOutInvalidOptionalFields; + ResponseEntity response = restTemplate.postForEntity(generateUrl, + entity, String.class, generateURLTemplate.getMap(msgProtocol, msgType)); + + if (response.getStatusCode() == HttpStatus.OK) { + log.info("The result from REMReM Generate is: " + response.getStatusCodeValue()); + + JsonArray responseArray = JsonParser.parseString(response.getBody()).getAsJsonArray(); + for (int i = 0; i < responseArray.size(); i++) { + eventResponse = new HashMap<>(); + JsonElement jsonResponse = responseArray.get(i); + if (isValid(jsonResponse.getAsJsonObject())) { + + MsgService msgService = PublishUtils.getMessageService(msgProtocol, msgServices); + // publishing requires an array if you want status code + String responseBody = "[" + response.getBody() + "]"; + log.debug("mp: " + msgProtocol); + log.debug("body: " + responseBody); + log.debug("user domain suffix: " + userDomain + " tag: " + tag + " routing key: " + routingKey); + + if (msgService != null && msgProtocol != null) { + rmqHelper.rabbitMqPropertiesInit(msgProtocol); + } + + synchronized (this) { + JsonObject jsonObject = jsonResponse.getAsJsonObject(); + String validResponse = "[" + jsonObject.toString() + "]"; + SendResult result = messageService.send(validResponse, msgService, userDomain, tag, routingKey); + eventResponse.put("Result", result); + } + } else { + eventResponse.put("Status", HttpStatus.BAD_REQUEST); + eventResponse.put("Event", jsonResponse.toString()); } - - synchronized (this) { - SendResult result = messageService.send(responseBody, msgService, userDomain, tag, routingKey); - //return new ResponseEntity(result, messageService.getHttpStatus()); - eventResponse.put("Result", result); - eventResponse.put("message", messageService.getHttpStatus()); - } - - - } else { - return response; - } - responseEvents.add(eventResponse); - } catch (RemRemPublishException e) { - responseEvents.add(eventResponse); - return new ResponseEntity(e.getMessage(), HttpStatus.NOT_FOUND); - //eventResponse.put("Message", responseEvents) - } catch (HttpStatusCodeException e) { - HttpStatus result = HttpStatus.BAD_REQUEST; - if (getStatus.contains(e.getStatusCode())) { - result = e.getStatusCode(); responseEvents.add(eventResponse); } - return new ResponseEntity(parser.parse(e.getResponseBodyAsString()), result); + } else { + return new ResponseEntity<>("Invalid Json event content", HttpStatus.BAD_REQUEST); + } + } catch (RemRemPublishException e) { + eventResponse = new HashMap<>(); + eventResponse.put("Message", e.getMessage()); + return new ResponseEntity(eventResponse, HttpStatus.NOT_FOUND); + } catch (HttpStatusCodeException e) { + eventResponse = new HashMap<>(); + eventResponse.put("Message", e.getMessage()); + HttpStatus result = HttpStatus.BAD_REQUEST; + if (getStatus.contains(e.getStatusCode())) { + result = e.getStatusCode(); } + eventResponse.put("Message", result); + return new ResponseEntity(eventResponse, result); + } + + return new ResponseEntity<>(responseEvents, HttpStatus.OK); + } + + /** + * This helper method check the json event is valid or not + */ + + private Boolean isValid(JsonObject jsonObject) { + try { + return jsonObject.has("meta") && jsonObject.has("data") && + jsonObject.has("links") && !jsonObject.has("Status code"); + } catch (Exception e) { + log.error("Error while validating event: ", e.getMessage()); + return false; } - Map responseBody = new HashMap<>(); - responseBody.put("events", responseEvents); - return new ResponseEntity<>(responseBody, HttpStatus.OK); } /** From 14a5f7fbe61b55af129d967b974f4317424755e0 Mon Sep 17 00:00:00 2001 From: Shudhansu Shekhar Date: Thu, 11 Jul 2024 19:10:40 +0530 Subject: [PATCH 03/13] Modified endpoint generateAndPublish to accept array of events --- .../controller/ProducerController.java | 104 ++++++++++-------- 1 file changed, 60 insertions(+), 44 deletions(-) diff --git a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java index 329e71d6..531f01c6 100644 --- a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java +++ b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java @@ -84,6 +84,12 @@ public class ProducerController { private Logger log = (Logger) LoggerFactory.getLogger(ProducerController.class); + public final String JSON_STATUS_RESULT = "result"; + + public final String JSON_EVENT_MESSAGE_FIELD = "status message"; + + public final String JSON_EVENT_STATUS_VALUE = "status"; + public void setMsgServices(MsgService[] msgServices) { this.msgServices = msgServices; } @@ -200,9 +206,9 @@ public ResponseEntity generateAndPublish(@ApiParam(value = "message protocol", r JsonObject errorResponse = new JsonObject(); log.error("Unexpected exception caught due to parse json data", e.getMessage()); String exceptionMessage = e.getMessage(); - errorResponse.addProperty("Status code", HttpStatus.BAD_REQUEST.value()); - errorResponse.addProperty("result", "Fatal"); - errorResponse.addProperty("message", "Invalid JSON parse data format due to: " + exceptionMessage); + errorResponse.addProperty("status code", HttpStatus.BAD_REQUEST.value()); + errorResponse.addProperty(JSON_STATUS_RESULT, "fatal"); + errorResponse.addProperty(JSON_EVENT_MESSAGE_FIELD, "Invalid JSON parse data format due to: " + exceptionMessage); return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR); } } @@ -214,6 +220,7 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String logUserName(); } + MsgService msgService = PublishUtils.getMessageService(msgProtocol, msgServices); List events = new ArrayList<>(); if (bodyJson.isJsonObject()) { events.add(bodyJson.getAsJsonObject()); @@ -222,11 +229,11 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String if (element.isJsonObject()) { events.add(element.getAsJsonObject()); } else { - return new ResponseEntity<>("Invalid events content or format", HttpStatus.BAD_REQUEST); + return new ResponseEntity<>("Invalid, array events must be a JSON object", HttpStatus.BAD_REQUEST); } } } else { - return new ResponseEntity<>("Invalid, event is not in the correct format", HttpStatus.BAD_REQUEST); + return new ResponseEntity<>("Invalid, event is neither in the form of JSON object nor in the JSON array", HttpStatus.BAD_REQUEST); } List> responseEvents = new ArrayList<>(); EnumSet getStatus = EnumSet.of(HttpStatus.SERVICE_UNAVAILABLE, HttpStatus.UNAUTHORIZED, @@ -239,7 +246,6 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String StringBuffer parsedTempaltes = new StringBuffer(); parsedTempaltes.append("["); for (JsonElement eventJson : events) { - // -- parse params in incoming request -> body ------------- JsonNode parsedTemplate = eventTemplateHandler.eventTemplateParser(eventJson.toString(), msgType); if (parsedTempaltes.length() > 1) { @@ -253,67 +259,77 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String } else { bodyJsonOut = bodyJson.toString(); } - HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON_UTF8); HttpEntity entity = new HttpEntity<>(bodyJsonOut, headers); - String generateUrl = generateURLTemplate.getUrl() + "&failIfMultipleFound=" + failIfMultipleFound + "&failIfNoneFound=" + failIfNoneFound + "&lookupInExternalERs=" + lookupInExternalERs + "&lookupLimit=" + lookupLimit + "&okToLeaveOutInvalidOptionalFields=" + okToLeaveOutInvalidOptionalFields; + ResponseEntity response = restTemplate.postForEntity(generateUrl, entity, String.class, generateURLTemplate.getMap(msgProtocol, msgType)); + String responseBody = null; + if (bodyJson.isJsonObject()) { + responseBody = "[" + response.getBody() + "]"; + } else if (bodyJson.isJsonArray()) { + responseBody = response.getBody(); + } if (response.getStatusCode() == HttpStatus.OK) { log.info("The result from REMReM Generate is: " + response.getStatusCodeValue()); + log.debug("mp: " + msgProtocol); + log.debug("body: " + responseBody); + log.debug("user domain suffix: " + userDomain + " tag: " + tag + " routing key: " + routingKey); - JsonArray responseArray = JsonParser.parseString(response.getBody()).getAsJsonArray(); - for (int i = 0; i < responseArray.size(); i++) { + if (msgService != null && msgProtocol != null) { + rmqHelper.rabbitMqPropertiesInit(msgProtocol); + } + JsonElement element = JsonParser.parseString(responseBody); + + for (int i = 0; i < element.getAsJsonArray().size(); i++) { eventResponse = new HashMap<>(); - JsonElement jsonResponse = responseArray.get(i); - if (isValid(jsonResponse.getAsJsonObject())) { - - MsgService msgService = PublishUtils.getMessageService(msgProtocol, msgServices); - // publishing requires an array if you want status code - String responseBody = "[" + response.getBody() + "]"; - log.debug("mp: " + msgProtocol); - log.debug("body: " + responseBody); - log.debug("user domain suffix: " + userDomain + " tag: " + tag + " routing key: " + routingKey); - - if (msgService != null && msgProtocol != null) { - rmqHelper.rabbitMqPropertiesInit(msgProtocol); + JsonElement jsonResponseEvent = element.getAsJsonArray().get(i); + if (isValid(jsonResponseEvent)) { + synchronized (this) { + JsonObject validResponse = jsonResponseEvent.getAsJsonObject(); + String validResponseBody = validResponse.toString(); + SendResult result = messageService.send(validResponseBody, msgService, userDomain, tag, routingKey); + eventResponse.put(JSON_STATUS_RESULT, result); } - - synchronized (this) { - JsonObject jsonObject = jsonResponse.getAsJsonObject(); - String validResponse = "[" + jsonObject.toString() + "]"; - SendResult result = messageService.send(validResponse, msgService, userDomain, tag, routingKey); - eventResponse.put("Result", result); - } } else { - eventResponse.put("Status", HttpStatus.BAD_REQUEST); - eventResponse.put("Event", jsonResponse.toString()); + eventResponse.put(JSON_EVENT_STATUS_VALUE, HttpStatus.BAD_REQUEST); + eventResponse.put("like", HttpStatus.ACCEPTED); + eventResponse.put(JSON_EVENT_MESSAGE_FIELD, jsonResponseEvent); } responseEvents.add(eventResponse); } } else { - return new ResponseEntity<>("Invalid Json event content", HttpStatus.BAD_REQUEST); + return response; } } catch (RemRemPublishException e) { eventResponse = new HashMap<>(); - eventResponse.put("Message", e.getMessage()); + eventResponse.put(JSON_EVENT_MESSAGE_FIELD, e.getMessage()); return new ResponseEntity(eventResponse, HttpStatus.NOT_FOUND); } catch (HttpStatusCodeException e) { - eventResponse = new HashMap<>(); - eventResponse.put("Message", e.getMessage()); - HttpStatus result = HttpStatus.BAD_REQUEST; - if (getStatus.contains(e.getStatusCode())) { - result = e.getStatusCode(); + JsonElement element = JsonParser.parseString(e.getResponseBodyAsString()); + + for (int i = 0; i < element.getAsJsonArray().size(); i++) { + eventResponse = new HashMap<>(); + JsonElement JsonResponseEvent = element.getAsJsonArray().get(i); + if (isValid(JsonResponseEvent)) { + synchronized (this) { + JsonObject validResponse = JsonResponseEvent.getAsJsonObject(); + String validResponseBody = validResponse.toString(); + SendResult result = messageService.send(validResponseBody, msgService, userDomain, tag, routingKey); + eventResponse.put(JSON_STATUS_RESULT, result); + } + } else { + eventResponse.put(JSON_EVENT_MESSAGE_FIELD, JsonResponseEvent); + } + responseEvents.add(eventResponse); } - eventResponse.put("Message", result); - return new ResponseEntity(eventResponse, result); + return new ResponseEntity<>(responseEvents, HttpStatus.BAD_REQUEST); } - return new ResponseEntity<>(responseEvents, HttpStatus.OK); } @@ -321,10 +337,10 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String * This helper method check the json event is valid or not */ - private Boolean isValid(JsonObject jsonObject) { + private Boolean isValid(JsonElement jsonObject) { try { - return jsonObject.has("meta") && jsonObject.has("data") && - jsonObject.has("links") && !jsonObject.has("Status code"); + return jsonObject.getAsJsonObject().has("meta") && jsonObject.getAsJsonObject().has("data") && + jsonObject.getAsJsonObject().has("links") && !jsonObject.getAsJsonObject().has("Status code"); } catch (Exception e) { log.error("Error while validating event: ", e.getMessage()); return false; From 2d55610ad1a8a882dc5800e102e6c07adf8c14a8 Mon Sep 17 00:00:00 2001 From: Shudhansu Shekhar Date: Tue, 16 Jul 2024 18:05:08 +0530 Subject: [PATCH 04/13] Modified endpoint generateAndPublish to accept array of events --- .../controller/ProducerController.java | 87 +++++++++---------- 1 file changed, 43 insertions(+), 44 deletions(-) diff --git a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java index 531f01c6..e83ea061 100644 --- a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java +++ b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java @@ -88,7 +88,9 @@ public class ProducerController { public final String JSON_EVENT_MESSAGE_FIELD = "status message"; - public final String JSON_EVENT_STATUS_VALUE = "status"; + public final String STATUS_CODE = "Status code"; + + public static final String META = "meta"; public void setMsgServices(MsgService[] msgServices) { this.msgServices = msgServices; @@ -206,7 +208,7 @@ public ResponseEntity generateAndPublish(@ApiParam(value = "message protocol", r JsonObject errorResponse = new JsonObject(); log.error("Unexpected exception caught due to parse json data", e.getMessage()); String exceptionMessage = e.getMessage(); - errorResponse.addProperty("status code", HttpStatus.BAD_REQUEST.value()); + errorResponse.addProperty(STATUS_CODE, HttpStatus.BAD_REQUEST.value()); errorResponse.addProperty(JSON_STATUS_RESULT, "fatal"); errorResponse.addProperty(JSON_EVENT_MESSAGE_FIELD, "Invalid JSON parse data format due to: " + exceptionMessage); return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR); @@ -223,11 +225,11 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String MsgService msgService = PublishUtils.getMessageService(msgProtocol, msgServices); List events = new ArrayList<>(); if (bodyJson.isJsonObject()) { - events.add(bodyJson.getAsJsonObject()); + events.add(getAsJsonObject(bodyJson)); } else if (bodyJson.isJsonArray()) { for (JsonElement element : bodyJson.getAsJsonArray()) { if (element.isJsonObject()) { - events.add(element.getAsJsonObject()); + events.add(getAsJsonObject(element)); } else { return new ResponseEntity<>("Invalid, array events must be a JSON object", HttpStatus.BAD_REQUEST); } @@ -284,25 +286,8 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String if (msgService != null && msgProtocol != null) { rmqHelper.rabbitMqPropertiesInit(msgProtocol); } - JsonElement element = JsonParser.parseString(responseBody); - - for (int i = 0; i < element.getAsJsonArray().size(); i++) { - eventResponse = new HashMap<>(); - JsonElement jsonResponseEvent = element.getAsJsonArray().get(i); - if (isValid(jsonResponseEvent)) { - synchronized (this) { - JsonObject validResponse = jsonResponseEvent.getAsJsonObject(); - String validResponseBody = validResponse.toString(); - SendResult result = messageService.send(validResponseBody, msgService, userDomain, tag, routingKey); - eventResponse.put(JSON_STATUS_RESULT, result); - } - } else { - eventResponse.put(JSON_EVENT_STATUS_VALUE, HttpStatus.BAD_REQUEST); - eventResponse.put("like", HttpStatus.ACCEPTED); - eventResponse.put(JSON_EVENT_MESSAGE_FIELD, jsonResponseEvent); - } - responseEvents.add(eventResponse); - } + responseEvents = processingValidEvent(responseBody, msgProtocol, userDomain, + tag, routingKey); } else { return response; } @@ -311,37 +296,51 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String eventResponse.put(JSON_EVENT_MESSAGE_FIELD, e.getMessage()); return new ResponseEntity(eventResponse, HttpStatus.NOT_FOUND); } catch (HttpStatusCodeException e) { - JsonElement element = JsonParser.parseString(e.getResponseBodyAsString()); - - for (int i = 0; i < element.getAsJsonArray().size(); i++) { - eventResponse = new HashMap<>(); - JsonElement JsonResponseEvent = element.getAsJsonArray().get(i); - if (isValid(JsonResponseEvent)) { - synchronized (this) { - JsonObject validResponse = JsonResponseEvent.getAsJsonObject(); - String validResponseBody = validResponse.toString(); - SendResult result = messageService.send(validResponseBody, msgService, userDomain, tag, routingKey); - eventResponse.put(JSON_STATUS_RESULT, result); - } - } else { - eventResponse.put(JSON_EVENT_MESSAGE_FIELD, JsonResponseEvent); - } - responseEvents.add(eventResponse); - } + responseEvents = processingValidEvent(e.getResponseBodyAsString(), msgProtocol, userDomain, + tag, routingKey); return new ResponseEntity<>(responseEvents, HttpStatus.BAD_REQUEST); } return new ResponseEntity<>(responseEvents, HttpStatus.OK); } + public List> processingValidEvent(String eventMsg, final String msgProtocol, final String userDomain, + final String tag, final String routingKey) { + MsgService msgService = PublishUtils.getMessageService(msgProtocol, msgServices); + List> responseEvent = new ArrayList<>(); + Map eventResponse; + JsonElement element = JsonParser.parseString(eventMsg); + for (int i = 0; i < element.getAsJsonArray().size(); i++) { + eventResponse = new HashMap<>(); + JsonElement jsonResponseEvent = element.getAsJsonArray().get(i); + if (isValid(jsonResponseEvent)) { + synchronized (this) { + JsonObject validResponse = jsonResponseEvent.getAsJsonObject(); + String validResponseBody = validResponse.toString(); + SendResult result = messageService.send(validResponseBody, msgService, userDomain, tag, routingKey); + eventResponse.put(JSON_STATUS_RESULT, result); + } + } else { + eventResponse.put(JSON_EVENT_MESSAGE_FIELD, jsonResponseEvent); + } + responseEvent.add(eventResponse); + } + return responseEvent; + } + + public JsonObject getAsJsonObject(JsonElement jsonElement) { + JsonObject jsonObject = jsonElement.getAsJsonObject(); + return jsonObject; + } + /** * This helper method check the json event is valid or not */ - private Boolean isValid(JsonElement jsonObject) { + private Boolean isValid(JsonElement jsonElement) { try { - return jsonObject.getAsJsonObject().has("meta") && jsonObject.getAsJsonObject().has("data") && - jsonObject.getAsJsonObject().has("links") && !jsonObject.getAsJsonObject().has("Status code"); - } catch (Exception e) { + return getAsJsonObject(jsonElement).has(META) && getAsJsonObject(jsonElement).has("data") && + getAsJsonObject(jsonElement).has("links") && !getAsJsonObject(jsonElement).has(STATUS_CODE); + } catch (IllegalStateException e) { log.error("Error while validating event: ", e.getMessage()); return false; } From 04c79e6c094a1717b6d36b935ea3cfe1a2d9855d Mon Sep 17 00:00:00 2001 From: Shudhansu Shekhar Date: Wed, 31 Jul 2024 15:20:00 +0530 Subject: [PATCH 05/13] Modified endpoint generateAndPublish to accept array of events --- .../controller/ProducerController.java | 81 ++++++++++++++----- 1 file changed, 61 insertions(+), 20 deletions(-) diff --git a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java index e83ea061..4f1af2e0 100644 --- a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java +++ b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java @@ -92,6 +92,12 @@ public class ProducerController { public static final String META = "meta"; + public static final String DATA = "data"; + + public static final String LINKS = "links"; + + public static final String JSON_FATAL_STATUS = "fatal"; + public void setMsgServices(MsgService[] msgServices) { this.msgServices = msgServices; } @@ -154,7 +160,6 @@ public ResponseEntity send( /** * This controller provides single RemRem REST API End Point for both RemRem * Generate and Publish. - * * @param msgProtocol * message protocol (required) * @param msgType @@ -167,6 +172,9 @@ public ResponseEntity send( * (not required) * @param parseData * (not required, default=false) + * @param body + * (Here json body of string type as input because just to parse + * the string in to JsonElement not using JsonElement directly here.) * @return A response entity which contains http status and result * * @use A typical CURL command: curl -H "Content-Type: application/json" -X POST @@ -205,16 +213,39 @@ public ResponseEntity generateAndPublish(@ApiParam(value = "message protocol", r return generateAndPublish(msgProtocol, msgType, userDomain, tag, routingKey, parseData, failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields, bodyJson); } catch (JsonSyntaxException e) { - JsonObject errorResponse = new JsonObject(); - log.error("Unexpected exception caught due to parse json data", e.getMessage()); String exceptionMessage = e.getMessage(); + JsonObject errorResponse = new JsonObject(); + log.error("Unexpected exception caught due to parsed json data", exceptionMessage); errorResponse.addProperty(STATUS_CODE, HttpStatus.BAD_REQUEST.value()); - errorResponse.addProperty(JSON_STATUS_RESULT, "fatal"); + errorResponse.addProperty(JSON_STATUS_RESULT, JSON_FATAL_STATUS); errorResponse.addProperty(JSON_EVENT_MESSAGE_FIELD, "Invalid JSON parse data format due to: " + exceptionMessage); return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR); } } + /** + * This controller provides single RemRem REST API End Point for both RemRem + * Generate and Publish. + * + * @param msgProtocol + * message protocol (required) + * @param msgType + * message type (required) + * @param userDomain + * user domain (not required) + * @param tag + * (not required) + * @param routingKey + * (not required) + * @param parseData + * (not required, default=false) + * @return A response entity which contains http status and result + * + * @use A typical CURL command: curl -H "Content-Type: application/json" -X POST + * --data "@inputGenerate_activity_finished.txt" + * "http://localhost:8986/generateAndPublish/?mp=eiffelsemantics&msgType=EiffelActivityFinished" + */ + public ResponseEntity generateAndPublish(final String msgProtocol, final String msgType, final String userDomain, final String tag, final String routingKey, final Boolean parseData, final Boolean failIfMultipleFound, final Boolean failIfNoneFound, final Boolean lookupInExternalERs, final int lookupLimit, final Boolean okToLeaveOutInvalidOptionalFields, final JsonElement bodyJson) { @@ -237,26 +268,26 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String } else { return new ResponseEntity<>("Invalid, event is neither in the form of JSON object nor in the JSON array", HttpStatus.BAD_REQUEST); } - List> responseEvents = new ArrayList<>(); + List> responseEvents; EnumSet getStatus = EnumSet.of(HttpStatus.SERVICE_UNAVAILABLE, HttpStatus.UNAUTHORIZED, HttpStatus.NOT_ACCEPTABLE, HttpStatus.EXPECTATION_FAILED, HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.UNPROCESSABLE_ENTITY); Map eventResponse; try { - String bodyJsonOut = null; + String bodyJsonOut; if (parseData) { EventTemplateHandler eventTemplateHandler = new EventTemplateHandler(); - StringBuffer parsedTempaltes = new StringBuffer(); - parsedTempaltes.append("["); + StringBuffer parsedTemplates = new StringBuffer(); + parsedTemplates.append("["); for (JsonElement eventJson : events) { // -- parse params in incoming request -> body ------------- JsonNode parsedTemplate = eventTemplateHandler.eventTemplateParser(eventJson.toString(), msgType); - if (parsedTempaltes.length() > 1) { - parsedTempaltes.append(","); + if (parsedTemplates.length() > 1) { + parsedTemplates.append(","); } - parsedTempaltes.append(parsedTemplate.toString()); + parsedTemplates.append(parsedTemplate.toString()); } - parsedTempaltes.append("]"); - bodyJsonOut = parsedTempaltes.toString(); + parsedTemplates.append("]"); + bodyJsonOut = parsedTemplates.toString(); log.info("Parsed template: " + bodyJsonOut); } else { bodyJsonOut = bodyJson.toString(); @@ -294,7 +325,7 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String } catch (RemRemPublishException e) { eventResponse = new HashMap<>(); eventResponse.put(JSON_EVENT_MESSAGE_FIELD, e.getMessage()); - return new ResponseEntity(eventResponse, HttpStatus.NOT_FOUND); + return new ResponseEntity<>(eventResponse, HttpStatus.NOT_FOUND); } catch (HttpStatusCodeException e) { responseEvents = processingValidEvent(e.getResponseBodyAsString(), msgProtocol, userDomain, tag, routingKey); @@ -303,15 +334,24 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String return new ResponseEntity<>(responseEvents, HttpStatus.OK); } + /** + * This one is helper method publish messages on mb + * @param eventMsg + * @param msgProtocol + * @param userDomain + * @param tag + * @param routingKey + * @return list of responses + */ public List> processingValidEvent(String eventMsg, final String msgProtocol, final String userDomain, final String tag, final String routingKey) { MsgService msgService = PublishUtils.getMessageService(msgProtocol, msgServices); List> responseEvent = new ArrayList<>(); Map eventResponse; - JsonElement element = JsonParser.parseString(eventMsg); - for (int i = 0; i < element.getAsJsonArray().size(); i++) { + JsonElement eventElement = JsonParser.parseString(eventMsg); + for (int i = 0; i < eventElement.getAsJsonArray().size(); i++) { eventResponse = new HashMap<>(); - JsonElement jsonResponseEvent = element.getAsJsonArray().get(i); + JsonElement jsonResponseEvent = eventElement.getAsJsonArray().get(i); if (isValid(jsonResponseEvent)) { synchronized (this) { JsonObject validResponse = jsonResponseEvent.getAsJsonObject(); @@ -334,12 +374,13 @@ public JsonObject getAsJsonObject(JsonElement jsonElement) { /** * This helper method check the json event is valid or not + * @param jsonElement AN event which need to check + * @return boolean type value like it is valid or not */ - private Boolean isValid(JsonElement jsonElement) { try { - return getAsJsonObject(jsonElement).has(META) && getAsJsonObject(jsonElement).has("data") && - getAsJsonObject(jsonElement).has("links") && !getAsJsonObject(jsonElement).has(STATUS_CODE); + return getAsJsonObject(jsonElement).has(META) && getAsJsonObject(jsonElement).has(DATA) && + getAsJsonObject(jsonElement).has(LINKS) && !getAsJsonObject(jsonElement).has(STATUS_CODE); } catch (IllegalStateException e) { log.error("Error while validating event: ", e.getMessage()); return false; From 5566a4d998bab3151b61143292f5c63552202adb Mon Sep 17 00:00:00 2001 From: Shudhansu Shekhar Date: Thu, 1 Aug 2024 12:16:23 +0530 Subject: [PATCH 06/13] Modified endpoint generateAndPublish to accept array of events --- .../controller/ProducerController.java | 104 ++++++++++++++---- 1 file changed, 81 insertions(+), 23 deletions(-) diff --git a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java index 4f1af2e0..803ca0fd 100644 --- a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java +++ b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java @@ -88,7 +88,7 @@ public class ProducerController { public final String JSON_EVENT_MESSAGE_FIELD = "status message"; - public final String STATUS_CODE = "Status code"; + public final String JSON_STATUS_CODE = "status code"; public static final String META = "meta"; @@ -98,6 +98,8 @@ public class ProducerController { public static final String JSON_FATAL_STATUS = "fatal"; + public static final String JSON_ERROR_STATUS = "fail"; + public void setMsgServices(MsgService[] msgServices) { this.msgServices = msgServices; } @@ -214,12 +216,9 @@ public ResponseEntity generateAndPublish(@ApiParam(value = "message protocol", r failIfNoneFound, lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields, bodyJson); } catch (JsonSyntaxException e) { String exceptionMessage = e.getMessage(); - JsonObject errorResponse = new JsonObject(); log.error("Unexpected exception caught due to parsed json data", exceptionMessage); - errorResponse.addProperty(STATUS_CODE, HttpStatus.BAD_REQUEST.value()); - errorResponse.addProperty(JSON_STATUS_RESULT, JSON_FATAL_STATUS); - errorResponse.addProperty(JSON_EVENT_MESSAGE_FIELD, "Invalid JSON parse data format due to: " + exceptionMessage); - return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR); + return createResponseEntity(HttpStatus.BAD_REQUEST, JSON_FATAL_STATUS, + "Invalid JSON parse data format due to: " + exceptionMessage); } } @@ -254,6 +253,10 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String } MsgService msgService = PublishUtils.getMessageService(msgProtocol, msgServices); + if (msgService == null) { + return createResponseEntity(HttpStatus.BAD_REQUEST, JSON_ERROR_STATUS, + "No protocol service has been found registered"); + } List events = new ArrayList<>(); if (bodyJson.isJsonObject()) { events.add(getAsJsonObject(bodyJson)); @@ -262,16 +265,17 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String if (element.isJsonObject()) { events.add(getAsJsonObject(element)); } else { - return new ResponseEntity<>("Invalid, array events must be a JSON object", HttpStatus.BAD_REQUEST); + return createResponseEntity(HttpStatus.BAD_REQUEST, JSON_ERROR_STATUS, + "Invalid, array events must be a JSON object"); } } } else { - return new ResponseEntity<>("Invalid, event is neither in the form of JSON object nor in the JSON array", HttpStatus.BAD_REQUEST); + return createResponseEntity(HttpStatus.BAD_REQUEST, JSON_ERROR_STATUS, + "Invalid, event is neither in the form of JSON object nor in the JSON array"); } List> responseEvents; EnumSet getStatus = EnumSet.of(HttpStatus.SERVICE_UNAVAILABLE, HttpStatus.UNAUTHORIZED, HttpStatus.NOT_ACCEPTABLE, HttpStatus.EXPECTATION_FAILED, HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.UNPROCESSABLE_ENTITY); - Map eventResponse; try { String bodyJsonOut; if (parseData) { @@ -323,35 +327,84 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String return response; } } catch (RemRemPublishException e) { - eventResponse = new HashMap<>(); - eventResponse.put(JSON_EVENT_MESSAGE_FIELD, e.getMessage()); - return new ResponseEntity<>(eventResponse, HttpStatus.NOT_FOUND); + String exceptionMessage = e.getMessage(); + return createResponseEntity(HttpStatus.NOT_FOUND, JSON_ERROR_STATUS, exceptionMessage); } catch (HttpStatusCodeException e) { - responseEvents = processingValidEvent(e.getResponseBodyAsString(), msgProtocol, userDomain, - tag, routingKey); + String responseBody = null; + String responseMessage = e.getResponseBodyAsString(); + if (bodyJson.isJsonObject()) { + responseBody = "[" + responseMessage + "]"; + } else if (bodyJson.isJsonArray()) { + responseBody = responseMessage; + } + responseEvents = processingValidEvent(responseBody, msgProtocol, userDomain, tag, routingKey); return new ResponseEntity<>(responseEvents, HttpStatus.BAD_REQUEST); } return new ResponseEntity<>(responseEvents, HttpStatus.OK); } /** - * This one is helper method publish messages on mb - * @param eventMsg + * To display response in browser or application + * @param status response code for the HTTP request + * @param responseMessage the message according to response + * @param resultMessage whatever the result this message gives you idea about that + * @param errorResponse is to collect all the responses here. + * @return ResponseEntity + */ + public ResponseEntity createResponseEntity(HttpStatus status, String responseMessage, String resultMessage, + JsonObject errorResponse) { + initializeResponse(status, responseMessage, resultMessage, errorResponse); + return new ResponseEntity<>(errorResponse, status); + } + + /** + * To display response in browser or application + * @param status response code for the HTTP request + * @param responseMessage the message according to response + * @param resultMessage whatever the result this message gives you idea about that + * @return ResponseEntity + */ + public ResponseEntity createResponseEntity(HttpStatus status, String responseMessage, String resultMessage) { + return createResponseEntity(status, responseMessage, resultMessage, new JsonObject()); + } + + /** + * To initialize in the @{createResponseEntity} method + * @param status response code for the HTTP request + * @param resultMessage whatever the result this message gives you idea about that + * @param errorResponse is to collect all the responses here. + */ + public void initializeResponse(HttpStatus status, String errorMessage, String resultMessage, JsonObject errorResponse) { + errorResponse.addProperty(JSON_STATUS_CODE, status.value()); + errorResponse.addProperty(JSON_STATUS_RESULT, resultMessage); + errorResponse.addProperty(JSON_EVENT_MESSAGE_FIELD, errorMessage); + } + + /** + * This one is helper method to process or publish messages or event + * @param eventResponseMessage * @param msgProtocol * @param userDomain * @param tag * @param routingKey * @return list of responses */ - public List> processingValidEvent(String eventMsg, final String msgProtocol, final String userDomain, + public List> processingValidEvent(String eventResponseMessage, final String msgProtocol, final String userDomain, final String tag, final String routingKey) { MsgService msgService = PublishUtils.getMessageService(msgProtocol, msgServices); List> responseEvent = new ArrayList<>(); - Map eventResponse; - JsonElement eventElement = JsonParser.parseString(eventMsg); - for (int i = 0; i < eventElement.getAsJsonArray().size(); i++) { + Map eventResponse = null; + + if (eventResponseMessage == null) { + eventResponse.put("Parameter 'eventResponseMessage' must not be null", HttpStatus.BAD_REQUEST); + responseEvent.add(eventResponse); + return responseEvent; + } + JsonElement eventElement = JsonParser.parseString(eventResponseMessage); + JsonArray eventArray = eventElement.getAsJsonArray(); + for (int i = 0; i < eventArray.size(); i++) { eventResponse = new HashMap<>(); - JsonElement jsonResponseEvent = eventElement.getAsJsonArray().get(i); + JsonElement jsonResponseEvent = eventArray.get(i); if (isValid(jsonResponseEvent)) { synchronized (this) { JsonObject validResponse = jsonResponseEvent.getAsJsonObject(); @@ -367,6 +420,11 @@ public List> processingValidEvent(String eventMsg, final Str return responseEvent; } + /** + * This helper method to get JsonObject type from JsonElement + * @param jsonElement AN event which need to convert + * @return JsonObject converted Json of JsonObject type + */ public JsonObject getAsJsonObject(JsonElement jsonElement) { JsonObject jsonObject = jsonElement.getAsJsonObject(); return jsonObject; @@ -377,10 +435,10 @@ public JsonObject getAsJsonObject(JsonElement jsonElement) { * @param jsonElement AN event which need to check * @return boolean type value like it is valid or not */ - private Boolean isValid(JsonElement jsonElement) { + private Boolean isValid(JsonElement jsonElement) { try { return getAsJsonObject(jsonElement).has(META) && getAsJsonObject(jsonElement).has(DATA) && - getAsJsonObject(jsonElement).has(LINKS) && !getAsJsonObject(jsonElement).has(STATUS_CODE); + getAsJsonObject(jsonElement).has(LINKS) && !getAsJsonObject(jsonElement).has(JSON_STATUS_CODE); } catch (IllegalStateException e) { log.error("Error while validating event: ", e.getMessage()); return false; From 7870507ee870057737219ba991530264edc22d64 Mon Sep 17 00:00:00 2001 From: Shudhansu Shekhar Date: Fri, 2 Aug 2024 15:07:06 +0530 Subject: [PATCH 07/13] Modified endpoint generateAndPublish to accept array of events --- .../controller/ProducerController.java | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java index 803ca0fd..367479a9 100644 --- a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java +++ b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java @@ -96,9 +96,9 @@ public class ProducerController { public static final String LINKS = "links"; - public static final String JSON_FATAL_STATUS = "fatal"; + public static final String JSON_FATAL_STATUS = "FATAL"; - public static final String JSON_ERROR_STATUS = "fail"; + public static final String JSON_ERROR_STATUS = "FAIL"; public void setMsgServices(MsgService[] msgServices) { this.msgServices = msgServices; @@ -396,18 +396,28 @@ public List> processingValidEvent(String eventResponseMessag Map eventResponse = null; if (eventResponseMessage == null) { - eventResponse.put("Parameter 'eventResponseMessage' must not be null", HttpStatus.BAD_REQUEST); + eventResponse.put("Missing response from 'generate' service", HttpStatus.BAD_REQUEST); responseEvent.add(eventResponse); return responseEvent; } JsonElement eventElement = JsonParser.parseString(eventResponseMessage); JsonArray eventArray = eventElement.getAsJsonArray(); + if (eventArray == null) { + String errorMessage = "Invalid, response json event is not in the form of JSON array"; + log.error(errorMessage); + eventResponse.put(errorMessage, HttpStatus.BAD_REQUEST); + } for (int i = 0; i < eventArray.size(); i++) { eventResponse = new HashMap<>(); JsonElement jsonResponseEvent = eventArray.get(i); if (isValid(jsonResponseEvent)) { synchronized (this) { JsonObject validResponse = jsonResponseEvent.getAsJsonObject(); + if (validResponse == null) { + String errorMessage = "Invalid, response json event is not in the form of JSON object"; + log.error(errorMessage); + eventResponse.put(errorMessage, HttpStatus.BAD_REQUEST); + } String validResponseBody = validResponse.toString(); SendResult result = messageService.send(validResponseBody, msgService, userDomain, tag, routingKey); eventResponse.put(JSON_STATUS_RESULT, result); @@ -435,12 +445,13 @@ public JsonObject getAsJsonObject(JsonElement jsonElement) { * @param jsonElement AN event which need to check * @return boolean type value like it is valid or not */ - private Boolean isValid(JsonElement jsonElement) { + private boolean isValid(JsonElement jsonElement) { try { - return getAsJsonObject(jsonElement).has(META) && getAsJsonObject(jsonElement).has(DATA) && - getAsJsonObject(jsonElement).has(LINKS) && !getAsJsonObject(jsonElement).has(JSON_STATUS_CODE); + JsonObject jsonObject = getAsJsonObject(jsonElement); + return jsonObject.has(META) && jsonObject.has(DATA) && + jsonObject.has(LINKS) && !jsonObject.has(JSON_STATUS_CODE); } catch (IllegalStateException e) { - log.error("Error while validating event: ", e.getMessage()); + log.error("Error while validating json event: ", e.getMessage()); return false; } } From be8ff35ba5d4032de4f377382bc2f4dd5f29f752 Mon Sep 17 00:00:00 2001 From: Shudhansu Shekhar Date: Mon, 5 Aug 2024 17:48:50 +0530 Subject: [PATCH 08/13] Modified endpoint generateAndPublish to accept array of events --- .../remrem/publish/controller/ProducerController.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java index 367479a9..07ccd871 100644 --- a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java +++ b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java @@ -403,7 +403,7 @@ public List> processingValidEvent(String eventResponseMessag JsonElement eventElement = JsonParser.parseString(eventResponseMessage); JsonArray eventArray = eventElement.getAsJsonArray(); if (eventArray == null) { - String errorMessage = "Invalid, response json event is not in the form of JSON array"; + String errorMessage = "Invalid, array item expected to be in the form of JSON array"; log.error(errorMessage); eventResponse.put(errorMessage, HttpStatus.BAD_REQUEST); } @@ -414,7 +414,7 @@ public List> processingValidEvent(String eventResponseMessag synchronized (this) { JsonObject validResponse = jsonResponseEvent.getAsJsonObject(); if (validResponse == null) { - String errorMessage = "Invalid, response json event is not in the form of JSON object"; + String errorMessage = "Invalid, array item expected to be in the form of JSON object"; log.error(errorMessage); eventResponse.put(errorMessage, HttpStatus.BAD_REQUEST); } @@ -451,7 +451,7 @@ private boolean isValid(JsonElement jsonElement) { return jsonObject.has(META) && jsonObject.has(DATA) && jsonObject.has(LINKS) && !jsonObject.has(JSON_STATUS_CODE); } catch (IllegalStateException e) { - log.error("Error while validating json event: ", e.getMessage()); + log.error("Error while validating JSON event: ", e.getMessage()); return false; } } From 6d9aca496cce4285b5057352935a1d9730a33b33 Mon Sep 17 00:00:00 2001 From: Shudhansu Shekhar Date: Thu, 8 Aug 2024 16:57:45 +0530 Subject: [PATCH 09/13] Modified endpoint generateAndPublish to accept array of events --- .../remrem/publish/controller/ProducerController.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java index 07ccd871..21463584 100644 --- a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java +++ b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java @@ -274,6 +274,7 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String "Invalid, event is neither in the form of JSON object nor in the JSON array"); } List> responseEvents; + HttpStatus responseStatus = HttpStatus.BAD_REQUEST; EnumSet getStatus = EnumSet.of(HttpStatus.SERVICE_UNAVAILABLE, HttpStatus.UNAUTHORIZED, HttpStatus.NOT_ACCEPTABLE, HttpStatus.EXPECTATION_FAILED, HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.UNPROCESSABLE_ENTITY); try { @@ -306,13 +307,15 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String ResponseEntity response = restTemplate.postForEntity(generateUrl, entity, String.class, generateURLTemplate.getMap(msgProtocol, msgType)); + responseStatus = response.getStatusCode(); String responseBody = null; if (bodyJson.isJsonObject()) { responseBody = "[" + response.getBody() + "]"; } else if (bodyJson.isJsonArray()) { responseBody = response.getBody(); } - if (response.getStatusCode() == HttpStatus.OK) { + + if (responseStatus == HttpStatus.OK || responseStatus == HttpStatus.MULTI_STATUS) { log.info("The result from REMReM Generate is: " + response.getStatusCodeValue()); log.debug("mp: " + msgProtocol); log.debug("body: " + responseBody); @@ -340,7 +343,8 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String responseEvents = processingValidEvent(responseBody, msgProtocol, userDomain, tag, routingKey); return new ResponseEntity<>(responseEvents, HttpStatus.BAD_REQUEST); } - return new ResponseEntity<>(responseEvents, HttpStatus.OK); + //Status here is the status returned from generate service, except BAD_REQUEST which already handled above + return new ResponseEntity<>(responseEvents, responseStatus); } /** From 1fdf99d002d09bc3e9581eca213b26b7af452162 Mon Sep 17 00:00:00 2001 From: Shudhansu Shekhar Date: Wed, 14 Aug 2024 21:12:17 +0530 Subject: [PATCH 10/13] Modified endpoint generateAndPublish to accept array of events --- .../controller/ProducerController.java | 76 ++++++++++++++----- 1 file changed, 59 insertions(+), 17 deletions(-) diff --git a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java index 21463584..7cad1c9f 100644 --- a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java +++ b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java @@ -120,22 +120,23 @@ public void logUserName() { } } - @SuppressWarnings({ "rawtypes", "unchecked" }) - @ApiOperation(value = "To publish eiffel event to message bus", response = String.class) - @ApiResponses(value = { @ApiResponse(code = 200, message = "Event sent successfully"), - @ApiResponse(code = 400, message = "Invalid event content"), - @ApiResponse(code = 404, message = "RabbitMq properties not found"), - @ApiResponse(code = 500, message = "Internal server error"), - @ApiResponse(code = 503, message = "Service Unavailable") }) - @RequestMapping(value = "/producer/msg", method = RequestMethod.POST) - @ResponseBody - public ResponseEntity send( - @ApiParam(value = "message protocol", required = true) @RequestParam(value = "mp") final String msgProtocol, - @ApiParam(value = "user domain") @RequestParam(value = "ud", required = false) final String userDomain, - @ApiParam(value = "tag") @RequestParam(value = "tag", required = false) final String tag, - @ApiParam(value = "routing key") @RequestParam(value = "rk", required = false) final String routingKey, - @ApiParam(value = "eiffel event", required = true) @RequestBody final JsonElement body) { - if(isAuthenticationEnabled) { + /** + * This controller used as producer to send messages or event + * @param msgProtocol + * message protocol (required) + * @param userDomain + * user domain (required) + * @param tag + * (not required) + * @param routingKey + * (not required) + * @param body + * (required) + * @return A response entity which contains http status and result + */ + public ResponseEntity send(final String msgProtocol, final String userDomain, final String tag, + final String routingKey, final JsonElement body) { + if (isAuthenticationEnabled) { logUserName(); } @@ -154,11 +155,52 @@ public ResponseEntity send( } synchronized (this) { SendResult result = messageService.send(body, msgService, userDomain, tag, routingKey); - log.info("HTTP Status: {}", messageService.getHttpStatus().value()); + log.info("HTTP Status: {}", messageService.getHttpStatus().value()); return new ResponseEntity(result, messageService.getHttpStatus()); } } + /** + * This controller used as producer to send messages or event + * @param msgProtocol + * message protocol (required) + * @param userDomain + * user domain (required) + * @param tag + * (not required) + * @param routingKey + * (not required) + * @param body + * (Here json body of string type as input because just to parse + * the string in to JsonElement not using JsonElement directly here.) + * @return A response entity which contains http status and result + */ + @SuppressWarnings({ "rawtypes", "unchecked" }) + @ApiOperation(value = "To publish eiffel event to message bus", response = String.class) + @ApiResponses(value = {@ApiResponse(code = 200, message = "Event sent successfully"), + @ApiResponse(code = 400, message = "Invalid event content"), + @ApiResponse(code = 404, message = "RabbitMq properties not found"), + @ApiResponse(code = 500, message = "Internal server error"), + @ApiResponse(code = 503, message = "Service Unavailable")}) + @RequestMapping(value = "/producer/msg", method = RequestMethod.POST) + @ResponseBody + public ResponseEntity send( + @ApiParam(value = "message protocol", required = true) @RequestParam(value = "mp") final String msgProtocol, + @ApiParam(value = "user domain") @RequestParam(value = "ud", required = false) final String userDomain, + @ApiParam(value = "tag") @RequestParam(value = "tag", required = false) final String tag, + @ApiParam(value = "routing key") @RequestParam(value = "rk", required = false) final String routingKey, + @ApiParam(value = "eiffel event", required = true) @RequestBody final String body) { + try { + JsonElement inputBody = JsonParser.parseString(body); + return send(msgProtocol, userDomain, tag, routingKey, inputBody); + } catch (JsonSyntaxException e) { + String exceptionMessage = e.getMessage(); + log.error("Unexpected exception caught due to parsed json data", exceptionMessage); + return createResponseEntity(HttpStatus.BAD_REQUEST, JSON_FATAL_STATUS, + "Invalid JSON parse data format due to: " + exceptionMessage); + } + } + /** * This controller provides single RemRem REST API End Point for both RemRem * Generate and Publish. From 4a596e374de283f3ec7a17e3811339daae78a697 Mon Sep 17 00:00:00 2001 From: Shudhansu Shekhar Date: Fri, 16 Aug 2024 12:11:41 +0530 Subject: [PATCH 11/13] Modified endpoint generateAndPublish to accept array of events --- .../eiffel/remrem/publish/controller/ProducerController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java index 7cad1c9f..9fba9e89 100644 --- a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java +++ b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java @@ -195,9 +195,9 @@ public ResponseEntity send( return send(msgProtocol, userDomain, tag, routingKey, inputBody); } catch (JsonSyntaxException e) { String exceptionMessage = e.getMessage(); - log.error("Unexpected exception caught due to parsed json data", exceptionMessage); + log.error("Cannot parse the following JSON data: " + body, exceptionMessage); return createResponseEntity(HttpStatus.BAD_REQUEST, JSON_FATAL_STATUS, - "Invalid JSON parse data format due to: " + exceptionMessage); + "Invalid JSON data:" + exceptionMessage); } } From 4c624e54ee28706bff082a9efaa9e51c95371316 Mon Sep 17 00:00:00 2001 From: Shudhansu Shekhar Date: Fri, 16 Aug 2024 16:02:50 +0530 Subject: [PATCH 12/13] Modified endpoint generateAndPublish to accept array of events --- .../eiffel/remrem/publish/controller/ProducerController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java index 9fba9e89..65637987 100644 --- a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java +++ b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java @@ -195,9 +195,9 @@ public ResponseEntity send( return send(msgProtocol, userDomain, tag, routingKey, inputBody); } catch (JsonSyntaxException e) { String exceptionMessage = e.getMessage(); - log.error("Cannot parse the following JSON data: " + body, exceptionMessage); + log.error("Cannot parse the following JSON data:\n" + body + '\n', exceptionMessage); return createResponseEntity(HttpStatus.BAD_REQUEST, JSON_FATAL_STATUS, - "Invalid JSON data:" + exceptionMessage); + "Invalid JSON data: " + exceptionMessage); } } From acb0c70ab41275d83e1b128ea3ba9bda860a8c87 Mon Sep 17 00:00:00 2001 From: Shudhansu Shekhar Date: Fri, 16 Aug 2024 18:16:00 +0530 Subject: [PATCH 13/13] Modified endpoint generateAndPublish to accept array of events --- .../eiffel/remrem/publish/controller/ProducerController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java index 65637987..d61289e2 100644 --- a/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java +++ b/publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java @@ -195,7 +195,7 @@ public ResponseEntity send( return send(msgProtocol, userDomain, tag, routingKey, inputBody); } catch (JsonSyntaxException e) { String exceptionMessage = e.getMessage(); - log.error("Cannot parse the following JSON data:\n" + body + '\n', exceptionMessage); + log.error("Cannot parse the following JSON data:\n" + body + "\n\n" + exceptionMessage); return createResponseEntity(HttpStatus.BAD_REQUEST, JSON_FATAL_STATUS, "Invalid JSON data: " + exceptionMessage); }