From c576b914b43a55ab314da1ebeff703b97958a91a Mon Sep 17 00:00:00 2001 From: Derek Hulley Date: Fri, 30 Jul 2010 15:48:02 +0000 Subject: [PATCH] ALF-4106 (ALF-4103): AuditService REST API - Enable/disable auditing and tests - TODO: Use .ftl to generate JSON from model git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@21520 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- config/alfresco/audit-services-context.xml | 2 +- .../alfresco/repo/audit/AuditComponent.java | 36 ++++++--- .../repo/audit/AuditComponentImpl.java | 81 ++++++++++++++----- .../repo/audit/AuditComponentTest.java | 14 +++- .../alfresco/repo/audit/AuditServiceImpl.java | 40 ++++++--- .../repo/audit/model/AuditModelRegistry.java | 4 + .../audit/model/AuditModelRegistryImpl.java | 12 +-- .../service/cmr/audit/AuditService.java | 38 +++++++-- 8 files changed, 168 insertions(+), 59 deletions(-) diff --git a/config/alfresco/audit-services-context.xml b/config/alfresco/audit-services-context.xml index 19d8e28e00..4a808fb0cc 100644 --- a/config/alfresco/audit-services-context.xml +++ b/config/alfresco/audit-services-context.xml @@ -16,7 +16,7 @@ - + diff --git a/source/java/org/alfresco/repo/audit/AuditComponent.java b/source/java/org/alfresco/repo/audit/AuditComponent.java index 4692c1aa31..121cfb145d 100644 --- a/source/java/org/alfresco/repo/audit/AuditComponent.java +++ b/source/java/org/alfresco/repo/audit/AuditComponent.java @@ -35,6 +35,24 @@ import org.alfresco.service.cmr.audit.AuditService.AuditQueryCallback; */ public interface AuditComponent { + /** + * Determines whether audit is globally enabled or disabled. + * + * @return Returns true if audit is enabled + * + * @since 3.3 + */ + public boolean isAuditEnabled(); + + /** + * Switch auditing on or off + * + * @param enable true to enable auditing or false to disable + * + * @since 3.4 + */ + public void setAuditEnabled(boolean enable); + /** * Get all registered audit applications, whether active or not. * @@ -44,15 +62,6 @@ public interface AuditComponent */ public Set getAuditApplications(); - /** - * Determines whether audit is globally enabled or disabled. - * - * @return Returns true if audit is enabled - * - * @since 3.3 - */ - public boolean isAuditEnabled(); - /** * Determines whether the given source path is mapped to any audit applications. Allows optimizations to be made in * calling components. @@ -81,7 +90,8 @@ public interface AuditComponent * data is audited. * * @param applicationName the name of the application being logged to - * @param path the audit path to check + * @param path the audit path to check or null to assume the + * application's root path * @return Returns true if the audit path has been disabled * * @since 3.2 @@ -97,7 +107,8 @@ public interface AuditComponent * If the enabled * * @param applicationName the name of the application being logged to - * @param path the audit path to enable auditing on + * @param path the audit path to check or null to assume the + * application's root path * * @since 3.2 */ @@ -114,7 +125,8 @@ public interface AuditComponent * the root path of the application, then auditing for that application is effectively disabled. * * @param applicationName the name of the application being logged to - * @param path the audit path to enable auditing on + * @param path the audit path to check or null to assume the + * application's root path * * @since 3.2 */ diff --git a/source/java/org/alfresco/repo/audit/AuditComponentImpl.java b/source/java/org/alfresco/repo/audit/AuditComponentImpl.java index e981b43adb..5e1e643cde 100644 --- a/source/java/org/alfresco/repo/audit/AuditComponentImpl.java +++ b/source/java/org/alfresco/repo/audit/AuditComponentImpl.java @@ -31,6 +31,7 @@ import org.alfresco.repo.audit.extractor.DataExtractor; import org.alfresco.repo.audit.generator.DataGenerator; import org.alfresco.repo.audit.model.AuditApplication; import org.alfresco.repo.audit.model.AuditModelRegistry; +import org.alfresco.repo.audit.model.AuditModelRegistryImpl; import org.alfresco.repo.domain.audit.AuditDAO; import org.alfresco.repo.domain.propval.PropertyValueDAO; import org.alfresco.repo.security.authentication.AuthenticationUtil; @@ -60,7 +61,7 @@ public class AuditComponentImpl implements AuditComponent { private static Log logger = LogFactory.getLog(AuditComponentImpl.class); - private AuditModelRegistry auditModelRegistry; + private AuditModelRegistryImpl auditModelRegistry; private PropertyValueDAO propertyValueDAO; private AuditDAO auditDAO; private TransactionService transactionService; @@ -76,7 +77,7 @@ public class AuditComponentImpl implements AuditComponent * Set the registry holding the audit models * @since 3.2 */ - public void setAuditModelRegistry(AuditModelRegistry auditModelRegistry) + public void setAuditModelRegistry(AuditModelRegistryImpl auditModelRegistry) { this.auditModelRegistry = auditModelRegistry; } @@ -157,6 +158,34 @@ public class AuditComponentImpl implements AuditComponent } } + /** + * {@inheritDoc} + * @since 3.2 + */ + public boolean isAuditEnabled() + { + return auditModelRegistry.isAuditEnabled(); + } + + /** + * {@inheritDoc} + * @since 3.4 + */ + @Override + public void setAuditEnabled(boolean enable) + { + boolean alreadyEnabled = auditModelRegistry.isAuditEnabled(); + if (alreadyEnabled != enable) + { + // It is changing + auditModelRegistry.stop(); + auditModelRegistry.setProperty( + AuditModelRegistry.AUDIT_PROPERTY_AUDIT_ENABLED, + Boolean.toString(enable).toLowerCase()); + auditModelRegistry.start(); + } + } + /** * {@inheritDoc} * @since 3.4 @@ -167,15 +196,6 @@ public class AuditComponentImpl implements AuditComponent return auditApps.keySet(); } - /** - * {@inheritDoc} - * @since 3.2 - */ - public boolean isAuditEnabled() - { - return auditModelRegistry.isAuditEnabled(); - } - /** * {@inheritDoc} * @since 3.2 @@ -192,7 +212,6 @@ public class AuditComponentImpl implements AuditComponent public boolean isAuditPathEnabled(String applicationName, String path) { ParameterCheck.mandatory("applicationName", applicationName); - ParameterCheck.mandatory("path", path); AlfrescoTransactionSupport.checkTransactionReadState(false); AuditApplication application = auditModelRegistry.getAuditApplicationByName(applicationName); @@ -204,8 +223,16 @@ public class AuditComponentImpl implements AuditComponent } return false; } - // Check the path against the application - application.checkPath(path); + // Ensure that the path gets a valid value + if (path == null) + { + path = AuditApplication.AUDIT_PATH_SEPARATOR + application.getApplicationKey(); + } + else + { + // Check the path against the application + application.checkPath(path); + } Set disabledPaths = getDisabledPaths(application); @@ -238,7 +265,6 @@ public class AuditComponentImpl implements AuditComponent public void enableAudit(String applicationName, String path) { ParameterCheck.mandatory("applicationName", applicationName); - ParameterCheck.mandatory("path", path); AlfrescoTransactionSupport.checkTransactionReadState(true); AuditApplication application = auditModelRegistry.getAuditApplicationByName(applicationName); @@ -250,8 +276,16 @@ public class AuditComponentImpl implements AuditComponent } return; } - // Check the path against the application - application.checkPath(path); + // Ensure that the path gets a valid value + if (path == null) + { + path = AuditApplication.AUDIT_PATH_SEPARATOR + application.getApplicationKey(); + } + else + { + // Check the path against the application + application.checkPath(path); + } Long disabledPathsId = application.getDisabledPathsId(); Set disabledPaths = getDisabledPaths(application); @@ -290,7 +324,6 @@ public class AuditComponentImpl implements AuditComponent public void disableAudit(String applicationName, String path) { ParameterCheck.mandatory("applicationName", applicationName); - ParameterCheck.mandatory("path", path); AlfrescoTransactionSupport.checkTransactionReadState(true); AuditApplication application = auditModelRegistry.getAuditApplicationByName(applicationName); @@ -302,8 +335,16 @@ public class AuditComponentImpl implements AuditComponent } return; } - // Check the path against the application - application.checkPath(path); + // Ensure that the path gets a valid value + if (path == null) + { + path = AuditApplication.AUDIT_PATH_SEPARATOR + application.getApplicationKey(); + } + else + { + // Check the path against the application + application.checkPath(path); + } Long disabledPathsId = application.getDisabledPathsId(); Set disabledPaths = getDisabledPaths(application); diff --git a/source/java/org/alfresco/repo/audit/AuditComponentTest.java b/source/java/org/alfresco/repo/audit/AuditComponentTest.java index 520c350c0a..223f80edfd 100644 --- a/source/java/org/alfresco/repo/audit/AuditComponentTest.java +++ b/source/java/org/alfresco/repo/audit/AuditComponentTest.java @@ -455,6 +455,18 @@ public class AuditComponentTest extends TestCase result = auditTestAction("action-01", nodeRef, parameters); checkAuditMaps(result, expectedInner); + // Disable using the root of the application by passing a null root + auditComponent.disableAudit(APPLICATION_ACTIONS_TEST, null); + expectedInner.clear(); + result = auditTestAction("action-01", nodeRef, parameters); + checkAuditMaps(result, expectedInner); + + // Enabling the root using a null root parameter should give back everything + auditComponent.enableAudit(APPLICATION_ACTIONS_TEST, null); + expectedInner = new HashMap(expected); + result = auditTestAction("action-01", nodeRef, parameters); + checkAuditMaps(result, expectedInner); + return null; } }; @@ -619,7 +631,7 @@ public class AuditComponentTest extends TestCase @Override public Void doWork() throws Exception { - auditService.clearAudit(applicationName); + auditService.clearAudit(applicationName, null, null); return null; } }; diff --git a/source/java/org/alfresco/repo/audit/AuditServiceImpl.java b/source/java/org/alfresco/repo/audit/AuditServiceImpl.java index 34cf5c091c..f15f10fdbf 100644 --- a/source/java/org/alfresco/repo/audit/AuditServiceImpl.java +++ b/source/java/org/alfresco/repo/audit/AuditServiceImpl.java @@ -43,6 +43,25 @@ public class AuditServiceImpl implements AuditService this.auditComponent = auditComponent; } + /** + * {@inheritDoc} + * @since 3.4 + */ + public boolean isAuditEnabled() + { + return auditComponent.isAuditEnabled(); + } + + /** + * {@inheritDoc} + * @since 3.4 + */ + @Override + public void setAuditEnabled(boolean enable) + { + auditComponent.setAuditEnabled(enable); + } + /** * {@inheritDoc} * @since 3.4 @@ -53,22 +72,12 @@ public class AuditServiceImpl implements AuditService return auditComponent.getAuditApplications(); } - /** - * {@inheritDoc} - * @since 3.4 - */ - public boolean isAuditEnabled() - { - return auditComponent.isAuditEnabled(); - } - /** * {@inheritDoc} * @since 3.2 */ public boolean isAuditEnabled(String applicationName, String path) { - // Get the root path for the application return auditComponent.isAuditPathEnabled(applicationName, path); } @@ -100,6 +109,17 @@ public class AuditServiceImpl implements AuditService auditComponent.deleteAuditEntries(applicationName, null, now); } + /** + * {@inheritDoc} + * @since 3.4 + */ + @Override + public void clearAudit(String applicationName, Long fromTime, Long toTime) + { + toTime = (toTime == null) ? Long.valueOf(System.currentTimeMillis()) : toTime; + auditComponent.deleteAuditEntries(applicationName, fromTime, toTime); + } + /** * {@inheritDoc} * @since 3.3 diff --git a/source/java/org/alfresco/repo/audit/model/AuditModelRegistry.java b/source/java/org/alfresco/repo/audit/model/AuditModelRegistry.java index 5ce04f8164..0fbe91dcc8 100644 --- a/source/java/org/alfresco/repo/audit/model/AuditModelRegistry.java +++ b/source/java/org/alfresco/repo/audit/model/AuditModelRegistry.java @@ -30,6 +30,10 @@ import org.alfresco.util.PathMapper; */ public interface AuditModelRegistry { + /** The name of the global enablement property. */ + public static final String AUDIT_PROPERTY_AUDIT_ENABLED = "audit.enabled"; + public static final String AUDIT_SCHEMA_LOCATION = "classpath:alfresco/audit/alfresco-audit-3.2.xsd"; + public static final String AUDIT_RESERVED_KEY_USERNAME = "username"; public static final String AUDIT_RESERVED_KEY_SYSTEMTIME = "systemTime"; diff --git a/source/java/org/alfresco/repo/audit/model/AuditModelRegistryImpl.java b/source/java/org/alfresco/repo/audit/model/AuditModelRegistryImpl.java index e0c3dccaca..c46d89edfa 100644 --- a/source/java/org/alfresco/repo/audit/model/AuditModelRegistryImpl.java +++ b/source/java/org/alfresco/repo/audit/model/AuditModelRegistryImpl.java @@ -77,10 +77,6 @@ import org.xml.sax.SAXParseException; */ public class AuditModelRegistryImpl extends AbstractPropertyBackedBean implements AuditModelRegistry { - /** The name of the global enablement property. */ - private static final String PROPERTY_AUDIT_ENABLED = "audit.enabled"; - private static final String AUDIT_SCHEMA_LOCATION = "classpath:alfresco/audit/alfresco-audit-3.2.xsd"; - private static final Log logger = LogFactory.getLog(AuditModelRegistryImpl.class); private String[] searchPath; @@ -213,7 +209,7 @@ public class AuditModelRegistryImpl extends AbstractPropertyBackedBean implement @Override public boolean isAuditEnabled() { - String value = getProperty(PROPERTY_AUDIT_ENABLED); + String value = getProperty(AUDIT_PROPERTY_AUDIT_ENABLED); return value != null && value.equalsIgnoreCase("true"); } @@ -226,7 +222,7 @@ public class AuditModelRegistryImpl extends AbstractPropertyBackedBean implement public synchronized void registerModel(URL auditModelUrl) { stop(); - setProperty(PROPERTY_AUDIT_ENABLED, "true"); + setProperty(AUDIT_PROPERTY_AUDIT_ENABLED, "true"); getState(false).registerModel(auditModelUrl); } @@ -255,7 +251,7 @@ public class AuditModelRegistryImpl extends AbstractPropertyBackedBean implement properties = new HashMap(7); // Default value for global enabled property - properties.put(PROPERTY_AUDIT_ENABLED, false); + properties.put(AUDIT_PROPERTY_AUDIT_ENABLED, false); // Let's search for config files in the appropriate places. The individual applications they contain can still // be enabled/disabled by the bean properties @@ -364,7 +360,7 @@ public class AuditModelRegistryImpl extends AbstractPropertyBackedBean implement auditPathMapper = new PathMapper(); // If we are globally disabled, skip processing the models - Boolean enabled = properties.get(PROPERTY_AUDIT_ENABLED); + Boolean enabled = properties.get(AUDIT_PROPERTY_AUDIT_ENABLED); if (enabled != null && enabled) { final RetryingTransactionCallback loadModelsCallback = new RetryingTransactionCallback() diff --git a/source/java/org/alfresco/service/cmr/audit/AuditService.java b/source/java/org/alfresco/service/cmr/audit/AuditService.java index dd30947ce1..9f0f2d7abc 100644 --- a/source/java/org/alfresco/service/cmr/audit/AuditService.java +++ b/source/java/org/alfresco/service/cmr/audit/AuditService.java @@ -32,6 +32,22 @@ import org.alfresco.service.PublicService; @PublicService public interface AuditService { + /** + * @return Returns true if auditing is globally enabled + * + * @since 3.4 + */ + boolean isAuditEnabled(); + + /** + * Enable or disable the global auditing state + * + * @param enable true to enable auditing globally or false to disable + * + * @since 3.4 + */ + void setAuditEnabled(boolean enable); + /** * Get all registered audit applications * @@ -41,13 +57,6 @@ public interface AuditService */ Set getAuditApplications(); - /** - * @return Returns true if auditing is globally enabled - * - * @since 3.4 - */ - boolean isAuditEnabled(); - /** * @param applicationName the name of the application to check * @param path the path to check @@ -83,9 +92,24 @@ public interface AuditService * @param applicationName the name of the application for which to remove entries * * @since 3.2 + * + * @deprecated Use {@link #clearAudit(String, Long, Long)} */ void clearAudit(String applicationName); + /** + * Remove audit entries for the given application between the time ranges. If no start + * time is given then entries are deleted as far back as they exist. If no end time is + * given then entries are deleted up until the current time. + * + * @param applicationName the name of the application for which to remove entries + * @param fromTime the start time of entries to remove (inclusive and optional) + * @param toTime the end time of entries to remove (exclusive and optional) + * + * @since 3.4 + */ + void clearAudit(String applicationName, Long fromTime, Long toTime); + /** * The interface that will be used to give query results to the calling code. *