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
This commit is contained in:
Derek Hulley
2010-07-30 15:48:02 +00:00
parent 54fc5b8f83
commit efc0ca3913
8 changed files with 194 additions and 30 deletions

View File

@@ -6,6 +6,16 @@
<authentication>admin</authentication> <authentication>admin</authentication>
<transaction allow="readonly">required</transaction> <transaction allow="readonly">required</transaction>
<lifecycle>internal</lifecycle> <lifecycle>internal</lifecycle>
<args>
<arg>
<name>application</name>
<description>Name of the audit application (omit to assume all applications)</description>
</arg>
<arg>
<name>path</name>
<description>Path within the application (omit to assume application root)</description>
</arg>
</args>
<!-- turn off the multipart formdata processing --> <!-- turn off the multipart formdata processing -->
<formdata multipart-processing="false" /> <formdata multipart-processing="false" />

View File

@@ -0,0 +1,27 @@
<webscript>
<shortname>Alfresco Audit Service Control</shortname>
<description>Change the audit status for a given application and path</description>
<url>/api/audit/control/{action}?application={application?}&amp;path={path?}</url>
<format default="json" />
<authentication>admin</authentication>
<transaction>required</transaction>
<lifecycle>internal</lifecycle>
<args>
<arg>
<name>action</name>
<description>Set to 'enable' or 'disable' to change the audit state</description>
</arg>
<arg>
<name>application</name>
<description>Name of the audit application (omit to assume all applications)</description>
</arg>
<arg>
<name>path</name>
<description>Path within the application (omit to assume application root)</description>
</arg>
</args>
<!-- turn off the multipart formdata processing -->
<formdata multipart-processing="false" />
</webscript>

View File

@@ -1,3 +1,4 @@
# Audit Control Web Script I18N # Audit Control Web Script I18N
audit.err.app.mandatory=Parameter 'app' is mandatory audit.err.app.mandatory=Parameter 'app' is mandatory
audit.err.path.startsWith=Parameter 'path', when supplied, must start with '/' audit.err.path.startsWith=Parameter 'path', when supplied, must start with '/'
audit.err.action.invalid=Parameter 'action' must be either 'enable' or 'disable'

View File

@@ -812,7 +812,7 @@
parent="abstractWorkflowWebScript"></bean> parent="abstractWorkflowWebScript"></bean>
<!-- --> <!-- -->
<!-- Workflow Service REST API --> <!-- Audit Service REST API -->
<!-- --> <!-- -->
<!-- abstract audit web script --> <!-- abstract audit web script -->
@@ -824,10 +824,15 @@
<!-- Gets the current audit state (enabled/disabled) for an application and path --> <!-- Gets the current audit state (enabled/disabled) for an application and path -->
<bean id="webscript.org.alfresco.repository.audit.control.get" <bean id="webscript.org.alfresco.repository.audit.control.get"
class="org.alfresco.repo.web.scripts.audit.ControlGet" class="org.alfresco.repo.web.scripts.audit.AuditControlGet"
parent="abstractAuditWebScript"> parent="abstractAuditWebScript">
</bean> </bean>
<!-- Change audit state (enabled/disabled) for an application and path -->
<bean id="webscript.org.alfresco.repository.audit.control.post"
class="org.alfresco.repo.web.scripts.audit.AuditControlPost"
parent="abstractAuditWebScript">
</bean>
<!-- --> <!-- -->
<!-- Replication Service REST API --> <!-- Replication Service REST API -->

View File

