43
43
44
44
import javax .inject .Named ;
45
45
46
+ import org .apache .commons .lang3 .StringUtils ;
46
47
import org .slf4j .Logger ;
47
48
import org .slf4j .LoggerFactory ;
48
49
@@ -92,8 +93,13 @@ public class SemanticsService implements MsgService {
92
93
private static final String ID = "id" ;
93
94
private static final String META = "meta" ;
94
95
private static final String TYPE = "type" ;
96
+ private static final String SOURCE = "source" ;
97
+ private static final String DOMAIN_ID = "domainId" ;
98
+ private static final String PROTOCOL = "eiffel" ;
99
+ private static final String DOT = "." ;
95
100
private final ArrayList <String > supportedEventTypes = new ArrayList <String >();
96
101
public static final Logger log = LoggerFactory .getLogger (SemanticsService .class );
102
+ private Event event = new Event ();
97
103
98
104
private static Gson gson = new Gson ();
99
105
private static Map <EiffelEventType , Class <? extends Event >> eventTypes = SemanticsService .eventType ();
@@ -224,21 +230,37 @@ public String getInputEventType(JsonObject json) {
224
230
}
225
231
226
232
@ Override
227
- public String getFamily (JsonObject eiffelMessage ) {
233
+ public String getEventType (JsonObject json ) {
234
+ if (json .isJsonObject () && json .getAsJsonObject ().has (META ) && json .getAsJsonObject ()
235
+ .getAsJsonObject (META ).has (TYPE )) {
236
+ return json .getAsJsonObject ().getAsJsonObject (META )
237
+ .get (TYPE ).getAsString ();
238
+ }
239
+ return null ;
240
+ }
241
+
242
+ /**
243
+ * Returns Family Routing Key Word from the messaging library based on the eiffel message eventType.
244
+ * @param JsonObject eiffelMessage
245
+ * @return family routing key word in String format.
246
+ */
247
+ private String getFamily (JsonObject eiffelMessage ) {
228
248
if (eiffelMessage .isJsonObject () && eiffelMessage .getAsJsonObject ().has (META )
229
249
&& eiffelMessage .getAsJsonObject ().getAsJsonObject (META ).has (TYPE )) {
230
- return Event
231
- .getFamilyRoutingKey (eiffelMessage .getAsJsonObject ().getAsJsonObject (META ).get (TYPE ).getAsString ());
250
+ return event .getFamilyRoutingKey (eiffelMessage .getAsJsonObject ().getAsJsonObject (META ).get (TYPE ).getAsString ());
232
251
}
233
252
return null ;
234
253
}
235
254
236
- @ Override
237
- public String getType (JsonObject eiffelMessage ) {
255
+ /**
256
+ * Returns Type Routing Key Word from the messaging library based on the eiffel message eventType.
257
+ * @param JsonObject eiffelMessage
258
+ * @return type routing key word in String format.
259
+ */
260
+ private String getType (JsonObject eiffelMessage ) {
238
261
if (eiffelMessage .isJsonObject () && eiffelMessage .getAsJsonObject ().has (META )
239
262
&& eiffelMessage .getAsJsonObject ().getAsJsonObject (META ).has (TYPE )) {
240
- return Event
241
- .getTypeRoutingKey (eiffelMessage .getAsJsonObject ().getAsJsonObject (META ).get (TYPE ).getAsString ());
263
+ return event .getTypeRoutingKey (eiffelMessage .getAsJsonObject ().getAsJsonObject (META ).get (TYPE ).getAsString ());
242
264
}
243
265
return null ;
244
266
}
@@ -261,4 +283,42 @@ public ValidationResult validateMsg(String msgType, JsonObject jsonvalidateMessa
261
283
}
262
284
return validationResult ;
263
285
}
286
+
287
+ /**
288
+ * Returns the domain Id from json formatted eiffel message.
289
+ * @param eiffelMessage eiffel message in json format
290
+ * @return the domainId from eiffelMessage if domainId not available then returns the null value
291
+ */
292
+ private String getDomainId (JsonObject eiffelMessage ) {
293
+ if (eiffelMessage .isJsonObject () && eiffelMessage .getAsJsonObject ().has (META ) && eiffelMessage .getAsJsonObject ()
294
+ .getAsJsonObject (META ).has (SOURCE ) && eiffelMessage .getAsJsonObject ()
295
+ .getAsJsonObject (META ).getAsJsonObject (SOURCE ).has (DOMAIN_ID )) {
296
+ return eiffelMessage .getAsJsonObject ().getAsJsonObject (META ).getAsJsonObject (SOURCE )
297
+ .get (DOMAIN_ID ).getAsString ();
298
+ }
299
+ return null ;
300
+ }
301
+
302
+ @ Override
303
+ public String generateRoutingKey (JsonObject eiffelMessage , String tag , String domain , String userDomainSuffix ) {
304
+ String family = getFamily (eiffelMessage );
305
+ String type = getType (eiffelMessage );
306
+ if (StringUtils .isNotEmpty (family ) && StringUtils .isNotEmpty (type )) {
307
+ if (StringUtils .isNotBlank (tag ) && (tag .contains ("." ) || StringUtils .deleteWhitespace (tag ).length () > 16 )) {
308
+ log .error ("tag must not contain any dots and must not exceed 16 characters" );
309
+ return null ;
310
+ }
311
+ String domainId = getDomainId (eiffelMessage );
312
+ //If domainId from input message is null then configured domain will be considered
313
+ domainId = StringUtils .defaultIfBlank (domainId , domain );
314
+ if (StringUtils .isNotBlank (domainId )) {
315
+ if (StringUtils .isNotBlank (userDomainSuffix )) {
316
+ domainId = domainId + DOT + userDomainSuffix ;
317
+ }
318
+ return StringUtils .deleteWhitespace (PROTOCOL + DOT + family + DOT + type + DOT + StringUtils .defaultIfBlank (tag , "notag" ) + DOT + domainId );
319
+ }
320
+ log .error ("domain needed for Routing key generation in the format <protocol>.<family>.<type>.<tag>.<domain> is not provided in either input message or configuration" );
321
+ }
322
+ return null ;
323
+ }
264
324
}
0 commit comments