Expand my Community achievements bar.

SOLVED

Event Handler for Asset's modification (edit, copy ..etc)

Avatar

Community Advisor

use case is When any assets got updated , send mail to admin using event handler .Please help/advise if you have worked upon such requirement.

 

Thanks in advance.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @KNan,

In general you can use DAM EVENT_TOPIC which is dedicated to handle asset related operations, it defines multiple types related to different activities (asset metadata modification, asset move etc), you can find full list under:

In terms of usage in code you can have something like this:

package com.mysite.core.models;

import com.day.cq.dam.api.DamEvent;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(
        immediate = true,
        service = EventHandler.class,
        property = EventConstants.EVENT_TOPIC + "=" + DamEvent.EVENT_TOPIC)
public class DAMEventHandler implements EventHandler {

    private static final Logger LOG = LoggerFactory.getLogger(DAMEventHandler.class);

    @Override
    public void handleEvent(Event event) {
        DamEvent damEvent = DamEvent.fromEvent(event);
        if (shouldBeProcessed(damEvent)) {
            // place for your code
        }
    }

    private boolean shouldBeProcessed(DamEvent damEvent) {
        // list of Asset related events
        return DamEvent.Type.METADATA_UPDATED.equals(damEvent.getType() ||
                DamEvent.Type.ASSET_MOVED.equals(damEvent.getType();
    }
}

You can easily extend list of events that should trigger email to be send.

Regarding sending email itself - please do not do this directly from event handler - this activity should be delegated to Sling Job and maybe queued. In general any code that is not related to event information processing should be executed outside, in other case your event could be blacklisted.

View solution in original post

3 Replies

Avatar

Employee Advisor

hi @KNan , for the above requirement, you can write ResourceChangeListener. Sample code can be found here.

 

aem63app-repo/SampleResourceChangeListener2.java at master · arunpatidar02/aem63app-repo · GitHub

 

Hope this helps

 

Thanks,

Nikita

Avatar

Correct answer by
Community Advisor

Hi @KNan,

In general you can use DAM EVENT_TOPIC which is dedicated to handle asset related operations, it defines multiple types related to different activities (asset metadata modification, asset move etc), you can find full list under:

In terms of usage in code you can have something like this:

package com.mysite.core.models;

import com.day.cq.dam.api.DamEvent;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(
        immediate = true,
        service = EventHandler.class,
        property = EventConstants.EVENT_TOPIC + "=" + DamEvent.EVENT_TOPIC)
public class DAMEventHandler implements EventHandler {

    private static final Logger LOG = LoggerFactory.getLogger(DAMEventHandler.class);

    @Override
    public void handleEvent(Event event) {
        DamEvent damEvent = DamEvent.fromEvent(event);
        if (shouldBeProcessed(damEvent)) {
            // place for your code
        }
    }

    private boolean shouldBeProcessed(DamEvent damEvent) {
        // list of Asset related events
        return DamEvent.Type.METADATA_UPDATED.equals(damEvent.getType() ||
                DamEvent.Type.ASSET_MOVED.equals(damEvent.getType();
    }
}

You can easily extend list of events that should trigger email to be send.

Regarding sending email itself - please do not do this directly from event handler - this activity should be delegated to Sling Job and maybe queued. In general any code that is not related to event information processing should be executed outside, in other case your event could be blacklisted.

Avatar

Level 4

Hi @lukasz-m ,

 

Is it possible to use path restriction with this event handler ?

 

Best regards,

Vijay