Search in sources :

Example 11 with RenditionDefinition

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

the class RenditionServiceIntegrationTest method makeReformatAction.

/**
 * Creates a RenderingAction (RenditionDefinition) for the
 * ReformatActionExecutor, setting the mimetype parameter value to plain
 * text.
 *
 * @param renditionObjectType requested node type of the rendition object
 * @param targetMimetype
 * @return A new RenderingAction.
 */
private RenditionDefinition makeReformatAction(QName renditionObjectType, String targetMimetype) {
    // Create the rendering action.
    RenditionDefinition action = renditionService.createRenditionDefinition(REFORMAT_RENDER_DEFN_NAME, ReformatRenderingEngine.NAME);
    action.setParameterValue(AbstractRenderingEngine.PARAM_MIME_TYPE, targetMimetype);
    if (renditionObjectType != null) {
        action.setParameterValue(RenditionService.PARAM_RENDITION_NODETYPE, renditionObjectType);
    }
    return action;
}
Also used : RenditionDefinition(org.alfresco.service.cmr.rendition.RenditionDefinition) CompositeRenditionDefinition(org.alfresco.service.cmr.rendition.CompositeRenditionDefinition)

Example 12 with RenditionDefinition

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

the class RenditionServiceIntegrationTest method testRenderValidContentThenUpdateToInvalidContent.

/**
 * This test checks that for a node with an existing rendition, that if you update its content with content
 * that cannot be renditioned (thumbnailed), that existing rendition nodes for failed re-renditions are removed.
 * See ALF-6730.
 *
 * @since 3.4.2
 */
@Test
public void testRenderValidContentThenUpdateToInvalidContent() throws Exception {
    transactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            // Need to use one of the standard, persisted rendition definitions, as non-persisted rendition definitions
            // do not get automatcially updated.
            RenditionDefinition doclib = loadAndValidateRenditionDefinition("doclib");
            // We want the rendition to be asynchronous, but we don't care about called-back data.
            RenderCallback dummyCallback = new RenderCallback() {

                @Override
                public void handleFailedRendition(Throwable t) {
                }

                @Override
                public void handleSuccessfulRendition(ChildAssociationRef primaryParentOfNewRendition) {
                }
            };
            renditionService.render(nodeWithDocContent, doclib, dummyCallback);
            return null;
        }
    }, false, true);
    // Now wait for the actionService thread to complete the rendering.
    Thread.sleep(5000);
    // The rendition should have succeeded.
    transactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            List<ChildAssociationRef> renditions = renditionService.getRenditions(nodeWithDocContent);
            assertNotNull("Renditions missing.", renditions);
            assertEquals("Wrong rendition count", 1, renditions.size());
            return null;
        }
    }, false, true);
    // Now update the content to a corrupt PDF file that cannot be rendered.
    transactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            ContentWriter writer = contentService.getWriter(nodeWithDocContent, ContentModel.PROP_CONTENT, true);
            writer.setMimetype(MimetypeMap.MIMETYPE_PDF);
            writer.setEncoding("UTF-8");
            InputStream corruptIn = applicationContext.getResource("classpath:/quick/quickCorrupt.pdf").getInputStream();
            writer.putContent(corruptIn);
            corruptIn.close();
            return null;
        }
    }, false, true);
    // Now wait for the actionService thread to complete the rendition removal.
    Thread.sleep(5000);
    transactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            List<ChildAssociationRef> renditions = renditionService.getRenditions(nodeWithDocContent);
            assertNotNull("Renditions missing.", renditions);
            assertEquals("Wrong rendition count", 0, renditions.size());
            return null;
        }
    }, false, true);
}
Also used : ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) InputStream(java.io.InputStream) List(java.util.List) ArrayList(java.util.ArrayList) RenderCallback(org.alfresco.service.cmr.rendition.RenderCallback) RenditionDefinition(org.alfresco.service.cmr.rendition.RenditionDefinition) CompositeRenditionDefinition(org.alfresco.service.cmr.rendition.CompositeRenditionDefinition) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) BaseAlfrescoSpringTest(org.alfresco.util.BaseAlfrescoSpringTest) Test(org.junit.Test) AbstractContentTransformerTest(org.alfresco.repo.content.transform.AbstractContentTransformerTest)

Example 13 with RenditionDefinition

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

the class RenditionServiceIntegrationTest method renderPdfDocumentLongRunningTest.

