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