Search in sources :

Example 1 with ContentServiceTransientException

use of org.alfresco.service.cmr.repository.ContentServiceTransientException in project alfresco-repository by Alfresco.

the class CreateThumbnailActionExecuter method executeImpl.

/**
 * @see org.alfresco.repo.action.executer.ActionExecuterAbstractBase#executeImpl(org.alfresco.service.cmr.action.Action, org.alfresco.service.cmr.repository.NodeRef)
 */
@Override
protected void executeImpl(Action action, NodeRef actionedUponNodeRef) {
    // Check if thumbnailing is generally disabled
    if (!thumbnailService.getThumbnailsEnabled()) {
        if (logger.isDebugEnabled()) {
            logger.debug("Thumbnail transformations are not enabled");
        }
        return;
    }
    if (this.nodeService.exists(actionedUponNodeRef) == true) {
        // Get the thumbnail Name
        String thumbnailName = (String) action.getParameterValue(PARAM_THUMBANIL_NAME);
        // Get the details of the thumbnail
        ThumbnailRegistry registry = this.thumbnailService.getThumbnailRegistry();
        ThumbnailDefinition details = registry.getThumbnailDefinition(thumbnailName);
        if (details == null) {
            // Throw exception
            throw new AlfrescoRuntimeException("The thumbnail name '" + thumbnailName + "' is not registered");
        }
        // Get the content property
        QName contentProperty = (QName) action.getParameterValue(PARAM_CONTENT_PROPERTY);
        if (contentProperty == null) {
            contentProperty = ContentModel.PROP_CONTENT;
        }
        // If there isn't a currently active transformer for this, log and skip
        Serializable contentProp = nodeService.getProperty(actionedUponNodeRef, contentProperty);
        if (contentProp == null) {
            logger.info("Creation of thumbnail, null content for " + details.getName());
            return;
        }
        if (contentProp instanceof ContentData) {
            ContentData content = (ContentData) contentProp;
            String mimetype = content.getMimetype();
            if (!registry.isThumbnailDefinitionAvailable(content.getContentUrl(), mimetype, content.getSize(), actionedUponNodeRef, details)) {
                logger.debug("Unable to create thumbnail '" + details.getName() + "' for " + mimetype + " as no transformer is currently available");
                return;
            }
            if (mimetypeMaxSourceSizeKBytes != null) {
                Long maxSourceSizeKBytes = mimetypeMaxSourceSizeKBytes.get(mimetype);
                if (maxSourceSizeKBytes != null && maxSourceSizeKBytes >= 0 && maxSourceSizeKBytes < (content.getSize() / 1024L)) {
                    logger.debug("Unable to create thumbnail '" + details.getName() + "' for " + mimetype + " as content is too large (" + (content.getSize() / 1024L) + "K > " + maxSourceSizeKBytes + "K)");
                    // avoid transform
                    return;
                }
            }
        }
        // Create the thumbnail
        try {
            TransformationOptions options = details.getTransformationOptions();
            this.thumbnailService.createThumbnail(actionedUponNodeRef, contentProperty, details.getMimetype(), options, thumbnailName, null);
        } catch (ContentServiceTransientException cste) {
            // any transient failures in the thumbnail creation must be handled as transient failures of the action to execute.
            StringBuilder msg = new StringBuilder();
            msg.append("Creation of thumbnail '").append(details.getName()).append("' declined");
            if (logger.isDebugEnabled()) {
                logger.debug(msg.toString());
            }
            throw new ActionServiceTransientException(msg.toString(), cste);
        } catch (Exception exception) {
            final String msg = "Creation of thumbnail '" + details.getName() + "' failed";
            logger.info(msg);
            // See AddFailedThumbnailActionExecuter
            throw new AlfrescoRuntimeException(msg, exception);
        }
    }
}
Also used : Serializable(java.io.Serializable) QName(org.alfresco.service.namespace.QName) ContentServiceTransientException(org.alfresco.service.cmr.repository.ContentServiceTransientException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ActionServiceTransientException(org.alfresco.service.cmr.action.ActionServiceTransientException) TransformationOptions(org.alfresco.service.cmr.repository.TransformationOptions) ContentData(org.alfresco.service.cmr.repository.ContentData) ActionServiceTransientException(org.alfresco.service.cmr.action.ActionServiceTransientException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ContentServiceTransientException(org.alfresco.service.cmr.repository.ContentServiceTransientException)

Example 2 with ContentServiceTransientException

use of org.alfresco.service.cmr.repository.ContentServiceTransientException in project alfresco-repository by Alfresco.

the class AbstractContentTransformer2 method transform.

/**
 * @see org.alfresco.repo.content.transform.ContentTransformer#transform(org.alfresco.service.cmr.repository.ContentReader, org.alfresco.service.cmr.repository.ContentWriter, org.alfresco.service.cmr.repository.TransformationOptions)
 */
public void transform(ContentReader reader, ContentWriter writer, TransformationOptions options) throws ContentIOException {
    try {
        depth.set(depth.get() + 1);
        // begin timing
        long before = System.currentTimeMillis();
        String sourceMimetype = reader.getMimetype();
        String targetMimetype = writer.getMimetype();
        // check options map
        if (options == null) {
            options = new TransformationOptions();
        }
        try {
            if (transformerDebug.isEnabled()) {
                ((LegacyTransformerDebug) transformerDebug).pushTransform(this, reader.getContentUrl(), sourceMimetype, targetMimetype, reader.getSize(), options);
            }
            // MNT-16381: check the mimetype of the file supplied by the user
            // matches the sourceMimetype of the reader. Intermediate files are
            // not checked.
            strictMimetypeCheck(reader, options, sourceMimetype);
            // Check the transformability
            checkTransformable(reader, writer, options);
            // Pass on any limits to the reader
            setReaderLimits(reader, writer, options);
            // Transform
            // MNT-12238: CLONE - CLONE - Upload of PPTX causes very high memory usage leading to system instability
            // Limiting transformation up to configured amount of milliseconds to avoid very high RAM consumption
            // and OOM during transforming problematic documents
            TransformationOptionLimits limits = getLimits(reader.getMimetype(), writer.getMimetype(), options);
            long timeoutMs = limits.getTimeoutMs();
            if (!useTimeoutThread || (null == limits) || (-1 == timeoutMs)) {
                transformInternal(reader, writer, options);
            } else {
                Future<?> submittedTask = null;
                StreamAwareContentReaderProxy proxiedReader = new StreamAwareContentReaderProxy(reader);
                StreamAwareContentWriterProxy proxiedWriter = new StreamAwareContentWriterProxy(writer);
                try {
                    submittedTask = getExecutorService().submit(new TransformInternalCallable(proxiedReader, proxiedWriter, options));
                    submittedTask.get(timeoutMs + additionalThreadTimout, TimeUnit.MILLISECONDS);
                } catch (TimeoutException e) {
                    releaseResources(submittedTask, proxiedReader, proxiedWriter);
                    throw new TimeoutException("Transformation failed due to timeout limit");
                } catch (InterruptedException e) {
                    releaseResources(submittedTask, proxiedReader, proxiedWriter);
                    throw new InterruptedException("Transformation failed, because the thread of the transformation was interrupted");
                } catch (ExecutionException e) {
                    Throwable cause = e.getCause();
                    if (cause instanceof TransformInternalCallableException) {
                        cause = ((TransformInternalCallableException) cause).getCause();
                    }
                    throw cause;
                }
            }
            // record time
            long after = System.currentTimeMillis();
            recordTime(sourceMimetype, targetMimetype, after - before);
        } catch (ContentServiceTransientException cste) {
            // update the transformer's average time.
            if (logger.isDebugEnabled()) {
                logger.debug("Transformation has been transiently declined: \n" + "   reader: " + reader + "\n" + "   writer: " + writer + "\n" + "   options: " + options + "\n" + "   transformer: " + this);
            }
            // We rethrow the exception
            throw cste;
        } catch (UnsupportedTransformationException e) {
            // Don't record an error or even the time, as this is normal in compound transformations.
            transformerDebug.debug("          Failed", e);
            throw e;
        } catch (Throwable e) {
            // Make sure that this transformation gets set back i.t.o. time taken.
            // This will ensure that transformers that compete for the same transformation
            // will be prejudiced against transformers that tend to fail
            long after = System.currentTimeMillis();
            recordError(sourceMimetype, targetMimetype, after - before);
            // Ask Tika to detect the document, and report back on if
            // the current mime type is plausible
            String differentType = getMimetypeService().getMimetypeIfNotMatches(reader.getReader());
            // Report the error
            if (differentType == null) {
                transformerDebug.debug("          Failed", e);
                throw new ContentIOException("Content conversion failed: \n" + "   reader: " + reader + "\n" + "   writer: " + writer + "\n" + "   options: " + options.toString(false) + "\n" + "   limits: " + getLimits(reader, writer, options), e);
            } else {
                transformerDebug.debug("          Failed: Mime type was '" + differentType + "'", e);
                if (retryTransformOnDifferentMimeType) {
                    // MNT-11015 fix.
                    // Set a new reader to refresh the input stream.
                    reader = reader.getReader();
                    // set the actual file MIME type detected by Tika for content reader
                    reader.setMimetype(differentType);
                    // Get correct transformer according actual file MIME type and try to transform file with
                    // actual transformer
                    ContentTransformer transformer = this.registry.getTransformer(differentType, reader.getSize(), targetMimetype, options);
                    if (null != transformer) {
                        transformer.transform(reader, writer, options);
                    } else {
                        transformerDebug.debug("          Failed", e);
                        throw new ContentIOException("Content conversion failed: \n" + "   reader: " + reader + "\n" + "   writer: " + writer + "\n" + "   options: " + options.toString(false) + "\n" + "   limits: " + getLimits(reader, writer, options) + "\n" + "   claimed mime type: " + reader.getMimetype() + "\n" + "   detected mime type: " + differentType + "\n" + "   transformer not found" + "\n", e);
                    }
                } else {
                    throw new ContentIOException("Content conversion failed: \n" + "   reader: " + reader + "\n" + "   writer: " + writer + "\n" + "   options: " + options.toString(false) + "\n" + "   limits: " + getLimits(reader, writer, options) + "\n" + "   claimed mime type: " + reader.getMimetype() + "\n" + "   detected mime type: " + differentType, e);
                }
            }
        } finally {
            transformerDebug.popTransform();
            // check that the reader and writer are both closed
            if (reader.isChannelOpen()) {
                logger.error("Content reader not closed by transformer: \n" + "   reader: " + reader + "\n" + "   transformer: " + this);
            }
            if (writer.isChannelOpen()) {
                logger.error("Content writer not closed by transformer: \n" + "   writer: " + writer + "\n" + "   transformer: " + this);
            }
        }
        // done
        if (logger.isDebugEnabled()) {
            logger.debug("Completed transformation: \n" + "   reader: " + reader + "\n" + "   writer: " + writer + "\n" + "   options: " + options + "\n" + "   transformer: " + this);
        }
    } finally {
        depth.set(depth.get() - 1);
    }
}
Also used : StreamAwareContentWriterProxy(org.alfresco.repo.content.StreamAwareContentWriterProxy) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) TransformationOptions(org.alfresco.service.cmr.repository.TransformationOptions) StreamAwareContentReaderProxy(org.alfresco.repo.content.StreamAwareContentReaderProxy) TransformationOptionLimits(org.alfresco.service.cmr.repository.TransformationOptionLimits) ContentServiceTransientException(org.alfresco.service.cmr.repository.ContentServiceTransientException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

ContentServiceTransientException (org.alfresco.service.cmr.repository.ContentServiceTransientException)2 TransformationOptions (org.alfresco.service.cmr.repository.TransformationOptions)2 Serializable (java.io.Serializable)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 StreamAwareContentReaderProxy (org.alfresco.repo.content.StreamAwareContentReaderProxy)1 StreamAwareContentWriterProxy (org.alfresco.repo.content.StreamAwareContentWriterProxy)1 ActionServiceTransientException (org.alfresco.service.cmr.action.ActionServiceTransientException)1 ContentData (org.alfresco.service.cmr.repository.ContentData)1 ContentIOException (org.alfresco.service.cmr.repository.ContentIOException)1 TransformationOptionLimits (org.alfresco.service.cmr.repository.TransformationOptionLimits)1 QName (org.alfresco.service.namespace.QName)1