Skip to content

Commit 65fc749

Browse files
Modified endpoint generateAndPublish to accept array of events
1 parent c17059a commit 65fc749

File tree

1 file changed

+82
-50
lines changed

1 file changed

+82
-50
lines changed

publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java

Lines changed: 82 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
*/
1515
package com.ericsson.eiffel.remrem.publish.controller;
1616

17-
import java.util.EnumSet;
18-
import java.util.Map;
17+
import java.util.*;
1918

2019
import org.slf4j.LoggerFactory;
2120
import org.springframework.beans.factory.annotation.Autowired;
@@ -193,65 +192,98 @@ public ResponseEntity generateAndPublish(@ApiParam(value = "message protocol", r
193192
@ApiParam(value = "okToLeaveOutInvalidOptionalFields true will remove the optional "
194193
+ "event fields from the input event data that does not validate successfully, "
195194
+ "and add those removed field information into customData/remremGenerateFailures") @RequestParam(value = "okToLeaveOutInvalidOptionalFields", required = false, defaultValue = "false") final Boolean okToLeaveOutInvalidOptionalFields,
196-
@ApiParam(value = "JSON message", required = true) @RequestBody final JsonObject bodyJson) {
195+
@ApiParam(value = "JSON message", required = true) @RequestBody final JsonElement bodyJson) {
197196
if (isAuthenticationEnabled) {
198197
logUserName();
199198
}
200199

201-
String bodyJsonOut = null;
202-
if(parseData) {
203-
// -- parse params in incoming request -> body -------------
204-
EventTemplateHandler eventTemplateHandler = new EventTemplateHandler();
205-
JsonNode parsedTemplate = eventTemplateHandler.eventTemplateParser(bodyJson.toString(), msgType);
206-
bodyJsonOut = parsedTemplate.toString();
207-
log.info("Parsed template: " + bodyJsonOut);
208-
}else{
209-
bodyJsonOut = bodyJson.toString();
200+
List<JsonObject> events = new ArrayList<>();
201+
if (bodyJson.isJsonObject()) {
202+
events.add(bodyJson.getAsJsonObject());
203+
} else if (bodyJson.isJsonArray()) {
204+
for (JsonElement element : bodyJson.getAsJsonArray()) {
205+
if (element.isJsonObject()) {
206+
events.add(element.getAsJsonObject());
207+
} else {
208+
return new ResponseEntity<>("Invalid event content", HttpStatus.BAD_REQUEST);
209+
}
210+
}
211+
} else {
212+
return new ResponseEntity<>("Invalid event content", HttpStatus.BAD_REQUEST);
210213
}
211214

212-
HttpHeaders headers = new HttpHeaders();
213-
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
214-
HttpEntity<String> entity = new HttpEntity<>(bodyJsonOut, headers);
215-
EnumSet<HttpStatus> getStatus = EnumSet.of(HttpStatus.SERVICE_UNAVAILABLE, HttpStatus.UNAUTHORIZED, HttpStatus.NOT_ACCEPTABLE, HttpStatus.EXPECTATION_FAILED, HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.UNPROCESSABLE_ENTITY);
216-
217-
try {
218-
String generateUrl = generateURLTemplate.getUrl() + "&failIfMultipleFound=" + failIfMultipleFound
219-
+ "&failIfNoneFound=" + failIfNoneFound + "&lookupInExternalERs=" + lookupInExternalERs
220-
+ "&lookupLimit=" + lookupLimit + "&okToLeaveOutInvalidOptionalFields=" + okToLeaveOutInvalidOptionalFields;
221-
ResponseEntity<String> response = restTemplate.postForEntity(generateUrl,
222-
entity, String.class, generateURLTemplate.getMap(msgProtocol, msgType));
223-
224-
if (response.getStatusCode() == HttpStatus.OK) {
225-
log.info("The result from REMReM Generate is: " + response.getStatusCodeValue());
226-
227-
// publishing requires an array if you want status code
228-
String responseBody = "[" + response.getBody() + "]";
229-
MsgService msgService = PublishUtils.getMessageService(msgProtocol, msgServices);
230-
231-
log.debug("mp: " + msgProtocol);
232-
log.debug("body: " + responseBody);
233-
log.debug("user domain suffix: " + userDomain + " tag: " + tag + " routing key: " + routingKey);
234-
if (msgService != null && msgProtocol != null) {
235-
rmqHelper.rabbitMqPropertiesInit(msgProtocol);
236-
}
237-
synchronized(this) {
238-
SendResult result = messageService.send(responseBody, msgService, userDomain, tag, routingKey);
239-
return new ResponseEntity(result, messageService.getHttpStatus());
240-
}
215+
List<Map<String, Object>> responseEvents = new ArrayList<>();
216+
for (JsonObject eventJson : events) {
217+
Map<String, Object> eventResponse = new HashMap<>();
218+
String bodyJsonOut = null;
219+
if (parseData) {
220+
// -- parse params in incoming request -> body -------------
221+
EventTemplateHandler eventTemplateHandler = new EventTemplateHandler();
222+
JsonNode parsedTemplate = eventTemplateHandler.eventTemplateParser(eventJson.toString(), msgType);
223+
bodyJsonOut = parsedTemplate.toString();
224+
log.info("Parsed template: " + bodyJsonOut);
241225
} else {
242-
return response;
226+
bodyJsonOut = eventJson.toString();
243227
}
244-
}
245-
catch (RemRemPublishException e) {
228+
229+
HttpHeaders headers = new HttpHeaders();
230+
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
231+
HttpEntity<String> entity = new HttpEntity<>(bodyJsonOut, headers);
232+
EnumSet<HttpStatus> getStatus = EnumSet.of(HttpStatus.SERVICE_UNAVAILABLE, HttpStatus.UNAUTHORIZED, HttpStatus.NOT_ACCEPTABLE, HttpStatus.EXPECTATION_FAILED, HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.UNPROCESSABLE_ENTITY);
233+
234+
try {
235+
String generateUrl = generateURLTemplate.getUrl() + "&failIfMultipleFound=" + failIfMultipleFound
236+
+ "&failIfNoneFound=" + failIfNoneFound + "&lookupInExternalERs=" + lookupInExternalERs
237+
+ "&lookupLimit=" + lookupLimit + "&okToLeaveOutInvalidOptionalFields=" + okToLeaveOutInvalidOptionalFields;
238+
ResponseEntity<String> response = restTemplate.postForEntity(generateUrl,
239+
entity, String.class, generateURLTemplate.getMap(msgProtocol, msgType));
240+
241+
242+
//eventResponse.put("ID", UUID.randomUUID().toString());
243+
//eventResponse.put("Status Code", response.getStatusCodeValue());
244+
245+
if (response.getStatusCode() == HttpStatus.OK) {
246+
log.info("The result from REMReM Generate is: " + response.getStatusCodeValue());
247+
248+
// publishing requires an array if you want status code
249+
String responseBody = "[" + response.getBody() + "]";
250+
MsgService msgService = PublishUtils.getMessageService(msgProtocol, msgServices);
251+
252+
log.debug("mp: " + msgProtocol);
253+
log.debug("body: " + responseBody);
254+
log.debug("user domain suffix: " + userDomain + " tag: " + tag + " routing key: " + routingKey);
255+
if (msgService != null && msgProtocol != null) {
256+
rmqHelper.rabbitMqPropertiesInit(msgProtocol);
257+
}
258+
259+
synchronized (this) {
260+
SendResult result = messageService.send(responseBody, msgService, userDomain, tag, routingKey);
261+
//return new ResponseEntity(result, messageService.getHttpStatus());
262+
eventResponse.put("Result", result);
263+
eventResponse.put("message", messageService.getHttpStatus());
264+
}
265+
266+
267+
} else {
268+
return response;
269+
}
270+
responseEvents.add(eventResponse);
271+
} catch (RemRemPublishException e) {
272+
responseEvents.add(eventResponse);
246273
return new ResponseEntity(e.getMessage(), HttpStatus.NOT_FOUND);
247-
}
248-
catch (HttpStatusCodeException e) {
249-
HttpStatus result = HttpStatus.BAD_REQUEST;
250-
if (getStatus.contains(e.getStatusCode())) {
251-
result = e.getStatusCode();
274+
//eventResponse.put("Message", responseEvents)
275+
} catch (HttpStatusCodeException e) {
276+
HttpStatus result = HttpStatus.BAD_REQUEST;
277+
if (getStatus.contains(e.getStatusCode())) {
278+
result = e.getStatusCode();
279+
responseEvents.add(eventResponse);
280+
}
281+
return new ResponseEntity(parser.parse(e.getResponseBodyAsString()), result);
252282
}
253-
return new ResponseEntity(parser.parse(e.getResponseBodyAsString()), result);
254283
}
284+
Map<String, Object> responseBody = new HashMap<>();
285+
responseBody.put("events", responseEvents);
286+
return new ResponseEntity<>(responseBody, HttpStatus.OK);
255287
}
256288

257289
/**

0 commit comments

Comments
 (0)