Java Code Examples for org.alfresco.service.cmr.repository.ContentWriter#getMimetype()

The following examples show how to use org.alfresco.service.cmr.repository.ContentWriter#getMimetype() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileFolderServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public ContentWriter getWriter(NodeRef nodeRef)
{
    FileInfo fileInfo = toFileInfo(nodeRef, false);
    if (fileInfo.isFolder())
    {
        throw new InvalidTypeException("Unable to get a content writer for a folder: " + fileInfo);
    }
    final ContentWriter writer = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
    // Ensure that a mimetype is set based on the filename (ALF-6560)
    // This has been removed from the create code in 3.4 to prevent insert-update behaviour
    // of the ContentData.
    if (writer.getMimetype() == null)
    {
        final String name = fileInfo.getName();
        writer.guessMimetype(name);
    }
    // Done
    return writer;
}
 
Example 2
Source File: AbstractRemoteContentTransformer.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void transformInternal(ContentReader reader, ContentWriter writer, TransformationOptions options)
        throws Exception
{
    if (remoteTransformerClientConfigured())
    {
        String sourceMimetype = getMimetype(reader);
        String targetMimetype = writer.getMimetype();
        String targetEncoding = writer.getEncoding();

        MimetypeService mimetypeService = getMimetypeService();
        String sourceExtension = mimetypeService.getExtension(sourceMimetype);
        String targetExtension = mimetypeService.getExtension(targetMimetype);
        if (sourceExtension == null || targetExtension == null)
        {
            throw new AlfrescoRuntimeException("Unknown extensions for mimetypes: \n" +
                    "   source mimetype: " + sourceMimetype + "\n" +
                    "   source extension: " + sourceExtension + "\n" +
                    "   target mimetype: " + targetMimetype + "\n" +
                    "   target extension: " + targetExtension + "\n" +
                    "   target encoding: " + targetEncoding);
        }

        transformRemote(remoteTransformerClient, reader, writer, options, sourceMimetype, targetMimetype,
                sourceExtension, targetExtension, targetEncoding);
    }
    else
    {
        transformLocal(reader, writer, options);
    }

    Log logger = getLogger();
    if (logger.isDebugEnabled())
    {
        logger.debug("Transformation completed: \n" +
                "   source: " + reader + "\n" +
                "   target: " + writer + "\n" +
                "   options: " + options);
    }
}
 
Example 3
Source File: AbstractMappingMetadataExtracter.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Checks if embedding for the mimetype is supported.
 *
 * @param writer the writer to check
 * @throws AlfrescoRuntimeException if embedding for the mimetype is not supported
 */
protected void checkIsEmbedSupported(ContentWriter writer)
{
    String mimetype = writer.getMimetype();
    if (!isEmbeddingSupported(mimetype))
    {
        throw new AlfrescoRuntimeException(
                "Metadata extracter does not support embedding mimetype: \n" +
                "   writer: " + writer + "\n" +
                "   supported: " + supportedEmbedMimetypes + "\n" +
                "   extracter: " + this);
    }
}
 
Example 4
Source File: ContentTransformServiceAdaptor.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Deprecated
@Override
public boolean isTransformable(ContentReader reader, ContentWriter writer, TransformationOptions transformationOptions)
{
    String sourceMimetype = reader.getMimetype();
    long sourceSizeInBytes = reader.getSize();
    String contentUrl = reader.getContentUrl();
    String targetMimetype = writer.getMimetype();
    NodeRef sourceNodeRef = transformationOptions.getSourceNodeRef();
    Map<String, String> options = converter.getOptions(transformationOptions, null, null);
    return synchronousTransformClient.isSupported(sourceMimetype, sourceSizeInBytes, contentUrl, targetMimetype,
            options, null, sourceNodeRef);
}
 
