Search in sources :

Example 1 with NodeBinaryResource

use of org.alfresco.rest.framework.resource.content.NodeBinaryResource in project alfresco-remote-api by Alfresco.

the class AbstractResourceWebScript method streamResponse.

protected void streamResponse(final WebScriptRequest req, final WebScriptResponse res, BinaryResource resource) throws IOException {
    if (resource instanceof FileBinaryResource) {
        FileBinaryResource fileResource = (FileBinaryResource) resource;
        // if requested, set attachment
        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);
        // if requested, set attachment
        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);
    }
}
Also used : NodeBinaryResource(org.alfresco.rest.framework.resource.content.NodeBinaryResource) ContentInfo(org.alfresco.rest.framework.resource.content.ContentInfo) HashMap(java.util.HashMap) Map(java.util.Map) FileBinaryResource(org.alfresco.rest.framework.resource.content.FileBinaryResource)

Example 2 with NodeBinaryResource

use of org.alfresco.rest.framework.resource.content.NodeBinaryResource in project alfresco-remote-api by Alfresco.

the class NodesImpl method getContent.

@Override
public BinaryResource getContent(NodeRef nodeRef, Parameters parameters, boolean recordActivity) {
    if (!nodeMatches(nodeRef, Collections.singleton(ContentModel.TYPE_CONTENT), null, false)) {
        throw new InvalidArgumentException("NodeId of content is expected: " + nodeRef.getId());
    }
    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;
    String mimeType = null;
    if (cd != null) {
        mimeType = cd.getMimetype();
        ci = new org.alfresco.rest.framework.resource.content.ContentInfoImpl(mimeType, cd.getEncoding(), cd.getSize(), cd.getLocale());
    }
    // By default set attachment header (with filename) unless attachment=false *and* content type is pre-configured as non-attach
    boolean attach = true;
    String attachment = parameters.getParameter("attachment");
    if (attachment != null) {
        Boolean a = Boolean.valueOf(attachment);
        if (!a) {
            if (nonAttachContentTypes.contains(mimeType)) {
                attach = false;
            } else {
                logger.warn("Ignored attachment=false for " + nodeRef.getId() + " since " + mimeType + " is not in the whitelist for non-attach content types");
            }
        }
    }
    String attachFileName = (attach ? name : null);
    if (recordActivity) {
        final ActivityInfo activityInfo = getActivityInfo(getParentNodeRef(nodeRef), nodeRef);
        postActivity(Activity_Type.DOWNLOADED, activityInfo, true);
    }
    return new NodeBinaryResource(nodeRef, ContentModel.PROP_CONTENT, ci, attachFileName);
}
Also used : ActivityInfo(org.alfresco.service.cmr.activities.ActivityInfo) Serializable(java.io.Serializable) QName(org.alfresco.service.namespace.QName) ContentInfoImpl(org.alfresco.rest.framework.resource.content.ContentInfoImpl) NodeBinaryResource(org.alfresco.rest.framework.resource.content.NodeBinaryResource) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) ContentData(org.alfresco.service.cmr.repository.ContentData) FilterPropBoolean(org.alfresco.repo.node.getchildren.FilterPropBoolean)

Example 3 with NodeBinaryResource

use of org.alfresco.rest.framework.resource.content.NodeBinaryResource 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)

Aggregations

NodeBinaryResource (org.alfresco.rest.framework.resource.content.NodeBinaryResource)3 Serializable (java.io.Serializable)2 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)2 ContentInfoImpl (org.alfresco.rest.framework.resource.content.ContentInfoImpl)2 FileBinaryResource (org.alfresco.rest.framework.resource.content.FileBinaryResource)2 ContentData (org.alfresco.service.cmr.repository.ContentData)2 QName (org.alfresco.service.namespace.QName)2 File (java.io.File)1 InputStream (java.io.InputStream)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 FilterPropBoolean (org.alfresco.repo.node.getchildren.FilterPropBoolean)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 NotFoundException (org.alfresco.rest.framework.core.exceptions.NotFoundException)1 CacheDirective (org.alfresco.rest.framework.resource.content.CacheDirective)1 ContentInfo (org.alfresco.rest.framework.resource.content.ContentInfo)1 ActivityInfo (org.alfresco.service.cmr.activities.ActivityInfo)1