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

122418 gjames: RA-827 Supporting custom status codes


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@126473 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Jamal Kaabi-Mofrad
2016-05-10 11:05:56 +00:00
parent 84947ba6de
commit c03efd351c
19 changed files with 239 additions and 211 deletions

View File

@@ -93,7 +93,7 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements
executor.execute(resource, params, new ExecutionCallback()
{
@Override
public void onSuccess(Object result, ContentInfo contentInfo)
public void onSuccess(Object result, ContentInfo contentInfo, int statusCode)
{
respons.put("toSerialize", result);
respons.put("contentInfo", contentInfo);
@@ -108,14 +108,9 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements
}
}
if (params.getStatus().getRedirect())
{
res.setStatus(params.getStatus().getCode());
}
else
{
setSuccessResponseStatus(res);
}
// The response status must be set before the response is written by Jackson (which will by default close and commit the response).
// In a r/w txn, web script buffered responses ensure that it doesn't really matter but for r/o txns this is important.
res.setStatus(statusCode);
}
});
@@ -210,17 +205,6 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements
});
}
/**
* The response status must be set before the response is written by Jackson (which will by default close and commit the response).
* In a r/w txn, web script buffered responses ensure that it doesn't really matter but for r/o txns this is important.
* @param res WebScriptResponse
*/
protected void setSuccessResponseStatus(final WebScriptResponse res)
{
// default for GET, HEAD, OPTIONS, PUT, TRACE
res.setStatus(Status.STATUS_OK);
}
/**
* Finds the action executor to execute actions on.
* @param httpMethod - the http method

View File

@@ -3,6 +3,7 @@ package org.alfresco.rest.framework.webscripts;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.rest.framework.core.ResourceLocator;
import org.alfresco.rest.framework.core.ResourceMetadata;
import org.alfresco.rest.framework.core.ResourceOperation;
import org.alfresco.rest.framework.core.ResourceWithMetadata;
import org.alfresco.rest.framework.core.exceptions.DeletedResourceException;
import org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException;
@@ -87,7 +88,7 @@ public class ResourceWebScriptDelete extends AbstractResourceWebScript implement
* @param params parameters to use
* @return anObject the result of the execute
*/
private Object executeInternal(ResourceWithMetadata resource, Params params)
private void executeInternal(ResourceWithMetadata resource, Params params)
{
switch (resource.getMetaData().getType())
{
@@ -99,7 +100,7 @@ public class ResourceWebScriptDelete extends AbstractResourceWebScript implement
EntityResourceAction.Delete entityDeleter = (EntityResourceAction.Delete) resource.getResource();
entityDeleter.delete(params.getEntityId(), params);
//Don't pass anything to the callback - its just successful
return null;
return;
case RELATIONSHIP:
if (resource.getMetaData().isDeleted(RelationshipResourceAction.Delete.class))
{
@@ -108,7 +109,7 @@ public class ResourceWebScriptDelete extends AbstractResourceWebScript implement
RelationshipResourceAction.Delete relationDeleter = (RelationshipResourceAction.Delete) resource.getResource();
relationDeleter.delete(params.getEntityId(), params.getRelationshipId(), params);
//Don't pass anything to the callback - its just successful
return null;
return;
case PROPERTY:
if (BinaryResourceAction.Delete.class.isAssignableFrom(resource.getResource().getClass()))
{
@@ -119,7 +120,7 @@ public class ResourceWebScriptDelete extends AbstractResourceWebScript implement
BinaryResourceAction.Delete binDeleter = (BinaryResourceAction.Delete) resource.getResource();
binDeleter.deleteProperty(params.getEntityId(), params);
//Don't pass anything to the callback - its just successful
return null;
return;
}
if (RelationshipResourceBinaryAction.Delete.class.isAssignableFrom(resource.getResource().getClass()))
{
@@ -130,7 +131,7 @@ public class ResourceWebScriptDelete extends AbstractResourceWebScript implement
RelationshipResourceBinaryAction.Delete binDeleter = (RelationshipResourceBinaryAction.Delete) resource.getResource();
binDeleter.deleteProperty(params.getEntityId(), params.getRelationshipId(), params);
//Don't pass anything to the callback - its just successful
return null;
return;
}
default:
throw new UnsupportedResourceOperationException("DELETE not supported for Actions");
@@ -146,17 +147,12 @@ public class ResourceWebScriptDelete extends AbstractResourceWebScript implement
@Override
public Void execute() throws Throwable
{
final ResourceOperation operation = resource.getMetaData().getOperation(HttpMethod.DELETE);
executeInternal(resource, params); //ignore return result
executionCallback.onSuccess(null, DEFAULT_JSON_CONTENT);
executionCallback.onSuccess(null, DEFAULT_JSON_CONTENT, operation.getSuccessStatus());
return null;
}
}, false, true);
}
@Override
protected void setSuccessResponseStatus(WebScriptResponse res)
{
res.setStatus(Status.STATUS_NO_CONTENT);
}
}

