AuditLogServlet.java
6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* 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;
}
}
}