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

126543 jkaabimofrad: 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/BRANCHES/DEV/5.2.N/root@126888 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Ancuta Morarasu
2016-05-11 12:06:18 +00:00
parent 7925bca309
commit ae11fef3de
9 changed files with 378 additions and 189 deletions

View File

@@ -27,14 +27,21 @@ package org.alfresco.rest.framework.resource.content;
public class AbstractBinaryResource implements BinaryResource
{
final String attachFileName;
final CacheDirective cacheDirective;
public AbstractBinaryResource(String attachFileName)
public AbstractBinaryResource(String attachFileName, CacheDirective cacheDirective)
{
this.attachFileName = attachFileName;
this.cacheDirective = cacheDirective;
}
public String getAttachFileName()
{
return attachFileName;
}
public CacheDirective getCacheDirective()
{
return cacheDirective;
}
}

View File

@@ -0,0 +1,133 @@
/*
* Copyright (C) 2005-2016 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.rest.framework.resource.content;
import java.util.Date;
/**
* An immutable builder for setting the HTTP cache.
*
* @author Jamal Kaabi-Mofrad
*/
public class CacheDirective
{
private final boolean neverCache;
private final boolean isPublic;
private final boolean mustRevalidate;
private final Date lastModified;
private final String eTag;
private final Long maxAge;
private CacheDirective(Builder builder)
{
this.neverCache = builder.neverCache;
this.isPublic = builder.isPublic;
this.mustRevalidate = builder.mustRevalidate;
this.lastModified = builder.lastModified == null ? null : new Date(builder.lastModified.getTime());
this.eTag = builder.eTag;
this.maxAge = builder.maxAge;
}
public boolean isNeverCache()
{
return neverCache;
}
public boolean isPublic()
{
return isPublic;
}
public boolean isMustRevalidate()
{
return mustRevalidate;
}
public Date getLastModified()
{
if (lastModified != null)
{
return new Date(lastModified.getTime());
}
return null;
}
public String getETag()
{
return eTag;
}
public Long getMaxAge()
{
return maxAge;
}
public static class Builder
{
// The default values are the same as the org.springframework.extensions.webscripts.Cache
private boolean neverCache = true;
private boolean isPublic = false;
private boolean mustRevalidate = true;
private Date lastModified = null;
private String eTag = null;
private Long maxAge = null;
public Builder setNeverCache(boolean neverCache)
{
this.neverCache = neverCache;
return this;
}
public Builder setPublic(boolean aPublic)
{
isPublic = aPublic;
return this;
}
public Builder setMustRevalidate(boolean mustRevalidate)
{
this.mustRevalidate = mustRevalidate;
return this;
}
public Builder setLastModified(Date lastModified)
{
this.lastModified = lastModified;
return this;
}
public Builder setETag(String eTag)
{
this.eTag = eTag;
return this;
}
public Builder setMaxAge(Long maxAge)
{
this.maxAge = maxAge;
return this;
}
public CacheDirective build()
{
return new CacheDirective(this);
}
}
}

View File

@@ -44,7 +44,7 @@ public class FileBinaryResource extends AbstractBinaryResource
public FileBinaryResource(File file, String attachFileName)
{
super(attachFileName);
super(attachFileName, null);
this.file = file;
}

View File

@@ -42,7 +42,12 @@ public class NodeBinaryResource extends AbstractBinaryResource
public NodeBinaryResource(NodeRef nodeRef, QName propertyQName, ContentInfo contentInfo, String attachFileName)
{
super(attachFileName);
this(nodeRef, propertyQName, contentInfo, attachFileName, null);
}
public NodeBinaryResource(NodeRef nodeRef, QName propertyQName, ContentInfo contentInfo, String attachFileName, CacheDirective cacheDirective)
{
super(attachFileName, cacheDirective);
this.nodeRef = nodeRef;
this.propertyQName = propertyQName;
this.contentInfo = contentInfo;

View File

@@ -26,14 +26,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;
@@ -49,6 +47,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;
@@ -60,7 +59,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;
@@ -91,8 +89,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
@@ -194,27 +190,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;
}
/**