Event Handler for Asset's modification (edit, copy ..etc) | Community
Skip to main content
Community Advisor
November 30, 2022
Solved

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

  • November 30, 2022
  • 2 replies
  • 2105 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by lukasz-m

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.

2 replies

Adobe Employee
November 30, 2022

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

lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
November 30, 2022

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.

vjleo94
Level 3
July 30, 2024

Hi @lukasz-m ,

 

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

 

Best regards,

Vijay