Search in sources :

Example 1 with NoTransformerException

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

the class TransformActionExecuter method executeImpl.

/**
 * @see org.alfresco.repo.action.executer.ActionExecuterAbstractBase#executeImpl(Action, org.alfresco.service.cmr.repository.NodeRef)
 */
@Override
protected void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef) {
    if (this.nodeService.exists(actionedUponNodeRef) == false) {
        // node doesn't exist - can't do anything
        return;
    }
    // First check that the node is a sub-type of content
    QName typeQName = this.nodeService.getType(actionedUponNodeRef);
    if (this.dictionaryService.isSubClass(typeQName, ContentModel.TYPE_CONTENT) == false) {
        // it is not content, so can't transform
        return;
    }
    // Get the mime type
    String mimeType = (String) ruleAction.getParameterValue(PARAM_MIME_TYPE);
    // Get the content reader
    ContentReader contentReader = this.contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT);
    if (null == contentReader || !contentReader.exists()) {
        throw new RuleServiceException(CONTENT_READER_NOT_FOUND_MESSAGE);
    }
    TransformationOptions transformationOptions = newTransformationOptions(ruleAction, actionedUponNodeRef);
    // getExecuteAsynchronously() is not true for async convert content rules, so using Thread name
    // options.setUse(ruleAction.getExecuteAsynchronously() ? "asyncRule" :"syncRule");
    transformationOptions.setUse(Thread.currentThread().getName().contains("Async") ? "asyncRule" : "syncRule");
    String sourceMimetype = contentReader.getMimetype();
    long sourceSizeInBytes = contentReader.getSize();
    String contentUrl = contentReader.getContentUrl();
    Map<String, String> options = converter.getOptions(transformationOptions, sourceMimetype, mimeType);
    if (!synchronousTransformClient.isSupported(sourceMimetype, sourceSizeInBytes, contentUrl, mimeType, options, null, actionedUponNodeRef)) {
        String optionsString = TransformerDebug.toString(options);
        throw new RuleServiceException(String.format(TRANSFORMER_NOT_EXISTS_MESSAGE_PATTERN, sourceMimetype, mimeType, optionsString));
    }
    // Get the details of the copy destination
    NodeRef destinationParent = (NodeRef) ruleAction.getParameterValue(PARAM_DESTINATION_FOLDER);
    QName destinationAssocTypeQName = (QName) ruleAction.getParameterValue(PARAM_ASSOC_TYPE_QNAME);
    QName destinationAssocQName = (QName) ruleAction.getParameterValue(PARAM_ASSOC_QNAME);
    // default the assoc params if they're not present
    if (destinationAssocTypeQName == null) {
        destinationAssocTypeQName = ContentModel.ASSOC_CONTAINS;
    }
    if (destinationAssocQName == null) {
        destinationAssocQName = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "copy");
    }
    // Get the overwirte value
    boolean overwrite = true;
    Boolean overwriteValue = (Boolean) ruleAction.getParameterValue(PARAM_OVERWRITE_COPY);
    if (overwriteValue != null) {
        overwrite = overwriteValue.booleanValue();
    }
    // Calculate the destination name
    String originalName = (String) nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_NAME);
    String newName = transformName(this.mimetypeService, originalName, mimeType, true);
    // Since we are overwriting we need to figure out whether the destination node exists
    NodeRef copyNodeRef = null;
    if (overwrite == true) {
        // Try and find copies of the actioned upon node reference.
        // Include the parent folder because that's where the copy will be if this action
        // had done the first copy.
        PagingResults<CopyInfo> copies = copyService.getCopies(actionedUponNodeRef, destinationParent, new PagingRequest(1000));
        for (CopyInfo copyInfo : copies.getPage()) {
            NodeRef copy = copyInfo.getNodeRef();
            String copyName = copyInfo.getName();
            // We know that it is in the destination parent, but avoid working copies
            if (checkOutCheckInService.isWorkingCopy(copy)) {
                // It is a working copy
                continue;
            } else if (!newName.equals(copyName)) {
                // The copy's name is not what this action would have set it to
                continue;
            }
            if (copyNodeRef == null) {
                copyNodeRef = copy;
            } else {
                throw new RuleServiceException(ERR_OVERWRITE);
            }
        }
    }
    if (copyNodeRef == null) {
        // Copy the content node
        copyNodeRef = this.copyService.copy(actionedUponNodeRef, destinationParent, destinationAssocTypeQName, QName.createQName(destinationAssocQName.getNamespaceURI(), newName));
        // Adjust the name of the copy
        nodeService.setProperty(copyNodeRef, ContentModel.PROP_NAME, newName);
        String originalTitle = (String) nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_TITLE);
        if (originalTitle != null) {
            nodeService.setProperty(copyNodeRef, ContentModel.PROP_TITLE, originalTitle);
        }
    }
    // Only do the transformation if some content is available
    if (contentReader != null) {
        // get the writer and set it up
        ContentWriter contentWriter = this.contentService.getWriter(copyNodeRef, ContentModel.PROP_CONTENT, true);
        // new mimetype
        contentWriter.setMimetype(mimeType);
        // original encoding
        contentWriter.setEncoding(contentReader.getEncoding());
        // TODO: Check failure patterns for actions.
        try {
            doTransform(ruleAction, actionedUponNodeRef, contentReader, copyNodeRef, contentWriter);
            ruleAction.setParameterValue(PARAM_RESULT, copyNodeRef);
        } catch (NoTransformerException e) {
            if (logger.isDebugEnabled()) {
                logger.debug("No transformer found to execute rule: \n" + "   reader: " + contentReader + "\n" + "   writer: " + contentWriter + "\n" + "   action: " + this);
            }
            throw new RuleServiceException(TRANSFORMING_ERROR_MESSAGE + e.getMessage());
        }
    }
}
Also used : RuleServiceException(org.alfresco.service.cmr.rule.RuleServiceException) QName(org.alfresco.service.namespace.QName) ContentReader(org.alfresco.service.cmr.repository.ContentReader) CopyInfo(org.alfresco.service.cmr.repository.CopyService.CopyInfo) PagingRequest(org.alfresco.query.PagingRequest) NoTransformerException(org.alfresco.service.cmr.repository.NoTransformerException) TransformationOptions(org.alfresco.service.cmr.repository.TransformationOptions) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter)

