Search in sources :

Example 1 with FileBinaryResource

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

the class RenditionsImpl method getContentImpl.

private BinaryResource getContentImpl(NodeRef nodeRef, String renditionId, Parameters parameters) {
    NodeRef renditionNodeRef = getRenditionByName(nodeRef, renditionId, parameters);
    // By default set attachment header (with rendition Id) unless attachment=false
    boolean attach = true;
    String attachment = parameters.getParameter(PARAM_ATTACHMENT);
    if (attachment != null) {
        attach = Boolean.valueOf(attachment);
    }
    final String attachFileName = (attach ? renditionId : null);
    if (renditionNodeRef == null) {
        boolean isPlaceholder = Boolean.valueOf(parameters.getParameter(PARAM_PLACEHOLDER));
        if (!isPlaceholder) {
            throw new NotFoundException("Thumbnail was not found for [" + renditionId + ']');
        }
        String sourceNodeMimeType = null;
        try {
            sourceNodeMimeType = (nodeRef != null ? getMimeType(nodeRef) : 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) VersionDoesNotExistException(org.alfresco.service.cmr.version.VersionDoesNotExistException) StaleEntityException(org.alfresco.rest.framework.core.exceptions.StaleEntityException) 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 FileBinaryResource

use of org.alfresco.rest.framework.resource.content.FileBinaryResource 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) Map(java.util.Map) FileBinaryResource(org.alfresco.rest.framework.resource.content.FileBinaryResource)

Example 3 with FileBinaryResource

use of org.alfresco.rest.framework.resource.content.FileBinaryResource 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 4 with FileBinaryResource

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

the class ProcessDefinitionsImpl method getProcessDefinitionImage.

@Override
public BinaryResource getProcessDefinitionImage(String definitionId) {
    ProcessDefinitionQuery query = activitiProcessEngine.getRepositoryService().createProcessDefinitionQuery().processDefinitionId(definitionId);
    if (tenantService.isEnabled() && deployWorkflowsInTenant) {
        query.processDefinitionKeyLike("@" + TenantUtil.getCurrentDomain() + "@%");
    }
    org.activiti.engine.repository.ProcessDefinition processDefinition = query.singleResult();
    if (processDefinition == null) {
        throw new EntityNotFoundException(definitionId);
    }
    try {
        InputStream processDiagram = activitiProcessEngine.getRepositoryService().getProcessDiagram(definitionId);
        if (processDiagram != null) {
            File file = TempFileProvider.createTempFile(definitionId + UUID.randomUUID(), ".png");
            FileOutputStream fos = new FileOutputStream(file);
            IOUtils.copy(processDiagram, fos);
            fos.close();
            return new FileBinaryResource(file);
        } else {
            throw new ApiException("No image available for definitionId " + definitionId);
        }
    } catch (IOException error) {
        throw new ApiException("Error while getting process definition image.");
    }
}
Also used : InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ProcessDefinitionQuery(org.activiti.engine.repository.ProcessDefinitionQuery) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) IOException(java.io.IOException) File(java.io.File) FileBinaryResource(org.alfresco.rest.framework.resource.content.FileBinaryResource) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException)

Example 5 with FileBinaryResource

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

the class ProcessesImpl method getProcessImage.

@Override
public BinaryResource getProcessImage(String processId) {
    validateIfUserAllowedToWorkWithProcess(processId);
    ProcessInstance processInstance = activitiProcessEngine.getRuntimeService().createProcessInstanceQuery().processInstanceId(processId).singleResult();
    if (processInstance == null) {
        throw new EntityNotFoundException(processId);
    }
    try {
        BpmnModel model = activitiProcessEngine.getRepositoryService().getBpmnModel(processInstance.getProcessDefinitionId());
        if (model != null && model.getLocationMap().size() > 0) {
            List<String> activeActivities = activitiProcessEngine.getRuntimeService().getActiveActivityIds(processId);
            ProcessDiagramGenerator generator = new DefaultProcessDiagramGenerator();
            InputStream generateDiagram = generator.generateDiagram(model, "png", activeActivities);
            File file = TempFileProvider.createTempFile(processId + UUID.randomUUID(), ".png");
            FileOutputStream fos = new FileOutputStream(file);
            IOUtils.copy(generateDiagram, fos);
            fos.close();
            return new FileBinaryResource(file);
        } else {
            throw new EntityNotFoundException(processId + "/image");
        }
    } catch (IOException error) {
        throw new ApiException("Error while getting process image.");
    }
}
Also used : InputStream(java.io.InputStream) DefaultProcessDiagramGenerator(org.activiti.image.impl.DefaultProcessDiagramGenerator) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) IOException(java.io.IOException) FileBinaryResource(org.alfresco.rest.framework.resource.content.FileBinaryResource) BpmnModel(org.activiti.bpmn.model.BpmnModel) ProcessDiagramGenerator(org.activiti.image.ProcessDiagramGenerator) DefaultProcessDiagramGenerator(org.activiti.image.impl.DefaultProcessDiagramGenerator) FileOutputStream(java.io.FileOutputStream) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) File(java.io.File) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException)

Aggregations

FileBinaryResource (org.alfresco.rest.framework.resource.content.FileBinaryResource)5 File (java.io.File)4 InputStream (java.io.InputStream)4 ApiException (org.alfresco.rest.framework.core.exceptions.ApiException)4 NodeBinaryResource (org.alfresco.rest.framework.resource.content.NodeBinaryResource)3 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 Serializable (java.io.Serializable)2 Date (java.util.Date)2 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)2 DisabledServiceException (org.alfresco.rest.framework.core.exceptions.DisabledServiceException)2 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)2 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)2 NotFoundException (org.alfresco.rest.framework.core.exceptions.NotFoundException)2 CacheDirective (org.alfresco.rest.framework.resource.content.CacheDirective)2 ContentInfoImpl (org.alfresco.rest.framework.resource.content.ContentInfoImpl)2 ContentData (org.alfresco.service.cmr.repository.ContentData)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 QName (org.alfresco.service.namespace.QName)2 Map (java.util.Map)1