Search in sources :

Example 6 with TransformationOptions

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

the class AbstractContentTransformerLimitsTest method setUp.

@Before
public void setUp() throws Exception {
    ApplicationContext ctx = MiscContextTestSuite.getMinimalContext();
    ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
    MimetypeService mimetypeService = serviceRegistry.getMimetypeService();
    TransformerDebug transformerDebug = (TransformerDebug) ctx.getBean("transformerDebug");
    TransformerConfig transformerConfig = (TransformerConfig) ctx.getBean("transformerConfig");
    transformer = new AbstractContentTransformer2() {

        @Override
        public boolean isTransformableMimetype(String sourceMimetype, String targetMimetype, TransformationOptions options) {
            return false;
        }

        @Override
        protected void transformInternal(ContentReader reader, ContentWriter writer, TransformationOptions options) throws Exception {
        }
    };
    transformer.setMimetypeService(mimetypeService);
    transformer.setTransformerDebug(transformerDebug);
    transformer.setTransformerConfig(transformerConfig);
    transformer.setBeanName("transformer.test" + System.currentTimeMillis() % 100000);
    limits = new TransformationOptionLimits();
    options = new TransformationOptions();
}
Also used : AbstractContentReader(org.alfresco.repo.content.AbstractContentReader) ContentReader(org.alfresco.service.cmr.repository.ContentReader) IOException(java.io.IOException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) TransformationOptions(org.alfresco.service.cmr.repository.TransformationOptions) ApplicationContext(org.springframework.context.ApplicationContext) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) TransformationOptionLimits(org.alfresco.service.cmr.repository.TransformationOptionLimits) MimetypeService(org.alfresco.service.cmr.repository.MimetypeService) ServiceRegistry(org.alfresco.service.ServiceRegistry) Before(org.junit.Before)

Example 7 with TransformationOptions

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

the class DifferrentMimeTypeTest method testTransformToJPeg.

/**
 * Method tries to transform a quick file to a JPeg, setting the declared source Mimetype.
 * @param quickname name of the quickfile.
 * @param extensionMimetype the expected mimetype of the quick file from the extension
 *        (this might be wrong - checked).
 * @param declaredMimetype source mimetype to be set on the node
 *        (this may be wrong - simply used).
 * @param detectedMimetype the expected mimetype detected from the content
 *        (this might be wrong - checked).
 * @param approach indicates that the initial transform should be using
 *        STRICT mimetype checking,
 *        RETRY with another transformer if the declared and derived mimetypes
 *        don't match, or
 *        NEITHER if it should uses the original approach with no
 *        retries and no strict checking.
 * @param expectSuccess indicates if the transform is expected to work.
 * @throws IOException if the quick file cannot be loaded.
 */
