First part of ALF-4133 - webscript to fetch running actions

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@21665 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Nick Burch
2010-08-06 15:34:46 +00:00
parent 681c9b6785
commit e4565431bb
7 changed files with 611 additions and 2 deletions

View File

@@ -0,0 +1,14 @@
<#-- Renders the details of a running action. -->
<#macro runningActionJSON action>
<#escape x as jsonUtils.encodeJSONString(x)>
{
"actionId": "${action.id}",
"actionType": "${action.type}",
"actionInstance": "${action.instance?string}",
"actionNodeRef": <#if action.nodeRef??>"${action.nodeRef.nodeRef}"<#else>null</#if>,
"startedAt": "${action.startedAt}",
"cancelRequested": "${action.cancelRequested?string}",
"details": "${"/api/running-action/" + action.key}",
}
</#escape>
</#macro>

View File

@@ -0,0 +1,10 @@
<webscript>
<shortname>List Running Actions</shortname>
<description>
Returns (limited) details on all currently running actions.
</description>
<url>/api/running-actions?type={type?}&amp;nodeRef={nodeRef?}</url>
<format default="json"/>
<authentication>admin</authentication>
<transaction allow="readonly">required</transaction>
</webscript>

View File

@@ -0,0 +1,10 @@
<#import "running-action.lib.ftl" as actionLib />
{
"data":
[
<#list runningActions as action>
<@actionLib.runningActionJSON action=action />
<#if action_has_next>,</#if>
</#list>
]
}

View File

@@ -882,4 +882,25 @@
parent="abstractReplicationWebScript">
</bean>
<!-- -->
<!-- Running Actions REST API -->
<!-- -->
<!-- abstract action web script -->
<bean id="abstractActionWebScript"
class="org.alfresco.repo.web.scripts.action.AbstractActionWebscript"
parent="webscript" abstract="true">
<property name="nodeService" ref="NodeService"/>
<property name="actionService" ref="ActionService"/>
<property name="runtimeActionService" ref="actionService"/>
<property name="actionTrackingService" ref="actionTrackingService" />
</bean>
<!-- Lists the running actions -->
<bean id="webscript.org.alfresco.repository.action.running-actions.get"
class="org.alfresco.repo.web.scripts.action.RunningActionsGet"
parent="abstractActionWebScript">
</bean>
</beans>

View File

@@ -18,9 +18,19 @@
*/
package org.alfresco.repo.web.scripts.action;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alfresco.service.cmr.action.ActionService;
import org.alfresco.service.cmr.action.ActionTrackingService;
import org.alfresco.service.cmr.action.ExecutionDetails;
import org.alfresco.service.cmr.action.ExecutionSummary;
import org.alfresco.service.cmr.replication.ReplicationDefinition;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.util.ISO8601DateFormat;
/**
* Builds up models for running actions
@@ -33,6 +43,15 @@ public class RunningActionModelBuilder
protected static final String MODEL_DATA_ITEM = "runningAction";
protected static final String MODEL_DATA_LIST = "runningActions";
protected static final String ACTION_ID = "id";
protected static final String ACTION_TYPE = "type";
protected static final String ACTION_INSTANCE = "instance";
protected static final String ACTION_NODE_REF = "nodeRef";
protected static final String ACTION_STARTED_AT = "startedAt";
protected static final String ACTION_RUNNING_ON = "runningOn";
protected static final String ACTION_CANCEL_REQUESTED = "cancelRequested";
protected static final String ACTION_KEY = "key";
protected NodeService nodeService;
protected ActionService actionService;
@@ -45,4 +64,40 @@ public class RunningActionModelBuilder
this.actionService = actionService;
this.actionTrackingService = actionTrackingService;
}
/**
* Build a model containing a list of running actions for the given
* list of Running Actions
*/
protected Map<String,Object> buildSimpleList(List<ExecutionSummary> runningActions)
{
List<Map<String,Object>> models = new ArrayList<Map<String,Object>>();
for(ExecutionSummary summary : runningActions) {
ExecutionDetails details = actionTrackingService.getExecutionDetails(summary);
// Only record if still running - may have finished
// between getting the list and now
if(details != null) {
Map<String, Object> ram = new HashMap<String,Object>();
ram.put(ACTION_ID, summary.getActionId());
ram.put(ACTION_TYPE, summary.getActionType());
ram.put(ACTION_INSTANCE, summary.getExecutionInstance());
ram.put(ACTION_KEY, AbstractActionWebscript.getRunningId(summary));
ram.put(ACTION_NODE_REF, details.getPersistedActionRef());
ram.put(ACTION_STARTED_AT, ISO8601DateFormat.format(details.getStartedAt()));
ram.put(ACTION_RUNNING_ON, details.getRunningOn());
ram.put(ACTION_CANCEL_REQUESTED, details.isCancelRequested());
models.add(ram);
}
}
// Finish up
Map<String, Object> model = new HashMap<String,Object>();
model.put(MODEL_DATA_LIST, models);
return model;
}
}

