Expand my Community achievements bar.

SOLVED

Logging Image Tag by Event Handler

Avatar

Level 2

I have to make an event handler that fires and prints logs whenever tags are assigned to images on the Dam. Finally I have to print the list of tags used on the images with the logs

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @Vodjakxa ,

 

I found that OOTB there is a Dam Event. 

konstantyn_diachenko_0-1741380697804.png

So, you can build your logic around this event.

import com.day.cq.dam.api.DamEvent;
import lombok.extern.slf4j.Slf4j;
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;
@Slf4j
@Component(
        service = EventHandler.class,
        property = {
                EventConstants.EVENT_TOPIC + "=" + DamEvent.EVENT_TOPIC
        },
        immediate = true
)
public class DamTagAssignEventListener implements EventHandler {
    @Override
    public void handleEvent(Event event) {
        log.info(event.toString());
        DamEvent damEvent = DamEvent.fromEvent(event);
        if (damEvent == null) {
            return;
        }
        if (DamEvent.Type.METADATA_UPDATED != damEvent.getType()) {
            return;
        }
        String additionalInfo = damEvent.getAdditionalInfo();
        if (additionalInfo == null || !additionalInfo.contains("jcr:content/metadata/cq:tags")) {
            return;
        }
        String tags = additionalInfo.substring(additionalInfo.indexOf("=") + 1);
        log.info("{} : {}", damEvent.getAssetPath(), tags);
    }
}

 

However, I noticed that additionalInfo always contains only 1st tag even if you assigned 2+ tags. Most likely you need to cache previous state of asset tag by extra call before saving of asset.

 

In addition, processing of Event Handler should not take more than 5 sec, otherwise it will be black-listed. If you have complex calculation, you need to start Sling Jobs immediately in Event Handler to outsource handing of event in another thread.

 

Best regards,

Kostiantyn Diachenko. 

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @Vodjakxa ,

 

I found that OOTB there is a Dam Event. 

konstantyn_diachenko_0-1741380697804.png

So, you can build your logic around this event.

import com.day.cq.dam.api.DamEvent;
import lombok.extern.slf4j.Slf4j;
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;
@Slf4j
@Component(
        service = EventHandler.class,
        property = {
                EventConstants.EVENT_TOPIC + "=" + DamEvent.EVENT_TOPIC
        },
        immediate = true
)
public class DamTagAssignEventListener implements EventHandler {
    @Override
    public void handleEvent(Event event) {
        log.info(event.toString());
        DamEvent damEvent = DamEvent.fromEvent(event);
        if (damEvent == null) {
            return;
        }
        if (DamEvent.Type.METADATA_UPDATED != damEvent.getType()) {
            return;
        }
        String additionalInfo = damEvent.getAdditionalInfo();
        if (additionalInfo == null || !additionalInfo.contains("jcr:content/metadata/cq:tags")) {
            return;
        }
        String tags = additionalInfo.substring(additionalInfo.indexOf("=") + 1);
        log.info("{} : {}", damEvent.getAssetPath(), tags);
    }
}

 

However, I noticed that additionalInfo always contains only 1st tag even if you assigned 2+ tags. Most likely you need to cache previous state of asset tag by extra call before saving of asset.

 

In addition, processing of Event Handler should not take more than 5 sec, otherwise it will be black-listed. If you have complex calculation, you need to start Sling Jobs immediately in Event Handler to outsource handing of event in another thread.

 

Best regards,

Kostiantyn Diachenko. 

Avatar

Employee

@Vodjakxa Are you trying to do this in AEM Cloud Service? If so, using AEM Eventing is the recommended approach. Refer to [0], [1] for more details. You need to use "Asset Metadata Updated" event for capturing the tags updated event.

 

[0] - https://experienceleague.adobe.com/en/docs/experience-manager-learn/cloud-service/aem-eventing/overv...

[1] - https://developer.adobe.com/experience-cloud/experience-manager-apis/guides/events/#enable-aem-event...