use of org.alfresco.service.cmr.rendition.RenditionServiceException in project alfresco-repository by Alfresco.
the class RenditionNodeManager method orphanOldRendition.
/**
* This method performs the 'orphaning' of the oldRendition. It removes the rendition aspect(s) and removes
* the child-association linking the old rendition to its source node. The old rendition node is not deleted.
*
* @param renditionName the name of the rendition.
* @throws RenditionServiceException if there was not exactly one parent assoc from the oldRendition having the specified renditionName
* or if the matching parent assoc was not to the correct source node.
*/
private void orphanOldRendition(QNamePattern renditionName) {
// Get all parent assocs from the old rendition of the specified renditionName.
List<ChildAssociationRef> parents = nodeService.getParentAssocs(existingLinkedRendition, RenditionModel.ASSOC_RENDITION, renditionName);
// There should only be one matching assoc.
if (parents.size() == 1) {
ChildAssociationRef parentAssoc = parents.get(0);
if (parentAssoc.getParentRef().equals(sourceNode)) {
if (logger.isDebugEnabled()) {
logger.debug("Orphaning old rendition node " + existingLinkedRendition);
}
behaviourFilter.disableBehaviour(existingLinkedRendition, ContentModel.ASPECT_AUDITABLE);
behaviourFilter.disableBehaviour(sourceNode, ContentModel.ASPECT_AUDITABLE);
try {
nodeService.removeAspect(existingLinkedRendition, RenditionModel.ASPECT_HIDDEN_RENDITION);
nodeService.removeAspect(existingLinkedRendition, RenditionModel.ASPECT_VISIBLE_RENDITION);
nodeService.removeChildAssociation(parentAssoc);
} finally {
behaviourFilter.enableBehaviour(existingLinkedRendition, ContentModel.ASPECT_AUDITABLE);
behaviourFilter.enableBehaviour(sourceNode, ContentModel.ASPECT_AUDITABLE);
}
return;
}
}
String msg = "Node: " + existingLinkedRendition + " is not a rendition of type: " + renditionName + " for source node: " + sourceNode;
if (logger.isDebugEnabled()) {
logger.debug(msg);
}
throw new RenditionServiceException(msg);
}
use of org.alfresco.service.cmr.rendition.RenditionServiceException in project alfresco-repository by Alfresco.
the class ThumbnailServiceImpl method createThumbnailNode.
private NodeRef createThumbnailNode(final NodeRef node, final QName contentProperty, final String mimetype, final TransformationOptions transformationOptions, final String thumbnailName, final ThumbnailParentAssociationDetails assocDetails) {
// Get the name of the thumbnail and add to properties map
QName thumbnailQName = getThumbnailQName(thumbnailName);
RenditionDefinition definition = createRenditionDefinition(contentProperty, mimetype, transformationOptions, thumbnailQName, assocDetails);
try {
ChildAssociationRef thumbnailAssoc = renditionService.render(node, definition);
NodeRef thumbnail = getThumbnailNode(thumbnailAssoc);
setThumbnailNameProperty(thumbnail, thumbnailName);
return thumbnail;
} catch (RenditionServiceException rsx) {
throw new ThumbnailException(rsx.getMessage(), rsx);
}
}
use of org.alfresco.service.cmr.rendition.RenditionServiceException in project alfresco-repository by Alfresco.
the class HTMLRenderingEngine method render.
/*
* (non-Javadoc)
* @see org.alfresco.repo.rendition.executer.AbstractRenderingEngine#render(org.alfresco.repo.rendition.executer.AbstractRenderingEngine.RenderingContext)
*/
@Override
protected void render(RenderingContext context) {
ContentReader contentReader = context.makeContentReader();
String sourceMimeType = contentReader.getMimetype();
// Check that Tika supports the supplied file
AutoDetectParser p = new AutoDetectParser(tikaConfig);
MediaType sourceMediaType = MediaType.parse(sourceMimeType);
if (!p.getParsers().containsKey(sourceMediaType)) {
throw new RenditionServiceException("Source mime type of " + sourceMimeType + " is not supported by Tika for HTML conversions");
}
// Make the HTML Version using Tika
// This will also extract out any images as found
generateHTML(p, context);
}
use of org.alfresco.service.cmr.rendition.RenditionServiceException in project alfresco-repository by Alfresco.
the class HTMLRenderingEngine method generateHTML.
/**
* Asks Tika to translate the contents into HTML
*/
private void generateHTML(Parser p, RenderingContext context) {
ContentReader contentReader = context.makeContentReader();
// Setup things to parse with
StringWriter sw = new StringWriter();
ContentHandler handler = buildContentHandler(sw, context);
// Tell Tika what we're dealing with
Metadata metadata = new Metadata();
metadata.set(Metadata.CONTENT_TYPE, contentReader.getMimetype());
metadata.set(Metadata.RESOURCE_NAME_KEY, nodeService.getProperty(context.getSourceNode(), ContentModel.PROP_NAME).toString());
// Our parse context needs to extract images
ParseContext parseContext = new ParseContext();
parseContext.set(Parser.class, new TikaImageExtractingParser(context));
// Parse
try {
p.parse(contentReader.getContentInputStream(), handler, metadata, parseContext);
} catch (Exception e) {
throw new RenditionServiceException("Tika HTML Conversion Failed", e);
}
// As a string
String html = sw.toString();
// If we're doing body-only, remove all the html namespaces
// that will otherwise clutter up the document
boolean bodyOnly = context.getParamWithDefault(PARAM_BODY_CONTENTS_ONLY, false);
if (bodyOnly) {
html = html.replaceAll("<\\?xml.*?\\?>", "");
html = html.replaceAll("<p xmlns=\"http://www.w3.org/1999/xhtml\"", "<p");
html = html.replaceAll("<h(\\d) xmlns=\"http://www.w3.org/1999/xhtml\"", "<h\\1");
html = html.replaceAll("<div xmlns=\"http://www.w3.org/1999/xhtml\"", "<div");
html = html.replaceAll("<table xmlns=\"http://www.w3.org/1999/xhtml\"", "<table");
html = html.replaceAll(" ", "");
}
// Save it
ContentWriter contentWriter = context.makeContentWriter();
contentWriter.setMimetype("text/html");
contentWriter.putContent(html);
}
use of org.alfresco.service.cmr.rendition.RenditionServiceException in project alfresco-repository by Alfresco.
the class XSLTRenderingEngine method buildModel.
@SuppressWarnings({ "serial", "unchecked" })
protected Object buildModel(RenderingContext context) {
Map<String, Serializable> suppliedParams = context.getCheckedParam(PARAM_MODEL, Map.class);
final NodeRef sourceNode = context.getSourceNode();
final NodeRef parentNode = nodeService.getPrimaryParent(context.getSourceNode()).getParentRef();
final String sourcePath = getPath(sourceNode);
final String parentPath = getPath(parentNode);
XSLTemplateModel model = new XSLTemplateModel();
// add simple scalar parameters
model.put(QName.createQName(NamespaceService.ALFRESCO_URI, "date"), new Date());
model.put(QName.createQName(NamespaceService.ALFRESCO_PREFIX, "source_file_name", namespacePrefixResolver), nodeService.getProperty(context.getSourceNode(), ContentModel.PROP_NAME));
model.put(QName.createQName(NamespaceService.ALFRESCO_PREFIX, "source_path", namespacePrefixResolver), sourcePath);
model.put(QName.createQName(NamespaceService.ALFRESCO_PREFIX, "parent_path", namespacePrefixResolver), parentPath);
// add methods
model.put(QName.createQName(NamespaceService.ALFRESCO_PREFIX, "encodeQuotes", namespacePrefixResolver), new TemplateProcessorMethod() {
public Object exec(final Object[] arguments) throws IOException, SAXException {
if (arguments.length != 1) {
throw new IllegalArgumentException("expected 1 argument to encodeQuotes. got " + arguments.length);
}
if (!(arguments[0] instanceof String)) {
throw new ClassCastException("expected arguments[0] to be a " + String.class.getName() + ". got a " + arguments[0].getClass().getName() + ".");
}
String text = (String) arguments[0];
if (log.isDebugEnabled()) {
log.debug("tpm_encodeQuotes('" + text + "'), parentPath = " + parentPath);
}
final String result = xsltFunctions.encodeQuotes(text);
return result;
}
});
model.put(QName.createQName(NamespaceService.ALFRESCO_PREFIX, "parseXMLDocument", namespacePrefixResolver), new TemplateProcessorMethod() {
public Object exec(final Object[] arguments) throws IOException, SAXException {
if (arguments.length != 1) {
throw new IllegalArgumentException("expected 1 argument to parseXMLDocument. got " + arguments.length);
}
if (!(arguments[0] instanceof String)) {
throw new ClassCastException("expected arguments[0] to be a " + String.class.getName() + ". got a " + arguments[0].getClass().getName() + ".");
}
String path = (String) arguments[0];
if (log.isDebugEnabled()) {
log.debug("parseXMLDocument('" + path + "'), parentPath = " + parentPath);
}
Document d = null;
try {
d = xsltFunctions.parseXMLDocument(parentNode, path);
} catch (Exception ex) {
log.warn("Received an exception from parseXMLDocument()", ex);
}
return d == null ? null : d.getDocumentElement();
}
});
model.put(QName.createQName(NamespaceService.ALFRESCO_PREFIX, "parseXMLDocuments", namespacePrefixResolver), new TemplateProcessorMethod() {
public Object exec(final Object[] arguments) throws IOException, SAXException {
if (arguments.length > 2) {
throw new IllegalArgumentException("expected one or two arguments to " + "parseXMLDocuments. got " + arguments.length);
}
if (!(arguments[0] instanceof String)) {
throw new ClassCastException("expected arguments[0] to be a " + String.class.getName() + ". got a " + arguments[0].getClass().getName() + ".");
}
if (arguments.length == 2 && !(arguments[1] instanceof String)) {
throw new ClassCastException("expected arguments[1] to be a " + String.class.getName() + ". got a " + arguments[1].getClass().getName() + ".");
}
String path = arguments.length == 2 ? (String) arguments[1] : "";
final String typeName = (String) arguments[0];
if (log.isDebugEnabled()) {
log.debug("tpm_parseXMLDocuments('" + typeName + "','" + path + "'), parentPath = " + parentPath);
}
final Map<String, Document> resultMap = xsltFunctions.parseXMLDocuments(typeName, parentNode, path);
if (log.isDebugEnabled()) {
log.debug("received " + resultMap.size() + " documents in " + path + " with form name " + typeName);
}
// create a root document for rooting all the results. we do this
// so that each document root element has a common parent node
// and so that xpath axes work properly
final Document rootNodeDocument = XMLUtil.newDocument();
final Element rootNodeDocumentEl = rootNodeDocument.createElementNS(NamespaceService.ALFRESCO_URI, NamespaceService.ALFRESCO_PREFIX + ":file_list");
rootNodeDocumentEl.setAttribute("xmlns:" + NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI);
rootNodeDocument.appendChild(rootNodeDocumentEl);
final List<Node> result = new ArrayList<Node>(resultMap.size());
for (Map.Entry<String, Document> e : resultMap.entrySet()) {
final Element documentEl = e.getValue().getDocumentElement();
documentEl.setAttribute("xmlns:" + NamespaceService.ALFRESCO_PREFIX, NamespaceService.ALFRESCO_URI);
documentEl.setAttributeNS(NamespaceService.ALFRESCO_URI, NamespaceService.ALFRESCO_PREFIX + ":file_name", e.getKey());
final Node n = rootNodeDocument.importNode(documentEl, true);
rootNodeDocumentEl.appendChild(n);
result.add(n);
}
return result.toArray(new Node[result.size()]);
}
});
if (suppliedParams != null) {
for (Map.Entry<String, Serializable> suppliedParam : suppliedParams.entrySet()) {
model.put(QName.createQName(suppliedParam.getKey()), suppliedParam.getValue());
}
}
// add the xml document
try {
model.put(XSLTProcessor.ROOT_NAMESPACE, XMLUtil.parse(sourceNode, contentService));
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new RenditionServiceException("Failed to parse XML from source node.", ex);
}
return model;
}
Aggregations