From c0ca44c8223640a4e6763c5f7e5711375e7cd910 Mon Sep 17 00:00:00 2001 From: Ancuta Morarasu Date: Wed, 11 May 2016 11:42:35 +0000 Subject: [PATCH] Merged HEAD (5.2) to 5.2.N (5.2.1) 126487 jkaabimofrad: Merged FILE-FOLDER-API (5.2.0) to HEAD (5.2) 122629 gjames: RA-211: Adding a response callback for setting the response outside a transaction git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@126831 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../webscripts/AbstractResourceWebScript.java | 21 +++-- .../webscripts/ResourceWebScriptDelete.java | 6 +- .../webscripts/ResponseCallBack.java | 94 +++++++++++++++++++ 3 files changed, 112 insertions(+), 9 deletions(-) create mode 100644 source/java/org/alfresco/rest/framework/webscripts/ResponseCallBack.java diff --git a/source/java/org/alfresco/rest/framework/webscripts/AbstractResourceWebScript.java b/source/java/org/alfresco/rest/framework/webscripts/AbstractResourceWebScript.java index e25a5aa676..3f47430ea3 100644 --- a/source/java/org/alfresco/rest/framework/webscripts/AbstractResourceWebScript.java +++ b/source/java/org/alfresco/rest/framework/webscripts/AbstractResourceWebScript.java @@ -55,8 +55,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; @@ -152,15 +150,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. @@ -168,6 +167,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; @@ -201,14 +202,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 ba12488e8d..06dff6bb03 100644 --- a/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptDelete.java +++ b/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptDelete.java @@ -166,19 +166,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; + } + + +}