-
Notifications
You must be signed in to change notification settings - Fork 77
Modified endpoint generateAndPublish to accept array of events #294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modified endpoint generateAndPublish to accept array of events #294
Conversation
...-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java
Fixed
Show fixed
Hide fixed
} | ||
} | ||
} else { | ||
return new ResponseEntity<>("Invalid event content", HttpStatus.BAD_REQUEST); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can user understand what is wrong?
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON_UTF8); | ||
HttpEntity<String> entity = new HttpEntity<>(bodyJsonOut, headers); | ||
EnumSet<HttpStatus> getStatus = EnumSet.of(HttpStatus.SERVICE_UNAVAILABLE, HttpStatus.UNAUTHORIZED, HttpStatus.NOT_ACCEPTABLE, HttpStatus.EXPECTATION_FAILED, HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.UNPROCESSABLE_ENTITY); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why you create a const object over and over?
String generateUrl = generateURLTemplate.getUrl() + "&failIfMultipleFound=" + failIfMultipleFound | ||
+ "&failIfNoneFound=" + failIfNoneFound + "&lookupInExternalERs=" + lookupInExternalERs | ||
+ "&lookupLimit=" + lookupLimit + "&okToLeaveOutInvalidOptionalFields=" + okToLeaveOutInvalidOptionalFields; | ||
ResponseEntity<String> response = restTemplate.postForEntity(generateUrl, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why send just one vent to generate service? There's generate endpoint accepting array of events. This restores the former behaviour: a network connection per event.
} catch (JsonSyntaxException e) { | ||
JsonObject errorResponse = new JsonObject(); | ||
log.error("Unexpected exception caught due to parse json data", e.getMessage()); | ||
String exceptionMessage = e.getMessage(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use exceptionMessage at line above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
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"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Upper-case vs. lower-case property names. Remember, JSON is case-sensitive. If the case is mismatched, user's handlers may not work properly. Define constants for property names.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
if (element.isJsonObject()) { | ||
events.add(element.getAsJsonObject()); | ||
} else { | ||
return new ResponseEntity<>("Invalid events content or format", HttpStatus.BAD_REQUEST); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not to say directly: "Array item must be a JSON object"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes we can do like that
} | ||
} | ||
} else { | ||
return new ResponseEntity<>("Invalid, event is not in the correct format", HttpStatus.BAD_REQUEST); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, very confusing message. Inform directly what went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
headers.setContentType(MediaType.APPLICATION_JSON_UTF8); | ||
HttpEntity<String> entity = new HttpEntity<>(bodyJsonOut, headers); | ||
EnumSet<HttpStatus> getStatus = EnumSet.of(HttpStatus.SERVICE_UNAVAILABLE, HttpStatus.UNAUTHORIZED, HttpStatus.NOT_ACCEPTABLE, HttpStatus.EXPECTATION_FAILED, HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.UNPROCESSABLE_ENTITY); | ||
// -- parse params in incoming request -> body ------------- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the comment still valid?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
// publishing requires an array if you want status code | ||
String responseBody = "[" + response.getBody() + "]"; | ||
MsgService msgService = PublishUtils.getMessageService(msgProtocol, msgServices); | ||
JsonArray responseArray = JsonParser.parseString(response.getBody()).getAsJsonArray(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if getJsonArray() fails?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah from his documentation, it seems it will throw exception with message like Not a JSON Array
} | ||
|
||
synchronized (this) { | ||
JsonObject jsonObject = jsonResponse.getAsJsonObject(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if getJsonObject() fails?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By his method documentation, it seems it will throw exception with message like not a jsonObject
} | ||
} else { | ||
return response; | ||
return new ResponseEntity<>("Invalid Json event content", HttpStatus.BAD_REQUEST); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not to forward the message obtained from generate service?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes we can do but in the else part not sure ,for some reason event processing failed so we should notify user with detailed or more failed info right?
try { | ||
return jsonObject.has("meta") && jsonObject.has("data") && | ||
jsonObject.has("links") && !jsonObject.has("Status code"); | ||
} catch (Exception e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When this exception can be thrown?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This exception throw for various reason like if there is any incorrect json structure, unexpected data types etc
return generateAndPublish(msgProtocol, msgType, userDomain, tag, routingKey, parseData, failIfMultipleFound, | ||
failIfNoneFound, lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields, bodyJson); |
Check warning
Code scanning / CodeQL
Cross-site scripting Medium
user-provided value
eventResponse = new HashMap<>(); | ||
JsonElement jsonResponseEvent = element.getAsJsonArray().get(i); | ||
if (isValid(jsonResponseEvent)) { | ||
synchronized (this) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the same code starts at line 314?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah put it one helper method
|
||
private Boolean isValid(JsonElement jsonObject) { | ||
try { | ||
return jsonObject.getAsJsonObject().has("meta") && jsonObject.getAsJsonObject().has("data") && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getAsJsonObject() used over and over.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can make one simple method for this
|
||
private Boolean isValid(JsonElement jsonObject) { | ||
try { | ||
return jsonObject.getAsJsonObject().has("meta") && jsonObject.getAsJsonObject().has("data") && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace "meta", ... by constants.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK
try { | ||
return jsonObject.getAsJsonObject().has("meta") && jsonObject.getAsJsonObject().has("data") && | ||
jsonObject.getAsJsonObject().has("links") && !jsonObject.getAsJsonObject().has("Status code"); | ||
} catch (Exception e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't handle general exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
@ApiParam(value = "JSON message", required = true) @RequestBody final String body){ | ||
|
||
try { | ||
JsonElement bodyJson = JsonParser.parseString(body); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should JSON parser be configured as in case of Generate service, i.e. not allowing duplicate keys, etc.?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah can do this but why we need this here? Is it necessary if already there is check for correct event publishing means like incorrect event is not published on mb basically
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, so we have the check in generator, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, so we have the check in generator, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that duplicate key check we implement basically in generate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
When generate service is down, the following error message is displayed
{
"timestamp": "Jul 29, 2024, 1:05:51 PM",
"status": 500,
"error": "Internal Server Error",
"path": "/generateAndPublish"
}
It should say clearly what's wrong -
When an error is reported
{
"status message": {
"status code": 400,
"result": "{"status_code": 400, "result": "FAIL", "message":"Template is not correct format or something is missing in the template, please check"}",
"message": "fail"
}
Value of result shouldn't be escaped. Value of a property can be JSON object. -
The error message above: failure is should be unified, i.e. "FAIL" vs. "fail".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, add comment like "Done" or "Resolved" to resolved comments.
@@ -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"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why upper-case 'S'? Other properties are lower-case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we need this Uppercase 'S' here
} 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there should be createResponesEntity method available.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -223,11 +225,11 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String | |||
MsgService msgService = PublishUtils.getMessageService(msgProtocol, msgServices); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we continue if msgService == null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no we can do it here just like we do in generate ok?
if (msgService == null) {
return createResponseEntity(HttpStatus.SERVICE_UNAVAILABLE,
"No protocol service has been found registered", JSON_ERROR_STATUS).getBody();
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's how it should work.
@@ -284,25 +286,8 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String | |||
if (msgService != null && msgProtocol != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is msgServcie set to rmqHelper repeatedly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah but it seems set only once not repeatedly, please clear if i am wrong here
List<Map<String, Object>> responseEvent = new ArrayList<>(); | ||
Map<String, Object> eventResponse; | ||
JsonElement element = JsonParser.parseString(eventMsg); | ||
for (int i = 0; i < element.getAsJsonArray().size(); i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is element isn't an array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically here eventMsg is a response body of string type
JsonElement element = JsonParser.parseString(eventMsg); | ||
for (int i = 0; i < element.getAsJsonArray().size(); i++) { | ||
eventResponse = new HashMap<>(); | ||
JsonElement jsonResponseEvent = element.getAsJsonArray().get(i); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why to call getAsJsonArray() in each iteration?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah i put in one variable, use that one in each iteration right?
JsonElement jsonResponseEvent = element.getAsJsonArray().get(i); | ||
if (isValid(jsonResponseEvent)) { | ||
synchronized (this) { | ||
JsonObject validResponse = jsonResponseEvent.getAsJsonObject(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it guaranteed that validResponse is non-null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we can add check before doing this process like that ?
if (eventResponseMessage == null){
eventResponse.put("Parameter 'eventResponseMessage' must not be null", HttpStatus.BAD_REQUEST);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't mean eventResponseMessage, I mean validResponse.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah but here 'validResponse' is extracting from 'eventResponseMessage' So if this 'eventResponseMessage' is not null then validResponse will also not null because there is not much changes between that
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") && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use constants instead of hard-coded values, i.e. "data" and "links". There're constants defined in Event class or somewhere...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah i do that but i need defined that with in this class...There is no separate class for constant as in generate
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) && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I wrote, use existing constants defined for event properties instead of defining new ones.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no there is not any existing constant, i have to define new one
Map<String, Object> eventResponse = null; | ||
|
||
if (eventResponseMessage == null) { | ||
eventResponse.put("Parameter 'eventResponseMessage' must not be null", HttpStatus.BAD_REQUEST); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is response to end user. How the user knows what 'eventResponseMessage' is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah so we can put it like that?
eventResponse.put("Parameter 'eventResponseMessage' that is response generated by generate must not be null",
HttpStatus.BAD_REQUEST);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd write something like 'Missing response from 'generate' service.'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
return responseEvent; | ||
} | ||
JsonElement eventElement = JsonParser.parseString(eventResponseMessage); | ||
JsonArray eventArray = eventElement.getAsJsonArray(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it guaranteed that eventElement is an array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah i check it and it seems eventElement is an array, if you want i can show
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't see that...
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the same code 'getAsJsonObject(jsonElement)' repeats 4 times?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
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()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Event ID should be published whenever a message related to event is produced.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah right but i am not getting any scope or need might be here, might be i am wrong?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like ...get(META).get(ID), but I haven't checked...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it can do like this but not sure about the place you suggest to put not sure it will benefit anyway or not but yeah we can discuss once
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please check my comment
if (element.isJsonObject()) { | ||
events.add(element.getAsJsonObject()); | ||
} else { | ||
return new ResponseEntity<>("Invalid events content or format", HttpStatus.BAD_REQUEST); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes we can do like that
} | ||
} | ||
} else { | ||
return new ResponseEntity<>("Invalid, event is not in the correct format", HttpStatus.BAD_REQUEST); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
// publishing requires an array if you want status code | ||
String responseBody = "[" + response.getBody() + "]"; | ||
MsgService msgService = PublishUtils.getMessageService(msgProtocol, msgServices); | ||
JsonArray responseArray = JsonParser.parseString(response.getBody()).getAsJsonArray(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah from his documentation, it seems it will throw exception with message like Not a JSON Array
} | ||
} else { | ||
return response; | ||
return new ResponseEntity<>("Invalid Json event content", HttpStatus.BAD_REQUEST); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes we can do but in the else part not sure ,for some reason event processing failed so we should notify user with detailed or more failed info right?
try { | ||
return jsonObject.has("meta") && jsonObject.has("data") && | ||
jsonObject.has("links") && !jsonObject.has("Status code"); | ||
} catch (Exception e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This exception throw for various reason like if there is any incorrect json structure, unexpected data types etc
return responseEvent; | ||
} | ||
JsonElement eventElement = JsonParser.parseString(eventResponseMessage); | ||
JsonArray eventArray = eventElement.getAsJsonArray(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah i check it and it seems eventElement is an array, if you want i can show
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()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah right but i am not getting any scope or need might be here, might be i am wrong?
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()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah right but i am not getting any scope or need might be here, might be i am wrong?
eventResponse = new HashMap<>(); | ||
JsonElement jsonResponseEvent = eventElement.getAsJsonArray().get(i); | ||
JsonElement jsonResponseEvent = eventArray.get(i); | ||
if (isValid(jsonResponseEvent)) { | ||
synchronized (this) { | ||
JsonObject validResponse = jsonResponseEvent.getAsJsonObject(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we can add check before doing this process like that ?
if (eventResponseMessage == null){
eventResponse.put("Parameter 'eventResponseMessage' must not be null", HttpStatus.BAD_REQUEST);
}
return responseEvent; | ||
} | ||
JsonElement eventElement = JsonParser.parseString(eventResponseMessage); | ||
JsonArray eventArray = eventElement.getAsJsonArray(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah i check it and it seems eventElement is an array, if you want i can show
@@ -223,11 +225,11 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String | |||
MsgService msgService = PublishUtils.getMessageService(msgProtocol, msgServices); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's how it should work.
Map<String, Object> eventResponse = null; | ||
|
||
if (eventResponseMessage == null) { | ||
eventResponse.put("Parameter 'eventResponseMessage' must not be null", HttpStatus.BAD_REQUEST); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd write something like 'Missing response from 'generate' service.'
return responseEvent; | ||
} | ||
JsonElement eventElement = JsonParser.parseString(eventResponseMessage); | ||
JsonArray eventArray = eventElement.getAsJsonArray(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't see that...
JsonElement jsonResponseEvent = element.getAsJsonArray().get(i); | ||
if (isValid(jsonResponseEvent)) { | ||
synchronized (this) { | ||
JsonObject validResponse = jsonResponseEvent.getAsJsonObject(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't mean eventResponseMessage, I mean validResponse.
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()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like ...get(META).get(ID), but I haven't checked...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check my comment and if need we can discuss
Map<String, Object> eventResponse = null; | ||
|
||
if (eventResponseMessage == null) { | ||
eventResponse.put("Parameter 'eventResponseMessage' must not be null", HttpStatus.BAD_REQUEST); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
JsonElement jsonResponseEvent = element.getAsJsonArray().get(i); | ||
if (isValid(jsonResponseEvent)) { | ||
synchronized (this) { | ||
JsonObject validResponse = jsonResponseEvent.getAsJsonObject(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah but here 'validResponse' is extracting from 'eventResponseMessage' So if this 'eventResponseMessage' is not null then validResponse will also not null because there is not much changes between that
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()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it can do like this but not sure about the place you suggest to put not sure it will benefit anyway or not but yeah we can discuss once
@ApiParam(value = "JSON message", required = true) @RequestBody final String body){ | ||
|
||
try { | ||
JsonElement bodyJson = JsonParser.parseString(body); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, so we have the check in generator, right?
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"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Misleading error message, use something like "Invalid, array item expected to be in the form of JSON object".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
} catch (IllegalStateException e) { | ||
log.error("Error while validating event: ", e.getMessage()); | ||
log.error("Error while validating json event: ", e.getMessage()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
json -> JSON
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
When submitting a bad request, something like that is reported { while result of parsing (viewing in debugger) is {"message":"Cannot validate given JSON string","cause":"com.ericsson.eiffel.remrem.semantics.validator.EiffelValidationException: Multiple trace links are not allowed for link type COMPOSITION"} Where this error message disappears? Why is it replaced by a "Template is not correct format or something is missing in the template, please check"? |
Done, now the responses look as same in debugger |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please check my comment
@ApiParam(value = "JSON message", required = true) @RequestBody final String body){ | ||
|
||
try { | ||
JsonElement bodyJson = JsonParser.parseString(body); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that duplicate key check we implement basically in generate
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"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
} catch (IllegalStateException e) { | ||
log.error("Error while validating event: ", e.getMessage()); | ||
log.error("Error while validating json event: ", e.getMessage()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, add documentation, as in case of generate service.
No need there is already in publish |
...-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java
Fixed
Show fixed
Hide fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works, fine. Just change the error messages.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use log.error("Cannot parse the following JSON data:\n" + inputBody, exceptionMessage);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we use inputBodyhere in the catch section we need initiailize this and it seems null in the catch, so it is not better to use body instead of inputBody?
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Invalid JSON data: " + exceptionMessage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the error messages, please check the comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please check my comment, just need to check one thing
@ApiParam(value = "eiffel event", required = true) @RequestBody final String body) { | ||
try { | ||
JsonElement inputBody = JsonParser.parseString(body); | ||
return send(msgProtocol, userDomain, tag, routingKey, inputBody); |
Check warning
Code scanning / CodeQL
Information exposure through an error message Medium
Error information
return createResponseEntity(HttpStatus.BAD_REQUEST, JSON_FATAL_STATUS, | ||
"Invalid JSON parse data format due to: " + exceptionMessage); | ||
"Invalid JSON data:" + exceptionMessage); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A space missing after ':'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separate the JSON data from the rest of logs:
... data:\n" + body + '\n'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed now, please check
@@ -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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't see the error message in log file.
Applicable Issues
Description of the Change
In this after these changes, publish service generateAndPublish endpoint accept array of events
Alternate Designs
Possible Drawbacks
Sign-off
Developer's Certificate of Origin 1.1
By making a contribution to this project, I certify that:
(a) The contribution was created in whole or in part by me and I
have the right to submit it under the open source license
indicated in the file; or
(b) The contribution is based upon previous work that, to the best
of my knowledge, is covered under an appropriate open source
license and I have the right under that license to submit that
work with modifications, whether created in whole or in part
by me, under the same open source license (unless I am
permitted to submit under a different license), as indicated
in the file; or
(c) The contribution was provided directly to me by some other
person who certified (a), (b) or (c) and I have not modified
it.
(d) I understand and agree that this project and the contribution
are public and that a record of the contribution (including all
personal information I submit with it, including my sign-off) is
maintained indefinitely and may be redistributed consistent with
this project or the open source license(s) involved.
Signed-off-by: shudhansu.shekhar.ext@ericsson.com