diff --git a/source/java/org/alfresco/rest/framework/webscripts/AbstractResourceWebScript.java b/source/java/org/alfresco/rest/framework/webscripts/AbstractResourceWebScript.java index ca46bbb947..a487032a80 100644 --- a/source/java/org/alfresco/rest/framework/webscripts/AbstractResourceWebScript.java +++ b/source/java/org/alfresco/rest/framework/webscripts/AbstractResourceWebScript.java @@ -48,8 +48,6 @@ import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.springframework.extensions.surf.util.URLEncoder; import org.springframework.extensions.webscripts.Cache; -import org.springframework.extensions.webscripts.Description; -import org.springframework.extensions.webscripts.Status; import org.springframework.extensions.webscripts.WebScriptException; import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptResponse; @@ -145,15 +143,16 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements public Object execute(final ResourceWithMetadata resource, final Params params, final WebScriptResponse res, boolean isReadOnly) { final String entityCollectionName = ResourceInspector.findEntityCollectionNameName(resource.getMetaData()); - return transactionService.getRetryingTransactionHelper().doInTransaction( + final ResourceOperation operation = resource.getMetaData().getOperation(getHttpMethod()); + final ResponseCallBack callBack = new ResponseCallBack(operation.getSuccessStatus(),DEFAULT_JSON_CONTENT,ApiWebScript.CACHE_NEVER); + Object toReturn = transactionService.getRetryingTransactionHelper().doInTransaction( new RetryingTransactionHelper.RetryingTransactionCallback() { @Override public Object execute() throws Throwable { - final ResourceOperation operation = resource.getMetaData().getOperation(getHttpMethod()); + Object result = executeAction(resource, params); - setResponse(res,operation.getSuccessStatus(),ApiWebScript.CACHE_NEVER, DEFAULT_JSON_CONTENT); if (result instanceof BinaryResource) { return result; //don't postprocess it. @@ -161,6 +160,8 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements return helper.processAdditionsToTheResponse(res, resource.getMetaData().getApi(), entityCollectionName, params, result); } }, isReadOnly, true); + setResponse(res,callBack); + return toReturn; } public abstract Object executeAction(ResourceWithMetadata resource, Params params) throws Throwable; @@ -194,14 +195,20 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements * @param status * @param cache * @param contentInfo + * @param headers */ - protected void setResponse(final WebScriptResponse res, int status, Cache cache, ContentInfo contentInfo) + protected void setResponse(final WebScriptResponse res, int status, Cache cache, ContentInfo contentInfo, Map headers) { res.setStatus(status); res.setCache(cache); setContentInfoOnResponse(res,contentInfo); } - + + protected void setResponse(final WebScriptResponse res, ResponseCallBack withResponse) + { + setResponse(res, withResponse.getStatus(), withResponse.getCache(), withResponse.getContentInfo(), withResponse.getHeaders()); + } + /** * Renders the result of an execution. * diff --git a/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptDelete.java b/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptDelete.java index 987c0ac5da..b35603edf0 100644 --- a/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptDelete.java +++ b/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptDelete.java @@ -141,19 +141,21 @@ public class ResourceWebScriptDelete extends AbstractResourceWebScript implement @Override public Void execute(final ResourceWithMetadata resource, final Params params, final WebScriptResponse res, boolean isReadOnly) { + final ResourceOperation operation = resource.getMetaData().getOperation(HttpMethod.DELETE); + final ResponseCallBack callBack = new ResponseCallBack(operation.getSuccessStatus(),DEFAULT_JSON_CONTENT,ApiWebScript.CACHE_NEVER); transactionService.getRetryingTransactionHelper().doInTransaction( new RetryingTransactionCallback() { @Override public Void execute() throws Throwable { - final ResourceOperation operation = resource.getMetaData().getOperation(HttpMethod.DELETE); executeAction(resource, params); //ignore return result - setResponse(res,operation.getSuccessStatus(),ApiWebScript.CACHE_NEVER, DEFAULT_JSON_CONTENT); return null; } }, false, true); + setResponse(res,callBack); return null; + } } diff --git a/source/java/org/alfresco/rest/framework/webscripts/ResponseCallBack.java b/source/java/org/alfresco/rest/framework/webscripts/ResponseCallBack.java new file mode 100644 index 0000000000..66107b92e2 --- /dev/null +++ b/source/java/org/alfresco/rest/framework/webscripts/ResponseCallBack.java @@ -0,0 +1,94 @@ +package org.alfresco.rest.framework.webscripts; + +import org.alfresco.rest.framework.resource.content.ContentInfo; +import org.springframework.extensions.webscripts.Cache; + +import java.util.HashMap; +import java.util.Map; + +/** + * Values to be set on the response at the appropriate time. + * + * It should be ok to set these variables multiple times but only the latest values are used. + * + * @author Gethin James + */ +public class ResponseCallBack +{ + private ContentInfo contentInfo; + private int status; + private Map headers; + private Cache cache; + + public ResponseCallBack(int status, ContentInfo contentInfo, Cache cache) + { + this.contentInfo = contentInfo; + this.status = status; + this.headers = new HashMap<>(); + this.cache = cache; + } + + /** + * Sets the information about the content: mimetype, encoding, locale, length + * + * @param contentInfo + */ + public void setContentInfo(ContentInfo contentInfo) + { + this.contentInfo = contentInfo; + } + + /** + * Sets the Response Status + * + * @param status int + */ + public void setStatus(int status) + { + this.status = status; + } + + /** + * Set a response header with the given name and value. If the header has + * already been set, the new value overwrites the previous one. + * + * @param name header name + * @param value header value + */ + public void setHeader(String name, String value) + { + headers.put(name, value); + } + + /** + * Sets the Cache control + * + * @param cache cache control + */ + public void setCache(Cache cache) + { + this.cache = cache; + } + + public ContentInfo getContentInfo() + { + return contentInfo; + } + + public int getStatus() + { + return status; + } + + public Cache getCache() + { + return cache; + } + + public Map getHeaders() + { + return headers; + } + + +}