AuditLogServlet.java 6.32 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.audit.AuditLog
 *  com.day.cq.audit.AuditLogEntry
 *  com.day.cq.wcm.msm.api.RolloutManager
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.servlet.ServletException
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.jackrabbit.api.security.user.Authorizable
 *  org.apache.jackrabbit.api.security.user.UserManager
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.servlets.SlingSafeMethodsServlet
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.io.JSONWriter
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.msm.impl.servlets;

import com.day.cq.audit.AuditLog;
import com.day.cq.audit.AuditLogEntry;
import com.day.cq.wcm.msm.api.RolloutManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Date;
import java.util.Dictionary;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=1)
@Service
@Properties(value={@Property(name="sling.servlet.paths", value={"/bin/msm/audit"}, propertyPrivate=1), @Property(name="sling.servlet.extensions", value={"json"}, propertyPrivate=1)})
public class AuditLogServlet
extends SlingSafeMethodsServlet {
    private final Logger log;
    @Reference
    private AuditLog auditLog;
    public static final String PARAM_N_EVENTS = "count";
    @Property(intValue={100})
    public static final String PROP_DEFAULT_EVENTS_COUNT = "auditlogservlet.default.events.count";
    private int defaultEventsCount;
    @Property(value={"/"})
    public static final String PROP_DEFAULT_PATH = "auditlogservlet.default.path";
    private String defaultPath;

    public AuditLogServlet() {
        this.log = LoggerFactory.getLogger(this.getClass());
        this.auditLog = null;
    }

    @Activate
    protected void activate(ComponentContext ctx) {
        Dictionary props = ctx.getProperties();
        this.defaultEventsCount = (Integer)props.get("auditlogservlet.default.events.count");
        this.defaultPath = (String)props.get("auditlogservlet.default.path");
        this.log.info("Activated, default events count={}, default path={}", (Object)this.defaultEventsCount, (Object)this.defaultPath);
    }

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        int n = this.defaultEventsCount;
        String str = request.getParameter("count");
        if (str != null) {
            n = Integer.parseInt(str);
        }
        String path = this.defaultPath;
        Session requestSession = (Session)request.getResourceResolver().adaptTo(Session.class);
        if (requestSession == null) {
            this.log.debug("Got Request without session, ignore");
            return;
        }
        UserManager userManager = (UserManager)request.adaptTo(UserManager.class);
        try {
            JSONWriter w = new JSONWriter((Writer)response.getWriter());
            String[] categories = new String[]{RolloutManager.class.getName()};
            AuditLogEntry[] entries = this.auditLog.getLatestEventsFromTree(categories, path, n);
            this.log.info("AuditLogQuery with path={} and n={} returns {} events", new Object[]{path, n, entries.length});
            int count = 0;
            w.object();
            w.key("entries");
            w.array();
            for (AuditLogEntry e : entries) {
                String auditPath = e.getPath();
                if (!requestSession.hasPermission(auditPath, "read")) continue;
                w.object();
                w.key("category").value((Object)e.getCategory());
                w.key("path").value((Object)e.getPath());
                w.key("user").value((Object)(userManager == null || userManager.getAuthorizable(e.getUserId()) == null ? "" : e.getUserId()));
                w.key("time").value((Object)e.getTime());
                Map props = e.getProperties();
                for (String key : new String[]{"exception", "exceptionMessage", "operation", "target"}) {
                    Object value = props.get(key);
                    if (value == null) continue;
                    w.key(key).value((Object)value.toString());
                }
                ++count;
                w.endObject();
            }
            w.endArray();
            w.key("results").value((long)count);
            w.endObject();
        }
        catch (JSONException jex) {
            throw new ServletException("JSONException while building response", (Throwable)jex);
        }
        catch (RepositoryException e) {
            throw new ServletException("Repository failure while checking permissions", (Throwable)e);
        }
    }

    protected void bindAuditLog(AuditLog auditLog) {
        this.auditLog = auditLog;
    }

    protected void unbindAuditLog(AuditLog auditLog) {
        if (this.auditLog == auditLog) {
            this.auditLog = null;
        }
    }
}