Start on ALF-4134 - Get Details webscript

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@21666 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Nick Burch
2010-08-06 17:21:19 +00:00
parent e4565431bb
commit a013f35828
7 changed files with 240 additions and 19 deletions

View File

@@ -19,6 +19,7 @@
package org.alfresco.repo.web.scripts.action;
import java.util.Map;
import java.util.NoSuchElementException;
import org.alfresco.repo.action.ActionTrackingServiceImpl;
import org.alfresco.repo.action.RuntimeActionService;
@@ -31,6 +32,7 @@ import org.springframework.extensions.webscripts.DeclarativeWebScript;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptRequest;
/**
* @author Nick Burch
* @since 3.4
@@ -112,7 +114,13 @@ public abstract class AbstractActionWebscript extends DeclarativeWebScript
private static ExecutionSummary getSummaryFromKey(String key)
{
return ActionTrackingServiceImpl.buildExecutionSummary(key);
try {
// Try to have the key turned into a summary for us
return ActionTrackingServiceImpl.buildExecutionSummary(key);
} catch(NoSuchElementException e) {
// Wrong format
return null;
}
}
}
}

View File

@@ -0,0 +1,58 @@
/*
* 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.Map;
import org.alfresco.service.cmr.action.ExecutionSummary;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
/**
* @author Nick Burch
* @since 3.4
*/
public class RunningActionGet extends AbstractActionWebscript
{
@Override
protected Map<String, Object> buildModel(
RunningActionModelBuilder modelBuilder, WebScriptRequest req,
Status status, Cache cache) {
// Which action did they ask for?
String actionTrackingId =
req.getServiceMatch().getTemplateVars().get("action_tracking_id");
ExecutionSummary action =
getSummaryFromKey(actionTrackingId);
// Get the details, if we can
Map<String,Object> model = modelBuilder.buildSimpleModel(action);
if(model == null) {
throw new WebScriptException(
Status.STATUS_NOT_FOUND,
"No Running Action found with that tracking id"
);
}
return model;
}
}

View File

@@ -19,7 +19,6 @@
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;
@@ -28,7 +27,6 @@ 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;
@@ -66,6 +64,20 @@ public class RunningActionModelBuilder
}
/**
* Build a model containing a single running action
*/
protected Map<String,Object> buildSimpleModel(ExecutionSummary summary)
{
Map<String, Object> ram = buildModel(summary);
if(ram != null) {
Map<String, Object> model = new HashMap<String,Object>();
model.put(MODEL_DATA_ITEM, ram);
return model;
}
return null;
}
/**
* Build a model containing a list of running actions for the given
* list of Running Actions
@@ -75,22 +87,8 @@ public class RunningActionModelBuilder
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());
Map<String, Object> ram = buildModel(summary);
if(ram != null) {
models.add(ram);
}
}
@@ -100,4 +98,36 @@ public class RunningActionModelBuilder
model.put(MODEL_DATA_LIST, models);
return model;
}
/**
* Build a model for a single action
*/
private Map<String,Object> buildModel(ExecutionSummary summary)
{
if(summary == null) {
return null;
}
// Get the details, if we can
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());
return ram;
}
return null;
}
}

View File

@@ -367,6 +367,113 @@ public class RunningActionRestApiTest extends BaseWebScriptTest
}
public void testRunningActionGet() throws Exception
{
Response response;
// Not allowed if you're not an admin
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getGuestUserName());
response = sendRequest(new GetRequest(URL_RUNNING_ACTION + "MadeUp"), Status.STATUS_UNAUTHORIZED);
assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
AuthenticationUtil.setFullyAuthenticatedUser(USER_NORMAL);
response = sendRequest(new GetRequest(URL_RUNNING_ACTION + "MadeUp"), Status.STATUS_UNAUTHORIZED);
assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
// If not found, you get a 404
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
response = sendRequest(new GetRequest(URL_RUNNING_ACTION + "MadeUp"), Status.STATUS_NOT_FOUND);
assertEquals(Status.STATUS_NOT_FOUND, response.getStatus());
// Create one
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());
String key1 = "replicationActionExecutor=" + id + "=" + instance;
// Fetch the details of it
response = sendRequest(new GetRequest(URL_RUNNING_ACTION + key1), Status.STATUS_OK);
assertEquals(Status.STATUS_OK, response.getStatus());
String jsonStr = response.getContentAsString();
JSONObject jsonRD = new JSONObject(jsonStr);
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 + key1, 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);
}
}
// Add another which is cancelled, check that
// we get the correct, different details for it
rd = replicationService.createReplicationDefinition("Test2", "Testing");
replicationService.saveReplicationDefinition(rd);
actionTrackingService.recordActionExecuting(rd);
actionTrackingService.requestActionCancellation(rd);
String id2 = rd.getId();
String instance2 = Integer.toString( ((ActionImpl)rd).getExecutionInstance() );
String startedAt2 = ISO8601DateFormat.format(rd.getExecutionStartDate());
String key2 = "replicationActionExecutor=" + id2 + "=" + instance2;
response = sendRequest(new GetRequest(URL_RUNNING_ACTION + key2), Status.STATUS_OK);
assertEquals(Status.STATUS_OK, response.getStatus());
jsonStr = response.getContentAsString();
jsonRD = new JSONObject(jsonStr);
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(true, jsonRD.getBoolean("cancelRequested"));
assertEquals("/" + URL_RUNNING_ACTION + key2, jsonRD.get("details"));
// Check that the original is unchanged
response = sendRequest(new GetRequest(URL_RUNNING_ACTION + key1), Status.STATUS_OK);
assertEquals(Status.STATUS_OK, response.getStatus());
rd = replicationService.loadReplicationDefinition("Test1");
jsonStr = response.getContentAsString();
jsonRD = new JSONObject(jsonStr);
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 + key1, jsonRD.get("details"));
}
@Override
protected void setUp() throws Exception
{