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 package info.magnolia.audit;
35
36 import info.magnolia.jcr.decoration.AbstractContentDecorator;
37
38 import java.util.LinkedHashMap;
39 import java.util.LinkedList;
40 import java.util.Map;
41
42 import javax.jcr.Node;
43 import javax.jcr.Property;
44 import javax.jcr.RepositoryException;
45 import javax.jcr.Session;
46 import javax.jcr.Workspace;
47 import javax.jcr.nodetype.NodeType;
48
49
50
51
52 public class MgnlAuditLoggingContentDecorator extends AbstractContentDecorator {
53
54
55
56
57 private Map<String, LinkedList<MgnlAuditLogEntry>> logEntries = new LinkedHashMap<String, LinkedList<MgnlAuditLogEntry>>();
58
59
60
61
62
63
64
65 public Map<String, LinkedList<MgnlAuditLogEntry>> getLogEntries() {
66 return logEntries;
67 }
68
69
70
71
72 @Override
73 public Session wrapSession(Session session) {
74 return new MgnlAuditLoggingContentDecoratorSessionWrapper(session, this);
75 }
76
77
78
79
80 @Override
81 public Node wrapNode(Node node) {
82 return new MgnlAuditLoggingContentDecoratorNodeWrapper(node, this);
83 }
84
85
86
87
88 @Override
89 public Property wrapProperty(Property property) {
90 return new MgnlAuditLoggingContentDecoratorPropertyWrapper(property, this);
91 }
92
93
94
95
96 @Override
97 public Workspace wrapWorkspace(Workspace workspace) {
98 return new MgnlAuditLoggingContentDecoratorWorspaceWrapper(workspace, this);
99 }
100
101 @Override
102 public boolean isMultipleWrapEnabled() {
103 return false;
104 }
105
106
107
108
109 protected void logActionCreate(Node node) throws RepositoryException {
110 AuditLoggingManager auditLoggingManager = AuditLoggingManager.getInstance();
111 if (auditLoggingManager == null || !auditLoggingManager.isAuditLoggingActive()) {
112 return;
113 }
114
115 final long timeStamp = System.nanoTime();
116 logActionCreate(node.getPath(), node.getSession().getWorkspace().getName(), node.getPrimaryNodeType(), timeStamp);
117 }
118
119
120
121
122 protected void logActionCreate(String path, String workspace, NodeType nodeType, long timeStamp) {
123 AuditLoggingManager auditLoggingManager = AuditLoggingManager.getInstance();
124 if (auditLoggingManager == null || !auditLoggingManager.isAuditLoggingActive()) {
125 return;
126 }
127
128 if (!logEntries.containsKey(path)) {
129 MgnlAuditLogEntry entry = createLogEntry(AuditLoggingUtil.ACTION_CREATE, workspace, nodeType, path, timeStamp);
130 addPathFirstLogEntry(path, entry);
131 } else {
132
133 logEntries.get(path).add(createLogEntry(AuditLoggingUtil.ACTION_CREATE, workspace, nodeType, path, timeStamp));
134 }
135 }
136
137
138
139
140 protected void logActionModify(Node node) throws RepositoryException {
141 AuditLoggingManager auditLoggingManager = AuditLoggingManager.getInstance();
142 if (auditLoggingManager == null || !auditLoggingManager.isAuditLoggingActive()) {
143 return;
144 }
145
146 final long timeStamp = System.nanoTime();
147 logActionModify(node.getPath(), node.getSession().getWorkspace().getName(), node.getPrimaryNodeType(), timeStamp);
148 }
149
150
151
152
153 protected void logActionModify(String path, String workspace, NodeType nodeType, final long timeStamp) {
154 AuditLoggingManager auditLoggingManager = AuditLoggingManager.getInstance();
155 if (auditLoggingManager == null || !auditLoggingManager.isAuditLoggingActive()) {
156 return;
157 }
158
159 if (!logEntries.containsKey(path)) {
160 MgnlAuditLogEntry entry = createLogEntry(AuditLoggingUtil.ACTION_MODIFY, workspace, nodeType, path, timeStamp);
161 addPathFirstLogEntry(path, entry);
162 } else {
163
164 LinkedList<MgnlAuditLogEntry> pathEntries = logEntries.get(path);
165 if (!pathEntries.isEmpty()) {
166 final String lastAction = pathEntries.getLast().getAction();
167 if (lastAction.equals(AuditLoggingUtil.ACTION_CREATE) || lastAction.equals(AuditLoggingUtil.ACTION_MODIFY)) {
168 return;
169 }
170 }
171 pathEntries.add(createLogEntry(AuditLoggingUtil.ACTION_MODIFY, workspace, nodeType, path, timeStamp));
172 }
173 }
174
175
176
177
178 public void logActionMove(String srcAbsPath, String destAbsPath, String workspace) {
179 AuditLoggingManager auditLoggingManager = AuditLoggingManager.getInstance();
180 if (auditLoggingManager == null || !auditLoggingManager.isAuditLoggingActive()) {
181 return;
182 }
183
184 final long timeStamp = System.nanoTime();
185 if (!logEntries.containsKey(srcAbsPath)) {
186 MgnlAuditLogEntry entry = createLogEntry(AuditLoggingUtil.ACTION_MOVE, workspace, srcAbsPath, destAbsPath, timeStamp);
187 addPathFirstLogEntry(srcAbsPath, entry);
188 } else {
189 logEntries.get(srcAbsPath).add(createLogEntry(AuditLoggingUtil.ACTION_MOVE, workspace, srcAbsPath, destAbsPath, timeStamp));
190 }
191 }
192
193
194
195
196 protected void logActionDelete(String path, String workspace, NodeType nodeType) {
197 AuditLoggingManager auditLoggingManager = AuditLoggingManager.getInstance();
198 if (auditLoggingManager == null || !auditLoggingManager.isAuditLoggingActive()) {
199 return;
200 }
201
202 final long timeStamp = System.nanoTime();
203 if (!logEntries.containsKey(path)) {
204 MgnlAuditLogEntry entry = createLogEntry(AuditLoggingUtil.ACTION_DELETE, workspace, nodeType, path, timeStamp);
205 addPathFirstLogEntry(path, entry);
206 } else {
207 LinkedList<MgnlAuditLogEntry> pathEntries = logEntries.get(path);
208 if (!pathEntries.isEmpty()) {
209 MgnlAuditLogEntry lastPathEntry = pathEntries.getLast();
210 final String lastAction = lastPathEntry.getAction();
211 if (lastAction.equals(AuditLoggingUtil.ACTION_CREATE)) {
212
213 pathEntries.remove(lastPathEntry);
214 return;
215 } else if (lastAction.equals(AuditLoggingUtil.ACTION_MODIFY)) {
216
217 pathEntries.remove(lastPathEntry);
218 pathEntries.add(createLogEntry(AuditLoggingUtil.ACTION_DELETE, workspace, nodeType, path, timeStamp));
219 return;
220 }
221 }
222 pathEntries.add(createLogEntry(AuditLoggingUtil.ACTION_DELETE, workspace, nodeType, path, timeStamp));
223 }
224 }
225
226 private void addPathFirstLogEntry(String path, MgnlAuditLogEntry entry) {
227 LinkedList<MgnlAuditLogEntry> pathEntries = new LinkedList<MgnlAuditLogEntry>();
228 pathEntries.add(entry);
229 logEntries.put(path, pathEntries);
230 }
231
232
233
234
235 private MgnlAuditLogEntry createLogEntry(String action, String workspace, String pathFrom, String pathTo, long timeStamp) {
236 return new MgnlAuditLogEntry(action, workspace, pathFrom, pathTo, timeStamp);
237 }
238
239
240
241
242 private MgnlAuditLogEntry createLogEntry(String action, String workspace, NodeType nodeType, String path, long timeStamp) {
243 return new MgnlAuditLogEntry(action, workspace, nodeType, path, timeStamp);
244 }
245
246 }