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