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
Solved! Go to Solution.
Views
Replies
Total Likes
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.
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.
@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.
Views
Replies
Total Likes