|
16 | 16 |
|
17 | 17 | import com.ericsson.eiffel.remrem.generate.config.ErLookUpConfig;
|
18 | 18 | import com.ericsson.eiffel.remrem.generate.constants.RemremGenerateServiceConstants;
|
| 19 | +import com.ericsson.eiffel.remrem.generate.exception.ProtocolHandlerNotFoundException; |
19 | 20 | import com.ericsson.eiffel.remrem.generate.exception.REMGenerateException;
|
20 | 21 | import com.ericsson.eiffel.remrem.protocol.MsgService;
|
21 | 22 | import com.fasterxml.jackson.core.JsonFactory;
|
@@ -168,46 +169,48 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
|
168 | 169 | "The number of events in the input array is too high: " + inputEventJsonArray.size() + " > "
|
169 | 170 | + maxSizeOfInputArray + "; you can modify the property 'maxSizeOfInputArray' to increase it.");
|
170 | 171 | }
|
| 172 | + int successCount = 0; |
| 173 | + int failedCount = 0; |
171 | 174 | for (JsonElement element : inputEventJsonArray) {
|
172 |
| - JsonObject generatedEvent = (processEvent(msgProtocol, msgType, |
173 |
| - failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit, |
174 |
| - okToLeaveOutInvalidOptionalFields, element.getAsJsonObject())); |
175 |
| - generatedEventResults.add(generatedEvent); |
176 |
| - } |
177 |
| - boolean allSuccess = true; |
178 |
| - boolean partialSuccess = false; |
179 |
| - for (JsonElement result : generatedEventResults) { |
180 |
| - JsonObject jsonObject = result.getAsJsonObject(); |
181 |
| - allSuccess &= jsonObject.has(META); |
182 |
| - partialSuccess |= jsonObject.has(META); |
| 175 | + try { |
| 176 | + JsonObject generatedEvent = (processEvent(msgProtocol, msgType, |
| 177 | + failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit, |
| 178 | + okToLeaveOutInvalidOptionalFields, element.getAsJsonObject())); |
| 179 | + generatedEventResults.add(generatedEvent); |
| 180 | + successCount++; |
| 181 | + } catch (ProtocolHandlerNotFoundException e) { |
| 182 | + // Rethrow the exception. All events in array use the same protocol. If it's handler |
| 183 | + // cannot be found for one event, the others will fail, too. |
| 184 | + throw e; |
| 185 | + } catch (REMGenerateException e) { |
| 186 | + // Something went wrong. Add failure description to array of results. |
| 187 | + failedCount++; |
| 188 | + JsonObject response = new JsonObject(); |
| 189 | + createResponseEntity(HttpStatus.BAD_REQUEST, e.getMessage(), JSON_ERROR_STATUS, response); |
| 190 | + generatedEventResults.add(response); |
| 191 | + } |
183 | 192 | }
|
184 |
| - HttpStatus eventStatus = HttpStatus.BAD_REQUEST; |
185 |
| - if (allSuccess){ |
| 193 | + HttpStatus eventStatus; |
| 194 | + if (failedCount == 0) { |
186 | 195 | eventStatus = HttpStatus.OK;
|
187 |
| - } |
188 |
| - else if (partialSuccess){ |
189 |
| - eventStatus = HttpStatus.MULTI_STATUS; |
| 196 | + } else if (successCount > 0 && failedCount > 0) { |
| 197 | + eventStatus = HttpStatus.MULTI_STATUS; |
| 198 | + } else { |
| 199 | + eventStatus = HttpStatus.BAD_REQUEST; |
190 | 200 | }
|
191 | 201 | return new ResponseEntity<>(generatedEventResults, eventStatus);
|
192 | 202 |
|
193 | 203 | } else if (inputData.isJsonObject()) {
|
194 | 204 | JsonObject inputJsonObject = inputData.getAsJsonObject();
|
195 | 205 | JsonObject processedJson = processEvent(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound,
|
196 | 206 | lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields, inputJsonObject);
|
197 |
| - HttpStatus status; |
198 |
| - if (processedJson.has(META)) { |
199 |
| - status = HttpStatus.OK; |
| 207 | + if (!processedJson.has(JSON_STATUS_CODE)) { |
| 208 | + HttpStatus status = HttpStatus.OK; |
200 | 209 | return new ResponseEntity<>(processedJson, status);
|
201 | 210 | } else if (processedJson.has(JSON_STATUS_CODE)) {
|
202 | 211 | String statusValue = processedJson.get(JSON_STATUS_CODE).toString();
|
203 |
| - try { |
204 |
| - status = HttpStatus.resolve(Integer.parseInt(statusValue)); |
205 |
| - return new ResponseEntity<>(processedJson, status); |
206 |
| - } catch (NumberFormatException e) { |
207 |
| - String errorMessage = "Invalid status value: '" + statusValue + "' of response " + processedJson; |
208 |
| - log.error(errorMessage); |
209 |
| - return createResponseEntity(HttpStatus.BAD_REQUEST, errorMessage, JSON_ERROR_STATUS); |
210 |
| - } |
| 212 | + HttpStatus status = HttpStatus.resolve(Integer.parseInt(statusValue)); |
| 213 | + return new ResponseEntity<>(processedJson, status); |
211 | 214 | } else {
|
212 | 215 | String errorMessage = "There is no status value in the response " + processedJson;
|
213 | 216 | log.error(errorMessage);
|
@@ -316,9 +319,7 @@ public JsonObject processEvent(String msgProtocol, String msgType, Boolean failI
|
316 | 319 | JsonObject parsedJson = parsedResponse.getAsJsonObject();
|
317 | 320 |
|
318 | 321 | if (parsedJson.has(JSON_ERROR_MESSAGE_FIELD)) {
|
319 |
| - JsonObject eventResponse = new JsonObject(); |
320 |
| - createResponseEntity(HttpStatus.BAD_REQUEST, parsedJson.toString(), JSON_ERROR_STATUS, eventResponse); |
321 |
| - return eventResponse; |
| 322 | + throw new REMGenerateException(response); |
322 | 323 | } else {
|
323 | 324 | return parsedJson;
|
324 | 325 | }
|
|
0 commit comments