Search in sources :

Example 26 with TransformationOptions

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

the class TransformActionExecuter method doTransform.

/**
 * Executed in a new transaction so that failures don't cause the entire transaction to rollback.
 */
protected void doTransform(Action ruleAction, NodeRef sourceNodeRef, ContentReader contentReader, NodeRef destinationNodeRef, ContentWriter contentWriter) {
    // transform - will throw NoTransformerException if there are no transformers
    TransformationOptions transformationOptions = newTransformationOptions(ruleAction, sourceNodeRef);
    transformationOptions.setTargetNodeRef(destinationNodeRef);
    String sourceMimetype = contentReader.getMimetype();
    Map<String, String> options = converter.getOptions(transformationOptions, sourceMimetype, contentWriter.getMimetype());
    synchronousTransformClient.transform(contentReader, contentWriter, options, null, sourceNodeRef);
}
Also used : TransformationOptions(org.alfresco.service.cmr.repository.TransformationOptions)

Example 27 with TransformationOptions

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

the class LegacyTransformClient method transform.

@Override
public void transform(NodeRef sourceNodeRef, RenditionDefinition2 renditionDefinition, String user, int sourceContentHashCode) {
    String targetMimetype = renditionDefinition.getTargetMimetype();
    String renditionName = renditionDefinition.getRenditionName();
    Map<String, String> actualOptions = renditionDefinition.getTransformOptions();
    TransformationOptions options = converter.getTransformationOptions(renditionName, actualOptions);
    options.setSourceNodeRef(sourceNodeRef);
    ContentTransformer legacyTransform = transform.get();
    executorService.submit(() -> {
        AuthenticationUtil.runAs((AuthenticationUtil.RunAsWork<Void>) () -> transactionService.getRetryingTransactionHelper().doInTransaction(() -> {
            try {
                if (legacyTransform == null) {
                    throw new IllegalStateException("isSupported was not called prior to an asynchronous transform.");
                }
                ContentReader reader = contentService.getReader(sourceNodeRef, ContentModel.PROP_CONTENT);
                if (null == reader || !reader.exists()) {
                    throw new IllegalArgumentException("sourceNodeRef " + sourceNodeRef + " has no content.");
                }
                if (logger.isDebugEnabled()) {
                    logger.debug(TRANSFORM + "requested " + renditionName);
                }
                // Note: we don't call legacyTransform.transform(reader, writer, options) as the Legacy
                // transforms (unlike Local and Transform Service) automatically fail over to the next
                // highest priority. This was not done for the newer transforms, as a fail over can always be
                // defined and that makes it simpler to understand what is going on.
                ContentWriter writer = contentService.getTempWriter();
                writer.setMimetype(targetMimetype);
                legacySynchronousTransformClient.transform(reader, writer, options);
                InputStream inputStream = writer.getReader().getContentInputStream();
                if (logger.isDebugEnabled()) {
                    logger.debug(TRANSFORM + "to be consumed " + renditionName);
                }
                renditionService2.consume(sourceNodeRef, inputStream, renditionDefinition, sourceContentHashCode);
                if (logger.isDebugEnabled()) {
                    logger.debug(TRANSFORM + "consumed " + renditionName);
                }
            } catch (Exception e) {
                if (logger.isDebugEnabled()) {
                    logger.debug(TRANSFORM + "failed " + renditionName, e);
                }
                if (renditionDefinition instanceof TransformDefinition) {
                    ((TransformDefinition) renditionDefinition).setErrorMessage(e.getMessage());
                }
                renditionService2.failure(sourceNodeRef, renditionDefinition, sourceContentHashCode);
                throw e;
            }
            return null;
        }), user);
    });
}
Also used : InputStream(java.io.InputStream) ContentReader(org.alfresco.service.cmr.repository.ContentReader) TransformationOptions(org.alfresco.service.cmr.repository.TransformationOptions) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) AuthenticationUtil(org.alfresco.repo.security.authentication.AuthenticationUtil) ContentTransformer(org.alfresco.repo.content.transform.ContentTransformer)

Example 28 with TransformationOptions

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

the class LegacyTransformClient method checkSupported.

@Override
public void checkSupported(NodeRef sourceNodeRef, RenditionDefinition2 renditionDefinition, String sourceMimetype, long sourceSizeInBytes, String contentUrl) {
    String targetMimetype = renditionDefinition.getTargetMimetype();
    String renditionName = renditionDefinition.getRenditionName();
    Map<String, String> options = renditionDefinition.getTransformOptions();
    TransformationOptions transformationOptions = converter.getTransformationOptions(renditionName, options);
    transformationOptions.setSourceNodeRef(sourceNodeRef);
    ContentTransformer legacyTransform = legacySynchronousTransformClient.getTransformer(contentUrl, sourceMimetype, sourceSizeInBytes, targetMimetype, transformationOptions);
    transform.set(legacyTransform);
    String message = TRANSFORM + renditionName + " from " + sourceMimetype + (legacyTransform == null ? " is unsupported" : " is supported");
    logger.debug(message);
    if (legacyTransform == null) {
        throw new UnsupportedOperationException(message);
    }
}
Also used : ContentTransformer(org.alfresco.repo.content.transform.ContentTransformer) TransformationOptions(org.alfresco.service.cmr.repository.TransformationOptions)

