Search in sources :

Example 6 with RenditionServiceException

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);
}
Also used : ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) RenditionServiceException(org.alfresco.service.cmr.rendition.RenditionServiceException)

Example 7 with RenditionServiceException

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);
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) QName(org.alfresco.service.namespace.QName) ThumbnailException(org.alfresco.service.cmr.thumbnail.ThumbnailException) RenditionDefinition(org.alfresco.service.cmr.rendition.RenditionDefinition) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) RenditionServiceException(org.alfresco.service.cmr.rendition.RenditionServiceException)

Example 8 with RenditionServiceException

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);
}
Also used : ContentReader(org.alfresco.service.cmr.repository.ContentReader) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) MediaType(org.apache.tika.mime.MediaType) RenditionServiceException(org.alfresco.service.cmr.rendition.RenditionServiceException)

Example 9 with RenditionServiceException

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("&#13;", "");
    }
    // Save it
    ContentWriter contentWriter = context.makeContentWriter();
    contentWriter.setMimetype("text/html");
    contentWriter.putContent(html);
}
Also used : ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) StringWriter(java.io.StringWriter) ContentReader(org.alfresco.service.cmr.repository.ContentReader) Metadata(org.apache.tika.metadata.Metadata) ParseContext(org.apache.tika.parser.ParseContext) RenditionServiceException(org.alfresco.service.cmr.rendition.RenditionServiceException) BodyContentHandler(org.apache.tika.sax.BodyContentHandler) ContentHandler(org.xml.sax.ContentHandler) TikaException(org.apache.tika.exception.TikaException) RenditionServiceException(org.alfresco.service.cmr.rendition.RenditionServiceException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 10 with RenditionServiceException

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;
}
Also used : Serializable(java.io.Serializable) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) RenditionServiceException(org.alfresco.service.cmr.rendition.RenditionServiceException) Date(java.util.Date) FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) RenditionServiceException(org.alfresco.service.cmr.rendition.RenditionServiceException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ArrayList(java.util.ArrayList) List(java.util.List) TemplateProcessorMethod(org.alfresco.repo.template.TemplateProcessorMethod) XSLTemplateModel(org.alfresco.repo.template.XSLTemplateModel) Map(java.util.Map)

Aggregations

RenditionServiceException (org.alfresco.service.cmr.rendition.RenditionServiceException)20 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)8 NodeRef (org.alfresco.service.cmr.repository.NodeRef)8 RenditionDefinition (org.alfresco.service.cmr.rendition.RenditionDefinition)7 CompositeRenditionDefinition (org.alfresco.service.cmr.rendition.CompositeRenditionDefinition)5 QName (org.alfresco.service.namespace.QName)5 ContentReader (org.alfresco.service.cmr.repository.ContentReader)3 IOException (java.io.IOException)2 Serializable (java.io.Serializable)2 Date (java.util.Date)2 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)2 AbstractContentTransformerTest (org.alfresco.repo.content.transform.AbstractContentTransformerTest)2 RenderingContext (org.alfresco.repo.rendition.executer.AbstractRenderingEngine.RenderingContext)2 AuthenticationUtil (org.alfresco.repo.security.authentication.AuthenticationUtil)2 RenditionPreventedException (org.alfresco.service.cmr.rendition.RenditionPreventedException)2 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)2 NodeService (org.alfresco.service.cmr.repository.NodeService)2 BaseAlfrescoSpringTest (org.alfresco.util.BaseAlfrescoSpringTest)2 BodyContentHandler (org.apache.tika.sax.BodyContentHandler)2 Test (org.junit.Test)2