Search in sources :

Example 1 with CacheDirective

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);
}
Also used : Serializable(java.io.Serializable) InputStream(java.io.InputStream) QName(org.alfresco.service.namespace.QName) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) FileBinaryResource(org.alfresco.rest.framework.resource.content.FileBinaryResource) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException) DisabledServiceException(org.alfresco.rest.framework.core.exceptions.DisabledServiceException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) Date(java.util.Date) NodeBinaryResource(org.alfresco.rest.framework.resource.content.NodeBinaryResource) NodeRef(org.alfresco.service.cmr.repository.NodeRef) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) ContentData(org.alfresco.service.cmr.repository.ContentData) CacheDirective(org.alfresco.rest.framework.resource.content.CacheDirective) ContentInfoImpl(org.alfresco.rest.framework.resource.content.ContentInfoImpl) File(java.io.File) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException)

Example 2 with 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);
}
Also used : CacheDirective(org.alfresco.rest.framework.resource.content.CacheDirective) Cache(org.springframework.extensions.webscripts.Cache)

Aggregations

CacheDirective (org.alfresco.rest.framework.resource.content.CacheDirective)2 File (java.io.File)1 InputStream (java.io.InputStream)1 Serializable (java.io.Serializable)1 Date (java.util.Date)1 ApiException (org.alfresco.rest.framework.core.exceptions.ApiException)1 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)1 DisabledServiceException (org.alfresco.rest.framework.core.exceptions.DisabledServiceException)1 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)1 NotFoundException (org.alfresco.rest.framework.core.exceptions.NotFoundException)1 ContentInfoImpl (org.alfresco.rest.framework.resource.content.ContentInfoImpl)1 FileBinaryResource (org.alfresco.rest.framework.resource.content.FileBinaryResource)1 NodeBinaryResource (org.alfresco.rest.framework.resource.content.NodeBinaryResource)1 ContentData (org.alfresco.service.cmr.repository.ContentData)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 QName (org.alfresco.service.namespace.QName)1 Cache (org.springframework.extensions.webscripts.Cache)1