mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-14 17:58:59 +00:00
Fix ALF-2728: AtomPub renditions are not rendered as part of cmis:object, although their rel links are.
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@20125 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -20,21 +20,34 @@ package org.alfresco.repo.web.scripts.content;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.alfresco.cmis.CMISFilterNotValidException;
|
||||
import org.alfresco.cmis.CMISObjectReference;
|
||||
import org.alfresco.cmis.CMISRendition;
|
||||
import org.alfresco.cmis.CMISRenditionService;
|
||||
import org.alfresco.model.ApplicationModel;
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.cmis.reference.ReferenceFactory;
|
||||
import org.alfresco.repo.content.MimetypeMap;
|
||||
import org.alfresco.repo.web.scripts.FileTypeImageUtils;
|
||||
import org.alfresco.service.cmr.dictionary.DictionaryService;
|
||||
import org.alfresco.service.cmr.repository.FileTypeImageSize;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.namespace.NamespaceService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.extensions.webscripts.WebScriptException;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
import org.springframework.extensions.webscripts.WebScriptResponse;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.web.context.ServletContextAware;
|
||||
import org.springframework.web.context.support.ServletContextResource;
|
||||
|
||||
|
||||
/**
|
||||
@@ -44,16 +57,27 @@ import org.apache.commons.logging.LogFactory;
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public class ContentGet extends StreamContent
|
||||
public class ContentGet extends StreamContent implements ServletContextAware
|
||||
{
|
||||
// Logger
|
||||
@SuppressWarnings("unused")
|
||||
private static final Log logger = LogFactory.getLog(ContentGet.class);
|
||||
|
||||
// Component dependencies
|
||||
private ServletContext servletContext;
|
||||
private ReferenceFactory referenceFactory;
|
||||
private DictionaryService dictionaryService;
|
||||
private NamespaceService namespaceService;
|
||||
|
||||
private CMISRenditionService renditionService;
|
||||
|
||||
/**
|
||||
* @param
|
||||
*/
|
||||
public void setServletContext(ServletContext servletContext)
|
||||
{
|
||||
this.servletContext = servletContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param reference factory
|
||||
*/
|
||||
@@ -62,6 +86,14 @@ public class ContentGet extends StreamContent
|
||||
this.referenceFactory = referenceFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param dictionaryService
|
||||
*/
|
||||
public void setDictionaryService(DictionaryService dictionaryService)
|
||||
{
|
||||
this.dictionaryService = dictionaryService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param namespaceService
|
||||
*/
|
||||
@@ -69,6 +101,14 @@ public class ContentGet extends StreamContent
|
||||
{
|
||||
this.namespaceService = namespaceService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param renditionService
|
||||
*/
|
||||
public void setCMISRenditionService(CMISRenditionService renditionService)
|
||||
{
|
||||
this.renditionService = renditionService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.alfresco.web.scripts.WebScript#execute(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.WebScriptResponse)
|
||||
@@ -95,26 +135,139 @@ public class ContentGet extends StreamContent
|
||||
throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find " + reference.toString());
|
||||
}
|
||||
|
||||
// determine content property
|
||||
QName propertyQName = ContentModel.PROP_CONTENT;
|
||||
String contentPart = templateVars.get("property");
|
||||
if (contentPart.length() > 0 && contentPart.charAt(0) == ';')
|
||||
{
|
||||
if (contentPart.length() < 2)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Content property malformed");
|
||||
}
|
||||
String propertyName = contentPart.substring(1);
|
||||
if (propertyName.length() > 0)
|
||||
{
|
||||
propertyQName = QName.createQName(propertyName, namespaceService);
|
||||
}
|
||||
}
|
||||
|
||||
// determine attachment
|
||||
boolean attach = Boolean.valueOf(req.getParameter("a"));
|
||||
|
||||
// Stream the content
|
||||
streamContent(req, res, nodeRef, propertyQName, attach);
|
||||
// stream content on node, or rendition of node
|
||||
String streamId = req.getParameter("streamId");
|
||||
if (streamId != null && streamId.length() > 0)
|
||||
{
|
||||
// render content rendition
|
||||
streamRendition(req, res, reference, streamId, attach);
|
||||
}
|
||||
else
|
||||
{
|
||||
// render content
|
||||
QName propertyQName = ContentModel.PROP_CONTENT;
|
||||
String contentPart = templateVars.get("property");
|
||||
if (contentPart.length() > 0 && contentPart.charAt(0) == ';')
|
||||
{
|
||||
if (contentPart.length() < 2)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "Content property malformed");
|
||||
}
|
||||
String propertyName = contentPart.substring(1);
|
||||
if (propertyName.length() > 0)
|
||||
{
|
||||
propertyQName = QName.createQName(propertyName, namespaceService);
|
||||
}
|
||||
}
|
||||
|
||||
// Stream the content
|
||||
streamContent(req, res, nodeRef, propertyQName, attach);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stream content rendition
|
||||
*
|
||||
* @param req
|
||||
* @param res
|
||||
* @param reference
|
||||
* @param streamId
|
||||
* @param attach
|
||||
* @throws IOException
|
||||
*/
|
||||
private void streamRendition(WebScriptRequest req, WebScriptResponse res, CMISObjectReference reference, String streamId, boolean attach)
|
||||
throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
// find rendition
|
||||
CMISRendition rendition = null;
|
||||
List<CMISRendition> renditions = renditionService.getRenditions(reference.getNodeRef(), "*");
|
||||
for (CMISRendition candidateRendition : renditions)
|
||||
{
|
||||
if (candidateRendition.getStreamId().equals(streamId))
|
||||
{
|
||||
rendition = candidateRendition;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (rendition == null)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find rendition " + streamId + " for " + reference.toString());
|
||||
}
|
||||
|
||||
// determine if special case for icons
|
||||
if (streamId.startsWith("alf:icon"))
|
||||
{
|
||||
streamIcon(res, reference, streamId, attach);
|
||||
}
|
||||
else
|
||||
{
|
||||
streamContent(req, res, rendition.getNodeRef(), ContentModel.PROP_CONTENT, attach);
|
||||
}
|
||||
}
|
||||
catch(CMISFilterNotValidException e)
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Invalid Rendition Filter");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stream Icon
|
||||
*
|
||||
* @param res
|
||||
* @param reference
|
||||
* @param streamId
|
||||
* @param attach
|
||||
* @throws IOException
|
||||
*/
|
||||
private void streamIcon(WebScriptResponse res, CMISObjectReference reference, String streamId, boolean attach)
|
||||
throws IOException
|
||||
{
|
||||
// convert stream id to icon size
|
||||
FileTypeImageSize imageSize = streamId.equals("alf:icon16") ? FileTypeImageSize.Small : FileTypeImageSize.Medium;
|
||||
String iconSize = streamId.equals("alf:icon16") ? "-16" : "";
|
||||
|
||||
// calculate icon file name and path
|
||||
String iconPath = null;
|
||||
if (dictionaryService.isSubClass(nodeService.getType(reference.getNodeRef()), ContentModel.TYPE_CONTENT))
|
||||
{
|
||||
String name = (String)nodeService.getProperty(reference.getNodeRef(), ContentModel.PROP_NAME);
|
||||
iconPath = FileTypeImageUtils.getFileTypeImage(servletContext, name, imageSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
String icon = (String)nodeService.getProperty(reference.getNodeRef(), ApplicationModel.PROP_ICON);
|
||||
if (icon != null)
|
||||
{
|
||||
iconPath = "/images/icons/" + icon + iconSize + ".gif";
|
||||
}
|
||||
else
|
||||
{
|
||||
iconPath = "/images/icons/space-icon-default" + iconSize + ".gif";
|
||||
}
|
||||
}
|
||||
|
||||
// set mimetype
|
||||
String mimetype = MimetypeMap.MIMETYPE_BINARY;
|
||||
int extIndex = iconPath.lastIndexOf('.');
|
||||
if (extIndex != -1)
|
||||
{
|
||||
String ext = iconPath.substring(extIndex + 1);
|
||||
mimetype = mimetypeService.getMimetype(ext);
|
||||
}
|
||||
res.setContentType(mimetype);
|
||||
|
||||
// stream icon
|
||||
ServletContextResource resource = new ServletContextResource(servletContext, iconPath);
|
||||
if (!resource.exists())
|
||||
{
|
||||
throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find rendition " + streamId + " for " + reference.toString());
|
||||
}
|
||||
FileCopyUtils.copy(resource.getInputStream(), res.getOutputStream());
|
||||
}
|
||||
|
||||
}
|
@@ -489,22 +489,7 @@ public class StreamContent extends AbstractWebScript
|
||||
protected void streamContentImpl(WebScriptRequest req, WebScriptResponse res, ContentReader reader, boolean attach,
|
||||
Date modified, String eTag, String attachFileName) throws IOException
|
||||
{
|
||||
// handle attachment
|
||||
if (attach == true)
|
||||
{
|
||||
String headerValue = "attachment";
|
||||
if (attachFileName != null && attachFileName.length() > 0)
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("Attaching content using filename: " + attachFileName);
|
||||
|
||||
headerValue += "; filename=" + attachFileName;
|
||||
}
|
||||
|
||||
// set header based on filename - will force a Save As from the browse if it doesn't recognize it
|
||||
// this is better than the default response of the browser trying to display the contents
|
||||
res.setHeader("Content-Disposition", headerValue);
|
||||
}
|
||||
setAttachment(res, attach, attachFileName);
|
||||
|
||||
// establish mimetype
|
||||
String mimetype = reader.getMimetype();
|
||||
@@ -553,4 +538,31 @@ public class StreamContent extends AbstractWebScript
|
||||
logger.info("Client aborted stream read:\n\tcontent: " + reader);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set attachment header
|
||||
*
|
||||
* @param res
|
||||
* @param attach
|
||||
* @param attachFileName
|
||||
*/
|
||||
protected void setAttachment(WebScriptResponse res, boolean attach, String attachFileName)
|
||||
{
|
||||
if (attach == true)
|
||||
{
|
||||
String headerValue = "attachment";
|
||||
if (attachFileName != null && attachFileName.length() > 0)
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("Attaching content using filename: " + attachFileName);
|
||||
|
||||
headerValue += "; filename=" + attachFileName;
|
||||
}
|
||||
|
||||
// set header based on filename - will force a Save As from the browse if it doesn't recognize it
|
||||
// this is better than the default response of the browser trying to display the contents
|
||||
res.setHeader("Content-Disposition", headerValue);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user