@@ -18,6 +18,8 @@
*/ */
package org.alfresco.repo.web.scripts.audit; package org.alfresco.repo.web.scripts.audit;
import java.util.Map;
import org.alfresco.service.cmr.audit.AuditService; import org.alfresco.service.cmr.audit.AuditService;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@@ -37,12 +39,15 @@ public abstract class AbstractAuditWebScript extends AbstractWebScript
{ {
public static final String PARAM_APP = "app"; public static final String PARAM_APP = "app";
public static final String PARAM_PATH="path"; public static final String PARAM_PATH="path";
public static final String PARAM_ACTION = "action";
public static final String JSON_KEY_APPLICATIONS = "applications"; public static final String JSON_KEY_APPLICATIONS = "applications";
public static final String JSON_KEY_NAME = "name"; public static final String JSON_KEY_NAME = "name";
public static final String JSON_KEY_PATH = "path"; public static final String JSON_KEY_PATH = "path";
public static final String JSON_KEY_ENABLED = "enabled"; public static final String JSON_KEY_ENABLED = "enabled";
private static enum AuditWebScriptAction {enable, disable};
/** /**
* Logger that can be used by subclasses. * Logger that can be used by subclasses.
*/ */
@@ -86,16 +91,16 @@ public abstract class AbstractAuditWebScript extends AbstractWebScript
} }
/** /**
* Get the path from the request. If it is mandatory, then a value must have been supplied * Get the path from the request. If it is mandatory, then a value must have been supplied
* otherwise, at the very least, '/' is returned. * otherwise <tt>null</tt> is returned.
* @param mandatory <tt>true</tt> if the parameter is expected * @param mandatory <tt>true</tt> if the parameter is expected
* @return Returns the path or at least '/' (never <tt>null</tt>) * @return Returns the path or <tt>null</tt> if not present
*/ */
protected String getPath(WebScriptRequest req) protected String getPath(WebScriptRequest req)
{ {
String paramPath = req.getParameter(PARAM_PATH); String paramPath = req.getParameter(PARAM_PATH);
if (paramPath == null || paramPath.length() == 0) if (paramPath == null || paramPath.length() == 0)
{ {
paramPath = "/"; paramPath = null;
} }
else if (!paramPath.startsWith("/")) else if (!paramPath.startsWith("/"))
{ {
@@ -103,4 +108,27 @@ public abstract class AbstractAuditWebScript extends AbstractWebScript
} }
return paramPath; return paramPath;
} }
protected boolean getEnableDisable(WebScriptRequest req)
{
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
String enableStr = templateVars.get(PARAM_ACTION);
try
{
AuditWebScriptAction action = AuditWebScriptAction.valueOf(enableStr);
switch (action)
{
case enable:
return true;
case disable:
return false;
default:
return false;
}
}
catch (IllegalArgumentException e)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "audit.err.action.invalid");
}
}
} }

View File

@@ -19,7 +19,6 @@
package org.alfresco.repo.web.scripts.audit; package org.alfresco.repo.web.scripts.audit;
import java.io.IOException; import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Collections; import java.util.Collections;
import java.util.Set; import java.util.Set;
@@ -33,7 +32,7 @@ import org.springframework.extensions.webscripts.json.JSONWriter;
* @author Derek Hulley * @author Derek Hulley
* @since 3.4 * @since 3.4
*/ */
public class ControlGet extends AbstractAuditWebScript public class AuditControlGet extends AbstractAuditWebScript
{ {
@Override @Override
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException
@@ -43,14 +42,25 @@ public class ControlGet extends AbstractAuditWebScript
String app = getApp(req, false); String app = getApp(req, false);
String path = getPath(req); String path = getPath(req);
Set<String> apps = (app == null ? auditService.getAuditApplications() : Collections.singleton(app)); Set<String> apps = auditService.getAuditApplications();
// Check that the application exists
if (app != null)
{
if (apps.contains(app))
{
apps = Collections.singleton(app);
}
else
{
apps = Collections.emptySet();
}
}
boolean enabledGlobal = auditService.isAuditEnabled(); boolean enabledGlobal = auditService.isAuditEnabled();
json.startObject(); json.startObject();
{ {
json.writeValue(JSON_KEY_ENABLED, enabledGlobal); json.writeValue(JSON_KEY_ENABLED, enabledGlobal);
if (apps.size() > 0)
{
json.startValue(JSON_KEY_APPLICATIONS); json.startValue(JSON_KEY_APPLICATIONS);
{ {
json.startArray(); json.startArray();
@@ -71,7 +81,6 @@ public class ControlGet extends AbstractAuditWebScript
} }
json.endValue(); json.endValue();
} }
}
json.endObject(); json.endObject();
// Close off // Close off
@@ -82,9 +91,4 @@ public class ControlGet extends AbstractAuditWebScript
// res.addHeader("Content-Length", "" + length); // TODO: Do we need this? // res.addHeader("Content-Length", "" + length); // TODO: Do we need this?
res.setStatus(Status.STATUS_OK); res.setStatus(Status.STATUS_OK);
} }
protected void writeResponse(JSONWriter json)
{
}
} }

View File

