Logging Image Tag by Event Handler | Community
Skip to main content
March 7, 2025
Solved

Logging Image Tag by Event Handler

  • March 7, 2025
  • 1 reply
  • 528 views

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

Best answer by konstantyn_diachenko

Hi @vodjakxa ,

 

I found that OOTB there is a Dam Event. 

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. 

1 reply

konstantyn_diachenko
Community Advisor
konstantyn_diachenkoCommunity AdvisorAccepted solution
Community Advisor
March 7, 2025

Hi @vodjakxa ,

 

I found that OOTB there is a Dam Event. 

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. 

Kostiantyn DiachenkoCheck out AEM VLT Intellij plugin
Adobe Employee
March 11, 2025

@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/overview

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