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,65 +100,75 @@ 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 ArrayList <>();
119
+ routingKeyGenerationFailure (resultItemList );
120
+ return new SendResult (resultItemList );
133
121
} else {
134
- List <PublishResultItem > resultItemList = new CopyOnWriteArrayList <>();
135
- createFailureResult (resultItemList );
122
+ List <PublishResultItem > resultItemList = new ArrayList <>();
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 ArrayList <>();
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
}
150
143
}
151
144
145
+ protected SendResult createSendResult (List <PublishResultItem > resultList ) {
146
+ SendResult result = new SendResult ();
147
+ result .setEvents (resultList );
148
+ return result ;
149
+ }
150
+
152
151
/*
153
152
* (non-Javadoc)
154
153
* @see com.ericsson.eiffel.remrem.publish.service.MessageService#send(com.google.gson.JsonElement, com.ericsson.eiffel.remrem.protocol.MsgService, java.lang.String)
155
154
*/
156
155
@ Override
157
156
public SendResult send (JsonElement json , MsgService msgService , String userDomainSuffix , String tag , String routingKey ) {
157
+
158
+ List <PublishResultItem > resultList ;
159
+ boolean checkEventStatus ;
160
+
158
161
Map <String , String > map = new HashMap <>();
159
162
Map <String , String > routingKeyMap = new HashMap <>();
163
+
160
164
SendResult result ;
161
- resultList = new CopyOnWriteArrayList < PublishResultItem >();
165
+ resultList = new ArrayList < >();
162
166
if (json == null ) {
163
167
createFailureResult (resultList );
168
+ return createSendResult (resultList );
164
169
}
170
+
165
171
if (json .isJsonArray ()) {
166
- statusCodes = new CopyOnWriteArrayList <Integer >();
167
172
checkEventStatus = true ;
168
173
JsonArray bodyJson = json .getAsJsonArray ();
169
174
for (JsonElement obj : bodyJson ) {
@@ -174,57 +179,37 @@ public SendResult send(JsonElement json, MsgService msgService, String userDomai
174
179
if (StringUtils .isNotBlank (routing_key )) {
175
180
result = send (obj .toString (), msgService , userDomainSuffix , tag , routing_key );
176
181
resultList .addAll (result .getEvents ());
177
- int statusCode = result .getEvents ().get (0 ).getStatusCode ();
178
- if (!statusCodes .contains (statusCode ))
179
- statusCodes .add (statusCode );
180
182
} else if (routing_key == null ) {
181
183
routingKeyGenerationFailure (resultList );
182
- errorItems = new CopyOnWriteArrayList <JsonElement >();
183
- int statusCode = resultList .get (0 ).getStatusCode ();
184
- statusCodes .add (statusCode );
185
- errorItems .add (obj );
186
184
checkEventStatus = false ;
187
185
} else {
188
186
PublishResultItem resultItem = rabbitmqConfigurationNotFound (msgService );
189
187
resultList .add (resultItem );
190
- int statusCode = resultItem .getStatusCode ();
191
- statusCodes .add (statusCode );
192
188
break ;
193
189
}
194
190
} else {
195
191
if (!checkEventStatus ) {
196
- addUnsuccessfulResultItem (obj );
197
- int statusCode = resultList .get (0 ).getStatusCode ();
198
- statusCodes .add (statusCode );
192
+ addUnsuccessfulResultItem (resultList , obj );
199
193
} else {
200
194
createFailureResult (resultList );
201
- errorItems = new CopyOnWriteArrayList <JsonElement >();
202
- int statusCode = resultList .get (0 ).getStatusCode ();
203
- statusCodes .add (statusCode );
204
- errorItems .add (obj );
205
195
checkEventStatus = false ;
206
196
}
207
197
}
208
198
}
209
199
} else {
210
- statusCodes = new CopyOnWriteArrayList <Integer >();
211
200
result = send (json .toString (), msgService , userDomainSuffix , tag , routingKey );
212
201
resultList .addAll (result .getEvents ());
213
- int statusCode = result .getEvents ().get (0 ).getStatusCode ();
214
- if (!statusCodes .contains (statusCode ))
215
- statusCodes .add (statusCode );
216
202
}
217
- result = new SendResult ();
218
- result .setEvents (resultList );
219
- return result ;
203
+
204
+ return createSendResult (resultList );
220
205
}
221
206
222
- private String sendMessage (String routingKey , String msg , MsgService msgService ) throws IOException ,NackException , TimeoutException , RemRemPublishException {
207
+ private String sendMessage (String routingKey , String msg , MsgService msgService ) throws IOException ,TimeoutException , RemRemPublishException {
223
208
String resultMsg = PropertiesConfig .SUCCESS ;
224
209
try {
225
210
instantiateRmqHelper ();
226
211
} catch (RemRemPublishException e ) {
227
- log .error ("RemRemPublishException occurred::" + e .getMessage ());
212
+ log .error ("RemRemPublishException occurred: {}" , e .getMessage ());
228
213
}
229
214
rmqHelper .send (routingKey , msg , msgService );
230
215
return resultMsg ;
@@ -268,7 +253,7 @@ private String getAndCheckEvent(MsgService msgService, Map<String, String> map,
268
253
269
254
/**
270
255
* Method returns result for the failure event.
271
- * @param events for list the eiffel events results
256
+ * @param resultItemList for list the eiffel events results
272
257
*/
273
258
private void createFailureResult (List <PublishResultItem > resultItemList ) {
274
259
PublishResultItem resultItem = new PublishResultItem (null , 400 , PropertiesConfig .INVALID_MESSAGE ,
@@ -293,21 +278,9 @@ private void routingKeyGenerationFailure(List<PublishResultItem> resultItemList)
293
278
resultItemList .add (resultItem );
294
279
}
295
280
296
- private void addUnsuccessfulResultItem (JsonElement obj ) {
281
+ private void addUnsuccessfulResultItem (List < PublishResultItem > resultList , JsonElement obj ) {
297
282
PublishResultItem event = new PublishResultItem (null , 503 , PropertiesConfig .SERVICE_UNAVAILABLE ,
298
283
PropertiesConfig .UNSUCCESSFUL_EVENT_CONTENT );
299
284
resultList .add (event );
300
285
}
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
286
}
0 commit comments