Example 5
Source File: TestSynchronousTransformClient.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void transform(ContentReader reader, ContentWriter writer, Map<String, String> actualOptions, String transformName, NodeRef sourceNodeRef)
{
    String sourceMimetype = reader.getMimetype();
    String targetMimetype = writer.getMimetype();
    if (sourceMimetype.equals(TEST_FAILING_MIME_TYPE) || targetMimetype.equals(TEST_FAILING_MIME_TYPE))
    {
        throw new ContentServiceTransientException("Transformation intentionally failed for test purposes.");
    }
    else if (sourceMimetype.equals(TEST_LONG_RUNNING_MIME_TYPE) || targetMimetype.equals(TEST_LONG_RUNNING_MIME_TYPE))
    {
        try
        {
            Thread.sleep(TEST_LONG_RUNNING_TRANSFORM_TIME);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        writer.putContent("SUCCESS");
    }
    else if (sourceMimetype.equals(TEST_USER_MIME_TYPE) || targetMimetype.equals(TEST_USER_MIME_TYPE))
    {
        String username = AuthenticationUtil.getFullyAuthenticatedUser();
        if (!EXPECTED_USER.equals(username))
        {
            throw new ContentIOException(
                    "Expected username '" + EXPECTED_USER + "' but found '" + username + "'");
        }
        writer.putContent("SUCCESS");
    }
    else
    {
        delegate.transform(reader, writer, actualOptions, transformName, sourceNodeRef);
    }
}
 
Example 6
Source File: AbstractLocalTransform.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void transform(ContentReader reader, ContentWriter writer, Map<String, String> transformOptions,
                      String renditionName, NodeRef sourceNodeRef)
{
    if (isAvailable())
    {
        String sourceMimetype = reader.getMimetype();
        String targetMimetype = writer.getMimetype();

        String sourceExtension = mimetypeService.getExtension(sourceMimetype);
        String targetExtension = mimetypeService.getExtension(targetMimetype);
        if (sourceExtension == null || targetExtension == null)
        {
            throw new AlfrescoRuntimeException("Unknown extensions for mimetypes: \n" +
                    "   source mimetype: " + sourceMimetype + "\n" +
                    "   source extension: " + sourceExtension + "\n" +
                    "   target mimetype: " + targetMimetype + "\n" +
                    "   target extension: " + targetExtension);
        }

        transformOptions = getStrippedTransformOptions(transformOptions);
        transformWithDebug(reader, writer, transformOptions, renditionName, sourceNodeRef, sourceMimetype,
                targetMimetype, sourceExtension, targetExtension);

        if (log.isDebugEnabled())
        {
            log.debug("Local transformation completed: \n" +
                    "   source: " + reader + "\n" +
                    "   target: " + writer + "\n" +
                    "   options: " + transformOptions);
        }
    }
    else
    {
        if (log.isDebugEnabled())
        {
            transformOptions = getStrippedTransformOptions(transformOptions);
            log.debug("Local transformer not available: \n" +
                    "   source: " + reader + "\n" +
                    "   target: " + writer + "\n" +
                    "   options: " + transformOptions);
        }
    }
}
 
Example 7
Source File: OOXMLThumbnailContentTransformer.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
protected void transformInternal(ContentReader reader,
                                 ContentWriter writer,
                                 TransformationOptions options) throws Exception
{
    final String sourceMimetype = reader.getMimetype();
    final String sourceExtension = getMimetypeService().getExtension(sourceMimetype);
    final String targetMimetype = writer.getMimetype();
    
    
    if (log.isDebugEnabled())
    {
        StringBuilder msg = new StringBuilder();
        msg.append("Transforming from ").append(sourceMimetype)
           .append(" to ").append(targetMimetype);
        log.debug(msg.toString());
    }
    
    
    OPCPackage pkg = null;
    try 
    {
        File ooxmlTempFile = TempFileProvider.createTempFile(this.getClass().getSimpleName() + "_ooxml", sourceExtension);
        reader.getContent(ooxmlTempFile);
        
        // Load the file
        pkg = OPCPackage.open(ooxmlTempFile.getPath());
        
        // Does it have a thumbnail?
        PackageRelationshipCollection rels = 
            pkg.getRelationshipsByType(PackageRelationshipTypes.THUMBNAIL);
        if (rels.size() > 0)
        {
            // Get the thumbnail part
            PackageRelationship tRel = rels.getRelationship(0);
            PackagePart tPart = pkg.getPart(tRel);
            
            // Write it to the target
            InputStream tStream = tPart.getInputStream();
            writer.putContent( tStream );
            tStream.close();
        }
        else
        {
            log.debug("No thumbnail present in " + reader.toString());
            throw new UnimportantTransformException(NO_THUMBNAIL_PRESENT_IN_FILE + targetMimetype);
        }
    } 
    catch (IOException e) 
    {
       throw new AlfrescoRuntimeException("Unable to transform " + sourceExtension + " file.", e);
    }
    finally
    {
        if (pkg != null)
        {
            pkg.close();
        }
    }
}
 
Example 8
Source File: ContentTransformServiceAdaptor.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
private NoTransformerException newNoTransformerException(ContentReader reader, ContentWriter writer)
{
    String sourceMimetype = reader == null ? "null" : reader.getMimetype();
    String targetMimetype = writer == null ? "null" : writer.getMimetype();
    return new NoTransformerException(sourceMimetype, targetMimetype);
}
 
Example 9
Source File: ContentTransformServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@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);
        }
    }
}
 
