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);
}
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);
}
}
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);
}
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.");
}
}
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.");
}
}
Aggregations