Search in sources :

Example 1 with EmptyContentReader

use of org.alfresco.repo.content.EmptyContentReader in project alfresco-repository by Alfresco.

the class FileContentStore method getReader.

/**
 * This implementation requires that the URL start with
 * {@link FileContentStore#STORE_PROTOCOL } or {@link FileContentStore#SPOOF_PROTOCOL }
 */
public ContentReader getReader(String contentUrl) {
    // Handle the spoofed URL
    if (contentUrl.startsWith(SPOOF_PROTOCOL)) {
        return new SpoofedTextContentReader(contentUrl);
    }
    // else, it's a real file we are after
    try {
        File file = makeFile(contentUrl);
        ContentReader reader = null;
        if (file.exists()) {
            FileContentReader fileContentReader = new FileContentReader(file, contentUrl);
            fileContentReader.setAllowRandomAccess(allowRandomAccess);
            reader = fileContentReader;
        } else {
            reader = new EmptyContentReader(contentUrl);
        }
        // done
        if (logger.isDebugEnabled()) {
            logger.debug("Created content reader: \n" + "   url: " + contentUrl + "\n" + "   file: " + file + "\n" + "   reader: " + reader);
        }
        return reader;
    } catch (UnsupportedContentUrlException e) {
        // This can go out directly
        throw e;
    } catch (Throwable e) {
        throw new ContentIOException("Failed to get reader for URL: " + contentUrl, e);
    }
}
Also used : EmptyContentReader(org.alfresco.repo.content.EmptyContentReader) ContentReader(org.alfresco.service.cmr.repository.ContentReader) File(java.io.File) EmptyContentReader(org.alfresco.repo.content.EmptyContentReader) UnsupportedContentUrlException(org.alfresco.repo.content.UnsupportedContentUrlException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException)

Example 2 with EmptyContentReader

use of org.alfresco.repo.content.EmptyContentReader 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");
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) EmptyContentReader(org.alfresco.repo.content.EmptyContentReader) ContentReader(org.alfresco.service.cmr.repository.ContentReader) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) MimetypeMapTest(org.alfresco.repo.content.MimetypeMapTest) EmptyContentReader(org.alfresco.repo.content.EmptyContentReader) NoTransformerException(org.alfresco.service.cmr.repository.NoTransformerException) TransformationOptions(org.alfresco.service.cmr.repository.TransformationOptions) MimetypeMapTest(org.alfresco.repo.content.MimetypeMapTest) Test(org.junit.Test)

Example 3 with EmptyContentReader

use of org.alfresco.repo.content.EmptyContentReader in project alfresco-repository by Alfresco.

the class DictionaryRepositoryBootstrap method createM2Model.

/**
 * Create a M2Model from a dictionary model node
 *
 * @param nodeRef   the dictionary model node reference
 * @return          the M2Model
 */
public M2Model createM2Model(NodeRef nodeRef) {
    M2Model model = null;
    ContentReader contentReader = this.contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
    if (contentReader != null) {
        if (contentReader instanceof EmptyContentReader) {
            // belts-and-braces
            logger.error("Failed to create model (due to EmptyContentReader): " + nodeRef);
        } else {
            InputStream is = null;
            try {
                is = contentReader.getContentInputStream();
                model = M2Model.createModel(is);
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        logger.error("Failed to close input stream for " + nodeRef);
                    }
                }
            }
        }
    }
    // TODO should we inactivate the model node and put the error somewhere??
    return model;
}
Also used : InputStream(java.io.InputStream) EmptyContentReader(org.alfresco.repo.content.EmptyContentReader) ContentReader(org.alfresco.service.cmr.repository.ContentReader) IOException(java.io.IOException) EmptyContentReader(org.alfresco.repo.content.EmptyContentReader)

Aggregations

EmptyContentReader (org.alfresco.repo.content.EmptyContentReader)3 ContentReader (org.alfresco.service.cmr.repository.ContentReader)3 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 MimetypeMapTest (org.alfresco.repo.content.MimetypeMapTest)1 UnsupportedContentUrlException (org.alfresco.repo.content.UnsupportedContentUrlException)1 ContentIOException (org.alfresco.service.cmr.repository.ContentIOException)1 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)1 NoTransformerException (org.alfresco.service.cmr.repository.NoTransformerException)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 TransformationOptions (org.alfresco.service.cmr.repository.TransformationOptions)1 Test (org.junit.Test)1