Java Code Examples for org.alfresco.service.cmr.repository.ContentReader#setEncoding()

The following examples show how to use org.alfresco.service.cmr.repository.ContentReader#setEncoding() . 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: AbstractContentTransformerTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Writes the supplied text out to a temporary file, and opens
 *  a content reader onto it. 
 */
protected static ContentReader buildContentReader(String text, Charset encoding)
    throws IOException
{
    File tmpFile = TempFileProvider.createTempFile("AlfrescoTest_", ".txt");
    FileOutputStream out = new FileOutputStream(tmpFile);
    OutputStreamWriter wout = new OutputStreamWriter(out, encoding);
    wout.write(text);
    wout.close();
    out.close();
    
    ContentReader reader = new FileContentReader(tmpFile);
    reader.setEncoding(encoding.displayName());
    reader.setMimetype("text/plain");
    return reader;
}
 
Example 2
Source File: MediaWikiContentTransformerTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testMediaWikiToHTML() throws Exception
{
   File input = TempFileProvider.createTempFile("mediaWikiTest", ".mw");
   FileOutputStream fos = new FileOutputStream(input);
   fos.write(WIKI_TEXT.getBytes());
   fos.close();
   
   File output = TempFileProvider.createTempFile("mediaWikiTest", ".htm");
   
   ContentReader contentReader = new FileContentReader(input);
   contentReader.setMimetype(MimetypeMap.MIMETYPE_TEXT_MEDIAWIKI);
   contentReader.setEncoding("UTF-8");
   
   ContentWriter contentWriter = new FileContentWriter(output);
   contentWriter.setMimetype(MimetypeMap.MIMETYPE_HTML);
   contentWriter.setEncoding("UTF-8");
   
   transformer.transform(contentReader, contentWriter);
   
   String line = null;
   BufferedReader reader = new BufferedReader(new FileReader(output));
   while ((line = reader.readLine()) != null) 
   {
       System.out.println(line);
   }
}
 
Example 3
Source File: AbstractContentWriter.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Performs checks and copies required reader attributes
 */
public final ContentReader getReader() throws ContentIOException
{
    String contentUrl = getContentUrl();
    if (!isClosed())
    {
        return new EmptyContentReader(contentUrl);
    }
    ContentReader reader = createReader();
    if (reader == null)
    {
        throw new AlfrescoRuntimeException("ContentReader failed to create new reader: \n" +
                "   writer: " + this);
    }
    else if (reader.getContentUrl() == null || !reader.getContentUrl().equals(contentUrl))
    {
        throw new AlfrescoRuntimeException("ContentReader has different URL: \n" +
                "   writer: " + this + "\n" +
                "   new reader: " + reader);
    }
    // copy across common attributes
    reader.setMimetype(this.getMimetype());
    reader.setEncoding(this.getEncoding());
    reader.setLocale(this.getLocale());
    // done
    if (logger.isDebugEnabled())
    {
        logger.debug("Writer spawned new reader: \n" +
                "   writer: " + this + "\n" +
                "   new reader: " + reader);
    }
    return reader;
}
 
Example 4
Source File: AbstractContentReader.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Performs checks and copies required reader attributes
 */
public final ContentReader getReader() throws ContentIOException
{
    ContentReader reader = createReader();
    if (reader == null)
    {
        throw new AlfrescoRuntimeException("ContentReader failed to create new reader: \n" +
                "   reader: " + this);
    }
    else if (reader.getContentUrl() == null || !reader.getContentUrl().equals(getContentUrl()))
    {
        throw new AlfrescoRuntimeException("ContentReader has different URL: \n" +
                "   reader: " + this + "\n" +
                "   new reader: " + reader);
    }
    // copy across common attributes
    reader.setMimetype(this.getMimetype());
    reader.setEncoding(this.getEncoding());
    reader.setLocale(this.getLocale());
    // done
    if (logger.isDebugEnabled())
    {
        logger.debug("Reader spawned new reader: \n" +
                "   reader: " + this + "\n" +
                "   new reader: " + reader);
    }
    return reader;
}
 
Example 5
Source File: ContentServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** {@inheritDoc} */
public ContentReader getRawReader(String contentUrl)
{
    ContentReader reader = null;
    try
    {
        reader = store.getReader(contentUrl);
    }
    catch (UnsupportedContentUrlException e)
    {
        // The URL is not supported, so we spoof it
        reader = new EmptyContentReader(contentUrl);
    }
    if (reader == null)
    {
        throw new AlfrescoRuntimeException("ContentStore implementations may not return null ContentReaders");
    }
    // set extra data on the reader
    reader.setMimetype(MimetypeMap.MIMETYPE_BINARY);
    reader.setEncoding("UTF-8");
    reader.setLocale(I18NUtil.getLocale());
    
    // Done
    if (logger.isDebugEnabled())
    {
        logger.debug(
                "Direct request for reader: \n" +
                "   Content URL: " + contentUrl + "\n" +
                "   Reader:      " + reader);
    }
    return reader;
}
 
Example 6
Source File: ContentServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private ContentReader getReader(NodeRef nodeRef, QName propertyQName, boolean fireContentReadPolicy)
{
    ContentData contentData = getContentData(nodeRef, propertyQName);

    // check that the URL is available
    if (contentData == null || contentData.getContentUrl() == null)
    {
        // there is no URL - the interface specifies that this is not an error condition
        return null;                                
    }
    String contentUrl = contentData.getContentUrl();
    
    // The context of the read is entirely described by the URL
    ContentReader reader = store.getReader(contentUrl);
    if (reader == null)
    {
        throw new AlfrescoRuntimeException("ContentStore implementations may not return null ContentReaders");
    }
    
    // set extra data on the reader
    reader.setMimetype(contentData.getMimetype());
    reader.setEncoding(contentData.getEncoding());
    reader.setLocale(contentData.getLocale());
    
    // Fire the content read policy
    if (reader != null && fireContentReadPolicy == true)
    {
        // Fire the content update policy
        Set<QName> types = new HashSet<QName>(this.nodeService.getAspects(nodeRef));
        types.add(this.nodeService.getType(nodeRef));
        OnContentReadPolicy policy = this.onContentReadDelegate.get(nodeRef, types);
        policy.onContentRead(nodeRef);
    }
    
    // we don't listen for anything
    // result may be null - but interface contract says we may return null
    return reader;
}