Example 10
Source File: ContentMetadataEmbedder.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @see org.alfresco.repo.action.executer.ActionExecuter#execute(Action, NodeRef)
 */
public void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef)
{
    if (!nodeService.exists(actionedUponNodeRef))
    {
        // Node is gone
        return;
    }
    ContentReader reader = contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT);
    // The reader may be null, e.g. for folders and the like
    if (reader == null || reader.getMimetype() == null)
    {
        if(logger.isDebugEnabled())
        {
            logger.debug("no content or mimetype - do nothing");
        }
        // No content to extract data from
        return;
    }
    String mimetype = reader.getMimetype();
    MetadataEmbedder embedder = metadataExtracterRegistry.getEmbedder(mimetype);
    if (embedder == null)
    {
        if(logger.isDebugEnabled())
        {
            logger.debug("no embedder for mimetype:" + mimetype);
        }
        // There is no embedder to use
        return;
    }
    
    ContentWriter writer = contentService.getWriter(actionedUponNodeRef, ContentModel.PROP_CONTENT, true);
    // The writer may be null, e.g. for folders and the like
    if (writer == null || writer.getMimetype() == null)
    {
        if(logger.isDebugEnabled())
        {
            logger.debug("no content or mimetype - do nothing");
        }
        // No content to embed data in
        return;
    }

    // Get all the node's properties
    Map<QName, Serializable> nodeProperties = nodeService.getProperties(actionedUponNodeRef);

    try
    {
        embedder.embed(nodeProperties, reader, writer);
    }
    catch (Throwable e)
    {
        // Extracters should attempt to handle all error conditions and embed
        // as much as they can.  If, however, one should fail, we don't want the
        // action itself to fail.  We absorb and report the exception here.
        if (logger.isDebugEnabled())
        {
            logger.debug(
                    "Metadata embedding failed: \n" +
                    "   Extracter: " + this + "\n" +
                    "   Node:      " + actionedUponNodeRef + "\n" +
                    "   Content:   " + writer,
                    e);
        }
        else
        {
            logger.warn(
                    "Metadata embedding failed (turn on DEBUG for full error): \n" +
                    "   Extracter: " + this + "\n" +
                    "   Node:      " + actionedUponNodeRef + "\n" +
                    "   Content:   " + writer + "\n" +
                    "   Failure:   " + e.getMessage());
        }
    }
}