View File

@@ -22,6 +22,7 @@ import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransacti
import org.alfresco.rest.framework.core.ResourceInspector;
import org.alfresco.rest.framework.core.ResourceLocator;
import org.alfresco.rest.framework.core.ResourceMetadata;
import org.alfresco.rest.framework.core.ResourceOperation;
import org.alfresco.rest.framework.core.ResourceWithMetadata;
import org.alfresco.rest.framework.core.exceptions.DeletedResourceException;
import org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException;
@@ -40,6 +41,7 @@ import org.alfresco.rest.framework.resource.parameters.Params.RecognizedParams;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.http.HttpMethod;
@@ -227,6 +229,7 @@ public class ResourceWebScriptGet extends AbstractResourceWebScript implements P
@Override
public Void execute() throws Throwable
{
final ResourceOperation operation = resource.getMetaData().getOperation(HttpMethod.GET);
Object result = executeInternal(resource, params);
if (result instanceof BinaryResource)
{
@@ -235,11 +238,11 @@ public class ResourceWebScriptGet extends AbstractResourceWebScript implements P
{
ci = ((NodeBinaryResource)result).getContentInfo();
}
executionCallback.onSuccess(result, ci);
executionCallback.onSuccess(result, ci, operation.getSuccessStatus());
}
else
{
executionCallback.onSuccess(helper.processAdditionsToTheResponse(resource.getMetaData().getApi(), entityCollectionName, params, result), DEFAULT_JSON_CONTENT);
executionCallback.onSuccess(helper.processAdditionsToTheResponse(resource.getMetaData().getApi(), entityCollectionName, params, result), DEFAULT_JSON_CONTENT, operation.getSuccessStatus());
}
return null;
}

View File

