Skip to content

Commit 6eb83b3

Browse files
Modified generate's endpoint generate to accept array of events
1 parent 975bd4b commit 6eb83b3

File tree

1 file changed

+111
-46
lines changed

1 file changed

+111
-46
lines changed

service/src/main/java/com/ericsson/eiffel/remrem/generate/controller/RemremGenerateController.java

Lines changed: 111 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@
1818
import com.ericsson.eiffel.remrem.generate.constants.RemremGenerateServiceConstants;
1919
import com.ericsson.eiffel.remrem.generate.exception.REMGenerateException;
2020
import com.ericsson.eiffel.remrem.protocol.MsgService;
21-
import com.google.gson.Gson;
22-
import com.google.gson.GsonBuilder;
23-
import com.google.gson.JsonArray;
24-
import com.google.gson.JsonElement;
25-
import com.google.gson.JsonObject;
26-
import com.google.gson.JsonParser;
21+
import com.google.gson.*;
2722

2823
import ch.qos.logback.classic.Logger;
2924
import io.swagger.annotations.*;
@@ -36,6 +31,7 @@
3631
import org.springframework.http.MediaType;
3732
import org.springframework.http.RequestEntity;
3833
import org.springframework.http.ResponseEntity;
34+
import org.springframework.lang.NonNull;
3935
import org.springframework.web.bind.annotation.PathVariable;
4036
import org.springframework.web.bind.annotation.RequestBody;
4137
import org.springframework.web.bind.annotation.RequestMapping;
@@ -50,6 +46,7 @@
5046
import java.io.FileInputStream;
5147
import java.io.IOException;
5248
import java.io.InputStreamReader;
49+
import java.util.ArrayList;
5350
import java.util.List;
5451
import java.util.Map;
5552
import java.util.Map.Entry;
@@ -99,64 +96,132 @@ public void setRestTemplate(RestTemplate restTemplate) {
9996
* <p>
10097
* Returns: The event information as a json element
10198
*/
99+
102100
@ApiOperation(value = "To generate eiffel event based on the message protocol", response = String.class)
103101
@ApiResponses(value = { @ApiResponse(code = 200, message = "Event sent successfully"),
104102
@ApiResponse(code = 400, message = "Malformed JSON"),
105103
@ApiResponse(code = 500, message = "Internal server error"),
106104
@ApiResponse(code = 503, message = "Message protocol is invalid") })
107105
@RequestMapping(value = "/{mp" + REGEX + "}", method = RequestMethod.POST)
108-
public ResponseEntity<?> generate(
109-
@ApiParam(value = "message protocol", required = true) @PathVariable("mp") final String msgProtocol,
110-
@ApiParam(value = "message type", required = true) @RequestParam("msgType") final String msgType,
111-
@ApiParam(value = "ER lookup result multiple found, Generate will fail") @RequestParam(value = "failIfMultipleFound", required = false, defaultValue = "false") final Boolean failIfMultipleFound,
112-
@ApiParam(value = "ER lookup result none found, Generate will fail") @RequestParam(value = "failIfNoneFound", required = false, defaultValue = "false") final Boolean failIfNoneFound,
113-
@ApiParam(value = RemremGenerateServiceConstants.LOOKUP_IN_EXTERNAL_ERS) @RequestParam(value = "lookupInExternalERs", required = false, defaultValue = "false") final Boolean lookupInExternalERs,
114-
@ApiParam(value = RemremGenerateServiceConstants.LOOKUP_LIMIT) @RequestParam(value = "lookupLimit", required = false, defaultValue = "1") final int lookupLimit,
115-
@ApiParam(value = RemremGenerateServiceConstants.LenientValidation) @RequestParam(value = "okToLeaveOutInvalidOptionalFields", required = false, defaultValue = "false") final Boolean okToLeaveOutInvalidOptionalFields,
116-
@ApiParam(value = "JSON message", required = true) @RequestBody JsonObject bodyJson) {
106+
public ResponseEntity<?> generate(@ApiParam(value = "message protocol", required = true) @PathVariable("mp") final String msgProtocol,
107+
@ApiParam(value = "message type", required = true) @RequestParam("msgType") final String msgType,
108+
@ApiParam(value = "ER lookup result multiple found, Generate will fail") @RequestParam(value = "failIfMultipleFound", required = false, defaultValue = "false") final Boolean failIfMultipleFound,
109+
@ApiParam(value = "ER lookup result none found, Generate will fail") @RequestParam(value = "failIfNoneFound", required = false, defaultValue = "false") final Boolean failIfNoneFound,
110+
@ApiParam(value = RemremGenerateServiceConstants.LOOKUP_IN_EXTERNAL_ERS) @RequestParam(value = "lookupInExternalERs", required = false, defaultValue = "false") final Boolean lookupInExternalERs,
111+
@ApiParam(value = RemremGenerateServiceConstants.LOOKUP_LIMIT) @RequestParam(value = "lookupLimit", required = false, defaultValue = "1") final int lookupLimit,
112+
@ApiParam(value = RemremGenerateServiceConstants.LenientValidation) @RequestParam(value = "okToLeaveOutInvalidOptionalFields", required = false, defaultValue = "false") final Boolean okToLeaveOutInvalidOptionalFields,
113+
@ApiParam(value = "JSON message", required = true) @RequestBody String body){
114+
try {
115+
JsonElement bodyJson = JsonParser.parseString(body);
116+
117+
return generate(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound, lookupInExternalERs,
118+
lookupLimit, okToLeaveOutInvalidOptionalFields, bodyJson);
119+
120+
} catch (JsonSyntaxException e) {
121+
JsonObject errorResponse = new JsonObject();
122+
log.error("Unexpected exception caught due to parse json data", e.getMessage());
123+
String exceptionMessage = e.getMessage();
124+
errorResponse.addProperty("Status code", HttpStatus.BAD_REQUEST.value());
125+
errorResponse.addProperty("result", "Fatal");
126+
errorResponse.addProperty("message", "Invalid JSON parse data format due to: " + exceptionMessage);
127+
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
128+
}
129+
}
117130

131+
public ResponseEntity<?> generate(final String msgProtocol, final String msgType, final Boolean failIfMultipleFound, final Boolean failIfNoneFound, final Boolean lookupInExternalERs, final int lookupLimit, final Boolean okToLeaveOutInvalidOptionalFields, JsonElement bodyJson) {
132+
133+
JsonArray results = new JsonArray();
134+
JsonObject errorResponse = new JsonObject();
118135
try {
119-
bodyJson = erLookup(bodyJson, failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit);
136+
137+
if (bodyJson.isJsonArray()) {
138+
139+
JsonArray jsonArray = bodyJson.getAsJsonArray();
140+
for (JsonElement element : jsonArray) {
141+
results.add(processEvent(element.getAsJsonObject(), msgProtocol, msgType,
142+
failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit,
143+
okToLeaveOutInvalidOptionalFields));
144+
}
145+
return new ResponseEntity<>(results, HttpStatus.OK);
146+
147+
} else if (bodyJson.isJsonObject()) {
148+
JsonObject jsonObject = bodyJson.getAsJsonObject();
149+
JsonObject jsonObject1 = processEvent(jsonObject, msgProtocol, msgType, failIfMultipleFound, failIfNoneFound,
150+
lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields);
151+
return new ResponseEntity<>(jsonObject1, HttpStatus.OK);
152+
} else {
153+
errorResponse.addProperty("Status code", HttpStatus.BAD_REQUEST.value());
154+
errorResponse.addProperty("result", "fail");
155+
errorResponse.addProperty("message", "Invalid JSON format");
156+
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
157+
}
158+
} catch (Exception e) {
159+
log.error("Unexpected exception caught", e);
160+
errorResponse.addProperty("Status code", HttpStatus.BAD_REQUEST.value());
161+
errorResponse.addProperty("Result", "Fail");
162+
errorResponse.addProperty("Message", "Invalid JSON format");
163+
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
164+
165+
}
166+
}
167+
168+
/**
169+
* This helper method basically generate or process one event
170+
* @return JsonObject generated event
171+
*/
172+
173+
public JsonObject processEvent(JsonObject jsonElement, String msgProtocol, String msgType, Boolean failIfMultipleFound,
174+
Boolean okToLeaveOutInvalidOptionalFields,
175+
Boolean failIfNoneFound, int lookupLimit, Boolean lookupInExternalERs) {
176+
JsonObject eventResponse = new JsonObject();
177+
JsonElement parsedResponse = null;
178+
try {
179+
JsonObject event = jsonElement.getAsJsonObject();
180+
event = erLookup(event, failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit);
120181
MsgService msgService = getMessageService(msgProtocol);
121-
String response;
182+
122183
if (msgService != null) {
123-
response = msgService.generateMsg(msgType, bodyJson, isLenientEnabled(okToLeaveOutInvalidOptionalFields));
124-
JsonElement parsedResponse = parser.parse(response);
125-
if(lookupLimit <= 0) {
126-
return new ResponseEntity<>("LookupLimit must be greater than or equals to 1", HttpStatus.BAD_REQUEST);
127-
}
128-
if (!parsedResponse.getAsJsonObject().has(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD)) {
129-
return new ResponseEntity<>(parsedResponse, HttpStatus.OK);
184+
String response = msgService.generateMsg(msgType, event, isLenientEnabled(okToLeaveOutInvalidOptionalFields));
185+
parsedResponse = parser.parse(response);
186+
187+
if (lookupLimit <= 0) {
188+
eventResponse.addProperty("status code", HttpStatus.BAD_REQUEST.value());
189+
} else if (!parsedResponse.getAsJsonObject().has(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD)) {
190+
return parsedResponse.getAsJsonObject();
130191
} else {
131-
return new ResponseEntity<>(parsedResponse, HttpStatus.BAD_REQUEST);
192+
eventResponse.addProperty("Status code", HttpStatus.BAD_REQUEST.value());
193+
eventResponse.addProperty("Result", "Fail");
194+
eventResponse.addProperty("Message", RemremGenerateServiceConstants.NOT_ACCEPTABLE);
195+
return eventResponse;
132196
}
133-
} else {
134-
return new ResponseEntity<>(parser.parse(RemremGenerateServiceConstants.NO_SERVICE_ERROR),
135-
HttpStatus.SERVICE_UNAVAILABLE);
197+
136198
}
137199
} catch (REMGenerateException e1) {
138200
if (e1.getMessage().contains(Integer.toString(HttpStatus.NOT_ACCEPTABLE.value()))) {
139-
return new ResponseEntity<>(parser.parse(e1.getMessage()), HttpStatus.NOT_ACCEPTABLE);
140-
}
141-
else if (e1.getMessage().contains(Integer.toString(HttpStatus.EXPECTATION_FAILED.value()))) {
142-
return new ResponseEntity<>(parser.parse(e1.getMessage()), HttpStatus.EXPECTATION_FAILED);
143-
}
144-
else if (e1.getMessage().contains(Integer.toString(HttpStatus.EXPECTATION_FAILED.value()))) {
145-
return new ResponseEntity<>(parser.parse(e1.getMessage()), HttpStatus.EXPECTATION_FAILED);
146-
}
147-
else if (e1.getMessage()
148-
.contains(Integer.toString(HttpStatus.SERVICE_UNAVAILABLE.value()))) {
149-
return new ResponseEntity<>(parser.parse(RemremGenerateServiceConstants.NO_ER),
150-
HttpStatus.SERVICE_UNAVAILABLE);
151-
}
152-
else {
153-
return new ResponseEntity<>(parser.parse(e1.getMessage()), HttpStatus.UNPROCESSABLE_ENTITY);
201+
eventResponse.addProperty("Status code", HttpStatus.NOT_ACCEPTABLE.value());
202+
} else if (e1.getMessage().contains(Integer.toString(HttpStatus.EXPECTATION_FAILED.value()))) {
203+
eventResponse.addProperty("Status code", HttpStatus.EXPECTATION_FAILED.value());
204+
} else if (e1.getMessage().contains(Integer.toString(HttpStatus.EXPECTATION_FAILED.value()))) {
205+
eventResponse.addProperty("Status code", HttpStatus.EXPECTATION_FAILED.value());
206+
207+
} else if (e1.getMessage()
208+
.contains(Integer.toString(HttpStatus.SERVICE_UNAVAILABLE.value()))) {
209+
eventResponse.addProperty("Status code", HttpStatus.SERVICE_UNAVAILABLE.value());
210+
eventResponse.addProperty("Message", RemremGenerateServiceConstants.NO_SERVICE_ERROR);
211+
} else {
212+
eventResponse.addProperty("Status code", HttpStatus.UNPROCESSABLE_ENTITY.value());
154213
}
214+
eventResponse.addProperty("Result", "Fail");
215+
eventResponse.add("Message", parser.parse(e1.getMessage()));
216+
return eventResponse;
155217
} catch (Exception e) {
156-
log.error("Unexpected exception caught", e);
157-
return new ResponseEntity<>(parser.parse(RemremGenerateServiceConstants.INTERNAL_SERVER_ERROR),
158-
HttpStatus.INTERNAL_SERVER_ERROR);
218+
log.error("Unexpected error caught", e);
219+
eventResponse.addProperty("Status code", HttpStatus.INTERNAL_SERVER_ERROR.value());
220+
eventResponse.addProperty("Result", "Fail");
221+
eventResponse.addProperty("Message", RemremGenerateServiceConstants.INTERNAL_SERVER_ERROR);
222+
return eventResponse;
159223
}
224+
return eventResponse;
160225
}
161226

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

0 commit comments

Comments
 (0)