View File

@@ -0,0 +1,439 @@
/*
* 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.action;
import javax.transaction.UserTransaction;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.action.ActionImpl;
import org.alfresco.repo.cache.EhCacheAdapter;
import org.alfresco.repo.model.Repository;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.person.TestPersonManager;
import org.alfresco.repo.web.scripts.BaseWebScriptTest;
import org.alfresco.service.cmr.action.ActionStatus;
import org.alfresco.service.cmr.action.ActionTrackingService;
import org.alfresco.service.cmr.action.ExecutionDetails;
import org.alfresco.service.cmr.action.ExecutionSummary;
import org.alfresco.service.cmr.replication.ReplicationDefinition;
import org.alfresco.service.cmr.replication.ReplicationService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.security.MutableAuthenticationService;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.GUID;
import org.alfresco.util.ISO8601DateFormat;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.context.ApplicationContext;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest;
import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest;
import org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest;
import org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest;
import org.springframework.extensions.webscripts.TestWebScriptServer.Response;
/**
* Tests for the Running Action Webscripts
* @author Nick Burch
*/
public class RunningActionRestApiTest extends BaseWebScriptTest
{
private static final String URL_RUNNING_ACTION = "api/running-action/";
private static final String URL_RUNNING_ACTIONS = "api/running-actions";
private static final String URL_RUNNING_REPLICATION_ACTIONS = "api/running-replication-actions/";
private static final String JSON = "application/json";
private static final String USER_NORMAL = "Normal" + GUID.generate();
private NodeService nodeService;
private TestPersonManager personManager;
private ReplicationService replicationService;
private TransactionService transactionService;
private ActionTrackingService actionTrackingService;
private EhCacheAdapter<String, ExecutionDetails> executingActionsCache;
private Repository repositoryHelper;
private NodeRef dataDictionary;
public void testRunningActionsGet() throws Exception
{
Response response;
// Not allowed if you're not an admin
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getGuestUserName());
response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS), Status.STATUS_UNAUTHORIZED);
assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
AuthenticationUtil.setFullyAuthenticatedUser(USER_NORMAL);
response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS), Status.STATUS_UNAUTHORIZED);
assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
// If nothing running, you don't get anything back
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS), 200);
assertEquals(Status.STATUS_OK, response.getStatus());
String jsonStr = response.getContentAsString();
JSONObject json = new JSONObject(jsonStr);
JSONArray results = json.getJSONArray("data");
assertNotNull(results);
assertEquals(0, results.length());
// Add a running action, it should show up
ReplicationDefinition rd = replicationService.createReplicationDefinition("Test1", "Testing");
replicationService.saveReplicationDefinition(rd);
actionTrackingService.recordActionExecuting(rd);
String id = rd.getId();
String instance = Integer.toString( ((ActionImpl)rd).getExecutionInstance() );
String startedAt = ISO8601DateFormat.format(rd.getExecutionStartDate());
response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS), 200);
assertEquals(Status.STATUS_OK, response.getStatus());
jsonStr = response.getContentAsString();
json = new JSONObject(jsonStr);
results = json.getJSONArray("data");
assertNotNull(results);
assertEquals(1, results.length());
JSONObject jsonRD = (JSONObject)results.get(0);
assertNotNull(jsonRD);
assertEquals(id, jsonRD.get("actionId"));
assertEquals("replicationActionExecutor", jsonRD.get("actionType"));
assertEquals(instance, jsonRD.get("actionInstance"));
assertEquals(rd.getNodeRef().toString(), jsonRD.get("actionNodeRef"));
assertEquals(startedAt, jsonRD.get("startedAt"));
assertEquals(false, jsonRD.getBoolean("cancelRequested"));
assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" +
id + "=" + instance, jsonRD.get("details"));
// Ensure we didn't get any unexpected data back,
// only the keys we should have done
JSONArray keys = jsonRD.names();
for(int i=0; i<keys.length(); i++) {
String key = keys.getString(0);
if(key.equals("actionId") || key.equals("actionType") ||
key.equals("actionInstance") || key.equals("actionNodeRef") ||
key.equals("startedAt") || key.equals("cancelRequested") ||
key.equals("details")) {
// All good
} else {
fail("Unexpected key '"+key+"' found in json, raw json is\n" + jsonStr);
}
}
// Change the status to pending cancel, and re-check
actionTrackingService.requestActionCancellation(rd);
response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS), 200);
assertEquals(Status.STATUS_OK, response.getStatus());
jsonStr = response.getContentAsString();
json = new JSONObject(jsonStr);
results = json.getJSONArray("data");
assertNotNull(results);
assertEquals(1, results.length());
jsonRD = (JSONObject)results.get(0);
assertNotNull(jsonRD);
assertEquals(id, jsonRD.get("actionId"));
assertEquals("replicationActionExecutor", jsonRD.get("actionType"));
assertEquals(instance, jsonRD.get("actionInstance"));
assertEquals(rd.getNodeRef().toString(), jsonRD.get("actionNodeRef"));
assertEquals(startedAt, jsonRD.get("startedAt"));
assertEquals(true, jsonRD.getBoolean("cancelRequested"));
assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" +
id + "=" + instance, jsonRD.get("details"));
// Add a 2nd and 3rd
rd = replicationService.createReplicationDefinition("Test2", "2nd Testing");
replicationService.saveReplicationDefinition(rd);
actionTrackingService.recordActionExecuting(rd);
String id2 = rd.getId();
String instance2 = Integer.toString( ((ActionImpl)rd).getExecutionInstance() );
String startedAt2 = ISO8601DateFormat.format(rd.getExecutionStartDate());
rd = replicationService.createReplicationDefinition("AnotherTest", "3rd Testing");
replicationService.saveReplicationDefinition(rd);
actionTrackingService.recordActionExecuting(rd);
String id3 = rd.getId();
// Check we got all 3
boolean has1 = false;
boolean has2 = false;
boolean has3 = false;
response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS), 200);
assertEquals(Status.STATUS_OK, response.getStatus());
jsonStr = response.getContentAsString();
json = new JSONObject(jsonStr);
results = json.getJSONArray("data");
assertNotNull(results);
assertEquals(3, results.length());
for(int i=0; i<3; i++) {
jsonRD = (JSONObject)results.get(i);
if(jsonRD.get("actionId").equals(id)) {
has1 = true;
}
if(jsonRD.get("actionId").equals(id2)) {
has2 = true;
}
if(jsonRD.get("actionId").equals(id3)) {
has3 = true;
}
}
assertTrue(has1);
assertTrue(has2);
assertTrue(has3);
// Remove one, check it goes
actionTrackingService.recordActionComplete(rd);
has1 = false;
has2 = false;
has3 = false;
response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS), 200);
assertEquals(Status.STATUS_OK, response.getStatus());
jsonStr = response.getContentAsString();
json = new JSONObject(jsonStr);
results = json.getJSONArray("data");
assertNotNull(results);
assertEquals(2, results.length());
for(int i=0; i<2; i++) {
jsonRD = (JSONObject)results.get(i);
if(jsonRD.get("actionId").equals(id)) {
has1 = true;
}
if(jsonRD.get("actionId").equals(id2)) {
has2 = true;
}
if(jsonRD.get("actionId").equals(id3)) {
has3 = true;
}
}
assertTrue(has1);
assertTrue(has2);
assertFalse(has3);
// Check we correctly filter by node ID
rd = replicationService.loadReplicationDefinition("Test1");
response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS + "?nodeRef=" +
rd.getNodeRef().toString()), 200);
assertEquals(Status.STATUS_OK, response.getStatus());
jsonStr = response.getContentAsString();
json = new JSONObject(jsonStr);
results = json.getJSONArray("data");
assertNotNull(results);
assertEquals(1, results.length());
jsonRD = (JSONObject)results.get(0);
assertNotNull(jsonRD);
assertEquals(id, jsonRD.get("actionId"));
assertEquals("replicationActionExecutor", jsonRD.get("actionType"));
assertEquals(instance, jsonRD.get("actionInstance"));
assertEquals(rd.getNodeRef().toString(), jsonRD.get("actionNodeRef"));
assertEquals(startedAt, jsonRD.get("startedAt"));
assertEquals(true, jsonRD.getBoolean("cancelRequested"));
assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" +
id + "=" + instance, jsonRD.get("details"));
// Check the other one
rd = replicationService.loadReplicationDefinition("Test2");
response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS + "?nodeRef=" +
rd.getNodeRef().toString()), 200);
assertEquals(Status.STATUS_OK, response.getStatus());
jsonStr = response.getContentAsString();
json = new JSONObject(jsonStr);
results = json.getJSONArray("data");
assertNotNull(results);
assertEquals(1, results.length());
jsonRD = (JSONObject)results.get(0);
assertNotNull(jsonRD);
assertEquals(id2, jsonRD.get("actionId"));
assertEquals("replicationActionExecutor", jsonRD.get("actionType"));
assertEquals(instance2, jsonRD.get("actionInstance"));
assertEquals(rd.getNodeRef().toString(), jsonRD.get("actionNodeRef"));
assertEquals(startedAt2, jsonRD.get("startedAt"));
assertEquals(false, jsonRD.getBoolean("cancelRequested"));
assertEquals("/" + URL_RUNNING_ACTION + "replicationActionExecutor=" +
id2 + "=" + instance2, jsonRD.get("details"));
// Check we correctly filter by type
ActionImpl alt1 = new ActionImpl(null, "12345", "MadeUp1");
ActionImpl alt2 = new ActionImpl(null, "54321", "MadeUp2");
actionTrackingService.recordActionExecuting(alt1);
actionTrackingService.recordActionExecuting(alt2);
String startAtAlt2 = ISO8601DateFormat.format( alt2.getExecutionStartDate() );
String instanceAlt2 = Integer.toString( alt2.getExecutionInstance() );
// Goes up to 4 not by type
response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS), 200);
assertEquals(Status.STATUS_OK, response.getStatus());
jsonStr = response.getContentAsString();
json = new JSONObject(jsonStr);
results = json.getJSONArray("data");
assertNotNull(results);
assertEquals(4, results.length());
// 2 replication actions
response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS +
"?type=replicationActionExecutor"), 200);
assertEquals(Status.STATUS_OK, response.getStatus());
jsonStr = response.getContentAsString();
json = new JSONObject(jsonStr);
results = json.getJSONArray("data");
assertNotNull(results);
assertEquals(2, results.length());
// 0 if doesn't exist
response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS +
"?type=MadeUp4"), 200);
assertEquals(Status.STATUS_OK, response.getStatus());
jsonStr = response.getContentAsString();
json = new JSONObject(jsonStr);
results = json.getJSONArray("data");
assertNotNull(results);
assertEquals(0, results.length());
// 1 each of the made up ones
response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS +
"?type=MadeUp1"), 200);
assertEquals(Status.STATUS_OK, response.getStatus());
jsonStr = response.getContentAsString();
json = new JSONObject(jsonStr);
results = json.getJSONArray("data");
assertNotNull(results);
assertEquals(1, results.length());
response = sendRequest(new GetRequest(URL_RUNNING_ACTIONS +
"?type=MadeUp2"), 200);
assertEquals(Status.STATUS_OK, response.getStatus());
jsonStr = response.getContentAsString();
json = new JSONObject(jsonStr);
results = json.getJSONArray("data");
assertNotNull(results);
assertEquals(1, results.length());
// Check the details of one of these
jsonRD = (JSONObject)results.get(0);
assertNotNull(jsonRD);
assertEquals("54321", jsonRD.get("actionId"));
assertEquals("MadeUp2", jsonRD.get("actionType"));
assertEquals(instanceAlt2, jsonRD.get("actionInstance"));
assertEquals(JSONObject.NULL, jsonRD.get("actionNodeRef"));
assertEquals(startAtAlt2, jsonRD.get("startedAt"));
assertEquals(false, jsonRD.getBoolean("cancelRequested"));
assertEquals("/" + URL_RUNNING_ACTION + "MadeUp2=54321=" +
instanceAlt2, jsonRD.get("details"));
}
@Override
protected void setUp() throws Exception
{
super.setUp();
ApplicationContext appContext = getServer().getApplicationContext();
nodeService = (NodeService)appContext.getBean("nodeService");
replicationService = (ReplicationService)appContext.getBean("ReplicationService");
actionTrackingService = (ActionTrackingService)appContext.getBean("actionTrackingService");
repositoryHelper = (Repository)appContext.getBean("repositoryHelper");
transactionService = (TransactionService)appContext.getBean("transactionService");
executingActionsCache = (EhCacheAdapter<String, ExecutionDetails>)appContext.getBean("executingActionsSharedCache");
MutableAuthenticationService authenticationService = (MutableAuthenticationService)appContext.getBean("AuthenticationService");
PersonService personService = (PersonService)appContext.getBean("PersonService");
personManager = new TestPersonManager(authenticationService, personService, nodeService);
UserTransaction txn = transactionService.getUserTransaction();
txn.begin();
personManager.createPerson(USER_NORMAL);
// Ensure we start with no replication definitions
// (eg another test left them behind)
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
for(ReplicationDefinition rd : replicationService.loadReplicationDefinitions()) {
replicationService.deleteReplicationDefinition(rd);
}
txn.commit();
// Grab a reference to the data dictionary
dataDictionary = nodeService.getChildByName(
repositoryHelper.getCompanyHome(),
ContentModel.ASSOC_CONTAINS,
"Data Dictionary"
);
AuthenticationUtil.clearCurrentSecurityContext();
}
/* (non-Javadoc)
* @see junit.framework.TestCase#tearDown()
*/
@Override
protected void tearDown() throws Exception
{
super.tearDown();
UserTransaction txn = transactionService.getUserTransaction();
txn.begin();
personManager.clearPeople();
// Zap any replication definitions we created
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
for(ReplicationDefinition rd : replicationService.loadReplicationDefinitions()) {
replicationService.deleteReplicationDefinition(rd);
}
AuthenticationUtil.clearCurrentSecurityContext();
// Clear out the running actions
for(ExecutionSummary es : actionTrackingService.getAllExecutingActions()) {
executingActionsCache.remove(
AbstractActionWebscript.getRunningId(es)
);
}
txn.commit();
}
}

View File

@@ -0,0 +1,60 @@
/*
* 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.action;
import java.util.List;
import java.util.Map;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ExecutionSummary;
import org.alfresco.service.cmr.repository.NodeRef;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptRequest;
/**
* @author Nick Burch
* @since 3.4
*/
public class RunningActionsGet extends AbstractActionWebscript
{
@Override
protected Map<String, Object> buildModel(
RunningActionModelBuilder modelBuilder, WebScriptRequest req,
Status status, Cache cache) {
List<ExecutionSummary> actions = null;
// Do they want all actions, or only certain ones?
String type = req.getParameter("type");
String nodeRef = req.getParameter("nodeRef");
if(type != null) {
actions = actionTrackingService.getExecutingActions(type);
} else if(nodeRef != null) {
NodeRef actionNodeRef = new NodeRef(nodeRef);
Action action = runtimeActionService.createAction(actionNodeRef);
actions = actionTrackingService.getExecutingActions(action);
} else {
actions = actionTrackingService.getAllExecutingActions();
}
// Build the model list
return modelBuilder.buildSimpleList(actions);
}
}