protected void renderPdfDocumentLongRunningTest(AbstractNodeModifyingRunnable nodeModifyingRunnable, boolean joinNodeModifyingThread) throws Exception {
    // Spawn a new thread that waits a bit then tries to modify the node
    Thread nodeModifyingThread = new Thread(nodeModifyingRunnable);
    nodeModifyingThread.start();
    // Start the long running rendition
    try {
        this.renditionNode = transactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<NodeRef>() {

            public NodeRef execute() throws Throwable {
                // Initially the node that provides the content
                // should not have the rn:renditioned aspect on it.
                assertFalse("Source node has unexpected renditioned aspect.", nodeService.hasAspect(nodeWithDocContent, RenditionModel.ASPECT_RENDITIONED));
                RenditionDefinition action = makeReformatAction(null, TEST_LONG_RUNNING_MIME_TYPE);
                // Cancellation is only possible with tracking enabled
                action.setTrackStatus(true);
                // Render the content and put the result underneath the content node
                ChildAssociationRef renditionAssoc = renditionService.render(nodeWithDocContent, action);
                // Now the source content node should have the renditioned aspect
                assertTrue("Source node is missing renditioned aspect.", nodeService.hasAspect(nodeWithDocContent, RenditionModel.ASPECT_RENDITIONED));
                return renditionAssoc.getChildRef();
            }
        }, false, true);
    } catch (RenditionCancelledException e) {
    // expected cancellation exception
    }
    // Give a moment for roll back of rendition and commit of node modification to occur
    Thread.sleep(3000);
    if (joinNodeModifyingThread) {
        nodeModifyingThread.join();
    }
    // be false if there was a failure
    if (nodeModifyingRunnable.getModificationException() != null) {
        throw new RuntimeException("Modification of node during long running rendition failed", nodeModifyingRunnable.getModificationException());
    }
}
Also used : RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) RenditionCancelledException(org.alfresco.service.cmr.rendition.RenditionCancelledException) RenditionDefinition(org.alfresco.service.cmr.rendition.RenditionDefinition) CompositeRenditionDefinition(org.alfresco.service.cmr.rendition.CompositeRenditionDefinition) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef)

Example 14 with RenditionDefinition

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

the class RenditionServiceIntegrationTest method loadAndValidateRenditionDefinition.

private RenditionDefinition loadAndValidateRenditionDefinition(String renditionLocalName) {
    QName renditionQName = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, renditionLocalName);
    RenditionDefinition renditionDefinition = renditionService.loadRenditionDefinition(renditionQName);
    assertNotNull("'" + renditionLocalName + "' rendition definition was missing.", renditionDefinition);
    assertEquals("'" + renditionLocalName + "' renditionDefinition had wrong renditionName", renditionQName, renditionDefinition.getRenditionName());
    assertNotNull("'" + renditionLocalName + "' renditionDefinition had null " + RenditionDefinitionImpl.RENDITION_DEFINITION_NAME + " parameter", renditionDefinition.getParameterValue(RenditionDefinitionImpl.RENDITION_DEFINITION_NAME));
    // All builtin renditions should be "runas" system
    assertEquals(AuthenticationUtil.getSystemUserName(), renditionDefinition.getParameterValue(AbstractRenderingEngine.PARAM_RUN_AS));
    return renditionDefinition;
}
Also used : QName(org.alfresco.service.namespace.QName) RenditionDefinition(org.alfresco.service.cmr.rendition.RenditionDefinition) CompositeRenditionDefinition(org.alfresco.service.cmr.rendition.CompositeRenditionDefinition)

Example 15 with RenditionDefinition

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

the class RenditionServiceIntegrationTest method testRenderFreemarkerTemplatePath.

