mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-21 18:09:20 +00:00
Merged FILE-FOLDER-API (5.2.0) to HEAD (5.2)
120445 jvonka: RA-631, RA-641: REST fwk - fix GET node binary resource (set content-disposition/attachment header) - by default, set attachment header when downloading content - unless "attachment=false" *and* content type is in nonAttachContentTypes white list (eg. pdf, images) git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@126383 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -442,10 +442,24 @@
|
|||||||
</property>
|
</property>
|
||||||
</bean>
|
</bean>
|
||||||
|
|
||||||
|
<bean id="nodes.nonAttachContentTypes" class="org.springframework.beans.factory.config.SetFactoryBean">
|
||||||
|
<property name="sourceSet">
|
||||||
|
<set>
|
||||||
|
<value>application/pdf</value>
|
||||||
|
<value>image/jpeg</value>
|
||||||
|
<value>image/gif</value>
|
||||||
|
<value>image/png</value>
|
||||||
|
<value>image/tiff</value>
|
||||||
|
<value>image/bmp</value>
|
||||||
|
</set>
|
||||||
|
</property>
|
||||||
|
</bean>
|
||||||
|
|
||||||
<bean id="nodes" class="org.alfresco.rest.api.impl.NodesImpl" init-method="init">
|
<bean id="nodes" class="org.alfresco.rest.api.impl.NodesImpl" init-method="init">
|
||||||
<property name="serviceRegistry" ref="ServiceRegistry"/>
|
<property name="serviceRegistry" ref="ServiceRegistry"/>
|
||||||
<property name="repositoryHelper" ref="repositoryHelper"/>
|
<property name="repositoryHelper" ref="repositoryHelper"/>
|
||||||
<property name="ignoreTypes" ref="nodes.ignoreTypes"/>
|
<property name="ignoreTypes" ref="nodes.ignoreTypes"/>
|
||||||
|
<property name="nonAttachContentTypes" ref="nodes.nonAttachContentTypes"/>
|
||||||
</bean>
|
</bean>
|
||||||
|
|
||||||
<bean id="Nodes" class="org.springframework.aop.framework.ProxyFactoryBean">
|
<bean id="Nodes" class="org.springframework.aop.framework.ProxyFactoryBean">
|
||||||
|
@@ -103,6 +103,10 @@ import java.util.Set;
|
|||||||
/**
|
/**
|
||||||
* Centralises access to file/folder/node services and maps between representations.
|
* Centralises access to file/folder/node services and maps between representations.
|
||||||
*
|
*
|
||||||
|
* Note:
|
||||||
|
* This class was originally used for returning some basic node info when listing Favourites.
|
||||||
|
* It has now been re-purposed and extended to implement the new File Folder (RESTful) API.
|
||||||
|
*
|
||||||
* @author steveglover
|
* @author steveglover
|
||||||
* @author janv
|
* @author janv
|
||||||
* @author Jamal Kaabi-Mofrad
|
* @author Jamal Kaabi-Mofrad
|
||||||
@@ -140,6 +144,12 @@ public class NodesImpl implements Nodes
|
|||||||
private Set<String> defaultIgnoreTypes;
|
private Set<String> defaultIgnoreTypes;
|
||||||
private Set<QName> ignoreTypeQNames;
|
private Set<QName> ignoreTypeQNames;
|
||||||
|
|
||||||
|
private Set<String> nonAttachContentTypes = Collections.EMPTY_SET; // pre-configured whitelist, eg. images & pdf
|
||||||
|
|
||||||
|
public void setNonAttachContentTypes(Set<String> nonAttachWhiteList) {
|
||||||
|
this.nonAttachContentTypes = nonAttachWhiteList;
|
||||||
|
}
|
||||||
|
|
||||||
public void init()
|
public void init()
|
||||||
{
|
{
|
||||||
this.namespaceService = sr.getNamespaceService();
|
this.namespaceService = sr.getNamespaceService();
|
||||||
@@ -1154,14 +1164,36 @@ public class NodesImpl implements Nodes
|
|||||||
throw new InvalidArgumentException("NodeId of content is expected: "+nodeRef);
|
throw new InvalidArgumentException("NodeId of content is expected: "+nodeRef);
|
||||||
}
|
}
|
||||||
|
|
||||||
ContentData cd = (ContentData)nodeService.getProperty(nodeRef, ContentModel.PROP_CONTENT);
|
Map<QName, Serializable> nodeProps = nodeService.getProperties(nodeRef);
|
||||||
|
ContentData cd = (ContentData)nodeProps.get(ContentModel.PROP_CONTENT);
|
||||||
|
String name = (String)nodeProps.get(ContentModel.PROP_NAME);
|
||||||
|
|
||||||
org.alfresco.rest.framework.resource.content.ContentInfo ci = null;
|
org.alfresco.rest.framework.resource.content.ContentInfo ci = null;
|
||||||
if (cd != null) {
|
if (cd != null) {
|
||||||
ci = new org.alfresco.rest.framework.resource.content.ContentInfoImpl(cd.getMimetype(), cd.getEncoding(), cd.getSize(), cd.getLocale());
|
ci = new org.alfresco.rest.framework.resource.content.ContentInfoImpl(cd.getMimetype(), cd.getEncoding(), cd.getSize(), cd.getLocale());
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO attachment header - update (or extend ?) REST fwk
|
// By default set attachment header (with filename) unless attachment=false *and* content type is pre-configured as non-attach
|
||||||
return new NodeBinaryResource(nodeRef, ContentModel.PROP_CONTENT, ci);
|
boolean attach = true;
|
||||||
|
String attachment = parameters.getParameter("attachment");
|
||||||
|
if (attachment != null)
|
||||||
|
{
|
||||||
|
Boolean a = new Boolean(attachment);
|
||||||
|
if ((a != null) && (a == false))
|
||||||
|
{
|
||||||
|
if (nonAttachContentTypes.contains(cd.getMimetype()))
|
||||||
|
{
|
||||||
|
attach = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.warn("Ignored attachment=false for "+fileNodeId+" since "+cd.getMimetype()+" is not in the whitelist for non-attach content types");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String attachFileName = (attach ? name : null);
|
||||||
|
|
||||||
|
return new NodeBinaryResource(nodeRef, ContentModel.PROP_CONTENT, ci, attachFileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -32,13 +32,16 @@ public class NodeBinaryResource implements BinaryResource
|
|||||||
final NodeRef nodeRef;
|
final NodeRef nodeRef;
|
||||||
final QName propertyQName;
|
final QName propertyQName;
|
||||||
final ContentInfo contentInfo;
|
final ContentInfo contentInfo;
|
||||||
|
final String attachFileName;
|
||||||
|
|
||||||
public NodeBinaryResource(NodeRef nodeRef, QName propertyQName, ContentInfo contentInfo)
|
public NodeBinaryResource(NodeRef nodeRef, QName propertyQName, ContentInfo contentInfo, String attachFileName)
|
||||||
{
|
{
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.nodeRef = nodeRef;
|
this.nodeRef = nodeRef;
|
||||||
this.propertyQName = propertyQName;
|
this.propertyQName = propertyQName;
|
||||||
this.contentInfo = contentInfo;
|
this.contentInfo = contentInfo;
|
||||||
|
this.attachFileName = attachFileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public NodeRef getNodeRef()
|
public NodeRef getNodeRef()
|
||||||
@@ -54,4 +57,8 @@ public class NodeBinaryResource implements BinaryResource
|
|||||||
public ContentInfo getContentInfo() {
|
public ContentInfo getContentInfo() {
|
||||||
return this.contentInfo;
|
return this.contentInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getAttachFileName() {
|
||||||
|
return this.attachFileName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,3 +1,21 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2005-2015 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.webscripts;
|
package org.alfresco.rest.framework.webscripts;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -23,6 +41,7 @@ import org.codehaus.jackson.JsonGenerationException;
|
|||||||
import org.codehaus.jackson.JsonGenerator;
|
import org.codehaus.jackson.JsonGenerator;
|
||||||
import org.codehaus.jackson.map.JsonMappingException;
|
import org.codehaus.jackson.map.JsonMappingException;
|
||||||
import org.codehaus.jackson.map.ObjectMapper;
|
import org.codehaus.jackson.map.ObjectMapper;
|
||||||
|
import org.springframework.extensions.surf.util.URLEncoder;
|
||||||
import org.springframework.extensions.webscripts.Status;
|
import org.springframework.extensions.webscripts.Status;
|
||||||
import org.springframework.extensions.webscripts.WebScriptException;
|
import org.springframework.extensions.webscripts.WebScriptException;
|
||||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||||
@@ -39,6 +58,7 @@ import org.springframework.http.HttpMethod;
|
|||||||
* 5) Renders the response
|
* 5) Renders the response
|
||||||
*
|
*
|
||||||
* @author Gethin James
|
* @author Gethin James
|
||||||
|
* @author janv
|
||||||
*/
|
*/
|
||||||
// TODO for requests that pass in input streams e.g. binary content for workflow, this is going to need a way to re-read the input stream a la
|
// TODO for requests that pass in input streams e.g. binary content for workflow, this is going to need a way to re-read the input stream a la
|
||||||
// code in RepositoryContainer due to retrying transaction logic
|
// code in RepositoryContainer due to retrying transaction logic
|
||||||
@@ -52,6 +72,8 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements
|
|||||||
private ContentStreamer streamer;
|
private ContentStreamer streamer;
|
||||||
protected ResourceWebScriptHelper helper;
|
protected ResourceWebScriptHelper helper;
|
||||||
|
|
||||||
|
public final static String HDR_NAME_CONTENT_DISPOSITION = "Content-Disposition";
|
||||||
|
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
@Override
|
@Override
|
||||||
public void execute(final Api api, final WebScriptRequest req, final WebScriptResponse res) throws IOException
|
public void execute(final Api api, final WebScriptRequest req, final WebScriptResponse res) throws IOException
|
||||||
@@ -72,6 +94,17 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements
|
|||||||
{
|
{
|
||||||
respons.put("toSerialize", result);
|
respons.put("toSerialize", result);
|
||||||
respons.put("contentInfo", contentInfo);
|
respons.put("contentInfo", contentInfo);
|
||||||
|
|
||||||
|
if (result instanceof NodeBinaryResource)
|
||||||
|
{
|
||||||
|
String attachFileName = ((NodeBinaryResource)result).getAttachFileName();
|
||||||
|
if ((attachFileName != null) && (attachFileName.length() > 0))
|
||||||
|
{
|
||||||
|
String headerValue = "attachment; filename=\"" + attachFileName + "\"; filename*=UTF-8''" + URLEncoder.encode(attachFileName);
|
||||||
|
res.setHeader(HDR_NAME_CONTENT_DISPOSITION, headerValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (params.getStatus().getRedirect())
|
if (params.getStatus().getRedirect())
|
||||||
{
|
{
|
||||||
res.setStatus(params.getStatus().getCode());
|
res.setStatus(params.getStatus().getCode());
|
||||||
|
Reference in New Issue
Block a user