View Javadoc

1   /**
2    * This file Copyright (c) 2013 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.audit;
35  
36  import info.magnolia.jcr.decoration.ContentDecoratorNodeWrapper;
37  
38  import java.io.InputStream;
39  import java.math.BigDecimal;
40  import java.util.Calendar;
41  import java.util.LinkedHashMap;
42  import java.util.LinkedList;
43  import java.util.Map;
44  import java.util.Map.Entry;
45  
46  import javax.jcr.AccessDeniedException;
47  import javax.jcr.Binary;
48  import javax.jcr.InvalidItemStateException;
49  import javax.jcr.ItemExistsException;
50  import javax.jcr.Node;
51  import javax.jcr.PathNotFoundException;
52  import javax.jcr.Property;
53  import javax.jcr.ReferentialIntegrityException;
54  import javax.jcr.RepositoryException;
55  import javax.jcr.Value;
56  import javax.jcr.ValueFormatException;
57  import javax.jcr.lock.LockException;
58  import javax.jcr.nodetype.ConstraintViolationException;
59  import javax.jcr.nodetype.NoSuchNodeTypeException;
60  import javax.jcr.nodetype.NodeType;
61  import javax.jcr.version.VersionException;
62  
63  /**
64   * A Node wrapper implementation which inform
65   * Magnolia audit logging content decorator {@link info.magnolia.audit.MgnlAuditLoggingContentDecorator} about action (Create, Modify, Delete) on the node.
66   * The implementation also take care about logging appropriate actions into audit-log output during save action of node.
67   */
68  public class MgnlAuditLoggingContentDecoratorNodeWrapper extends ContentDecoratorNodeWrapper<MgnlAuditLoggingContentDecorator> {
69  
70      public MgnlAuditLoggingContentDecoratorNodeWrapper(Node node, MgnlAuditLoggingContentDecorator contentDecorator) {
71          super(node, contentDecorator);
72      }
73  
74      @Override
75      public Node addNode(String relPath) throws ItemExistsException, PathNotFoundException, VersionException, ConstraintViolationException, LockException, RepositoryException {
76          Node node = super.addNode(relPath);
77          getContentDecorator().logActionCreate(node);
78          return node;
79      }
80  
81      @Override
82      public Node addNode(String relPath, String primaryNodeTypeName) throws ItemExistsException, PathNotFoundException, VersionException, ConstraintViolationException, LockException, RepositoryException {
83          Node node = super.addNode(relPath, primaryNodeTypeName);
84          getContentDecorator().logActionCreate(node);
85          return node;
86      }
87  
88      @Override
89      public void remove() throws VersionException, LockException, ConstraintViolationException, AccessDeniedException, RepositoryException {
90          final NodeType nodeType = getPrimaryNodeType();
91          final String path = getPath();
92          final String workspace = getSession().getWorkspace().getName();
93          super.remove();
94          getContentDecorator().logActionDelete(path, workspace, nodeType);
95  
96      }
97  
98      @Override
99      public void addMixin(String mixinName) throws NoSuchNodeTypeException, VersionException, ConstraintViolationException, LockException, RepositoryException {
100         super.addMixin(mixinName);
101         getContentDecorator().logActionModify(this);
102     }
103 
104     @Override
105     public void removeMixin(String mixinName) throws NoSuchNodeTypeException, VersionException, ConstraintViolationException, LockException, RepositoryException {
106         super.removeMixin(mixinName);
107         getContentDecorator().logActionModify(this);
108     }
109 
110     @Override
111     public void setPrimaryType(String nodeTypeName) throws NoSuchNodeTypeException, VersionException, ConstraintViolationException, LockException, RepositoryException {
112         super.setPrimaryType(nodeTypeName);
113         getContentDecorator().logActionModify(this);
114     }
115 
116     @Override
117     public Property setProperty(String name, Value value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
118         Property property = super.setProperty(name, value);
119         getContentDecorator().logActionModify(this);
120         return property;
121     }
122 
123     @Override
124     public Property setProperty(String name, Value[] values) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
125         Property property = super.setProperty(name, values);
126         getContentDecorator().logActionModify(this);
127         return property;
128     }
129 
130     @Override
131     public Property setProperty(String name, String[] values) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
132         Property property = super.setProperty(name, values);
133         getContentDecorator().logActionModify(this);
134         return property;
135     }
136 
137     @Override
138     public Property setProperty(String name, String value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
139         Property property = super.setProperty(name, value);
140         getContentDecorator().logActionModify(this);
141         return property;
142     }
143 
144     @Override
145     public Property setProperty(String name, InputStream value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
146         Property property = super.setProperty(name, value);
147         getContentDecorator().logActionModify(this);
148         return property;
149     }
150 
151     @Override
152     public Property setProperty(String name, Binary value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
153         Property property = super.setProperty(name, value);
154         getContentDecorator().logActionModify(this);
155         return property;
156     }
157 
158     @Override
159     public Property setProperty(String name, boolean value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
160         Property property = super.setProperty(name, value);
161         getContentDecorator().logActionModify(this);
162         return property;
163     }
164 
165     @Override
166     public Property setProperty(String name, double value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
167         Property property = super.setProperty(name, value);
168         getContentDecorator().logActionModify(this);
169         return property;
170     }
171 
172     @Override
173     public Property setProperty(String name, BigDecimal value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
174         Property property = super.setProperty(name, value);
175         getContentDecorator().logActionModify(this);
176         return property;
177     }
178 
179     @Override
180     public Property setProperty(String name, long value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
181         Property property = super.setProperty(name, value);
182         getContentDecorator().logActionModify(this);
183         return property;
184     }
185 
186     @Override
187     public Property setProperty(String name, Calendar value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
188         Property property = super.setProperty(name, value);
189         getContentDecorator().logActionModify(this);
190         return property;
191     }
192 
193     @Override
194     public Property setProperty(String name, Node value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
195         Property property = super.setProperty(name, value);
196         getContentDecorator().logActionModify(this);
197         return property;
198     }
199 
200     @Override
201     public Property setProperty(String name, Value value, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
202         Property property = super.setProperty(name, value);
203         getContentDecorator().logActionModify(this);
204         return property;
205     }
206 
207     @Override
208     public Property setProperty(String name, Value[] values, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
209         Property property = super.setProperty(name, values);
210         getContentDecorator().logActionModify(this);
211         return property;
212     }
213 
214     @Override
215     public Property setProperty(String name, String[] values, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
216         Property property = super.setProperty(name, values);
217         getContentDecorator().logActionModify(this);
218         return property;
219     }
220 
221     @Override
222     public Property setProperty(String name, String value, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
223         Property property = super.setProperty(name, value);
224         getContentDecorator().logActionModify(this);
225         return property;
226     }
227 
228     /**
229      * During node save action log all action which was done for the node and his childrens.
230      */
231     @Override
232     public void save() throws AccessDeniedException, ItemExistsException, ConstraintViolationException, InvalidItemStateException, ReferentialIntegrityException, VersionException, LockException, NoSuchNodeTypeException, RepositoryException {
233         super.save();
234 
235         Map<String, LinkedList<MgnlAuditLogEntry>> logMap = getContentDecorator().getLogEntries();
236         Map<String, LinkedList<MgnlAuditLogEntry>> temp = new LinkedHashMap<String, LinkedList<MgnlAuditLogEntry>>();
237         temp.putAll(logMap);
238         final String path = getPath();
239         for (Entry<String, LinkedList<MgnlAuditLogEntry>> pathLogEntries : temp.entrySet()) {
240             if (pathLogEntries.getKey().equals(path) || pathLogEntries.getKey().startsWith(path + "/")) {
241                 for (MgnlAuditLogEntry entry : pathLogEntries.getValue()) {
242                     AuditLoggingUtil.log(entry.getAction(), entry.getTimeStamp(), entry.getWorkspace(), entry.getNodeType(), entry.getPath(), entry.getPathTo());
243                 }
244                 logMap.remove(pathLogEntries.getKey());
245             }
246         }
247     }
248 
249 }