Example 29 with TransformationOptions

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

the class LegacyTransformerDebug method availableTransformers.

/**
 * Called once all available transformers have been identified.
 */
@Deprecated
public void availableTransformers(List<ContentTransformer> transformers, long sourceSize, String renditionName, NodeRef sourceNodeRef, String calledFrom) {
    if (isEnabled()) {
        Deque<Frame> ourStack = ThreadInfo.getStack();
        Frame frame = ourStack.peek();
        boolean firstLevel = ourStack.size() == 1;
        // Note once turned on we don't turn it off again.
        if (transformers.size() == 0) {
            frame.setFailureReason(NO_TRANSFORMERS);
            if (frame.unavailableTransformers != null && frame.unavailableTransformers.size() != 0) {
                ThreadInfo.setDebugOutput(true);
            }
        }
        frame.setSourceSize(sourceSize);
        // Log the basic info about this transformation
        logBasicDetails(frame, sourceSize, null, renditionName, calledFrom + ((transformers.size() == 0) ? " NO transformers" : ""), firstLevel);
        // Report available and unavailable transformers
        char c = 'a';
        int longestNameLength = getLongestTransformerNameLength(transformers, frame);
        for (ContentTransformer trans : transformers) {
            String name = getName(trans);
            int padName = longestNameLength - name.length() + 1;
            TransformationOptions options = new TransformationOptions();
            options.setUse(frame.renditionName);
            options.setSourceNodeRef(frame.sourceNodeRef);
            long maxSourceSizeKBytes = trans.getMaxSourceSizeKBytes(frame.sourceMimetype, frame.targetMimetype, options);
            String size = maxSourceSizeKBytes > 0 ? "< " + fileSize(maxSourceSizeKBytes * 1024) : "";
            int padSize = 10 - size.length();
            String priority = gePriority(trans, frame.sourceMimetype, frame.targetMimetype);
            log((c == 'a' ? "**" : "  ") + (c++) + ") " + priority + ' ' + name + spaces(padName) + size + spaces(padSize) + ms(trans.getTransformationTime(frame.sourceMimetype, frame.targetMimetype)));
        }
        if (frame.unavailableTransformers != null) {
            for (UnavailableTransformer unavailable : frame.unavailableTransformers) {
                int pad = longestNameLength - unavailable.name.length();
                String reason = "> " + fileSize(unavailable.maxSourceSizeKBytes * 1024);
                if (unavailable.debug || logger.isTraceEnabled()) {
                    log("--" + (c++) + ") " + unavailable.priority + ' ' + unavailable.name + spaces(pad + 1) + reason, unavailable.debug);
                }
            }
        }
    }
}
Also used : TransformationOptions(org.alfresco.service.cmr.repository.TransformationOptions)

Example 30 with TransformationOptions

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

the class LegacyTransformerDebug method transformationsByExtension.

/**
 * Returns a String and /or debug that provides a list of supported transformations
 * sorted by source and target mimetype extension.
 * @param sourceExtension restricts the list to one source extension. Unrestricted if null.
 * @param targetExtension restricts the list to one target extension. Unrestricted if null.
 * @param toString indicates that a String value should be returned in addition to any debug.
 * @param format42 indicates the new 4.2 rather than older 4.1.4 format should be used.
 *        The 4.1.4 format did not order the transformers or mimetypes and only included top
 *        level transformers.
 * @param onlyNonDeterministic if true only report transformations where there is more than
 *        one transformer available with the same priority.
 * @param renditionName to which the transformation will be put (such as "Index", "Preview", null).
 * @deprecated The transformations code is being moved out of the codebase and replaced by the new async RenditionService2 or other external libraries.
 */
