Merged FILE-FOLDER-API (5.2.0) to HEAD (5.2)

122596 gjames: RA-211: Post-processing (carefully) embedded/related resources


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@126485 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Jamal Kaabi-Mofrad
2016-05-10 11:09:54 +00:00
parent 06fa651c38
commit 63b7a24c69
3 changed files with 20 additions and 35 deletions

View File

@@ -19,9 +19,8 @@ public interface ActionExecutor extends HttpMethodSupport
* Invokes the resource with the Params * Invokes the resource with the Params
* @param resource ResourceWithMetadata * @param resource ResourceWithMetadata
* @param params Params * @param params Params
* @param boolean should we use a readonly transaction.
*/ */
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public Object execute(ResourceWithMetadata resource, Params params, WebScriptResponse res, boolean isReadOnly); public Object executeAction(ResourceWithMetadata resource, Params params) throws Throwable;
} }

View File

@@ -91,11 +91,10 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements
final Map<String, String> templateVars = req.getServiceMatch().getTemplateVars(); final Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
final ResourceWithMetadata resource = locator.locateResource(api,templateVars, httpMethod); final ResourceWithMetadata resource = locator.locateResource(api,templateVars, httpMethod);
final Params params = paramsExtractor.extractParams(resource.getMetaData(),req); final Params params = paramsExtractor.extractParams(resource.getMetaData(),req);
final ActionExecutor executor = findExecutor(httpMethod, params, resource, req.getContentType());
final boolean isReadOnly = HttpMethod.GET==httpMethod; final boolean isReadOnly = HttpMethod.GET==httpMethod;
//This execution usually takes place in a Retrying Transaction (see subclasses) //This execution usually takes place in a Retrying Transaction (see subclasses)
final Object toSerialize = executor.execute(resource, params, res, isReadOnly); final Object toSerialize = execute(resource, params, res, isReadOnly);
//Outside the transaction. //Outside the transaction.
if (toSerialize != null) if (toSerialize != null)
@@ -143,7 +142,6 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements
} }
} }
@Override
public Object execute(final ResourceWithMetadata resource, final Params params, final WebScriptResponse res, boolean isReadOnly) public Object execute(final ResourceWithMetadata resource, final Params params, final WebScriptResponse res, boolean isReadOnly)
{ {
final String entityCollectionName = ResourceInspector.findEntityCollectionNameName(resource.getMetaData()); final String entityCollectionName = ResourceInspector.findEntityCollectionNameName(resource.getMetaData());
@@ -165,7 +163,7 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements
}, isReadOnly, true); }, isReadOnly, true);
} }
protected abstract Object executeAction(ResourceWithMetadata resource, Params params) throws Throwable; public abstract Object executeAction(ResourceWithMetadata resource, Params params) throws Throwable;
protected void streamResponse(final WebScriptRequest req, final WebScriptResponse res, BinaryResource resource) throws IOException protected void streamResponse(final WebScriptRequest req, final WebScriptResponse res, BinaryResource resource) throws IOException
{ {
@@ -225,20 +223,6 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements
}); });
} }
/**
* Finds the action executor to execute actions on.
* @param httpMethod - the http method
* @param params Params
* @param resource ResourceWithMetadata
* @param contentType Request content type
* @return ActionExecutor the action executor
*/
public ActionExecutor findExecutor(HttpMethod httpMethod, Params params, ResourceWithMetadata resource, String contentType)
{
//Ignore all params and return this
return this;
}
public void setLocator(ResourceLocator locator) public void setLocator(ResourceLocator locator)
{ {
this.locator = locator; this.locator = locator;

View File

@@ -47,9 +47,7 @@ import org.alfresco.rest.framework.core.exceptions.PermissionDeniedException;
import org.alfresco.rest.framework.jacksonextensions.BeanPropertiesFilter; import org.alfresco.rest.framework.jacksonextensions.BeanPropertiesFilter;
import org.alfresco.rest.framework.jacksonextensions.ExecutionResult; import org.alfresco.rest.framework.jacksonextensions.ExecutionResult;
import org.alfresco.rest.framework.jacksonextensions.JacksonHelper; import org.alfresco.rest.framework.jacksonextensions.JacksonHelper;
import org.alfresco.rest.framework.resource.SerializablePagedCollection;
import org.alfresco.rest.framework.resource.actions.ActionExecutor; import org.alfresco.rest.framework.resource.actions.ActionExecutor;
import org.alfresco.rest.framework.resource.content.ContentInfo;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo; import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.InvalidSelectException; import org.alfresco.rest.framework.resource.parameters.InvalidSelectException;
import org.alfresco.rest.framework.resource.parameters.Paging; import org.alfresco.rest.framework.resource.parameters.Paging;
@@ -478,7 +476,7 @@ public class ResourceWebScriptHelper
if (objectToWrap instanceof CollectionWithPagingInfo<?>) if (objectToWrap instanceof CollectionWithPagingInfo<?>)
{ {
CollectionWithPagingInfo<?> collectionToWrap = (CollectionWithPagingInfo<?>) objectToWrap; CollectionWithPagingInfo<?> collectionToWrap = (CollectionWithPagingInfo<?>) objectToWrap;
Object sourceEntity = executeIncludedSource(res, api, params, entityCollectionName, collectionToWrap); Object sourceEntity = executeIncludedSource(api, params, entityCollectionName, collectionToWrap);
Collection<Object> resultCollection = new ArrayList(collectionToWrap.getCollection().size()); Collection<Object> resultCollection = new ArrayList(collectionToWrap.getCollection().size());
if (!collectionToWrap.getCollection().isEmpty()) if (!collectionToWrap.getCollection().isEmpty())
{ {
@@ -502,7 +500,7 @@ public class ResourceWebScriptHelper
Map<String,Pair<String,Method>> embeddded = ResourceInspector.findEmbeddedResources(objectToWrap.getClass()); Map<String,Pair<String,Method>> embeddded = ResourceInspector.findEmbeddedResources(objectToWrap.getClass());
if (embeddded != null && !embeddded.isEmpty()) if (embeddded != null && !embeddded.isEmpty())
{ {
Map<String, Object> results = executeEmbeddedResources(res, api, params,objectToWrap, embeddded); Map<String, Object> results = executeEmbeddedResources(api, params,objectToWrap, embeddded);
execRes.addEmbedded(results); execRes.addEmbedded(results);
} }
@@ -510,7 +508,7 @@ public class ResourceWebScriptHelper
{ {
Map<String, ResourceWithMetadata> relationshipResources = locator.locateRelationResource(api,entityCollectionName, params.getRelationsFilter().keySet(), HttpMethod.GET); Map<String, ResourceWithMetadata> relationshipResources = locator.locateRelationResource(api,entityCollectionName, params.getRelationsFilter().keySet(), HttpMethod.GET);
String uniqueEntityId = ResourceInspector.findUniqueId(objectToWrap); String uniqueEntityId = ResourceInspector.findUniqueId(objectToWrap);
Map<String,Object> relatedResources = executeRelatedResources(res, api, params, relationshipResources, uniqueEntityId); Map<String,Object> relatedResources = executeRelatedResources(api, params, relationshipResources, uniqueEntityId);
execRes.addRelated(relatedResources); execRes.addRelated(relatedResources);
} }
@@ -519,7 +517,7 @@ public class ResourceWebScriptHelper
} }
} }
private Object executeIncludedSource(WebScriptResponse response, Api api, Params params, String entityCollectionName, CollectionWithPagingInfo<?> collectionToWrap) private Object executeIncludedSource(Api api, Params params, String entityCollectionName, CollectionWithPagingInfo<?> collectionToWrap)
{ {
if (params.includeSource()) if (params.includeSource())
{ {
@@ -532,7 +530,7 @@ public class ResourceWebScriptHelper
ResourceWithMetadata res = locator.locateEntityResource(api, entityCollectionName, HttpMethod.GET); ResourceWithMetadata res = locator.locateEntityResource(api, entityCollectionName, HttpMethod.GET);
if (res != null) if (res != null)
{ {
Object result = executeRelatedResource(response, api, params, params.getEntityId(), null, res); Object result = executeResource(api, params, params.getEntityId(), null, res);
if (result!=null && result instanceof ExecutionResult) return ((ExecutionResult) result).getRoot(); if (result!=null && result instanceof ExecutionResult) return ((ExecutionResult) result).getRoot();
} }
} }
@@ -549,7 +547,7 @@ public class ResourceWebScriptHelper
* @param embeddded Map<String, Pair<String, Method>> * @param embeddded Map<String, Pair<String, Method>>
* @return Map * @return Map
*/ */
private Map<String, Object> executeEmbeddedResources(WebScriptResponse response, Api api, Params params, Object objectToWrap, Map<String, Pair<String, Method>> embeddded) private Map<String, Object> executeEmbeddedResources(Api api, Params params, Object objectToWrap, Map<String, Pair<String, Method>> embeddded)
{ {
final Map<String,Object> results = new HashMap<String,Object>(embeddded.size()); final Map<String,Object> results = new HashMap<String,Object>(embeddded.size());
for (Entry<String, Pair<String,Method>> embeddedEntry : embeddded.entrySet()) for (Entry<String, Pair<String,Method>> embeddedEntry : embeddded.entrySet())
@@ -560,7 +558,7 @@ public class ResourceWebScriptHelper
Object id = ResourceInspectorUtil.invokeMethod(embeddedEntry.getValue().getSecond(), objectToWrap); Object id = ResourceInspectorUtil.invokeMethod(embeddedEntry.getValue().getSecond(), objectToWrap);
if (id != null) if (id != null)
{ {
Object execEmbeddedResult = executeRelatedResource(response, api, params, String.valueOf(id), embeddedEntry.getKey(), res); Object execEmbeddedResult = executeResource(api, params, String.valueOf(id), embeddedEntry.getKey(), res);
if (execEmbeddedResult != null) if (execEmbeddedResult != null)
{ {
if (execEmbeddedResult instanceof ExecutionResult) if (execEmbeddedResult instanceof ExecutionResult)
@@ -590,14 +588,14 @@ public class ResourceWebScriptHelper
* @param uniqueEntityId String * @param uniqueEntityId String
* @return Map * @return Map
*/ */
private Map<String,Object> executeRelatedResources(final WebScriptResponse res, final Api api, Params params, private Map<String,Object> executeRelatedResources(final Api api, Params params,
Map<String, ResourceWithMetadata> relatedResources, Map<String, ResourceWithMetadata> relatedResources,
String uniqueEntityId) String uniqueEntityId)
{ {
final Map<String,Object> results = new HashMap<String,Object>(relatedResources.size()); final Map<String,Object> results = new HashMap<String,Object>(relatedResources.size());
for (final Entry<String, ResourceWithMetadata> relation : relatedResources.entrySet()) for (final Entry<String, ResourceWithMetadata> relation : relatedResources.entrySet())
{ {
Object execResult = executeRelatedResource(res, api, params, uniqueEntityId, relation.getKey(), relation.getValue()); Object execResult = executeResource(api, params, uniqueEntityId, relation.getKey(), relation.getValue());
if (execResult != null) if (execResult != null)
{ {
results.put(relation.getKey(), execResult); results.put(relation.getKey(), execResult);
@@ -617,7 +615,7 @@ public class ResourceWebScriptHelper
* @param resource ResourceWithMetadata * @param resource ResourceWithMetadata
* @return Object * @return Object
*/ */
private Object executeRelatedResource(final WebScriptResponse res, final Api api, Params params, private Object executeResource(final Api api, Params params,
final String uniqueEntityId, final String resourceKey, final ResourceWithMetadata resource) final String uniqueEntityId, final String resourceKey, final ResourceWithMetadata resource)
{ {
try try
@@ -631,7 +629,8 @@ public class ResourceWebScriptHelper
} }
final Params executionParams = Params.valueOf(paramFilter, uniqueEntityId, params.getRequest()); final Params executionParams = Params.valueOf(paramFilter, uniqueEntityId, params.getRequest());
//Read only because this only occurs for GET requests //Read only because this only occurs for GET requests
return executor.execute(resource, executionParams,res, true); Object result = executor.executeAction(resource, executionParams);
return processAdditionsToTheResponse(null, api, null, executionParams, result);
} }
catch(NotFoundException e) catch(NotFoundException e)
{ {
@@ -648,6 +647,9 @@ public class ResourceWebScriptHelper
{ {
logger.debug("Ignored error, cannot access the object so can't embed it ", e); logger.debug("Ignored error, cannot access the object so can't embed it ", e);
} }
} catch (Throwable throwable)
{
logger.warn("Failed to execute a RelatedResource for "+resourceKey+" "+throwable.getMessage());
} }
return null; //default return null; //default