@@ -627,7 +627,7 @@ public class ResourceWebScriptHelper
executor.execute(resource, executionParams, new ExecutionCallback()
{
@Override
public void onSuccess(Object result, ContentInfo contentInfo)
public void onSuccess(Object result, ContentInfo contentInfo, int statusCode)
{
resultOfExecution[0] = result;
}

View File

@@ -28,6 +28,7 @@ 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.ResourceMetadata;
import org.alfresco.rest.framework.core.ResourceOperation;
import org.alfresco.rest.framework.core.ResourceParameter;
import org.alfresco.rest.framework.core.ResourceWithMetadata;
import org.alfresco.rest.framework.core.exceptions.DeletedResourceException;
@@ -69,6 +70,7 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
final RecognizedParams params = ResourceWebScriptHelper.getRecognizedParams(req);
final String entityId = req.getServiceMatch().getTemplateVars().get(ResourceLocator.ENTITY_ID);
final String relationshipId = req.getServiceMatch().getTemplateVars().get(ResourceLocator.RELATIONSHIP_ID);
final ResourceOperation operation = resourceMeta.getOperation(HttpMethod.POST);
switch (resourceMeta.getType())
{
@@ -80,7 +82,7 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
}
else
{
Object postedObj = processRequest(resourceMeta, req);
Object postedObj = processRequest(resourceMeta, operation, req);
return Params.valueOf(null, params, postedObj);
}
case RELATIONSHIP:
@@ -90,7 +92,7 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
}
else
{
Object postedRel = processRequest(resourceMeta, req);
Object postedRel = processRequest(resourceMeta, operation, req);
return Params.valueOf(entityId, params, postedRel);
}
case OPERATION:
@@ -99,7 +101,7 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
if (StringUtils.isNotBlank(entityId) && StringUtils.isNotBlank(operationName))
{
Class objectType = resourceMeta.getObjectType(HttpMethod.POST);
Class objectType = resourceMeta.getObjectType(operation);
Object postedObj = null;
if (objectType!= null)
{
@@ -127,14 +129,14 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
* returns the {@link FormData}, otherwise it tries to extract the required
* object from the JSON payload.
*/
private Object processRequest(ResourceMetadata resourceMeta, WebScriptRequest req)
private Object processRequest(ResourceMetadata resourceMeta, ResourceOperation operation, WebScriptRequest req)
{
if (WebScriptRequestImpl.MULTIPART_FORM_DATA.equals(req.getContentType()))
{
return (FormData) req.parseContent();
}
return extractObjFromJson(resourceMeta, req);
return extractObjFromJson(resourceMeta, operation, req);
}
/**
@@ -144,10 +146,10 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
* @param req WebScriptRequest
* @return Either an object
*/
private Object extractObjFromJson(ResourceMetadata resourceMeta, WebScriptRequest req)
private Object extractObjFromJson(ResourceMetadata resourceMeta, ResourceOperation operation, WebScriptRequest req)
{
List<ResourceParameter> params = resourceMeta.getParameters(HttpMethod.POST);
Class<?> objType = resourceMeta.getObjectType(HttpMethod.POST);
List<ResourceParameter> params = operation.getParameters();
Class<?> objType = resourceMeta.getObjectType(operation);
if (!params.isEmpty())
{
@@ -212,6 +214,7 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
private Object executeInternal(ResourceWithMetadata resource, Params params) throws Throwable
{
final Object resObj = resource.getResource();
switch (resource.getMetaData().getType())
{
case ENTITY:
@@ -296,17 +299,13 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
@Override
public Void execute() throws Throwable
{
final ResourceOperation operation = resource.getMetaData().getOperation(HttpMethod.POST);
Object result = executeInternal(resource, params);
executionCallback.onSuccess(helper.processAdditionsToTheResponse(resource.getMetaData().getApi(), entityCollectionName, params, result), DEFAULT_JSON_CONTENT);
executionCallback.onSuccess(helper.processAdditionsToTheResponse(resource.getMetaData().getApi(), entityCollectionName, params, result),
DEFAULT_JSON_CONTENT, operation.getSuccessStatus());
return null;
}
}, false, true);
}
@Override
protected void setSuccessResponseStatus(WebScriptResponse res)
{
res.setStatus(Status.STATUS_CREATED);
}
}

View File

@@ -28,6 +28,7 @@ import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransacti
import org.alfresco.rest.framework.core.ResourceInspector;
import org.alfresco.rest.framework.core.ResourceLocator;
import org.alfresco.rest.framework.core.ResourceMetadata;
import org.alfresco.rest.framework.core.ResourceOperation;
import org.alfresco.rest.framework.core.ResourceWithMetadata;
import org.alfresco.rest.framework.core.exceptions.DeletedResourceException;
import org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException;
@@ -43,6 +44,7 @@ import org.alfresco.rest.framework.resource.parameters.Params.RecognizedParams;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WrappingWebScriptRequest;
import org.springframework.extensions.webscripts.servlet.WebScriptServletRequest;
@@ -72,7 +74,8 @@ public class ResourceWebScriptPut extends AbstractResourceWebScript implements P
final String relationshipId = req.getServiceMatch().getTemplateVars().get(ResourceLocator.RELATIONSHIP_ID);
final String entityId = req.getServiceMatch().getTemplateVars().get(ResourceLocator.ENTITY_ID);
final RecognizedParams params = ResourceWebScriptHelper.getRecognizedParams(req);
final ResourceOperation operation = resourceMeta.getOperation(HttpMethod.PUT);
switch (resourceMeta.getType())
{
case ENTITY:
@@ -82,7 +85,7 @@ public class ResourceWebScriptPut extends AbstractResourceWebScript implements P
} else
{
Object putEnt = ResourceWebScriptHelper.extractJsonContent(req, jsonHelper, resourceMeta.getObjectType(HttpMethod.PUT));
Object putEnt = ResourceWebScriptHelper.extractJsonContent(req, jsonHelper, resourceMeta.getObjectType(operation));
return Params.valueOf(entityId,params,putEnt);
}
case RELATIONSHIP:
@@ -91,7 +94,7 @@ public class ResourceWebScriptPut extends AbstractResourceWebScript implements P
throw new UnsupportedResourceOperationException("PUT is executed against the instance URL");
} else
{
Object putRel = ResourceWebScriptHelper.extractJsonContent(req, jsonHelper, resourceMeta.getObjectType(HttpMethod.PUT));
Object putRel = ResourceWebScriptHelper.extractJsonContent(req, jsonHelper, resourceMeta.getObjectType(operation));
ResourceWebScriptHelper.setUniqueId(putRel,relationshipId);
return Params.valueOf(entityId, params, putRel);
}
@@ -231,8 +234,10 @@ public class ResourceWebScriptPut extends AbstractResourceWebScript implements P
@Override
public Void execute() throws Throwable
{
final ResourceOperation operation = resource.getMetaData().getOperation(HttpMethod.PUT);
Object result = executeInternal(resource, params);
executionCallback.onSuccess(helper.processAdditionsToTheResponse(resource.getMetaData().getApi(), entityCollectionName, params, result), DEFAULT_JSON_CONTENT);
executionCallback.onSuccess(helper.processAdditionsToTheResponse(resource.getMetaData().getApi(), entityCollectionName, params, result),
DEFAULT_JSON_CONTENT, operation.getSuccessStatus());
return null;
}
}, false, true);