23
23
import com .fasterxml .jackson .databind .JsonMappingException ;
24
24
import com .fasterxml .jackson .databind .JsonNode ;
25
25
import com .fasterxml .jackson .databind .ObjectMapper ;
26
- import com .fasterxml .jackson .databind .node .ArrayNode ;
27
- import com .fasterxml .jackson .databind .node .ObjectNode ;
28
26
import com .google .gson .*;
29
27
30
28
import ch .qos .logback .classic .Logger ;
31
- import com .google .gson .stream .JsonReader ;
32
29
import io .swagger .annotations .*;
33
30
34
31
import org .apache .commons .lang3 .StringUtils ;
39
36
import org .springframework .http .MediaType ;
40
37
import org .springframework .http .RequestEntity ;
41
38
import org .springframework .http .ResponseEntity ;
42
- import org .springframework .lang .NonNull ;
43
39
import org .springframework .web .bind .annotation .PathVariable ;
44
40
import org .springframework .web .bind .annotation .RequestBody ;
45
41
import org .springframework .web .bind .annotation .RequestMapping ;
51
47
import springfox .documentation .annotations .ApiIgnore ;
52
48
53
49
import java .io .*;
54
- import java .lang .reflect .Type ;
55
- import java .security .Key ;
56
- import java .util .ArrayList ;
57
- import java .util .LinkedHashMap ;
58
50
import java .util .List ;
59
51
import java .util .Map ;
60
52
import java .util .Map .Entry ;
@@ -92,11 +84,6 @@ public void setRestTemplate(RestTemplate restTemplate) {
92
84
this .restTemplate = restTemplate ;
93
85
}
94
86
95
-
96
- public String statusCode = "status code" ;
97
- public String statusMessage = "message" ;
98
-
99
- public String statusResult = "result" ;
100
87
/**
101
88
* Returns event information as json element based on the message protocol,
102
89
* taking message type and json body as input.
@@ -131,127 +118,111 @@ public ResponseEntity<?> generate(@ApiParam(value = "message protocol", required
131
118
ObjectMapper mapper = new ObjectMapper (jsonFactory );
132
119
JsonNode node = mapper .readTree (body );
133
120
Gson gson = new Gson ();
134
- JsonElement element = gson .fromJson (node .toString (), JsonElement .class );
121
+ JsonElement userInputJson = gson .fromJson (node .toString (), JsonElement .class );
135
122
return generate (msgProtocol , msgType , failIfMultipleFound , failIfNoneFound , lookupInExternalERs ,
136
- lookupLimit , okToLeaveOutInvalidOptionalFields , element );
123
+ lookupLimit , okToLeaveOutInvalidOptionalFields , userInputJson );
137
124
} catch (JsonSyntaxException e ) {
138
125
String exceptionMessage = e .getMessage ();
139
- log .error (exceptionMessage , e .getMessage ());
140
- errorResponse .addProperty (statusCode , HttpStatus .BAD_REQUEST .value ());
141
- errorResponse .addProperty (statusResult , "fatal" );
142
- errorResponse .addProperty (statusMessage , "Invalid JSON parse data format due to: " + exceptionMessage );
126
+ 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 );
143
130
return new ResponseEntity <>(errorResponse , HttpStatus .INTERNAL_SERVER_ERROR );
144
131
} catch (JsonProcessingException e ) {
145
132
String exceptionMessage = e .getMessage ();
146
133
log .info ("duplicate key detected" , exceptionMessage );
147
- errorResponse .addProperty (statusResult , "fatal" );
148
- errorResponse .addProperty (statusMessage , "duplicate key detected, please check " + exceptionMessage );
134
+ errorResponse .addProperty (RemremGenerateServiceConstants . JSON_STATUS_RESULT , "fatal" );
135
+ errorResponse .addProperty (RemremGenerateServiceConstants . JSON_ERROR_MESSAGE_FIELD , "duplicate key detected, please check " + exceptionMessage );
149
136
return new ResponseEntity <>(errorResponse , HttpStatus .BAD_REQUEST );
150
137
}
151
138
}
152
139
153
- 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 ) {
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 ) {
154
141
155
142
JsonArray generatedEventResults = new JsonArray ();
156
143
JsonObject errorResponse = new JsonObject ();
157
144
try {
158
145
if (lookupLimit <= 0 ) {
159
146
return new ResponseEntity <>("LookupLimit must be greater than or equals to 1" , HttpStatus .BAD_REQUEST );
160
147
}
161
- if (bodyJson == null ) {
148
+ if (userInputData == null ) {
162
149
log .error ("Json event must not be null" );
163
- errorResponse .addProperty (statusMessage , "bodyJson must not be null" );
150
+ errorResponse .addProperty (RemremGenerateServiceConstants . JSON_ERROR_MESSAGE_FIELD , "userInputData must not be null" );
164
151
return new ResponseEntity <>(errorResponse , HttpStatus .BAD_REQUEST );
165
152
}
166
-
167
- if (bodyJson .isJsonArray ()) {
168
- JsonArray jsonArray = bodyJson .getAsJsonArray ();
169
- for (JsonElement element : jsonArray ) {
153
+ if (userInputData .isJsonArray ()) {
154
+ JsonArray inputEventJsonArray = userInputData .getAsJsonArray ();
155
+ for (JsonElement element : inputEventJsonArray ) {
170
156
JsonObject generatedEvent = (processEvent (msgProtocol , msgType ,
171
157
failIfMultipleFound , failIfNoneFound , lookupInExternalERs , lookupLimit ,
172
158
okToLeaveOutInvalidOptionalFields , element .getAsJsonObject ()));
173
159
generatedEventResults .add (generatedEvent );
174
160
}
175
-
176
- boolean hasSuccess = false ;
177
- boolean hasFailed = false ;
161
+ boolean success = true ;
178
162
for (JsonElement result : generatedEventResults ) {
179
163
JsonObject jsonObject = result .getAsJsonObject ();
180
-
181
- if (jsonObject .has ("meta" )) {
182
- hasSuccess = true ;
183
- } else if (jsonObject .has (statusCode ) &&
184
- "400" .equals (jsonObject .get (statusCode ).toString ())) {
185
- hasFailed = true ;
186
- }
187
-
188
- if (hasSuccess ) {
189
- return new ResponseEntity <>(generatedEventResults , HttpStatus .OK );
190
- } else if (hasFailed ) {
191
- return new ResponseEntity <>(generatedEventResults , HttpStatus .BAD_REQUEST );
192
- } else {
193
- return new ResponseEntity <>(generatedEventResults , HttpStatus .SERVICE_UNAVAILABLE );
194
- }
164
+ success &= jsonObject .has (RemremGenerateServiceConstants .META );
195
165
}
196
- return new ResponseEntity <>(generatedEventResults , HttpStatus .OK );
166
+ return new ResponseEntity <>(generatedEventResults , success ? HttpStatus .OK : HttpStatus . BAD_REQUEST );
197
167
198
- } else if (bodyJson .isJsonObject ()) {
199
- JsonObject inputJsonObject = bodyJson .getAsJsonObject ();
168
+ } else if (userInputData .isJsonObject ()) {
169
+ JsonObject inputJsonObject = userInputData .getAsJsonObject ();
200
170
JsonObject processedJson = processEvent (msgProtocol , msgType , failIfMultipleFound , failIfNoneFound ,
201
171
lookupInExternalERs , lookupLimit , okToLeaveOutInvalidOptionalFields , inputJsonObject );
202
172
203
- if (processedJson .has ("meta" )) {
173
+ if (processedJson .has (RemremGenerateServiceConstants . META )) {
204
174
return new ResponseEntity <>(processedJson , HttpStatus .OK );
205
175
}
206
- if (processedJson .has (statusCode ) && "400" .equals (processedJson .get (statusCode ).toString ())) {
176
+ if (processedJson .has (RemremGenerateServiceConstants . JSON_STATUS_CODE ) && "400" .equals (processedJson .get (RemremGenerateServiceConstants . JSON_STATUS_CODE ).toString ())) {
207
177
return new ResponseEntity <>(processedJson , HttpStatus .BAD_REQUEST );
208
178
} else {
209
179
return new ResponseEntity <>(processedJson , HttpStatus .SERVICE_UNAVAILABLE );
210
180
}
211
181
} else {
212
- errorResponse .addProperty (statusCode , HttpStatus .BAD_REQUEST .value ());
213
- errorResponse .addProperty (statusResult , "fail" );
214
- errorResponse .addProperty (statusMessage , "Invalid JSON format,expected either single template or array of templates" );
215
- return new ResponseEntity <>(errorResponse , HttpStatus .BAD_REQUEST );
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 ());
216
184
}
217
- } catch (Exception e ) {
185
+ } catch (REMGenerateException | JsonSyntaxException e ) {
218
186
return handleException (e );
219
187
}
220
188
}
221
189
222
- private ResponseEntity <JsonObject > handleException (Exception e ){
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 );
196
+ return new ResponseEntity <>(errorResponse , status );
197
+
198
+ }
199
+
200
+ private ResponseEntity <JsonObject > handleException (Exception e ) {
223
201
JsonObject errorResponse = new JsonObject ();
224
202
String exceptionMessage = e .getMessage ();
225
- if (e instanceof REMGenerateException ){
203
+ if (e instanceof REMGenerateException ) {
226
204
List <HttpStatus > statuses = List .of (
227
- HttpStatus .NOT_ACCEPTABLE ,HttpStatus .EXPECTATION_FAILED ,HttpStatus .SERVICE_UNAVAILABLE ,HttpStatus .UNPROCESSABLE_ENTITY
228
- );
229
- for (HttpStatus status : statuses ){
230
- if (exceptionMessage .contains (Integer .toString (status .value ()))){
231
- errorResponse . addProperty ( statusCode , status . value ());
232
- errorResponse . addProperty ( "message " , exceptionMessage );
233
- return new ResponseEntity <>( errorResponse , status );
205
+ HttpStatus .NOT_ACCEPTABLE , HttpStatus .EXPECTATION_FAILED , HttpStatus .SERVICE_UNAVAILABLE , HttpStatus .UNPROCESSABLE_ENTITY
206
+ );
207
+ for (HttpStatus status : statuses ) {
208
+ 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 () );
234
212
}
235
213
}
236
- errorResponse .addProperty (statusMessage , exceptionMessage );
237
- return new ResponseEntity <>(errorResponse , HttpStatus .BAD_REQUEST );
238
- } else if (e instanceof JsonSyntaxException ){
239
- log .error ("Failed to parse JSON: " , exceptionMessage );
240
- errorResponse .addProperty (statusCode , HttpStatus .INTERNAL_SERVER_ERROR .value ());
241
- errorResponse .addProperty (statusResult , "fail" );
242
- errorResponse .addProperty (statusMessage , exceptionMessage );
243
- return new ResponseEntity <>(errorResponse , HttpStatus .INTERNAL_SERVER_ERROR );
244
- } else if (e instanceof NullPointerException ){
245
- log .info (exceptionMessage );
246
- errorResponse .addProperty (statusMessage , "Json event must not be null" );
247
- return new ResponseEntity <>(errorResponse , HttpStatus .BAD_REQUEST );
248
-
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 ());
217
+ } else if (e instanceof JsonSyntaxException ) {
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 ());
249
221
} else {
250
222
log .error ("Unexpected exception caught" , exceptionMessage );
251
- errorResponse .addProperty (statusCode , HttpStatus .BAD_REQUEST .value ());
252
- errorResponse .addProperty (statusResult , "fail" );
253
- errorResponse .addProperty (statusMessage , exceptionMessage );
254
- return new ResponseEntity <>(errorResponse , HttpStatus .INTERNAL_SERVER_ERROR );
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 ());
255
226
}
256
227
}
257
228
@@ -261,29 +232,29 @@ private ResponseEntity<JsonObject> handleException(Exception e){
261
232
*/
262
233
263
234
public JsonObject processEvent (String msgProtocol , String msgType , Boolean failIfMultipleFound ,
264
- Boolean failIfNoneFound ,Boolean lookupInExternalERs , int lookupLimit ,
265
- Boolean okToLeaveOutInvalidOptionalFields , JsonObject jsonObject ) throws REMGenerateException , JsonSyntaxException {
235
+ Boolean failIfNoneFound , Boolean lookupInExternalERs , int lookupLimit ,
236
+ Boolean okToLeaveOutInvalidOptionalFields , JsonObject jsonObject ) throws REMGenerateException , JsonSyntaxException {
266
237
JsonObject eventResponse = new JsonObject ();
267
- JsonElement parsedResponse = null ;
238
+ JsonElement parsedResponse ;
268
239
269
- JsonObject event = erLookup (jsonObject , failIfMultipleFound , failIfNoneFound , lookupInExternalERs , lookupLimit );
270
- MsgService msgService = getMessageService (msgProtocol );
240
+ JsonObject event = erLookup (jsonObject , failIfMultipleFound , failIfNoneFound , lookupInExternalERs , lookupLimit );
241
+ MsgService msgService = getMessageService (msgProtocol );
271
242
272
- if (msgService != null ) {
273
- String response = msgService .generateMsg (msgType , event , isLenientEnabled (okToLeaveOutInvalidOptionalFields ));
274
- parsedResponse = parser .parse (response );
243
+ if (msgService != null ) {
244
+ String response = msgService .generateMsg (msgType , event , isLenientEnabled (okToLeaveOutInvalidOptionalFields ));
245
+ parsedResponse = parser .parse (response );
275
246
276
- JsonObject parsedJson = parsedResponse .getAsJsonObject ();
247
+ JsonObject parsedJson = parsedResponse .getAsJsonObject ();
277
248
278
- if (!parsedJson .has (RemremGenerateServiceConstants .JSON_ERROR_MESSAGE_FIELD )) {
279
- return parsedJson ;
280
- } else {
281
- eventResponse .addProperty (statusCode , HttpStatus .BAD_REQUEST .value ());
282
- eventResponse .addProperty (statusResult , "fail" );
283
- eventResponse .addProperty (statusMessage , RemremGenerateServiceConstants .TEMPLATE_ERROR );
284
- return eventResponse ;
285
- }
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 );
255
+ return eventResponse ;
286
256
}
257
+ }
287
258
return eventResponse ;
288
259
}
289
260
0 commit comments