Example 2 with NoTransformerException

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

the class ContentServiceImplTest method testTransformAndNulls.

@Test
@Deprecated
public void testTransformAndNulls() {
    NodeRef versionableNode = createNewVersionableNode();
    ContentReader contentReader = this.contentService.getReader(versionableNode, ContentModel.PROP_CONTENT);
    ContentWriter contentWriter = this.contentService.getWriter(versionableNode, ContentModel.PROP_CONTENT, false);
    // this.nodeService.setProperty(versionableNode, ContentModel.PROP_NAME, "for debugTransformers.txt");
    try {
        this.contentService.transform(new MimetypeMapTest.DummyContentReader(MimetypeMap.MIMETYPE_TEXT_PLAIN), new MimetypeMapTest.DummyContentWriter(MimetypeMap.MIMETYPE_IMAGE_PNG), new TransformationOptions(versionableNode, ContentModel.PROP_NAME, null, null));
    } catch (NoTransformerException nte) {
        nte.getMessage().contains("No transformation exists");
    }
    try {
        this.contentService.transform(null, null, new TransformationOptions());
        fail("Should throw exception");
    } catch (AlfrescoRuntimeException are) {
        are.getMessage().contains("The content reader must be set");
    }
    ContentReader empty = new EmptyContentReader("empty.txt");
    try {
        this.contentService.transform(empty, null, new TransformationOptions());
        fail("Should throw exception");
    } catch (AlfrescoRuntimeException are) {
        are.getMessage().contains("The content reader mimetype must be set");
    }
    try {
        contentWriter.setMimetype(null);
        this.contentService.transform(contentReader, contentWriter, new TransformationOptions());
        fail("Should throw exception");
    } catch (AlfrescoRuntimeException are) {
        are.getMessage().contains("The content writer mimetype must be set");
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) EmptyContentReader(org.alfresco.repo.content.EmptyContentReader) ContentReader(org.alfresco.service.cmr.repository.ContentReader) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) MimetypeMapTest(org.alfresco.repo.content.MimetypeMapTest) EmptyContentReader(org.alfresco.repo.content.EmptyContentReader) NoTransformerException(org.alfresco.service.cmr.repository.NoTransformerException) TransformationOptions(org.alfresco.service.cmr.repository.TransformationOptions) MimetypeMapTest(org.alfresco.repo.content.MimetypeMapTest) Test(org.junit.Test)

