Create and read Audit Log entry for an Event

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;


import org.apache.sling.event.EventUtil;

import org.osgi.service.event.Event;

import org.osgi.service.event.EventHandler;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;


import com.day.cq.audit.AuditLog;

import com.day.cq.audit.AuditLogEntry;

import com.day.cq.wcm.api.PageEvent;

import com.day.cq.wcm.api.PageModification;

import com.day.cq.wcm.api.PageModification.ModificationType;

/**

 * This listener listens for page modification events and creates audit log

 * entries.

 *

 * @scr.component metatype="no" immediate="true" policy="require"

 * @scr.service interface="EventHandler"

 * @scr.property name="event.topics" valueRef="PageEvent.EVENT_TOPIC"

 */

public class MovePageEventAuditListener implements EventHandler {

    /**

     * The logger

     */

    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    /**

     * @scr.reference

     */

    protected AuditLog auditLog;


    /**

     * @see org.osgi.service.event.EventHandler#handleEvent(org.osgi.service.event.Event)

     */

    public void handleEvent(Event event) {

        if (EventUtil.isLocal(event)) {

            PageEvent pageEvent = PageEvent.fromEvent(event);

            if (pageEvent != null) {

                AuditLog localAL = auditLog;

                if (localAL != null) {

                    List<String[]> movedPages = null;


                    List<AuditLogEntry> entries = new ArrayList<AuditLogEntry>();

                    Iterator<PageModification> i = pageEvent.getModifications();

                    while (i.hasNext()) {

                        PageModification pm = i.next();

                        String auditType = pm.getType().toString();

                        if (logger.isDebugEnabled()) {

                            logger.debug("recording {}", pm.toString());

                        }

                        // check for moves

                        // You can also check for any other page operation here

                        if (pm.getType() == ModificationType.MOVED) {

                            if (movedPages == null) {

                                movedPages = new ArrayList<String[]>();

                            }

                            // only create audit entry, if this was not a pure ordering

                            if (!pm.getPath().equals(pm.getDestination())) {

                                movedPages.add(new String[]{pm.getPath(), pm.getDestination()});

                            }

                        }


                        AuditLogEntry ae = new AuditLogEntry(PageEvent.EVENT_TOPIC,

                                pm.getModificationDate(),

                                pm.getUserId(),

                                pm.getPath(),

                                auditType,

                                pm.getEventProperties());

                               entries.add(ae);


                    }

                    // add audit log entries

                    localAL.add(entries);


                    // and now moves

                    if (movedPages != null) {

                        for (String[] args: movedPages) {

                            localAL.move(args[0], args[1]);

                        }

                    }

                }

            }

        }

    }

}




Further you can use http://dev.day.com/docs/en/cq/current/javadoc/ com.day.cq.audit api to read entry from log.


Something like

@Reference 

    private AuditLog auditLog;

final String path = <Any path under which you want to search>;

final String [] categories = { <ClassNameforwhichyouwantAudit>.class.getName() }; 

final AuditLogEntry [] entries = auditLog.getLatestEventsFromTree(categories, path, n);

            final int count = entries == null ? 0 : entries.length;

            log.info("AuditLogQuery with path={} and n={} returns {} events", new Object[] { path, n, count });

             w.key("results").value(count); //Count is number of result

          for(AuditLogEntry e : entries) {

                 w.key("category").value(e.getCategory());

                w.key("path").value(e.getPath());

                w.key("user").value(e.getUserId());

                w.key("time").value(e.getTime());

        }

Comments