AuditLogTimelineEventProvider.java
9.14 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.timeline.TimelineEvent
* com.adobe.granite.timeline.TimelineEventProvider
* com.adobe.granite.timeline.TimelineEventType
* com.day.cq.audit.AuditLog
* com.day.cq.audit.AuditLogEntry
* com.day.cq.wcm.api.AuditLogTimelineEventType
* com.day.cq.wcm.api.PageModification
* com.day.cq.wcm.api.PageModification$ModificationType
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.core.impl;
import com.adobe.granite.timeline.TimelineEvent;
import com.adobe.granite.timeline.TimelineEventProvider;
import com.adobe.granite.timeline.TimelineEventType;
import com.day.cq.audit.AuditLog;
import com.day.cq.audit.AuditLogEntry;
import com.day.cq.wcm.api.AuditLogTimelineEventType;
import com.day.cq.wcm.api.PageModification;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
public class AuditLogTimelineEventProvider
implements TimelineEventProvider {
public static final TimelineEventType EVENT_TYPE = new AuditLogTimelineEventType(){
public String getName() {
return AuditLogTimelineEventType.class.getCanonicalName();
}
};
private static final String[] AUDIT_TOPICS = new String[]{"com/day/cq/wcm/core/page", "com/day/cq/replication"};
private static final DateFormat FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private final Logger log = LoggerFactory.getLogger(AuditLogTimelineEventProvider.class);
@Reference
private AuditLog auditLog;
public boolean accepts(Resource resource) {
return null != resource;
}
public Collection<TimelineEvent> getEvents(Resource resource) {
ArrayList<TimelineEvent> events = new ArrayList<TimelineEvent>();
FilteredModificationsBucket modBucket = new FilteredModificationsBucket(resource);
AuditLogEntry[] logEntries = this.auditLog.getLatestEvents(AUDIT_TOPICS, resource.getPath(), -1);
this.log.debug("got [{}] audit log entries for resource [{}]", (Object)logEntries.length, (Object)resource.getPath());
boolean ignoreCreate = false;
block5 : for (AuditLogEntry logEntry : logEntries) {
String category = logEntry.getCategory();
if (category.equals("com/day/cq/wcm/core/page")) {
PageModification.ModificationType modificationType = PageModification.ModificationType.fromName((String)logEntry.getType());
switch (modificationType) {
case MODIFIED: {
this.log.debug("adding page modification audit log entry [{}] to bucket for [{}]: " + FORMAT_DATE.format(logEntry.getTime()), (Object)logEntry.getPath(), (Object)resource.getPath());
modBucket.add(logEntry, modificationType);
continue block5;
}
case CREATED: {
if (!ignoreCreate) {
this.log.debug("adding page creation audit log entry [{}] for [{}]", (Object)logEntry.getPath(), (Object)resource.getPath());
events.add(new AuditLogTimelineEvent(logEntry, resource));
} else {
this.log.debug("skipping page creation audit log entry [{}] for [{}]", (Object)logEntry.getPath(), (Object)resource.getPath());
}
ignoreCreate = true;
continue block5;
}
case VERSION_CREATED: {
this.log.debug("ignoring page versioning audit log entry [{}] for [{}]", (Object)logEntry.getPath(), (Object)resource.getPath());
modBucket.add(logEntry, modificationType);
continue block5;
}
}
this.log.debug("adding audit log entry [{}] for [{}]", (Object)logEntry.getType(), (Object)resource.getPath());
events.add(new AuditLogTimelineEvent(logEntry, resource));
continue;
}
if (category.equals("com/day/cq/replication")) {
this.log.debug("added replication audit log entry [{}] as timeline event for [{}]", (Object)logEntry.getPath(), (Object)resource.getPath());
events.add(new AuditLogTimelineEvent(logEntry, resource));
continue;
}
this.log.debug("ignoring unknown audit log event category [{}] on [{}]", (Object)category, (Object)resource.getPath());
}
Collection<TimelineEvent> bucketEvents = modBucket.getTimelineEvents();
this.log.debug("adding [{}] page modifications event from bucket for [{}]", (Object)bucketEvents.size(), (Object)resource.getPath());
events.addAll(bucketEvents);
this.log.debug("returning a total of [{}] timeline events for [{}]", (Object)events.size(), (Object)resource.getPath());
if (this.log.isDebugEnabled()) {
for (TimelineEvent event : events) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(event.getTime());
this.log.debug(" >> [" + FORMAT_DATE.format(cal.getTime()) + " :: " + event.getUserID() + "] {} : {} - [" + event.getOrigin() + "]", (Object)event.getType(), (Object)event.getAction());
}
}
return events;
}
public TimelineEventType getType() {
return EVENT_TYPE;
}
protected void bindAuditLog(AuditLog auditLog) {
this.auditLog = auditLog;
}
protected void unbindAuditLog(AuditLog auditLog) {
if (this.auditLog == auditLog) {
this.auditLog = null;
}
}
private static class FilteredModificationsBucket {
final Map<Long, ArrayList<AuditLogEntry>> daysMap = new HashMap<Long, ArrayList<AuditLogEntry>>();
private final Resource resource;
private String lastUserId;
private boolean versionCreated;
public FilteredModificationsBucket(Resource resource) {
this.resource = resource;
}
public void add(AuditLogEntry newEntry, PageModification.ModificationType modificationType) {
if (PageModification.ModificationType.VERSION_CREATED == modificationType) {
this.versionCreated = true;
} else {
String userId = newEntry.getUserId();
Calendar cal = Calendar.getInstance();
cal.setTime(newEntry.getTime());
cal.set(11, 0);
cal.set(12, 0);
cal.set(13, 0);
cal.set(14, 0);
ArrayList entriesByDay = this.daysMap.get(cal.getTimeInMillis());
if (null == entriesByDay) {
entriesByDay = new ArrayList();
}
if (!userId.equals(this.lastUserId) || this.versionCreated) {
entriesByDay.add(newEntry);
this.lastUserId = newEntry.getUserId();
this.versionCreated = false;
}
this.daysMap.put(cal.getTimeInMillis(), entriesByDay);
}
}
public Collection<TimelineEvent> getTimelineEvents() {
ArrayList<TimelineEvent> events = new ArrayList<TimelineEvent>();
for (List entriesByDay : this.daysMap.values()) {
for (AuditLogEntry entry : entriesByDay) {
events.add(new AuditLogTimelineEvent(entry, this.resource));
}
}
return events;
}
}
private static class AuditLogTimelineEvent
implements TimelineEvent {
private final AuditLogEntry logEntry;
private final Resource resource;
public AuditLogTimelineEvent(AuditLogEntry logEntry, Resource resource) {
this.logEntry = logEntry;
this.resource = resource;
}
public String getAction() {
return this.logEntry.getCategory().equals("com/day/cq/wcm/core/page") ? PageModification.ModificationType.fromName((String)this.logEntry.getType()).toString() : this.logEntry.getType();
}
public String getDescription() {
return null;
}
public String getOrigin() {
return this.resource.getPath();
}
public long getTime() {
return this.logEntry.getTime().getTime();
}
public TimelineEventType getType() {
return AuditLogTimelineEventProvider.EVENT_TYPE;
}
public String getUserID() {
return this.logEntry.getUserId();
}
}
}