Expand my Community achievements bar.

SOLVED

EVENT FILTER is not working

Avatar

Level 1

Hi All,

Have defined EVENT_FILTER in my code so that my replication event gets trigger only in that respective path mentioned as event filter. But here event gets trigger at every location path.

Below is the sample snippet have defined for filter:

@component(service = EventHandler.class, immediate = true, property = {
Constants.SERVICE_DESCRIPTION + "= DAM Asset Events",
EventConstants.EVENT_TOPIC + "=" + ReplicationAction.EVENT_TOPIC,
EventConstants.EVENT_FILTER + "path=/content/dam/projectA"})

If replication events are triggered under folder "/content/dam/projectB" then those are also triggering logic. This is not expected so needs to restrict it. Could anyone let me know how to resolve it.

Thanks

RSL

1 Accepted Solution

Avatar

Correct answer by
Employee

Try this

@component(service = EventHandler.class, immediate = true, property = {
Constants.SERVICE_DESCRIPTION + "= DAM Asset Events",
EventConstants.EVENT_TOPIC + "=" + ReplicationAction.EVENT_TOPIC,
EventConstants.EVENT_FILTER + "=(paths=/content/dam/projectA/*)" })

View solution in original post

8 Replies

Avatar

Community Advisor

Hi @sail83812260,

OSGI Event Filter property follows LDAP filter syntax. Hence try the below

EventConstants.EVENT_FILTER + "=" + "(path=/content/dam/projectA)"

 

@Component(service = EventHandler.class, immediate = true, property = {
Constants.SERVICE_DESCRIPTION + "= DAM Asset Events",
EventConstants.EVENT_TOPIC + "=" + ReplicationAction.EVENT_TOPIC,
EventConstants.EVENT_FILTER + "=" + "(path=/content/dam/projectA)"})

 

Avatar

Level 1

HI @Vijayalakshmi_S , have applied the changes but don`t see its working. The logic itself is not getting triggered on any replication event.

Avatar

Community Advisor

Hi @sail83812260,

After reproducing it in my local, could see that the culprit is "=" after the event.filter property.

With this correction, replication events are listened but path/filter value is not respected - It is listening to all paths.

Have tried below possible trials, couldn't see positive outcome. 

  • For replication events, event property for path would be paths. (Screenshot 1)
  • Combining path restriction with not condition. 

One last option left is to try with Felix SCR annotation. Will try and update my findings. 

 

As an alternative/interim, you can try solution from @ChitraMadan which uses Felix SCR annotation and the path is validated at the time of handling event. (all replication events are listened)

Screenshot 1:

Vijayalakshmi_S_1-1597146566585.png

 

Avatar

Community Advisor

Try with

EventConstants.EVENT_FILTER + "(path=/content/dam/projectA/*/jcr:content)"}

 

Example

https://github.com/arunpatidar02/aem63app-repo/blob/master/java/TestEventHandler.java



Arun Patidar

Avatar

Community Advisor

Hi @sail83812260 ,

 

If this still doesn't work, may be you can try this example using EventHandler. Its working for me.

 

@Service
@Component(label = "Listener on ACTIVATE Action", description = "REPLICATE sample content.", immediate = true, metatype = true)
public class SamplePublishListener implements EventHandler {

@Property(name = "event.topics", value = {ReplicationAction.EVENT_TOPIC})
private static final String EVENT_TOPIC = "";

@Property(name = "allowed.paths.regex", value = {
"/content/dam/projectA/(.*)"})
private static final String PATH_REGEX = "";

@Reference
private JobManager jobManager;

private List<String> regexPaths;

/**
* The job topic for activate job events.
*/
public static final String ACTIVATE_JOB_TOPIC = "com/sling/eventing/activate/sample/job";
/**
* The job name for activate job events.
*/
private static final String ACTIVATE_JOB_NAME = "Sample Activate Job";

@Activate
protected void activate(final Map<String, Object> props) {
this.update(props);
}

@Modified
protected void update(final Map<String, Object> props) {

initCollections();

String[] configuredPaths = (String[]) props.get("allowed.paths.regex");

if (null != configuredPaths) {

List<String> tempconfiguredPaths = Arrays.asList(configuredPaths);

for (String path : tempconfiguredPaths) {
this.regexPaths.add(path);
}
}

}

@Override
public void handleEvent(Event event) {
ReplicationAction action = ReplicationAction.fromEvent(event);

if (action != null) {
ReplicationActionType eventType = action.getType();
String path = action.getPaths()[0];

if (isValidPath(path) && eventType.equals(ReplicationActionType.ACTIVATE)) {
final Map<String, Object> payload = new HashMap<>();
payload.put("resourcePath", path);

jobManager.addJob(ACTIVATE_JOB_TOPIC, payload);
}
}

}

/**
* Init the collections.
*/
private void initCollections() {
if (null == this.regexPaths) {
this.regexPaths = new ArrayList<String>();
} else {
this.regexPaths.clear();
}
}

/**
* Return a true if the current path matches the regular expression.
*
* @param path current node path
* @return boolean true if matches
*/
private boolean isValidPath(String path) {
for (String regEx : this.regexPaths) {
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(path);
if (m.matches()) {
return true;
}
}
return false;
}

}

Avatar

Correct answer by
Employee

Try this

@component(service = EventHandler.class, immediate = true, property = {
Constants.SERVICE_DESCRIPTION + "= DAM Asset Events",
EventConstants.EVENT_TOPIC + "=" + ReplicationAction.EVENT_TOPIC,
EventConstants.EVENT_FILTER + "=(paths=/content/dam/projectA/*)" })

Avatar

Community Advisor
Hi @kautuk_sahni, Please mark this as the correct solution!!