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

123868 jkaabimofrad: RA-856: Added support for REST APIs “/content” endpoints to optionally set their own cache. (Currently, only the Renditions API is setting its own cache)
       - Removed the http attachment header setting from the AbstractResourceWebScript, as the ContentStreamer already has this feature
       - Reformatted ContentStreamer according to Alfresco code standards 


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@126543 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Jamal Kaabi-Mofrad
2016-05-10 11:24:48 +00:00
parent 27fe28b906
commit f79e7be94d
9 changed files with 372 additions and 164 deletions

View File

@@ -19,14 +19,12 @@
package org.alfresco.rest.framework.webscripts;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.repo.node.integrity.IntegrityException;
import org.alfresco.repo.tenant.TenantUtil;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.web.scripts.content.ContentStreamer;
@@ -42,6 +40,7 @@ import org.alfresco.rest.framework.resource.actions.ActionExecutor;
import org.alfresco.rest.framework.resource.actions.interfaces.BinaryResourceAction;
import org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceBinaryAction;
import org.alfresco.rest.framework.resource.content.BinaryResource;
import org.alfresco.rest.framework.resource.content.CacheDirective;
import org.alfresco.rest.framework.resource.content.ContentInfo;
import org.alfresco.rest.framework.resource.content.FileBinaryResource;
import org.alfresco.rest.framework.resource.content.NodeBinaryResource;
@@ -53,7 +52,6 @@ import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
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.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
@@ -84,8 +82,6 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements
private ContentStreamer streamer;
protected ResourceWebScriptHelper helper;
public final static String HDR_NAME_CONTENT_DISPOSITION = "Content-Disposition";
@SuppressWarnings("rawtypes")
@Override
public void execute(final Api api, final WebScriptRequest req, final WebScriptResponse res) throws IOException
@@ -187,27 +183,30 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements
{
FileBinaryResource fileResource = (FileBinaryResource) resource;
// if requested, set attachment
setAttachment(res, fileResource.getAttachFileName());
streamer.streamContent(req, res, fileResource.getFile(), null, false, null, null);
boolean attach = StringUtils.isNotEmpty(fileResource.getAttachFileName());
Map<String, Object> model = getModelForCacheDirective(fileResource.getCacheDirective());
streamer.streamContent(req, res, fileResource.getFile(), null, attach, fileResource.getAttachFileName(), model);
}
else if (resource instanceof NodeBinaryResource)
{
NodeBinaryResource nodeResource = (NodeBinaryResource) resource;
ContentInfo contentInfo = nodeResource.getContentInfo();
setContentInfoOnResponse(res,contentInfo);
setContentInfoOnResponse(res, contentInfo);
// if requested, set attachment
setAttachment(res, nodeResource.getAttachFileName());
streamer.streamContent(req, res, nodeResource.getNodeRef(), nodeResource.getPropertyQName(), false, null, null);
boolean attach = StringUtils.isNotEmpty(nodeResource.getAttachFileName());
Map<String, Object> model = getModelForCacheDirective(nodeResource.getCacheDirective());
streamer.streamContent(req, res, nodeResource.getNodeRef(), nodeResource.getPropertyQName(), attach, nodeResource.getAttachFileName(), model);
}
}
private void setAttachment(final WebScriptResponse res, final String attachFileName)
private static Map<String, Object> getModelForCacheDirective(CacheDirective cacheDirective)
{
if (StringUtils.isNotEmpty(attachFileName))
if (cacheDirective != null)
{
String headerValue = "attachment; filename=\"" + attachFileName + "\"; filename*=UTF-8''" + URLEncoder.encode(attachFileName);
res.setHeader(HDR_NAME_CONTENT_DISPOSITION, headerValue);
return Collections.singletonMap(ContentStreamer.KEY_CACHE_DIRECTIVE, (Object) cacheDirective);
}
return null;
}
/**