diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/audit/control.get.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/audit/control.get.desc.xml index 72e2e99fd8..75d6f64e56 100644 --- a/config/alfresco/templates/webscripts/org/alfresco/repository/audit/control.get.desc.xml +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/audit/control.get.desc.xml @@ -1,13 +1,13 @@ - - Alfresco Audit Service Control - Get audit status for a given application and path - /api/audit/control?app={application?}&path={path?} - - admin - required - internal - - - - - + + Alfresco Audit Service Control + Get audit status for a given application and path + /api/audit/control?application={application?}&path={path?} + + admin + required + internal + + + + + diff --git a/source/java/org/alfresco/repo/web/scripts/audit/AbstractAuditWebScript.java b/source/java/org/alfresco/repo/web/scripts/audit/AbstractAuditWebScript.java index 7576281964..929e9d8ce0 100644 --- a/source/java/org/alfresco/repo/web/scripts/audit/AbstractAuditWebScript.java +++ b/source/java/org/alfresco/repo/web/scripts/audit/AbstractAuditWebScript.java @@ -1,4 +1,4 @@ -/* +/* * Copyright (C) 2009-2010 Alfresco Software Limited. * * This file is part of Alfresco @@ -14,88 +14,93 @@ * 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 . - */ -package org.alfresco.repo.web.scripts.audit; - -import org.alfresco.service.cmr.audit.AuditService; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.extensions.surf.util.I18NUtil; -import org.springframework.extensions.webscripts.AbstractWebScript; -import org.springframework.extensions.webscripts.Status; -import org.springframework.extensions.webscripts.WebScriptException; -import org.springframework.extensions.webscripts.WebScriptRequest; - -/** - * Abstract implementation for scripts that access the {@link AuditService}. - * - * @author Derek Hulley - * @since 3.4 - */ -public abstract class AbstractAuditWebScript extends AbstractWebScript -{ - public static final String PARAM_APP = "app"; - public static final String PARAM_PATH="path"; - - /** - * Logger that can be used by subclasses. - */ - protected final Log logger = LogFactory.getLog(this.getClass()); - - protected AuditService auditService; - - /** - * @param auditService the service that provides the actual data - */ - public void setAuditService(AuditService auditService) - { - this.auditService = auditService; - } - - /** - * Return an I18N'd message for the given key or the key itself if not present - * - * @param args arguments to replace the variables in the message - */ - protected String getI18NMessage(String key, Object ... args) - { - return I18NUtil.getMessage(key, args); - } - - /** - * Get the application name from the request. - * - * @param mandatory true if the application name is expected - * @return Returns the application name or null if not present - */ - protected final String getApp(WebScriptRequest req, boolean mandatory) - { - // All URLs must contain the application - String paramApp = req.getParameter(PARAM_APP); - if (paramApp == null && mandatory) - { - throw new WebScriptException(Status.STATUS_BAD_REQUEST, "audit.err.app.mandatory"); - } - return paramApp; - } - /** - * Get the path from the request. If it is mandatory, then a value must have been supplied - * otherwise, at the very least, '/' is returned. - * @param mandatory true if the parameter is expected - * @return Returns the path or at least '/' (never null) - */ - protected String getPath(WebScriptRequest req) - { - String paramPath = req.getParameter(PARAM_PATH); - if (paramPath == null || paramPath.length() == 0) - { - paramPath = "/"; - } - else if (!paramPath.startsWith("/")) - { - throw new WebScriptException(Status.STATUS_BAD_REQUEST, "audit.err.path.startsWith"); - } - return paramPath; - } -} + * along with Alfresco. If not, see . + */ +package org.alfresco.repo.web.scripts.audit; + +import org.alfresco.service.cmr.audit.AuditService; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.extensions.surf.util.I18NUtil; +import org.springframework.extensions.webscripts.AbstractWebScript; +import org.springframework.extensions.webscripts.Status; +import org.springframework.extensions.webscripts.WebScriptException; +import org.springframework.extensions.webscripts.WebScriptRequest; + +/** + * Abstract implementation for scripts that access the {@link AuditService}. + * + * @author Derek Hulley + * @since 3.4 + */ +public abstract class AbstractAuditWebScript extends AbstractWebScript +{ + public static final String PARAM_APP = "app"; + public static final String PARAM_PATH="path"; + + public static final String JSON_KEY_APPLICATIONS = "applications"; + public static final String JSON_KEY_NAME = "name"; + public static final String JSON_KEY_PATH = "path"; + public static final String JSON_KEY_ENABLED = "enabled"; + + /** + * Logger that can be used by subclasses. + */ + protected final Log logger = LogFactory.getLog(this.getClass()); + + protected AuditService auditService; + + /** + * @param auditService the service that provides the actual data + */ + public void setAuditService(AuditService auditService) + { + this.auditService = auditService; + } + + /** + * Return an I18N'd message for the given key or the key itself if not present + * + * @param args arguments to replace the variables in the message + */ + protected String getI18NMessage(String key, Object ... args) + { + return I18NUtil.getMessage(key, args); + } + + /** + * Get the application name from the request. + * + * @param mandatory true if the application name is expected + * @return Returns the application name or null if not present + */ + protected final String getApp(WebScriptRequest req, boolean mandatory) + { + // All URLs must contain the application + String paramApp = req.getParameter(PARAM_APP); + if (paramApp == null && mandatory) + { + throw new WebScriptException(Status.STATUS_BAD_REQUEST, "audit.err.app.mandatory"); + } + return paramApp; + } + /** + * Get the path from the request. If it is mandatory, then a value must have been supplied + * otherwise, at the very least, '/' is returned. + * @param mandatory true if the parameter is expected + * @return Returns the path or at least '/' (never null) + */ + protected String getPath(WebScriptRequest req) + { + String paramPath = req.getParameter(PARAM_PATH); + if (paramPath == null || paramPath.length() == 0) + { + paramPath = "/"; + } + else if (!paramPath.startsWith("/")) + { + throw new WebScriptException(Status.STATUS_BAD_REQUEST, "audit.err.path.startsWith"); + } + return paramPath; + } +} diff --git a/source/java/org/alfresco/repo/web/scripts/audit/AuditWebScriptTest.java b/source/java/org/alfresco/repo/web/scripts/audit/AuditWebScriptTest.java index 21e450341d..85bed65cf1 100644 --- a/source/java/org/alfresco/repo/web/scripts/audit/AuditWebScriptTest.java +++ b/source/java/org/alfresco/repo/web/scripts/audit/AuditWebScriptTest.java @@ -18,9 +18,12 @@ */ package org.alfresco.repo.web.scripts.audit; +import java.util.Set; + import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.web.scripts.BaseWebScriptTest; import org.alfresco.service.cmr.audit.AuditService; +import org.json.JSONArray; import org.json.JSONObject; import org.springframework.context.ApplicationContext; import org.springframework.extensions.webscripts.TestWebScriptServer; @@ -57,7 +60,9 @@ public class AuditWebScriptTest extends BaseWebScriptTest public void testGetWithoutPermissions() throws Exception { - + String url = "/api/audit/control"; + TestWebScriptServer.GetRequest req = new TestWebScriptServer.GetRequest(url); + sendRequest(req, 401, AuthenticationUtil.getGuestRoleName()); } public void testGetIsAuditEnabledGlobally() throws Exception @@ -67,10 +72,27 @@ public class AuditWebScriptTest extends BaseWebScriptTest String url = "/api/audit/control"; TestWebScriptServer.GetRequest req = new TestWebScriptServer.GetRequest(url); - //First, we'll try the request as a simple, non-admin user (expect a 401) Response response = sendRequest(req, 200, admin); JSONObject json = new JSONObject(response.getContentAsString()); boolean enabled = json.getBoolean("enabled"); assertEquals("Mismatched global audit enabled", checkEnabled, enabled); } + + public void testGetIsAuditEnabledMissingApp() throws Exception + { + boolean checkEnabled = auditService.isAuditEnabled(); + Set checkApps = auditService.getAuditApplications(); + + String url = "/api/audit/control?app=xxx"; + TestWebScriptServer.GetRequest req = new TestWebScriptServer.GetRequest(url); + + //First, we'll try the request as a simple, non-admin user (expect a 401) + Response response = sendRequest(req, 200, admin); + JSONObject json = new JSONObject(response.getContentAsString()); + boolean enabled = json.getBoolean("enabled"); + assertEquals("Mismatched global audit enabled", checkEnabled, enabled); + JSONArray apps = json.getJSONArray(AbstractAuditWebScript.JSON_KEY_APPLICATIONS); + // We expect that the unknown application is returned with the others + assertEquals("Incorrect number of applications reported", checkApps.size()+1, apps.length()); + } } diff --git a/source/java/org/alfresco/repo/web/scripts/audit/ControlGet.java b/source/java/org/alfresco/repo/web/scripts/audit/ControlGet.java index 962aa8388d..e1d0d7b7e0 100644 --- a/source/java/org/alfresco/repo/web/scripts/audit/ControlGet.java +++ b/source/java/org/alfresco/repo/web/scripts/audit/ControlGet.java @@ -20,11 +20,13 @@ package org.alfresco.repo.web.scripts.audit; import java.io.IOException; import java.io.StringWriter; +import java.nio.charset.Charset; +import java.util.Collections; +import java.util.Set; import org.springframework.extensions.webscripts.Status; import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptResponse; -import org.springframework.extensions.webscripts.json.JSONUtils; import org.springframework.extensions.webscripts.json.JSONWriter; /** @@ -36,34 +38,53 @@ public class ControlGet extends AbstractAuditWebScript @Override public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException { + // return the unique transfer id (the lock id) + JSONWriter json = new JSONWriter(res.getWriter()); + String app = getApp(req, false); String path = getPath(req); + Set apps = (app == null ? auditService.getAuditApplications() : Collections.singleton(app)); - boolean enabled = false; - if (app == null) + boolean enabledGlobal = auditService.isAuditEnabled(); + json.startObject(); { - enabled = auditService.isAuditEnabled(); - } - else - { - enabled = auditService.isAuditEnabled(app, path); + json.writeValue(JSON_KEY_ENABLED, enabledGlobal); + if (apps.size() > 0) + { + json.startValue(JSON_KEY_APPLICATIONS); + { + json.startArray(); + { + for (String appName : apps) + { + boolean enabled = auditService.isAuditEnabled(appName, path); + json.startObject(); + { + json.writeValue(JSON_KEY_NAME, appName); + json.writeValue(JSON_KEY_PATH, path); + json.writeValue(JSON_KEY_ENABLED, enabled); + } + json.endObject(); + } + } + json.endArray(); + } + json.endValue(); + } } + json.endObject(); - // return the unique transfer id (the lock id) - StringWriter stringWriter = new StringWriter(300); - JSONWriter jsonWriter = new JSONWriter(stringWriter); - jsonWriter.startObject(); - jsonWriter.writeValue("app", app); - jsonWriter.writeValue("path", path); - jsonWriter.writeValue("enabled", enabled); - jsonWriter.endObject(); - String response = stringWriter.toString(); - + // Close off + res.getWriter().close(); + res.setContentType("application/json"); - res.setContentEncoding("UTF-8"); - int length = response.getBytes("UTF-8").length; - res.addHeader("Content-Length", "" + length); + 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); - res.getWriter().write(response); + } + + protected void writeResponse(JSONWriter json) + { + } } \ No newline at end of file