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