Example 3 with NoTransformerException

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

the class ContentTransformServiceImpl method transform.

@Deprecated
public void transform(ContentReader reader, ContentWriter writer, TransformationOptions options) throws NoTransformerException, ContentIOException {
    // check that source and target mimetypes are available
    if (reader == null) {
        throw new AlfrescoRuntimeException("The content reader must be set");
    }
    String sourceMimetype = reader.getMimetype();
    if (sourceMimetype == null) {
        throw new AlfrescoRuntimeException("The content reader mimetype must be set: " + reader);
    }
    String targetMimetype = writer.getMimetype();
    if (targetMimetype == null) {
        throw new AlfrescoRuntimeException("The content writer mimetype must be set: " + writer);
    }
    long sourceSize = reader.getSize();
    try {
        // look for a transformer
        transformerDebug.pushAvailable(reader.getContentUrl(), sourceMimetype, targetMimetype, options);
        List<ContentTransformer> transformers = getActiveTransformers(sourceMimetype, sourceSize, targetMimetype, options);
        transformerDebug.availableTransformers(transformers, sourceSize, options, "ContentService.transform(...)");
        int count = transformers.size();
        if (count == 0) {
            throw new NoTransformerException(sourceMimetype, targetMimetype);
        }
        if (count == 1 || !transformerFailover) {
            ContentTransformer transformer = transformers.size() == 0 ? null : transformers.get(0);
            transformer.transform(reader, writer, options);
        } else {
            failoverTransformers(reader, writer, options, targetMimetype, transformers);
        }
    } finally {
        if (transformerDebug.isEnabled()) {
            transformerDebug.popAvailable();
            debugTransformations(sourceMimetype, targetMimetype, sourceSize, options);
        }
    }
}
Also used : ContentTransformer(org.alfresco.repo.content.transform.ContentTransformer) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) NoTransformerException(org.alfresco.service.cmr.repository.NoTransformerException)

Example 4 with NoTransformerException

use of org.alfresco.service.cmr.repository.NoTransformerException in project acs-community-packaging by Alfresco.

the class Utils method addErrorMessage.

/**
 * Adds a global error message and logs exception details
 *
 * @param msg        The error message
 * @param err        The exception to log
 */
public static void addErrorMessage(String msg, Throwable err) {
    FacesContext context = FacesContext.getCurrentInstance();
    FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
    context.addMessage(null, facesMsg);
    if (err != null) {
        if ((err instanceof InvalidNodeRefException == false && err instanceof AccessDeniedException == false && err instanceof NoTransformerException == false) || logger.isDebugEnabled()) {
            logger.error(msg, err);
        }
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) FacesMessage(javax.faces.application.FacesMessage) NoTransformerException(org.alfresco.service.cmr.repository.NoTransformerException)

Aggregations

NoTransformerException (org.alfresco.service.cmr.repository.NoTransformerException)4 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)2 ContentReader (org.alfresco.service.cmr.repository.ContentReader)2 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 TransformationOptions (org.alfresco.service.cmr.repository.TransformationOptions)2 FacesMessage (javax.faces.application.FacesMessage)1 FacesContext (javax.faces.context.FacesContext)1 PagingRequest (org.alfresco.query.PagingRequest)1 EmptyContentReader (org.alfresco.repo.content.EmptyContentReader)1 MimetypeMapTest (org.alfresco.repo.content.MimetypeMapTest)1 ContentTransformer (org.alfresco.repo.content.transform.ContentTransformer)1 AccessDeniedException (org.alfresco.repo.security.permissions.AccessDeniedException)1 CopyInfo (org.alfresco.service.cmr.repository.CopyService.CopyInfo)1 InvalidNodeRefException (org.alfresco.service.cmr.repository.InvalidNodeRefException)1 RuleServiceException (org.alfresco.service.cmr.rule.RuleServiceException)1 QName (org.alfresco.service.namespace.QName)1 Test (org.junit.Test)1