51
51
import java .util .Map ;
52
52
import java .util .Map .Entry ;
53
53
54
+ import static com .ericsson .eiffel .remrem .generate .constants .RemremGenerateServiceConstants .*;
55
+
54
56
@ RestController
55
57
@ RequestMapping ("/*" )
56
58
@ Api (value = "REMReM Generate Service" , description = "REST API for generating Eiffel messages" )
@@ -118,40 +120,37 @@ public ResponseEntity<?> generate(@ApiParam(value = "message protocol", required
118
120
ObjectMapper mapper = new ObjectMapper (jsonFactory );
119
121
JsonNode node = mapper .readTree (body );
120
122
Gson gson = new Gson ();
121
- JsonElement userInputJson = gson .fromJson (node .toString (), JsonElement .class );
123
+ JsonElement inputJson = gson .fromJson (node .toString (), JsonElement .class );
122
124
return generate (msgProtocol , msgType , failIfMultipleFound , failIfNoneFound , lookupInExternalERs ,
123
- lookupLimit , okToLeaveOutInvalidOptionalFields , userInputJson );
125
+ lookupLimit , okToLeaveOutInvalidOptionalFields , inputJson );
124
126
} catch (JsonSyntaxException e ) {
125
127
String exceptionMessage = e .getMessage ();
126
128
log .error ("Invalid JSON parse data format due to:" , e .getMessage ());
127
- errorResponse .addProperty (RemremGenerateServiceConstants .JSON_STATUS_CODE , HttpStatus .BAD_REQUEST .value ());
128
- errorResponse .addProperty (RemremGenerateServiceConstants .JSON_STATUS_RESULT , "fatal" );
129
- errorResponse .addProperty (RemremGenerateServiceConstants .JSON_ERROR_MESSAGE_FIELD , "Invalid JSON parse data format due to: " + exceptionMessage );
130
- return new ResponseEntity <>(errorResponse , HttpStatus .INTERNAL_SERVER_ERROR );
129
+ return createResponseEntity (HttpStatus .BAD_REQUEST ,"Invalid JSON parse data format due to: " + exceptionMessage , "fatal" ,
130
+ errorResponse );
131
131
} catch (JsonProcessingException e ) {
132
132
String exceptionMessage = e .getMessage ();
133
- log .info ("duplicate key detected" , exceptionMessage );
134
- errorResponse .addProperty (RemremGenerateServiceConstants .JSON_STATUS_RESULT , "fatal" );
135
- errorResponse .addProperty (RemremGenerateServiceConstants .JSON_ERROR_MESSAGE_FIELD , "duplicate key detected, please check " + exceptionMessage );
136
- return new ResponseEntity <>(errorResponse , HttpStatus .BAD_REQUEST );
133
+ log .info ("Incorrect Json data" , exceptionMessage );
134
+ return createResponseEntity (HttpStatus .BAD_REQUEST ,"Incorrect Json data: " + exceptionMessage , "fatal" ,
135
+ errorResponse );
137
136
}
138
137
}
139
138
140
- 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 userInputData ) {
139
+ 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 inputData ) {
141
140
142
141
JsonArray generatedEventResults = new JsonArray ();
143
142
JsonObject errorResponse = new JsonObject ();
144
143
try {
145
144
if (lookupLimit <= 0 ) {
146
145
return new ResponseEntity <>("LookupLimit must be greater than or equals to 1" , HttpStatus .BAD_REQUEST );
147
146
}
148
- if (userInputData == null ) {
147
+ if (inputData == null ) {
149
148
log .error ("Json event must not be null" );
150
- errorResponse .addProperty (RemremGenerateServiceConstants . JSON_ERROR_MESSAGE_FIELD , "userInputData must not be null" );
149
+ errorResponse .addProperty (JSON_ERROR_MESSAGE_FIELD , "inputData must not be null" );
151
150
return new ResponseEntity <>(errorResponse , HttpStatus .BAD_REQUEST );
152
151
}
153
- if (userInputData .isJsonArray ()) {
154
- JsonArray inputEventJsonArray = userInputData .getAsJsonArray ();
152
+ if (inputData .isJsonArray ()) {
153
+ JsonArray inputEventJsonArray = inputData .getAsJsonArray ();
155
154
for (JsonElement element : inputEventJsonArray ) {
156
155
JsonObject generatedEvent = (processEvent (msgProtocol , msgType ,
157
156
failIfMultipleFound , failIfNoneFound , lookupInExternalERs , lookupLimit ,
@@ -161,40 +160,44 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
161
160
boolean success = true ;
162
161
for (JsonElement result : generatedEventResults ) {
163
162
JsonObject jsonObject = result .getAsJsonObject ();
164
- success &= jsonObject .has (RemremGenerateServiceConstants . META );
163
+ success &= jsonObject .has (META );
165
164
}
166
165
return new ResponseEntity <>(generatedEventResults , success ? HttpStatus .OK : HttpStatus .BAD_REQUEST );
167
166
168
- } else if (userInputData .isJsonObject ()) {
169
- JsonObject inputJsonObject = userInputData .getAsJsonObject ();
167
+ } else if (inputData .isJsonObject ()) {
168
+ JsonObject inputJsonObject = inputData .getAsJsonObject ();
170
169
JsonObject processedJson = processEvent (msgProtocol , msgType , failIfMultipleFound , failIfNoneFound ,
171
170
lookupInExternalERs , lookupLimit , okToLeaveOutInvalidOptionalFields , inputJsonObject );
172
171
173
- if (processedJson .has (RemremGenerateServiceConstants . META )) {
172
+ if (processedJson .has (META )) {
174
173
return new ResponseEntity <>(processedJson , HttpStatus .OK );
175
174
}
176
- if (processedJson .has (RemremGenerateServiceConstants . JSON_STATUS_CODE ) && "400" .equals (processedJson .get (RemremGenerateServiceConstants . JSON_STATUS_CODE ).toString ())) {
175
+ if (processedJson .has (JSON_STATUS_CODE ) && "400" .equals (processedJson .get (JSON_STATUS_CODE ).toString ())) {
177
176
return new ResponseEntity <>(processedJson , HttpStatus .BAD_REQUEST );
178
177
} else {
179
178
return new ResponseEntity <>(processedJson , HttpStatus .SERVICE_UNAVAILABLE );
180
179
}
180
+
181
181
} else {
182
- return createResponseEntity (RemremGenerateServiceConstants . JSON_STATUS_CODE , RemremGenerateServiceConstants . JSON_STATUS_RESULT , RemremGenerateServiceConstants . JSON_ERROR_MESSAGE_FIELD ,
183
- HttpStatus . BAD_REQUEST , "Invalid JSON format,expected either single template or array of templates" , " fail" , errorResponse , HttpStatus . BAD_REQUEST . value () );
182
+ return createResponseEntity (HttpStatus . BAD_REQUEST , "Invalid JSON format,expected either single template or array of templates" ,
183
+ " fail" , errorResponse );
184
184
}
185
185
} catch (REMGenerateException | JsonSyntaxException e ) {
186
186
return handleException (e );
187
187
}
188
188
}
189
189
190
- public ResponseEntity <JsonObject > createResponseEntity (String statusCode , String statusResult , String statusMessage ,
191
- HttpStatus status , String errorMessage , String resultMessage , JsonObject errorResponse ,
192
- int statusCodeValue ) {
193
- errorResponse .addProperty (statusCode , statusCodeValue );
194
- errorResponse .addProperty (statusResult , resultMessage );
195
- errorResponse .addProperty (statusMessage , errorMessage );
190
+ public ResponseEntity <JsonObject > createResponseEntity (HttpStatus status , String errorMessage , String resultMessage ,
191
+ JsonObject errorResponse ) {
192
+ initializeResponse (status , errorMessage , resultMessage , errorResponse );
196
193
return new ResponseEntity <>(errorResponse , status );
194
+ }
197
195
196
+ public void initializeResponse (HttpStatus status , String errorMessage , String resultMessage ,
197
+ JsonObject errorResponse ) {
198
+ errorResponse .addProperty (JSON_STATUS_CODE , status .value ());
199
+ errorResponse .addProperty (JSON_STATUS_RESULT , resultMessage );
200
+ errorResponse .addProperty (JSON_ERROR_MESSAGE_FIELD , errorMessage );
198
201
}
199
202
200
203
private ResponseEntity <JsonObject > handleException (Exception e ) {
@@ -206,23 +209,17 @@ private ResponseEntity<JsonObject> handleException(Exception e) {
206
209
);
207
210
for (HttpStatus status : statuses ) {
208
211
if (exceptionMessage .contains (Integer .toString (status .value ()))) {
209
- return createResponseEntity (RemremGenerateServiceConstants .JSON_STATUS_CODE , RemremGenerateServiceConstants .JSON_STATUS_RESULT ,
210
- RemremGenerateServiceConstants .JSON_ERROR_MESSAGE_FIELD ,
211
- status , e .getMessage (), "fail" , errorResponse , status .value ());
212
+ return createResponseEntity (
213
+ status , e .getMessage (), "fail" , errorResponse );
212
214
}
213
215
}
214
- return createResponseEntity (RemremGenerateServiceConstants .JSON_STATUS_CODE , RemremGenerateServiceConstants .JSON_STATUS_RESULT ,
215
- RemremGenerateServiceConstants .JSON_ERROR_MESSAGE_FIELD ,
216
- HttpStatus .BAD_REQUEST , e .getMessage (), "fail" , errorResponse , HttpStatus .BAD_REQUEST .value ());
216
+ return createResponseEntity (HttpStatus .BAD_REQUEST , e .getMessage (), "fail" , errorResponse );
217
217
} else if (e instanceof JsonSyntaxException ) {
218
218
log .error ("Failed to parse JSON: " , exceptionMessage );
219
- return createResponseEntity (RemremGenerateServiceConstants .JSON_STATUS_CODE , RemremGenerateServiceConstants .JSON_STATUS_RESULT , RemremGenerateServiceConstants .JSON_ERROR_MESSAGE_FIELD ,
220
- HttpStatus .BAD_REQUEST , e .getMessage (), "fail" , errorResponse , HttpStatus .BAD_REQUEST .value ());
219
+ return createResponseEntity (HttpStatus .BAD_REQUEST , e .getMessage (), "fail" , errorResponse );
221
220
} else {
222
221
log .error ("Unexpected exception caught" , exceptionMessage );
223
- return createResponseEntity (RemremGenerateServiceConstants .JSON_STATUS_CODE , RemremGenerateServiceConstants .JSON_STATUS_RESULT ,
224
- RemremGenerateServiceConstants .JSON_ERROR_MESSAGE_FIELD ,
225
- HttpStatus .INTERNAL_SERVER_ERROR , exceptionMessage , "fail" , errorResponse , HttpStatus .INTERNAL_SERVER_ERROR .value ());
222
+ return createResponseEntity (HttpStatus .INTERNAL_SERVER_ERROR , exceptionMessage , "fail" , errorResponse );
226
223
}
227
224
}
228
225
@@ -242,17 +239,14 @@ public JsonObject processEvent(String msgProtocol, String msgType, Boolean failI
242
239
243
240
if (msgService != null ) {
244
241
String response = msgService .generateMsg (msgType , event , isLenientEnabled (okToLeaveOutInvalidOptionalFields ));
245
- parsedResponse = parser .parse (response );
246
-
242
+ parsedResponse = JsonParser .parseString (response );
247
243
JsonObject parsedJson = parsedResponse .getAsJsonObject ();
248
244
249
- if (!parsedJson .has (RemremGenerateServiceConstants .JSON_ERROR_MESSAGE_FIELD )) {
250
- return parsedJson ;
251
- } else {
252
- eventResponse .addProperty (RemremGenerateServiceConstants .JSON_STATUS_CODE , HttpStatus .BAD_REQUEST .value ());
253
- eventResponse .addProperty (RemremGenerateServiceConstants .JSON_STATUS_RESULT , "fail" );
254
- eventResponse .addProperty (RemremGenerateServiceConstants .JSON_ERROR_MESSAGE_FIELD , RemremGenerateServiceConstants .TEMPLATE_ERROR );
245
+ if (parsedJson .has (JSON_ERROR_MESSAGE_FIELD )) {
246
+ createResponseEntity (HttpStatus .BAD_REQUEST , "fail" , TEMPLATE_ERROR , eventResponse );
255
247
return eventResponse ;
248
+ } else {
249
+ return parsedJson ;
256
250
}
257
251
}
258
252
return eventResponse ;
0 commit comments