private void testTransformToJPeg(String quickname, String extensionMimetype, String declaredMimetype, String detectedMimetype, int approach, boolean expectSuccess) throws IOException {
    boolean retry = (approach & RETRY) != 0;
    boolean strict = (approach & STRICT) != 0;
    testFile = AbstractContentTransformerTest.loadNamedQuickTestFile(quickname);
    options = new TransformationOptions();
    createContentNodeRef();
    String fileName = testFile.getName();
    String actualExtensionMimetype = mimetypeService.guessMimetype(fileName);
    assertEquals("The " + quickname + " extension indicates a mimetype of " + actualExtensionMimetype + " rather than " + extensionMimetype, extensionMimetype, actualExtensionMimetype);
    String targetMimeType = MimetypeMap.MIMETYPE_IMAGE_JPEG;
    String outputFileExtension = mimetypeService.getExtension(targetMimeType);
    AuthenticationUtil.RunAsWork<ContentReader> createTargetWork = new AuthenticationUtil.RunAsWork<ContentReader>() {

        @Override
        public ContentReader doWork() throws Exception {
            return contentService.getReader(contentNodeRef, ContentModel.PROP_CONTENT);
        }
    };
    ContentReader contentReader = AuthenticationUtil.runAs(createTargetWork, AuthenticationUtil.getSystemUserName());
    contentReader.setMimetype(declaredMimetype);
    String actualDetectedMimetype = mimetypeService.getMimetypeIfNotMatches(contentReader);
    if (actualDetectedMimetype != null) {
        assertEquals("The mimetype detected from the content was " + actualDetectedMimetype + " rather than " + detectedMimetype, detectedMimetype, actualDetectedMimetype);
    }
    long size = contentReader.getSize();
    contentTransformer = (AbstractContentTransformer2) registry.getTransformer(declaredMimetype, size, targetMimeType, options);
    assertNotNull("Transformer not found", contentTransformer);
    boolean originalRetry = (Boolean) contentTransformer.getRetryTransformOnDifferentMimeType();
    boolean originalStrict = contentTransformer.getStrictMimeTypeCheck();
    assertTrue("Content Transformations should be 'retry' by default", originalRetry);
    assertTrue("Content Transformations should be 'strict' by default", originalStrict);
    contentTransformer.setRetryTransformOnDifferentMimeType(retry);
    contentTransformer.setStrictMimeTypeCheck(strict);
    File outputFile = createATempFileForTheDifferentMimeTypeTest(outputFileExtension);
    try {
        ContentWriter outputWriter = new FileContentWriter(outputFile);
        outputWriter.setMimetype(targetMimeType);
        contentTransformer.transform(contentReader.getReader(), outputWriter, options);
        if (!expectSuccess) {
            if (strict) {
                fail("The contentTransformer should have failed with an UnsupportedTransformationException");
            } else {
                fail("The contentTransformer should have failed with a ContentIOException");
            }
        }
        // After successful transformation image size should be grater than 0
        assertTrue("File transformation failed. Output file size is '0'", outputWriter.getSize() > 0);
    } catch (ContentIOException e) {
        if (expectSuccess) {
            fail("The contentTransformer should NOT have failed with an ContentIOException " + e);
            e.printStackTrace();
        }
    } catch (UnsupportedTransformationException e) {
        if (expectSuccess) {
            fail("The contentTransformer should NOT have failed with an UnsupportedTransformationException " + e);
            e.printStackTrace();
        }
        if (strict) {
            validateErrorMessage(fileName, declaredMimetype, detectedMimetype, e);
        }
    } finally {
        contentTransformer.setRetryTransformOnDifferentMimeType(originalRetry);
        contentTransformer.setStrictMimeTypeCheck(originalStrict);
        outputFile.deleteOnExit();
    }
}
Also used : FileContentReader(org.alfresco.repo.content.filestore.FileContentReader) ContentReader(org.alfresco.service.cmr.repository.ContentReader) FileContentWriter(org.alfresco.repo.content.filestore.FileContentWriter) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) TransformationOptions(org.alfresco.service.cmr.repository.TransformationOptions) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) FileContentWriter(org.alfresco.repo.content.filestore.FileContentWriter) AuthenticationUtil(org.alfresco.repo.security.authentication.AuthenticationUtil) File(java.io.File)

Example 8 with TransformationOptions

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

the class StringExtractingContentTransformerTest method testDirectTransform.

public void testDirectTransform() throws Exception {
    ContentReader reader = writeContent("text/plain", "MacDingbat");
    // check transformability
    assertTrue(transformer.isTransformable(reader.getMimetype(), -1, targetWriter.getMimetype(), new TransformationOptions()));
    // transform
    transformer.transform(reader, targetWriter);
    // get a reader onto the transformed content and check
    ContentReader checkReader = targetWriter.getReader();
    String checkContent = checkReader.getContentString();
    assertEquals("Content check failed", SOME_CONTENT, checkContent);
}
Also used : FileContentReader(org.alfresco.repo.content.filestore.FileContentReader) ContentReader(org.alfresco.service.cmr.repository.ContentReader) TransformationOptions(org.alfresco.service.cmr.repository.TransformationOptions)

Example 9 with TransformationOptions

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

the class HtmlParserContentTransformerTest method checkIsTransformable.

public void checkIsTransformable() throws Exception {
    // check reliability
    boolean reliability = transformer.isTransformable(MimetypeMap.MIMETYPE_HTML, -1, MimetypeMap.MIMETYPE_TEXT_PLAIN, new TransformationOptions());
    // plain text to plain text is supported
    assertTrue(reliability);
    // check other way around
    reliability = transformer.isTransformable(MimetypeMap.MIMETYPE_TEXT_PLAIN, -1, MimetypeMap.MIMETYPE_HTML, new TransformationOptions());
    // plain text to plain text is not supported
    assertFalse(reliability);
}
Also used : TransformationOptions(org.alfresco.service.cmr.repository.TransformationOptions)

Example 10 with TransformationOptions

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

the class ContentTransformerRegistryTest method testGetTransformerEnabledDisabled.

public void testGetTransformerEnabledDisabled() throws Exception {
    TransformationOptions options = new TransformationOptions();
    Assert.assertNotNull(registry.getTransformer(MIMETYPE_TEXT_PLAIN, -1, MIMETYPE_TEXT_PLAIN, options));
    try {
        registry.setEnabled(false);
        Assert.assertNull(registry.getTransformer(MIMETYPE_TEXT_PLAIN, -1, MIMETYPE_TEXT_PLAIN, options));
    } finally {
        registry.setEnabled(true);
    }
    Assert.assertNotNull(registry.getTransformer(MIMETYPE_TEXT_PLAIN, -1, MIMETYPE_TEXT_PLAIN, options));
}
Also used : 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