use of org.alfresco.rest.framework.resource.content.CacheDirective in project alfresco-remote-api by Alfresco.
the class RenditionsImpl method getContentNoValidation.
@Override
public BinaryResource getContentNoValidation(NodeRef sourceNodeRef, String renditionId, Parameters parameters) {
NodeRef renditionNodeRef = getRenditionByName(sourceNodeRef, renditionId, parameters);
// By default set attachment header (with rendition Id) unless attachment=false
boolean attach = true;
String attachment = parameters.getParameter("attachment");
if (attachment != null) {
attach = Boolean.valueOf(attachment);
}
final String attachFileName = (attach ? renditionId : null);
if (renditionNodeRef == null) {
boolean isPlaceholder = Boolean.valueOf(parameters.getParameter("placeholder"));
if (!isPlaceholder) {
throw new NotFoundException("Thumbnail was not found for [" + renditionId + ']');
}
String sourceNodeMimeType = null;
try {
sourceNodeMimeType = (sourceNodeRef != null ? getMimeType(sourceNodeRef) : null);
} catch (InvalidArgumentException e) {
// No content for node, e.g. ASSOC_AVATAR rather than ASSOC_PREFERENCE_IMAGE
}
// resource based on the content's mimeType and rendition id
String phPath = scriptThumbnailService.getMimeAwarePlaceHolderResourcePath(renditionId, sourceNodeMimeType);
if (phPath == null) {
// 404 since no thumbnail was found
throw new NotFoundException("Thumbnail was not found and no placeholder resource available for [" + renditionId + ']');
} else {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Retrieving content from resource path [" + phPath + ']');
}
// get extension of resource
String ext = "";
int extIndex = phPath.lastIndexOf('.');
if (extIndex != -1) {
ext = phPath.substring(extIndex);
}
try {
final String resourcePath = "classpath:" + phPath;
InputStream inputStream = resourceLoader.getResource(resourcePath).getInputStream();
// create temporary file
File file = TempFileProvider.createTempFile(inputStream, "RenditionsApi-", ext);
return new FileBinaryResource(file, attachFileName);
} catch (Exception ex) {
if (LOGGER.isErrorEnabled()) {
LOGGER.error("Couldn't load the placeholder." + ex.getMessage());
}
throw new ApiException("Couldn't load the placeholder.");
}
}
}
Map<QName, Serializable> nodeProps = nodeService.getProperties(renditionNodeRef);
ContentData contentData = (ContentData) nodeProps.get(ContentModel.PROP_CONTENT);
Date modified = (Date) nodeProps.get(ContentModel.PROP_MODIFIED);
org.alfresco.rest.framework.resource.content.ContentInfo contentInfo = null;
if (contentData != null) {
contentInfo = new ContentInfoImpl(contentData.getMimetype(), contentData.getEncoding(), contentData.getSize(), contentData.getLocale());
}
// add cache settings
CacheDirective cacheDirective = new CacheDirective.Builder().setNeverCache(false).setMustRevalidate(false).setLastModified(modified).setETag(modified != null ? Long.toString(modified.getTime()) : null).setMaxAge(// one year (in seconds)
Long.valueOf(31536000)).build();
return new NodeBinaryResource(renditionNodeRef, ContentModel.PROP_CONTENT, contentInfo, attachFileName, cacheDirective);
}
use of org.alfresco.rest.framework.resource.content.CacheDirective in project alfresco-remote-api by Alfresco.
the class ContentStreamer method setResponseCache.
/**
* Set the cache settings on the response
*
* @param res WebScriptResponse
* @param modified Date
* @param eTag String
*/
protected void setResponseCache(WebScriptResponse res, Date modified, String eTag, Map<String, Object> model) {
Cache cache = new Cache();
Object obj;
if (model != null && (obj = model.get(KEY_CACHE_DIRECTIVE)) instanceof CacheDirective) {
CacheDirective cacheDirective = (CacheDirective) obj;
cache.setNeverCache(cacheDirective.isNeverCache());
cache.setMustRevalidate(cacheDirective.isMustRevalidate());
cache.setMaxAge(cacheDirective.getMaxAge());
cache.setLastModified(cacheDirective.getLastModified());
cache.setETag(cacheDirective.getETag());
cache.setIsPublic(cacheDirective.isPublic());
} else if (model == null || !getBooleanValue(model.get(KEY_ALLOW_BROWSER_TO_CACHE))) {
// if 'allowBrowserToCache' is null or false
cache.setNeverCache(false);
cache.setMustRevalidate(true);
cache.setMaxAge(0L);
cache.setLastModified(modified);
cache.setETag(eTag);
} else {
cache.setNeverCache(false);
cache.setMustRevalidate(false);
// one year
cache.setMaxAge(Long.valueOf(31536000));
cache.setLastModified(modified);
cache.setETag(eTag);
}
res.setCache(cache);
}
Aggregations