15
15
package com .ericsson .eiffel .remrem .publish .service ;
16
16
17
17
import java .io .IOException ;
18
+ import java .util .ArrayList ;
18
19
import java .util .HashMap ;
19
20
import java .util .List ;
20
21
import java .util .Map ;
49
50
50
51
Logger log = (Logger ) LoggerFactory .getLogger (MessageServiceRMQImpl .class );
51
52
52
- /*Variables handles status codes*/
53
- List <Integer > statusCodes ;
54
- List <JsonElement > errorItems ;
55
- List <PublishResultItem > resultList ;
56
- boolean checkEventStatus ;
57
53
/*
58
54
* (non-Javadoc)
59
55
* @see com.ericsson.eiffel.remrem.publish.service.MessageService#send(java.util.Map, java.util.Map)
@@ -74,7 +70,6 @@ public SendResult send(Map<String, String> routingKeyMap, Map<String, String> ms
74
70
} else {
75
71
event = new PublishResultItem (entryKey , HttpStatus .INTERNAL_SERVER_ERROR .value (), PropertiesConfig .SERVER_DOWN ,
76
72
PropertiesConfig .SERVER_DOWN_MESSAGE );
77
- checkEventStatus = false ;
78
73
}
79
74
} catch (NackException e ) {
80
75
event = new PublishResultItem (entryKey , HttpStatus .INTERNAL_SERVER_ERROR .value (), PropertiesConfig .SERVER_DOWN ,
@@ -105,45 +100,43 @@ public SendResult send(Map<String, String> routingKeyMap, Map<String, String> ms
105
100
*/
106
101
@ Override
107
102
public SendResult send (String jsonContent , MsgService msgService , String userDomainSuffix , String tag , String routingKey ) {
108
-
109
- JsonParser parser = new JsonParser ();
110
103
try {
111
- JsonElement json = parser . parse (jsonContent );
104
+ JsonElement json = JsonParser . parseString (jsonContent );
112
105
if (json .isJsonArray ()) {
113
106
return send (json , msgService , userDomainSuffix , tag , routingKey );
114
- } else {
115
- Map <String , String > map = new HashMap <>();
116
- Map <String , String > routingKeyMap = new HashMap <>();
117
- String eventId = msgService .getEventId (json .getAsJsonObject ());
118
- if (StringUtils .isNotBlank (eventId )) {
119
- String routing_key = PublishUtils .getRoutingKey (msgService , json .getAsJsonObject (), rmqHelper , userDomainSuffix , tag , routingKey );
120
- if (StringUtils .isNotBlank (routing_key )) {
121
- map .put (eventId , json .toString ());
122
- routingKeyMap .put (eventId , routing_key );
123
- } else if (routing_key == null ) {
124
- List <PublishResultItem > resultItemList = new CopyOnWriteArrayList <>();
125
- routingKeyGenerationFailure (resultItemList );
126
- return new SendResult (resultItemList );
127
- } else {
128
- List <PublishResultItem > resultItemList = new CopyOnWriteArrayList <>();
129
- PublishResultItem resultItem = rabbitmqConfigurationNotFound (msgService );
130
- resultItemList .add (resultItem );
131
- return new SendResult (resultItemList );
132
- }
107
+ }
108
+
109
+ Map <String , String > map = new HashMap <>();
110
+ Map <String , String > routingKeyMap = new HashMap <>();
111
+ String eventId = msgService .getEventId (json .getAsJsonObject ());
112
+ if (StringUtils .isNotBlank (eventId )) {
113
+ String routing_key = PublishUtils .getRoutingKey (msgService , json .getAsJsonObject (), rmqHelper , userDomainSuffix , tag , routingKey );
114
+ if (StringUtils .isNotBlank (routing_key )) {
115
+ map .put (eventId , json .toString ());
116
+ routingKeyMap .put (eventId , routing_key );
117
+ } else if (routing_key == null ) {
118
+ List <PublishResultItem > resultItemList = new CopyOnWriteArrayList <>();
119
+ routingKeyGenerationFailure (resultItemList );
120
+ return new SendResult (resultItemList );
133
121
} else {
134
122
List <PublishResultItem > resultItemList = new CopyOnWriteArrayList <>();
135
- createFailureResult (resultItemList );
123
+ PublishResultItem resultItem = rabbitmqConfigurationNotFound (msgService );
124
+ resultItemList .add (resultItem );
136
125
return new SendResult (resultItemList );
137
126
}
138
- return send (routingKeyMap , map , msgService );
127
+ } else {
128
+ List <PublishResultItem > resultItemList = new CopyOnWriteArrayList <>();
129
+ createFailureResult (resultItemList );
130
+ return new SendResult (resultItemList );
139
131
}
132
+ return send (routingKeyMap , map , msgService );
140
133
} catch (final JsonSyntaxException e ) {
141
134
String resultMsg = "Could not parse JSON." ;
142
135
if (e .getCause () != null ) {
143
136
resultMsg = resultMsg + " Cause: " + e .getCause ().getMessage ();
144
137
}
145
138
log .error (resultMsg , e .getMessage ());
146
- List <PublishResultItem > resultItemList = new CopyOnWriteArrayList <>();
139
+ List <PublishResultItem > resultItemList = new ArrayList <>();
147
140
createFailureResult (resultItemList );
148
141
return new SendResult (resultItemList );
149
142
}
@@ -155,76 +148,64 @@ public SendResult send(String jsonContent, MsgService msgService, String userDom
155
148
*/
156
149
@ Override
157
150
public SendResult send (JsonElement json , MsgService msgService , String userDomainSuffix , String tag , String routingKey ) {
151
+
152
+ List <PublishResultItem > resultList ;
153
+ boolean checkEventStatus ;
154
+
158
155
Map <String , String > map = new HashMap <>();
159
156
Map <String , String > routingKeyMap = new HashMap <>();
157
+
160
158
SendResult result ;
161
- resultList = new CopyOnWriteArrayList < PublishResultItem >();
159
+ resultList = new ArrayList < >();
162
160
if (json == null ) {
163
161
createFailureResult (resultList );
164
162
}
165
- if (json .isJsonArray ()) {
166
- statusCodes = new CopyOnWriteArrayList <Integer >();
167
- checkEventStatus = true ;
168
- JsonArray bodyJson = json .getAsJsonArray ();
169
- for (JsonElement obj : bodyJson ) {
170
- String eventId = msgService .getEventId (obj .getAsJsonObject ());
171
- if (StringUtils .isNotEmpty (eventId ) && checkEventStatus ) {
172
- String routing_key = getAndCheckEvent (msgService , map , resultList , obj , routingKeyMap ,
173
- userDomainSuffix , tag , routingKey );
174
- if (StringUtils .isNotBlank (routing_key )) {
175
- result = send (obj .toString (), msgService , userDomainSuffix , tag , routing_key );
176
- resultList .addAll (result .getEvents ());
177
- int statusCode = result .getEvents ().get (0 ).getStatusCode ();
178
- if (!statusCodes .contains (statusCode ))
179
- statusCodes .add (statusCode );
180
- } else if (routing_key == null ) {
181
- routingKeyGenerationFailure (resultList );
182
- errorItems = new CopyOnWriteArrayList <JsonElement >();
183
- int statusCode = resultList .get (0 ).getStatusCode ();
184
- statusCodes .add (statusCode );
185
- errorItems .add (obj );
186
- checkEventStatus = false ;
187
- } else {
188
- PublishResultItem resultItem = rabbitmqConfigurationNotFound (msgService );
189
- resultList .add (resultItem );
190
- int statusCode = resultItem .getStatusCode ();
191
- statusCodes .add (statusCode );
192
- break ;
193
- }
194
- } else {
195
- if (!checkEventStatus ) {
196
- addUnsuccessfulResultItem (obj );
197
- int statusCode = resultList .get (0 ).getStatusCode ();
198
- statusCodes .add (statusCode );
163
+ else {
164
+ if (json .isJsonArray ()) {
165
+ checkEventStatus = true ;
166
+ JsonArray bodyJson = json .getAsJsonArray ();
167
+ for (JsonElement obj : bodyJson ) {
168
+ String eventId = msgService .getEventId (obj .getAsJsonObject ());
169
+ if (StringUtils .isNotEmpty (eventId ) && checkEventStatus ) {
170
+ String routing_key = getAndCheckEvent (msgService , map , resultList , obj , routingKeyMap ,
171
+ userDomainSuffix , tag , routingKey );
172
+ if (StringUtils .isNotBlank (routing_key )) {
173
+ result = send (obj .toString (), msgService , userDomainSuffix , tag , routing_key );
174
+ resultList .addAll (result .getEvents ());
175
+ } else if (routing_key == null ) {
176
+ routingKeyGenerationFailure (resultList );
177
+ checkEventStatus = false ;
178
+ } else {
179
+ PublishResultItem resultItem = rabbitmqConfigurationNotFound (msgService );
180
+ resultList .add (resultItem );
181
+ break ;
182
+ }
199
183
} else {
200
- createFailureResult ( resultList );
201
- errorItems = new CopyOnWriteArrayList < JsonElement >( );
202
- int statusCode = resultList . get ( 0 ). getStatusCode ();
203
- statusCodes . add ( statusCode );
204
- errorItems . add ( obj ) ;
205
- checkEventStatus = false ;
184
+ if (! checkEventStatus ) {
185
+ addUnsuccessfulResultItem ( resultList , obj );
186
+ } else {
187
+ createFailureResult ( resultList );
188
+ checkEventStatus = false ;
189
+ }
206
190
}
207
191
}
192
+ } else {
193
+ result = send (json .toString (), msgService , userDomainSuffix , tag , routingKey );
194
+ resultList .addAll (result .getEvents ());
208
195
}
209
- } else {
210
- statusCodes = new CopyOnWriteArrayList <Integer >();
211
- result = send (json .toString (), msgService , userDomainSuffix , tag , routingKey );
212
- resultList .addAll (result .getEvents ());
213
- int statusCode = result .getEvents ().get (0 ).getStatusCode ();
214
- if (!statusCodes .contains (statusCode ))
215
- statusCodes .add (statusCode );
216
196
}
197
+
217
198
result = new SendResult ();
218
199
result .setEvents (resultList );
219
200
return result ;
220
201
}
221
202
222
- private String sendMessage (String routingKey , String msg , MsgService msgService ) throws IOException ,NackException , TimeoutException , RemRemPublishException {
203
+ private String sendMessage (String routingKey , String msg , MsgService msgService ) throws IOException ,TimeoutException , RemRemPublishException {
223
204
String resultMsg = PropertiesConfig .SUCCESS ;
224
205
try {
225
206
instantiateRmqHelper ();
226
207
} catch (RemRemPublishException e ) {
227
- log .error ("RemRemPublishException occurred::" + e .getMessage ());
208
+ log .error ("RemRemPublishException occurred::{}" , e .getMessage ());
228
209
}
229
210
rmqHelper .send (routingKey , msg , msgService );
230
211
return resultMsg ;
@@ -268,7 +249,7 @@ private String getAndCheckEvent(MsgService msgService, Map<String, String> map,
268
249
269
250
/**
270
251
* Method returns result for the failure event.
271
- * @param events for list the eiffel events results
252
+ * @param resultItemList for list the eiffel events results
272
253
*/
273
254
private void createFailureResult (List <PublishResultItem > resultItemList ) {
274
255
PublishResultItem resultItem = new PublishResultItem (null , 400 , PropertiesConfig .INVALID_MESSAGE ,
@@ -293,21 +274,9 @@ private void routingKeyGenerationFailure(List<PublishResultItem> resultItemList)
293
274
resultItemList .add (resultItem );
294
275
}
295
276
296
- private void addUnsuccessfulResultItem (JsonElement obj ) {
277
+ private void addUnsuccessfulResultItem (List < PublishResultItem > resultList , JsonElement obj ) {
297
278
PublishResultItem event = new PublishResultItem (null , 503 , PropertiesConfig .SERVICE_UNAVAILABLE ,
298
279
PropertiesConfig .UNSUCCESSFUL_EVENT_CONTENT );
299
280
resultList .add (event );
300
281
}
301
-
302
- /**
303
- * Method returns the Http response code for the events.
304
- */
305
- public HttpStatus getHttpStatus () {
306
- if (statusCodes .size () > 1 ) {
307
- return HttpStatus .MULTI_STATUS ;
308
- } else {
309
- return HttpStatus .valueOf (statusCodes .get (0 ));
310
-
311
- }
312
- }
313
282
}
0 commit comments