@@ -0,0 +1,57 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.web.scripts.audit;
import java.io.IOException;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
/**
* @author Derek Hulley
* @since 3.4
*/
public class AuditControlPost extends AbstractAuditWebScript
{
@Override
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException
{
String app = getApp(req, false);
String path = getPath(req);
boolean enable = getEnableDisable(req);
if (app == null)
{
// Global operation
auditService.setAuditEnabled(enable);
}
else
{
// Apply to a specific application
auditService.enableAudit(app, path);
}
// res.setContentType("application/json");
// res.setContentEncoding(Charset.defaultCharset().displayName()); // TODO: Should be settable on JSONWriter
// res.addHeader("Content-Length", "" + length); // TODO: Do we need this?
res.setStatus(Status.STATUS_OK);
}
}

View File

@@ -20,6 +20,7 @@ package org.alfresco.repo.web.scripts.audit;
import java.util.Set; import java.util.Set;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.web.scripts.BaseWebScriptTest; import org.alfresco.repo.web.scripts.BaseWebScriptTest;
import org.alfresco.service.cmr.audit.AuditService; import org.alfresco.service.cmr.audit.AuditService;
@@ -68,6 +69,7 @@ public class AuditWebScriptTest extends BaseWebScriptTest
public void testGetIsAuditEnabledGlobally() throws Exception public void testGetIsAuditEnabledGlobally() throws Exception
{ {
boolean checkEnabled = auditService.isAuditEnabled(); boolean checkEnabled = auditService.isAuditEnabled();
Set<String> checkApps = auditService.getAuditApplications();
String url = "/api/audit/control"; String url = "/api/audit/control";
TestWebScriptServer.GetRequest req = new TestWebScriptServer.GetRequest(url); TestWebScriptServer.GetRequest req = new TestWebScriptServer.GetRequest(url);
@@ -76,12 +78,13 @@ public class AuditWebScriptTest extends BaseWebScriptTest
JSONObject json = new JSONObject(response.getContentAsString()); JSONObject json = new JSONObject(response.getContentAsString());
boolean enabled = json.getBoolean("enabled"); boolean enabled = json.getBoolean("enabled");
assertEquals("Mismatched global audit enabled", checkEnabled, enabled); assertEquals("Mismatched global audit enabled", checkEnabled, enabled);
JSONArray apps = json.getJSONArray(AbstractAuditWebScript.JSON_KEY_APPLICATIONS);
assertEquals("Incorrect number of applications reported", checkApps.size(), apps.length());
} }
public void testGetIsAuditEnabledMissingApp() throws Exception public void testGetIsAuditEnabledMissingApp() throws Exception
{ {
boolean checkEnabled = auditService.isAuditEnabled(); boolean checkEnabled = auditService.isAuditEnabled();
Set<String> checkApps = auditService.getAuditApplications();
String url = "/api/audit/control?app=xxx"; String url = "/api/audit/control?app=xxx";
TestWebScriptServer.GetRequest req = new TestWebScriptServer.GetRequest(url); TestWebScriptServer.GetRequest req = new TestWebScriptServer.GetRequest(url);
@@ -93,6 +96,35 @@ public class AuditWebScriptTest extends BaseWebScriptTest
assertEquals("Mismatched global audit enabled", checkEnabled, enabled); assertEquals("Mismatched global audit enabled", checkEnabled, enabled);
JSONArray apps = json.getJSONArray(AbstractAuditWebScript.JSON_KEY_APPLICATIONS); JSONArray apps = json.getJSONArray(AbstractAuditWebScript.JSON_KEY_APPLICATIONS);
// We expect that the unknown application is returned with the others // We expect that the unknown application is returned with the others
assertEquals("Incorrect number of applications reported", checkApps.size()+1, apps.length()); assertEquals("Should not be any apps listed", 0, apps.length());
}
public void testSetAuditEnabled() throws Exception
{
boolean checkEnabled = auditService.isAuditEnabled();
// We need to set this back after the test
try
{
if (checkEnabled)
{
String url = "/api/audit/control/disable";
TestWebScriptServer.PostRequest req = new TestWebScriptServer.PostRequest(url, "", MimetypeMap.MIMETYPE_JSON);
sendRequest(req, 200, admin);
}
else
{
String url = "/api/audit/control/enable";
TestWebScriptServer.PostRequest req = new TestWebScriptServer.PostRequest(url, "", MimetypeMap.MIMETYPE_JSON);
sendRequest(req, 200, admin);
}
// Check that it worked
testGetIsAuditEnabledGlobally();
}
finally
{
auditService.setAuditEnabled(checkEnabled);
}
} }
} }