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