@Deprecated
@Override
public String transformationsByExtension(String sourceExtension, String targetExtension, boolean toString, boolean format42, boolean onlyNonDeterministic, String renditionName) {
    // (for example a test transform).
    if (getStringBuilder() != null) {
        return null;
    }
    Collection<ContentTransformer> transformers = format42 && !onlyNonDeterministic ? sortTransformersByName(null) : transformerRegistry.getTransformers();
    Collection<String> sourceMimetypes = format42 || sourceExtension != null ? getSourceMimetypes(sourceExtension) : mimetypeService.getMimetypes();
    Collection<String> targetMimetypes = format42 || targetExtension != null ? getTargetMimetypes(sourceExtension, targetExtension, sourceMimetypes) : mimetypeService.getMimetypes();
    TransformationOptions options = new TransformationOptions();
    options.setUse(renditionName);
    StringBuilder sb = null;
    try {
        if (toString) {
            sb = new StringBuilder();
            setStringBuilder(sb);
        }
        pushMisc();
        for (String sourceMimetype : sourceMimetypes) {
            for (String targetMimetype : targetMimetypes) {
                // Find available transformers
                List<ContentTransformer> availableTransformer = new ArrayList<ContentTransformer>();
                for (ContentTransformer transformer : transformers) {
                    if (transformer.isTransformable(sourceMimetype, -1, targetMimetype, options)) {
                        availableTransformer.add(transformer);
                    }
                }
                // Sort by priority
                final String currSourceMimetype = sourceExtension;
                final String currTargetMimetype = targetExtension;
                Collections.sort(availableTransformer, new Comparator<ContentTransformer>() {

                    @Override
                    public int compare(ContentTransformer transformer1, ContentTransformer transformer2) {
                        return transformerConfig.getPriority(transformer1, currSourceMimetype, currTargetMimetype) - transformerConfig.getPriority(transformer2, currSourceMimetype, currTargetMimetype);
                    }
                });
                // Do we need to produce any output?
                int size = availableTransformer.size();
                int priority = size >= 2 ? transformerConfig.getPriority(availableTransformer.get(0), sourceMimetype, targetMimetype) : -1;
                if (!onlyNonDeterministic || (size >= 2 && priority == transformerConfig.getPriority(availableTransformer.get(1), sourceMimetype, targetMimetype))) {
                    // Log the transformers
                    boolean supportedByTransformService = remoteTransformServiceRegistry == null || remoteTransformServiceRegistry instanceof DummyTransformServiceRegistry ? false : remoteTransformServiceRegistry.isSupported(sourceMimetype, -1, targetMimetype, Collections.emptyMap(), null);
                    List<SupportedTransform> localTransformers = localTransformServiceRegistryImpl == null ? Collections.emptyList() : localTransformServiceRegistryImpl.findTransformers(sourceMimetype, targetMimetype, Collections.emptyMap(), null);
                    if (!localTransformers.isEmpty() || supportedByTransformService || size >= 1) {
                        try {
                            pushMisc();
                            int transformerCount = 0;
                            if (supportedByTransformService) {
                                long maxSourceSizeKBytes = remoteTransformServiceRegistry.findMaxSize(sourceMimetype, targetMimetype, Collections.emptyMap(), null);
                                activeTransformer(sourceMimetype, targetMimetype, transformerCount, "     ", TRANSFORM_SERVICE_NAME, maxSourceSizeKBytes, transformerCount++ == 0);
                            }
                            for (SupportedTransform localTransformer : localTransformers) {
                                long maxSourceSizeKBytes = localTransformer.getMaxSourceSizeBytes();
                                String transformName = "Local:" + localTransformer.getName();
                                String transformerPriority = "[" + localTransformer.getPriority() + ']';
                                transformerPriority = spaces(5 - transformerPriority.length()) + transformerPriority;
                                activeTransformer(sourceMimetype, targetMimetype, transformerCount, transformerPriority, transformName, maxSourceSizeKBytes, transformerCount++ == 0);
                            }
                            for (ContentTransformer transformer : availableTransformer) {
                                if (!onlyNonDeterministic || transformerCount < 2 || priority == transformerConfig.getPriority(transformer, sourceMimetype, targetMimetype)) {
                                    long maxSourceSizeKBytes = transformer.getMaxSourceSizeKBytes(sourceMimetype, targetMimetype, options);
                                    activeTransformer(sourceMimetype, targetMimetype, transformerCount, transformer, maxSourceSizeKBytes, transformerCount++ == 0);
                                }
                            }
                        } finally {
                            popMisc();
                        }
                    }
                }
            }
        }
    } finally {
        popMisc();
        setStringBuilder(null);
    }
    stripFinishedLine(sb);
    return stripLeadingNumber(sb);
}
Also used : SupportedTransform(org.alfresco.transform.client.registry.SupportedTransform) ArrayList(java.util.ArrayList) TransformationOptions(org.alfresco.service.cmr.repository.TransformationOptions)

Aggregations

TransformationOptions (org.alfresco.service.cmr.repository.TransformationOptions)44 ContentReader (org.alfresco.service.cmr.repository.ContentReader)10 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)8 ImageTransformationOptions (org.alfresco.repo.content.transform.magick.ImageTransformationOptions)6 SWFTransformationOptions (org.alfresco.repo.content.transform.swf.SWFTransformationOptions)6 ContentTransformer (org.alfresco.repo.content.transform.ContentTransformer)5 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 Serializable (java.io.Serializable)3 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)3 FileContentReader (org.alfresco.repo.content.filestore.FileContentReader)3 ContentIOException (org.alfresco.service.cmr.repository.ContentIOException)3 NodeRef (org.alfresco.service.cmr.repository.NodeRef)3 TransformationOptionLimits (org.alfresco.service.cmr.repository.TransformationOptionLimits)3 QName (org.alfresco.service.namespace.QName)3 HashMap (java.util.HashMap)2 StringJoiner (java.util.StringJoiner)2 ExecutionException (java.util.concurrent.ExecutionException)2 FileContentWriter (org.alfresco.repo.content.filestore.FileContentWriter)2 ImageResizeOptions (org.alfresco.repo.content.transform.magick.ImageResizeOptions)2