|
18 | 18 | import com.ericsson.eiffel.remrem.generate.constants.RemremGenerateServiceConstants;
|
19 | 19 | import com.ericsson.eiffel.remrem.generate.exception.REMGenerateException;
|
20 | 20 | 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.*; |
27 | 22 |
|
28 | 23 | import ch.qos.logback.classic.Logger;
|
29 | 24 | import io.swagger.annotations.*;
|
|
36 | 31 | import org.springframework.http.MediaType;
|
37 | 32 | import org.springframework.http.RequestEntity;
|
38 | 33 | import org.springframework.http.ResponseEntity;
|
| 34 | +import org.springframework.lang.NonNull; |
39 | 35 | import org.springframework.web.bind.annotation.PathVariable;
|
40 | 36 | import org.springframework.web.bind.annotation.RequestBody;
|
41 | 37 | import org.springframework.web.bind.annotation.RequestMapping;
|
|
50 | 46 | import java.io.FileInputStream;
|
51 | 47 | import java.io.IOException;
|
52 | 48 | import java.io.InputStreamReader;
|
| 49 | +import java.util.ArrayList; |
53 | 50 | import java.util.List;
|
54 | 51 | import java.util.Map;
|
55 | 52 | import java.util.Map.Entry;
|
@@ -99,64 +96,132 @@ public void setRestTemplate(RestTemplate restTemplate) {
|
99 | 96 | * <p>
|
100 | 97 | * Returns: The event information as a json element
|
101 | 98 | */
|
| 99 | + |
102 | 100 | @ApiOperation(value = "To generate eiffel event based on the message protocol", response = String.class)
|
103 | 101 | @ApiResponses(value = { @ApiResponse(code = 200, message = "Event sent successfully"),
|
104 | 102 | @ApiResponse(code = 400, message = "Malformed JSON"),
|
105 | 103 | @ApiResponse(code = 500, message = "Internal server error"),
|
106 | 104 | @ApiResponse(code = 503, message = "Message protocol is invalid") })
|
107 | 105 | @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 | + } |
117 | 130 |
|
| 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(); |
118 | 135 | 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); |
120 | 181 | MsgService msgService = getMessageService(msgProtocol);
|
121 |
| - String response; |
| 182 | + |
122 | 183 | 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(); |
130 | 191 | } 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; |
132 | 196 | }
|
133 |
| - } else { |
134 |
| - return new ResponseEntity<>(parser.parse(RemremGenerateServiceConstants.NO_SERVICE_ERROR), |
135 |
| - HttpStatus.SERVICE_UNAVAILABLE); |
| 197 | + |
136 | 198 | }
|
137 | 199 | } catch (REMGenerateException e1) {
|
138 | 200 | 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()); |
154 | 213 | }
|
| 214 | + eventResponse.addProperty("Result", "Fail"); |
| 215 | + eventResponse.add("Message", parser.parse(e1.getMessage())); |
| 216 | + return eventResponse; |
155 | 217 | } 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; |
159 | 223 | }
|
| 224 | + return eventResponse; |
160 | 225 | }
|
161 | 226 |
|
162 | 227 | private JsonObject erLookup(final JsonObject bodyJson, Boolean failIfMultipleFound, Boolean failIfNoneFound,
|
|
0 commit comments