Search in sources :

Example 1 with ContentTransformer

use of org.alfresco.repo.content.transform.ContentTransformer in project alfresco-remote-api by Alfresco.

the class NodeContentGet method execute.

/**
 * @param req WebScriptRequest
 * @param res WebScriptResponse
 * @throws IOException
 */
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {
    ContentReader textReader = null;
    Exception transformException = null;
    String nodeIDString = req.getParameter("nodeId");
    if (nodeIDString == null) {
        throw new WebScriptException("nodeID parameter is required for GetNodeContent");
    }
    long nodeId = Long.valueOf(nodeIDString).longValue();
    String propertyQName = req.getParameter("propertyQName");
    QName propertyName = null;
    if (propertyQName == null) {
        propertyName = ContentModel.PROP_CONTENT;
    } else {
        propertyName = QName.createQName(propertyQName);
    }
    Pair<Long, NodeRef> pair = nodeDAO.getNodePair(nodeId);
    if (pair == null) {
        // If the node does not exists we treat it as if it has no content
        // We could be trying to update the content of a node in the index that has been deleted.
        res.setStatus(HttpStatus.SC_NO_CONTENT);
        return;
    }
    NodeRef nodeRef = pair.getSecond();
    // check If-Modified-Since header and set Last-Modified header as appropriate
    Date modified = (Date) nodeService.getProperty(nodeRef, ContentModel.PROP_MODIFIED);
    // May be null - if so treat as just changed
    if (modified == null) {
        modified = new Date();
    }
    long modifiedSince = -1;
    String modifiedSinceStr = req.getHeader("If-Modified-Since");
    if (modifiedSinceStr != null) {
        try {
            modifiedSince = dateFormat.parse(modifiedSinceStr).getTime();
        } catch (Throwable e) {
            if (logger.isWarnEnabled()) {
                logger.warn("Browser sent badly-formatted If-Modified-Since header: " + modifiedSinceStr);
            }
        }
        if (modifiedSince > 0L) {
            // round the date to the ignore millisecond value which is not supplied by header
            long modDate = (modified.getTime() / 1000L) * 1000L;
            if (modDate <= modifiedSince) {
                res.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                return;
            }
        }
    }
    ContentReader reader = contentService.getReader(nodeRef, propertyName);
    if (reader == null) {
        res.setStatus(HttpStatus.SC_NO_CONTENT);
        return;
    }
    try {
        // get the transformer
        TransformationOptions options = new TransformationOptions();
        options.setUse("index");
        options.setSourceNodeRef(nodeRef);
        transformerDebug.pushAvailable(reader.getContentUrl(), reader.getMimetype(), MimetypeMap.MIMETYPE_TEXT_PLAIN, options);
        long sourceSize = reader.getSize();
        List<ContentTransformer> transformers = contentService.getActiveTransformers(reader.getMimetype(), sourceSize, MimetypeMap.MIMETYPE_TEXT_PLAIN, options);
        transformerDebug.availableTransformers(transformers, sourceSize, options, "SolrIndexer");
        if (transformers.isEmpty()) {
            res.setHeader(TRANSFORM_STATUS_HEADER, "noTransform");
            res.setStatus(HttpStatus.SC_NO_CONTENT);
            return;
        }
        ContentTransformer transformer = transformers.get(0);
        // Perform transformation catering for mimetype AND encoding
        ContentWriter writer = contentService.getTempWriter();
        writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
        // Expect transformers to produce UTF-8
        writer.setEncoding("UTF-8");
        try {
            long start = System.currentTimeMillis();
            transformer.transform(reader, writer, options);
            long transformDuration = System.currentTimeMillis() - start;
            res.setHeader(TRANSFORM_DURATION_HEADER, String.valueOf(transformDuration));
        } catch (ContentIOException | UnsupportedTransformationException e) {
            transformException = e;
        }
        if (transformException == null) {
            // point the reader to the new-written content
            textReader = writer.getReader();
            // Check that the reader is a view onto something concrete
            if (textReader == null || !textReader.exists()) {
                transformException = new ContentIOException("The transformation did not write any content, yet: \n" + "   transformer:     " + transformer + "\n" + "   temp writer:     " + writer);
            }
        }
        if (transformException != null) {
            res.setHeader(TRANSFORM_STATUS_HEADER, "transformFailed");
            res.setHeader(TRANSFORM_EXCEPTION_HEADER, transformException.getMessage());
            res.setStatus(HttpStatus.SC_NO_CONTENT);
        } else {
            res.setStatus(HttpStatus.SC_OK);
            streamContentImpl(req, res, textReader, null, null, false, modified, String.valueOf(modified.getTime()), null, null);
        }
    } finally {
        transformerDebug.popAvailable();
    }
}
Also used : UnsupportedTransformationException(org.alfresco.repo.content.transform.UnsupportedTransformationException) QName(org.alfresco.service.namespace.QName) ContentReader(org.alfresco.service.cmr.repository.ContentReader) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) IOException(java.io.IOException) UnsupportedTransformationException(org.alfresco.repo.content.transform.UnsupportedTransformationException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) Date(java.util.Date) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) TransformationOptions(org.alfresco.service.cmr.repository.TransformationOptions) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) ContentTransformer(org.alfresco.repo.content.transform.ContentTransformer)

Example 2 with ContentTransformer

use of org.alfresco.repo.content.transform.ContentTransformer in project alfresco-remote-api by Alfresco.

the class RenditionsTest method isOpenOfficeAvailable.

/**
 * Returns <code>true</code> if OpenOffice-based transformations are currently known to
 * be available, else <code>false</code>.
 */
protected boolean isOpenOfficeAvailable() {
    ContentTransformer transformer = contentService.getTransformer(null, MimetypeMap.MIMETYPE_WORD, -1, MimetypeMap.MIMETYPE_PDF, new TransformationOptions());
    // A transformer may not be returned here if it is unavailable.
    if (transformer == null) {
        return false;
    }
    // Maybe it's non-null, but not available.
    boolean isTransformable = transformer.isTransformable(MimetypeMap.MIMETYPE_WORD, -1, MimetypeMap.MIMETYPE_PDF, null);
    return isTransformable;
}
Also used : ContentTransformer(org.alfresco.repo.content.transform.ContentTransformer) TransformationOptions(org.alfresco.service.cmr.repository.TransformationOptions)

Aggregations

ContentTransformer (org.alfresco.repo.content.transform.ContentTransformer)2 TransformationOptions (org.alfresco.service.cmr.repository.TransformationOptions)2 IOException (java.io.IOException)1 Date (java.util.Date)1 UnsupportedTransformationException (org.alfresco.repo.content.transform.UnsupportedTransformationException)1 ContentIOException (org.alfresco.service.cmr.repository.ContentIOException)1 ContentReader (org.alfresco.service.cmr.repository.ContentReader)1 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 QName (org.alfresco.service.namespace.QName)1 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)1