@Test
public void testRenderFreemarkerTemplatePath() throws Exception {
    // TODO displayName paths.
    final QName renditionName1 = QName.createQName(NamespaceService.RENDITION_MODEL_1_0_URI, FreemarkerRenderingEngine.NAME + "_UpdateOnAnyPropChange");
    final QName renditionName2 = QName.createQName(NamespaceService.RENDITION_MODEL_1_0_URI, FreemarkerRenderingEngine.NAME + "_UpdateOnContentPropChange");
    final Pair<NodeRef, NodeRef> renditions = transactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Pair<NodeRef, NodeRef>>() {

        public Pair<NodeRef, NodeRef> execute() throws Throwable {
            ChildAssociationRef parentAssoc = nodeService.getPrimaryParent(nodeWithFreeMarkerContent);
            QName assocName = parentAssoc.getQName();
            String templatePath = "/app:company_home/" + assocName.toPrefixString(namespaceService);
            // create test model 1 - rendition to update on any property change
            RenditionDefinition definition1 = renditionService.createRenditionDefinition(renditionName1, FreemarkerRenderingEngine.NAME);
            definition1.setParameterValue(FreemarkerRenderingEngine.PARAM_TEMPLATE_PATH, templatePath);
            definition1.setParameterValue(AbstractRenderingEngine.PARAM_UPDATE_RENDITIONS_ON_ANY_PROPERTY_CHANGE, Boolean.TRUE);
            // create test model 2 - rendition to update on content property change
            RenditionDefinition definition2 = renditionService.createRenditionDefinition(renditionName2, FreemarkerRenderingEngine.NAME);
            definition2.setParameterValue(FreemarkerRenderingEngine.PARAM_TEMPLATE_PATH, templatePath);
            definition2.setParameterValue(AbstractRenderingEngine.PARAM_UPDATE_RENDITIONS_ON_ANY_PROPERTY_CHANGE, Boolean.FALSE);
            // for automatic update.
            if (null == renditionService.loadRenditionDefinition(renditionName1)) {
                renditionService.saveRenditionDefinition(definition1);
            }
            if (null == renditionService.loadRenditionDefinition(renditionName2)) {
                renditionService.saveRenditionDefinition(definition2);
            }
            ChildAssociationRef renditionAssoc1 = renditionService.render(nodeWithDocContent, definition1);
            assertNotNull("The rendition association was null", renditionAssoc1);
            ChildAssociationRef renditionAssoc2 = renditionService.render(nodeWithDocContent, definition2);
            assertNotNull("The rendition association was null", renditionAssoc2);
            Pair<NodeRef, NodeRef> result = new Pair<NodeRef, NodeRef>(renditionAssoc1.getChildRef(), renditionAssoc2.getChildRef());
            return result;
        }
    });
    final String titleInitialValue = "Original test title";
    transactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            assertEquals("The source node should have title " + titleInitialValue, titleInitialValue, nodeService.getProperty(nodeWithDocContent, ContentModel.PROP_TITLE));
            String output1 = readTextContent(renditions.getFirst());
            assertNotNull("The rendition content was null.", output1);
            assertRenditionContainsTitle(titleInitialValue, output1);
            // check the output contains root node Id as expected.
            assertTrue(output1.contains(nodeWithDocContent.getId()));
            String output2 = readTextContent(renditions.getSecond());
            assertNotNull("The rendition content was null.", output2);
            assertRenditionContainsTitle(titleInitialValue, output2);
            return null;
        }
    });
    // Now change some properties on the source node and ensure that the renditions
    // are updated appropriately
    final String updatedTitle = "updatedTitle";
    transactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            nodeService.setProperty(nodeWithDocContent, ContentModel.PROP_TITLE, updatedTitle);
            return null;
        }
    });
    RetryingTransactionCallback<Void> checkRenditionCallback = new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            String output1 = readTextContent(renditions.getFirst());
            assertNotNull("The rendition content was null.", output1);
            assertRenditionContainsTitle(updatedTitle, output1);
            String output2 = readTextContent(renditions.getSecond());
            assertNotNull("The rendition content was null.", output2);
            assertRenditionContainsTitle(titleInitialValue, output2);
            return null;
        }
    };
    // Sleep to let the asynchronous action queue perform the updates to the renditions.
    int count = 0;
    while (count < 60) {
        count++;
        Thread.sleep(1000L);
        // Get the renditions and check their content for the new title
        try {
            transactionHelper.doInTransaction(checkRenditionCallback);
            // Success
            break;
        } catch (Exception e) {
            // Failure
            if (count > 60) {
                throw e;
            }
        }
    }
    transactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            renditionService.render(nodeWithDocContent, renditionName1, new RenderCallback() {

                @Override
                public void handleSuccessfulRendition(ChildAssociationRef primaryParentOfNewRendition) {
                    assertNotNull("The rendition association was null", primaryParentOfNewRendition);
                }

                @Override
                public void handleFailedRendition(Throwable t) {
                    fail("No error should be thrown: " + t.getMessage());
                }
            });
            return null;
        }
    });
}
Also used : RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) QName(org.alfresco.service.namespace.QName) RenderCallback(org.alfresco.service.cmr.rendition.RenderCallback) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) UnimportantTransformException(org.alfresco.repo.content.transform.UnimportantTransformException) RenditionCancelledException(org.alfresco.service.cmr.rendition.RenditionCancelledException) RenditionServiceException(org.alfresco.service.cmr.rendition.RenditionServiceException) IOException(java.io.IOException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) RenditionDefinition(org.alfresco.service.cmr.rendition.RenditionDefinition) CompositeRenditionDefinition(org.alfresco.service.cmr.rendition.CompositeRenditionDefinition) Pair(org.alfresco.util.Pair) BaseAlfrescoSpringTest(org.alfresco.util.BaseAlfrescoSpringTest) Test(org.junit.Test) AbstractContentTransformerTest(org.alfresco.repo.content.transform.AbstractContentTransformerTest)

Aggregations

RenditionDefinition (org.alfresco.service.cmr.rendition.RenditionDefinition)58 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)32 QName (org.alfresco.service.namespace.QName)28 Test (org.junit.Test)25 CompositeRenditionDefinition (org.alfresco.service.cmr.rendition.CompositeRenditionDefinition)22 BaseAlfrescoSpringTest (org.alfresco.util.BaseAlfrescoSpringTest)21 AbstractContentTransformerTest (org.alfresco.repo.content.transform.AbstractContentTransformerTest)14 NodeRef (org.alfresco.service.cmr.repository.NodeRef)13 Serializable (java.io.Serializable)11 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)11 RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)9 RenditionServiceException (org.alfresco.service.cmr.rendition.RenditionServiceException)9 ArrayList (java.util.ArrayList)6 ContentReader (org.alfresco.service.cmr.repository.ContentReader)6 AuthenticationUtil (org.alfresco.repo.security.authentication.AuthenticationUtil)5 FileInfo (org.alfresco.service.cmr.model.FileInfo)5 HashMap (java.util.HashMap)3 RenderingContext (org.alfresco.repo.rendition.executer.AbstractRenderingEngine.RenderingContext)3 List (java.util.List)2 ImageTransformationOptions (org.alfresco.repo.content.transform.magick.ImageTransformationOptions)2