Merged HEAD (5.2) to 5.2.N (5.2.1)

126429 jkaabimofrad: Merged FILE-FOLDER-API (5.2.0) to HEAD (5.2)
      121864 gjames: RA-774 Allow the use of "Void" parameter type


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@126775 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Ancuta Morarasu
2016-05-11 11:18:01 +00:00
parent 74638e0086
commit 764af23ef7
4 changed files with 79 additions and 27 deletions

View File

@@ -84,25 +84,28 @@ public class ResourceInspectorUtil
{ {
//Its an Action annotated method and its a bit special //Its an Action annotated method and its a bit special
Class<?>[] paramTypes = method.getParameterTypes(); Class<?>[] paramTypes = method.getParameterTypes();
if (paramTypes!= null)
if (!String.class.equals(paramTypes[0]) || !Parameters.class.equals(paramTypes[paramTypes.length-1]))
{ {
throw new IllegalArgumentException("An action method signature must start with a String uniqueId and end with the 'Parameters' object "); switch (paramTypes.length)
} {
if (paramTypes.length == 2) case 3:
//EntityResource action by id, same logic as RelationshipEntityResource action by id
case 4:
int position = paramTypes.length-2;
if (Void.class.equals(paramTypes[position]))
{ {
//No parameter required
return null; return null;
} }
else if (paramTypes.length == 3)
{
return paramTypes[1]; // Return the middle parameter
}
else else
{ {
throw new IllegalArgumentException("An action method signature should have 3 parameters (uniqueId, typePassedin, Parameters)"); return paramTypes[position];
} }
} }
}
throw new IllegalArgumentException("An action method signature should have 3 parameters (uniqueId, typePassedin, Parameters)," +
" use Void if you are not interested in the second argument.");
}
/** /**
* Finds methods for the given annotation * Finds methods for the given annotation
@@ -143,9 +146,17 @@ public class ResourceInspectorUtil
* @return result of method call * @return result of method call
*/ */
public static Object invokeMethod(Method annotatedMethod, Object obj) public static Object invokeMethod(Method annotatedMethod, Object obj)
{
try
{ {
return invokeMethod(annotatedMethod, obj, null); return invokeMethod(annotatedMethod, obj, null);
} }
catch (Throwable error)
{
logger.error("Invocation failure", error);
return null;
}
}
/** /**
* Invokes a method and returns the result * Invokes a method and returns the result
@@ -153,7 +164,7 @@ public class ResourceInspectorUtil
* @param obj Object * @param obj Object
* @return result of method call * @return result of method call
*/ */
public static Object invokeMethod(Method annotatedMethod, Object obj, Object... args) public static Object invokeMethod(Method annotatedMethod, Object obj, Object... args) throws Throwable
{ {
if (annotatedMethod != null) if (annotatedMethod != null)
{ {
@@ -172,6 +183,7 @@ public class ResourceInspectorUtil
catch (InvocationTargetException error) catch (InvocationTargetException error)
{ {
logger.warn("InvocationTargetException", error); logger.warn("InvocationTargetException", error);
throw error.getCause();
} }
} }
return null; return null;

View File

@@ -29,7 +29,9 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback; import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.rest.framework.core.ActionResourceMetaData;
import org.alfresco.rest.framework.core.ResourceInspector; import org.alfresco.rest.framework.core.ResourceInspector;
import org.alfresco.rest.framework.core.ResourceInspectorUtil;
import org.alfresco.rest.framework.core.ResourceLocator; import org.alfresco.rest.framework.core.ResourceLocator;
import org.alfresco.rest.framework.core.ResourceMetadata; import org.alfresco.rest.framework.core.ResourceMetadata;
import org.alfresco.rest.framework.core.ResourceParameter; import org.alfresco.rest.framework.core.ResourceParameter;
@@ -67,18 +69,17 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
setParamsExtractor(this); setParamsExtractor(this);
} }
@Override @Override
public Params extractParams(ResourceMetadata resourceMeta, WebScriptRequest req) public Params extractParams(ResourceMetadata resourceMeta, WebScriptRequest req)
{ {
final RecognizedParams params = ResourceWebScriptHelper.getRecognizedParams(req); final RecognizedParams params = ResourceWebScriptHelper.getRecognizedParams(req);
final String entityId = req.getServiceMatch().getTemplateVars().get(ResourceLocator.ENTITY_ID);
switch (resourceMeta.getType()) switch (resourceMeta.getType())
{ {
case ENTITY: case ENTITY:
String entityIdCheck = req.getServiceMatch().getTemplateVars().get(ResourceLocator.ENTITY_ID); if (StringUtils.isNotBlank(entityId))
if (StringUtils.isNotBlank(entityIdCheck))
{ {
throw new UnsupportedResourceOperationException("POST is executed against the collection URL"); throw new UnsupportedResourceOperationException("POST is executed against the collection URL");
} }
@@ -88,7 +89,6 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
return Params.valueOf(null, params, postedObj); return Params.valueOf(null, params, postedObj);
} }
case RELATIONSHIP: case RELATIONSHIP:
String entityId = req.getServiceMatch().getTemplateVars().get(ResourceLocator.ENTITY_ID);
String relationshipId = req.getServiceMatch().getTemplateVars().get(ResourceLocator.RELATIONSHIP_ID); String relationshipId = req.getServiceMatch().getTemplateVars().get(ResourceLocator.RELATIONSHIP_ID);
if (StringUtils.isNotBlank(relationshipId)) if (StringUtils.isNotBlank(relationshipId))
{ {
@@ -99,6 +99,20 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
Object postedRel = processRequest(resourceMeta, req); Object postedRel = processRequest(resourceMeta, req);
return Params.valueOf(entityId, params, postedRel); return Params.valueOf(entityId, params, postedRel);
} }
case ACTION:
final String actionName = req.getServiceMatch().getTemplateVars().get(ResourceLocator.RELATIONSHIP_RESOURCE);
if (StringUtils.isNotBlank(entityId) && StringUtils.isNotBlank(actionName))
{
Class objectType = resourceMeta.getObjectType(HttpMethod.POST);
Object postedObj = null;
if (objectType!= null)
{
//Actions don't support a List as json body
postedObj = ResourceWebScriptHelper.extractJsonContent(req, jsonHelper, objectType);
}
return Params.valueOf(entityId, params, postedObj);
}
//Fall through to unsupported.
default: default:
throw new UnsupportedResourceOperationException("POST not supported for Actions"); throw new UnsupportedResourceOperationException("POST not supported for Actions");
} }
@@ -160,6 +174,30 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
return ResourceWebScriptHelper.extractJsonContentAsList(req, jsonHelper, objType); return ResourceWebScriptHelper.extractJsonContentAsList(req, jsonHelper, objType);
} }
/**
* Execute a generic action method
* @param resource
* @param params
* @return the result of the execution.
*/
private Object executeAction(ResourceWithMetadata resource, Params params) throws Throwable
{
ActionResourceMetaData actionResourceMetaData = (ActionResourceMetaData) resource.getMetaData();
switch (actionResourceMetaData.getActionMethod().getParameterTypes().length)
{
case 3:
//EntityResource action by id
return ResourceInspectorUtil.invokeMethod(actionResourceMetaData.getActionMethod(),resource.getResource(), params.getEntityId(), params.getPassedIn(), params);
case 4:
//RelationshipEntityResource action by id
return ResourceInspectorUtil.invokeMethod(actionResourceMetaData.getActionMethod(),resource.getResource(), params.getEntityId(), params.getRelationshipId(), params.getPassedIn(), params);
}
throw new UnsupportedResourceOperationException("The action method has an invalid signature");
}
/** /**
* Executes the action on the resource * Executes the action on the resource
* @param resource ResourceWithMetadata * @param resource ResourceWithMetadata
@@ -167,7 +205,7 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
* @return anObject the result of the execute * @return anObject the result of the execute
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private Object executeInternal(ResourceWithMetadata resource, Params params) private Object executeInternal(ResourceWithMetadata resource, Params params) throws Throwable
{ {
final Object resObj = resource.getResource(); final Object resObj = resource.getResource();
switch (resource.getMetaData().getType()) switch (resource.getMetaData().getType())
@@ -224,6 +262,8 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
return wrapWithCollectionWithPaging(createdRel); return wrapWithCollectionWithPaging(createdRel);
} }
} }
case ACTION:
return executeAction(resource, params);
default: default:
throw new UnsupportedResourceOperationException("POST not supported for Actions"); throw new UnsupportedResourceOperationException("POST not supported for Actions");
} }

View File

@@ -45,7 +45,7 @@ public class GrassEntityResource implements EntityResourceAction.ReadById<Grass>
} }
@Action("cut") @Action("cut")
public String cutLawn(String id, Parameters parameters) { public String cutLawn(String id, Void notused, Parameters parameters) {
return "All done"; return "All done";
} }

View File

@@ -445,7 +445,7 @@ public class InspectorTests
assertTrue("GrassEntityResource supports POST", resourceMetadata.supports(HttpMethod.POST)); assertTrue("GrassEntityResource supports POST", resourceMetadata.supports(HttpMethod.POST));
assertFalse("GrassEntityResource does not support GET", resourceMetadata.supports(HttpMethod.GET)); assertFalse("GrassEntityResource does not support GET", resourceMetadata.supports(HttpMethod.GET));
assertNull(resourceMetadata.getObjectType(HttpMethod.POST)); assertNull(resourceMetadata.getObjectType(HttpMethod.POST));
result = (String) ResourceInspectorUtil.invokeMethod(actionMethod,grassEntityResource, "xyz", Params.valueOf("notUsed", null)); result = (String) ResourceInspectorUtil.invokeMethod(actionMethod,grassEntityResource, "xyz", null, Params.valueOf("notUsed", null));
assertEquals("All done",result); assertEquals("All done",result);
break; break;
default: default: