Search in sources :

Example 1 with UnimportantTransformException

use of org.alfresco.repo.content.transform.UnimportantTransformException in project alfresco-repository by Alfresco.

the class ScriptNode method createThumbnail.

/**
 * Creates a thumbnail for the content property of the node.
 *
 * The thumbnail name corresponds to pre-set thumbnail details stored in the
 * repository.
 *
 * If the thumbnail is created asynchronously then the result will be null and creation
 * of the thumbnail will occure at some point in the background.
 *
 * If foce param specified system.thumbnail.generate is ignoring. Could be used for preview creation
 *
 * @param  thumbnailName    the name of the thumbnail
 * @param  async            indicates whether the thumbnail is create asynchronously or not
 * @param  force            ignore system.thumbnail.generate=false
 * @return ScriptThumbnail  the newly create thumbnail node or null if async creation occures
 *
 * @deprecated The async flag in the method signature will not be applicable as all of
 * the future transformations will be asynchronous
 */
@Deprecated
public ScriptThumbnail createThumbnail(String thumbnailName, boolean async, boolean force) {
    final ThumbnailService thumbnailService = services.getThumbnailService();
    ScriptThumbnail result = null;
    // We need to create preview for node even if system.thumbnail.generate=false
    if (force || thumbnailService.getThumbnailsEnabled()) {
        // Use the thumbnail registy to get the details of the thumbail
        ThumbnailRegistry registry = thumbnailService.getThumbnailRegistry();
        ThumbnailDefinition details = registry.getThumbnailDefinition(thumbnailName);
        if (details == null) {
            // Throw exception
            throw new ScriptException("The thumbnail name '" + thumbnailName + "' is not registered");
        }
        // If there's nothing currently registered to generate thumbnails for the
        // specified mimetype, then log a message and bail out
        String nodeMimeType = getMimetype();
        Serializable value = this.nodeService.getProperty(nodeRef, ContentModel.PROP_CONTENT);
        ContentData contentData = DefaultTypeConverter.INSTANCE.convert(ContentData.class, value);
        if (!ContentData.hasContent(contentData)) {
            if (logger.isDebugEnabled())
                logger.debug("Unable to create thumbnail '" + details.getName() + "' as there is no content");
            return null;
        }
        if (!registry.isThumbnailDefinitionAvailable(contentData.getContentUrl(), nodeMimeType, getSize(), nodeRef, details)) {
            logger.info("Unable to create thumbnail '" + details.getName() + "' for " + nodeMimeType + " as no transformer is currently available.");
            return null;
        }
        // Have the thumbnail created
        if (async == false) {
            try {
                // Create the thumbnail
                NodeRef thumbnailNodeRef = thumbnailService.createThumbnail(this.nodeRef, ContentModel.PROP_CONTENT, details.getMimetype(), details.getTransformationOptions(), details.getName());
                // Create the thumbnail script object
                result = new ScriptThumbnail(thumbnailNodeRef, this.services, this.scope);
            } catch (AlfrescoRuntimeException e) {
                Throwable rootCause = e.getRootCause();
                if (rootCause instanceof UnimportantTransformException) {
                    logger.debug("Unable to create thumbnail '" + details.getName() + "' as " + rootCause.getMessage());
                    return null;
                }
                throw e;
            }
        } else {
            RenditionDefinition2 renditionDefinition = renditionDefinitionRegistry2.getRenditionDefinition(thumbnailName);
            if (renditionDefinition != null) {
                renditionService2.render(nodeRef, thumbnailName);
            } else {
                Action action = ThumbnailHelper.createCreateThumbnailAction(details, services);
                // Queue async creation of thumbnail
                this.services.getActionService().executeAction(action, this.nodeRef, true, true);
            }
        }
    }
    return result;
}
Also used : Serializable(java.io.Serializable) Action(org.alfresco.service.cmr.action.Action) ThumbnailService(org.alfresco.service.cmr.thumbnail.ThumbnailService) ThumbnailRegistry(org.alfresco.repo.thumbnail.ThumbnailRegistry) UnimportantTransformException(org.alfresco.repo.content.transform.UnimportantTransformException) ScriptException(org.alfresco.scripts.ScriptException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ThumbnailDefinition(org.alfresco.repo.thumbnail.ThumbnailDefinition) ContentData(org.alfresco.service.cmr.repository.ContentData) ScriptThumbnail(org.alfresco.repo.thumbnail.script.ScriptThumbnail) RenditionDefinition2(org.alfresco.repo.rendition2.RenditionDefinition2) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException)

Example 2 with UnimportantTransformException

use of org.alfresco.repo.content.transform.UnimportantTransformException in project alfresco-repository by Alfresco.

the class AbstractRenderingEngine method executeImpl.

protected void executeImpl(final RenditionDefinition renditionDef, final NodeRef sourceNode) {
    // MNT-10178
    if (!nodeService.exists(sourceNode)) {
        if (logger.isDebugEnabled() == true) {
            logger.debug("Rendition has not been created, because the node no longer exists.  (sourceNode=" + sourceNode + ")");
        }
        notifyCallbackOfException(renditionDef, new UnimportantTransformException("Rendition was cancelled, because the node no longer exists."));
        return;
    } else if (nodeService.getProperty(sourceNode, ContentModel.PROP_CONTENT) == null) {
        if (logger.isDebugEnabled() == true) {
            logger.debug("Rendition has not been created, because the node has no content to render.  (sourceNode=" + sourceNode + ")");
        }
        notifyCallbackOfException(renditionDef, new UnimportantTransformException("Rendition was cancelled, because the node has no content to render."));
        return;
    }
    if (logger.isDebugEnabled()) {
        StringBuilder msg = new StringBuilder();
        msg.append("Rendering node ").append(sourceNode).append(" with rendition definition ").append(renditionDef.getRenditionName());
        msg.append("\n").append("  parameters:").append("\n");
        if (renditionDef.getParameterValues().isEmpty() == false) {
            for (String paramKey : renditionDef.getParameterValues().keySet()) {
                msg.append("    ").append(paramKey).append("=").append(renditionDef.getParameterValue(paramKey)).append("\n");
            }
        } else {
            msg.append("    [None]");
        }
        logger.debug(msg.toString());
    }
    Serializable runAsParam = renditionDef.getParameterValue(AbstractRenderingEngine.PARAM_RUN_AS);
    String runAsName = runAsParam == null ? DEFAULT_RUN_AS_NAME : (String) runAsParam;
    // Renditions should all be created by system by default.
    // When renditions are created by a user and are to be created under a
    // node
    // other than the source node, it is possible that the user will not
    // have
    // permissions to create content under that node.
    // For that reason, we execute all rendition actions as system
    // by default.
    AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            ChildAssociationRef result = null;
            try {
                // Check whether this rendition is a component of a larger CompositeRendition
                boolean isComponentRendition = isComponentRendition(renditionDef);
                if (isComponentRendition == false) {
                    // Request that the rendition is initially created
                    // as a child of the source node
                    setTemporaryRenditionProperties(sourceNode, renditionDef);
                }
                // Have the concrete implementation do the actual rendition
                executeRenditionImpl(renditionDef, sourceNode);
                // 
                if (isComponentRendition == false) {
                    // Add renditioned aspect to the source node
                    tagSourceNodeAsRenditioned(renditionDef, sourceNode);
                    // Currently the rendition is on a temporary node, which may
                    // have the wrong name on it, and for path based renditions is
                    // in the wrong place
                    // So, have the correct node created, and switch everything to use it
                    switchToFinalRenditionNode(renditionDef, sourceNode);
                }
                // Grab a link to the rendition node - it's been saved as a parameter for us
                // (Wait until now to fetch in case it was moved)
                result = (ChildAssociationRef) renditionDef.getParameterValue(PARAM_RESULT);
            } catch (Throwable t) {
                notifyCallbackOfException(renditionDef, t);
                throwWrappedException(t);
            }
            if (result != null) {
                notifyCallbackOfResult(renditionDef, result);
            }
            return null;
        }
    }, runAsName);
}
Also used : UnimportantTransformException(org.alfresco.repo.content.transform.UnimportantTransformException) Serializable(java.io.Serializable) AuthenticationUtil(org.alfresco.repo.security.authentication.AuthenticationUtil) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) NamespaceException(org.alfresco.service.namespace.NamespaceException) ActionServiceException(org.alfresco.service.cmr.action.ActionServiceException) UnimportantTransformException(org.alfresco.repo.content.transform.UnimportantTransformException) RenditionServiceException(org.alfresco.service.cmr.rendition.RenditionServiceException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException)

Example 3 with UnimportantTransformException

use of org.alfresco.repo.content.transform.UnimportantTransformException in project alfresco-repository by Alfresco.

the class ContentTransformServiceImpl method failoverTransformers.

private void failoverTransformers(ContentReader reader, ContentWriter writer, TransformationOptions options, String targetMimetype, List<ContentTransformer> transformers) {
    List<AlfrescoRuntimeException> exceptions = null;
    boolean done = false;
    try {
        // Try the best transformer and then the next if it fails
        // and so on down the list
        char c = 'a';
        String outputFileExt = mimetypeService.getExtension(targetMimetype);
        for (ContentTransformer transformer : transformers) {
            ContentWriter currentWriter = writer;
            File tempFile = null;
            try {
                // We can't know in advance which of the
                // available transformer will work - if any.
                // We can't write into the ContentWriter stream.
                // So make a temporary file writer with the
                // current transformer name.
                tempFile = TempFileProvider.createTempFile("FailoverTransformer_intermediate_" + transformer.getClass().getSimpleName() + "_", "." + outputFileExt);
                currentWriter = new FileContentWriter(tempFile);
                currentWriter.setMimetype(targetMimetype);
                currentWriter.setEncoding(writer.getEncoding());
                if (c != 'a' && transformerDebug.isEnabled()) {
                    transformerDebug.debug("");
                    transformerDebug.debug("Try " + c + ")");
                }
                c++;
                transformer.transform(reader, currentWriter, options);
                if (tempFile != null) {
                    writer.putContent(tempFile);
                }
                // No need to close input or output streams
                // (according
                // to comment in FailoverContentTransformer)
                done = true;
                return;
            } catch (Exception e) {
                if (exceptions == null) {
                    exceptions = new ArrayList<AlfrescoRuntimeException>();
                }
                if (!(e instanceof AlfrescoRuntimeException)) {
                    e = new AlfrescoRuntimeException(e.getMessage(), e);
                }
                exceptions.add((AlfrescoRuntimeException) e);
                // Set a new reader to refresh the input stream.
                reader = reader.getReader();
            }
        }
        // others are consumed.
        if (exceptions != null) {
            throw exceptions.get(0);
        }
    } finally {
        // none of the transformers worked.
        if (exceptions != null) {
            boolean first = true;
            for (Exception e : exceptions) {
                Throwable rootCause = (e instanceof AlfrescoRuntimeException) ? ((AlfrescoRuntimeException) e).getRootCause() : null;
                String message = (rootCause == null ? null : rootCause.getMessage());
                if (done) {
                    message = "Transformer succeeded after previous transformer failed" + (message == null ? "" : ": " + message);
                    if (rootCause instanceof UnsupportedTransformationException || rootCause instanceof UnimportantTransformException) {
                        logger.debug(message);
                    } else {
                        logger.warn(message, e);
                    }
                } else if (// The first exception is logged later
                !first) {
                    message = "Transformer exception" + (message == null ? "" : ": " + message);
                    if (rootCause instanceof UnsupportedTransformationException || rootCause instanceof UnimportantTransformException) {
                        logger.debug(message);
                    } else {
                        logger.error(message, e);
                    }
                    first = false;
                }
            }
        }
    }
}
Also used : UnsupportedTransformationException(org.alfresco.repo.content.transform.UnsupportedTransformationException) FileContentWriter(org.alfresco.repo.content.filestore.FileContentWriter) ArrayList(java.util.ArrayList) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) UnsupportedTransformationException(org.alfresco.repo.content.transform.UnsupportedTransformationException) UnimportantTransformException(org.alfresco.repo.content.transform.UnimportantTransformException) NoTransformerException(org.alfresco.service.cmr.repository.NoTransformerException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) UnimportantTransformException(org.alfresco.repo.content.transform.UnimportantTransformException) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) FileContentWriter(org.alfresco.repo.content.filestore.FileContentWriter) ContentTransformer(org.alfresco.repo.content.transform.ContentTransformer) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) File(java.io.File)

Aggregations

AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)3 UnimportantTransformException (org.alfresco.repo.content.transform.UnimportantTransformException)3 Serializable (java.io.Serializable)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 FileContentWriter (org.alfresco.repo.content.filestore.FileContentWriter)1 ContentTransformer (org.alfresco.repo.content.transform.ContentTransformer)1 UnsupportedTransformationException (org.alfresco.repo.content.transform.UnsupportedTransformationException)1 RenditionDefinition2 (org.alfresco.repo.rendition2.RenditionDefinition2)1 AuthenticationUtil (org.alfresco.repo.security.authentication.AuthenticationUtil)1 ThumbnailDefinition (org.alfresco.repo.thumbnail.ThumbnailDefinition)1 ThumbnailRegistry (org.alfresco.repo.thumbnail.ThumbnailRegistry)1 ScriptThumbnail (org.alfresco.repo.thumbnail.script.ScriptThumbnail)1 ScriptException (org.alfresco.scripts.ScriptException)1 Action (org.alfresco.service.cmr.action.Action)1 ActionServiceException (org.alfresco.service.cmr.action.ActionServiceException)1 RenditionServiceException (org.alfresco.service.cmr.rendition.RenditionServiceException)1 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)1 ContentData (org.alfresco.service.cmr.repository.ContentData)1 ContentIOException (org.alfresco.service.cmr.repository.ContentIOException)1