Skip to content

Commit

Permalink
Modified endpoint generateAndPublish to accept array of events
Browse files Browse the repository at this point in the history
  • Loading branch information
shudhansu-shekhar committed Aug 2, 2024
1 parent 5566a4d commit 7870507
Showing 1 changed file with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -396,18 +396,28 @@ public List<Map<String, Object>> processingValidEvent(String eventResponseMessag
Map<String, Object> 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);
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 7870507

Please sign in to comment.