Java Code Examples for org.alfresco.service.cmr.repository.ContentService.getReader()

The following are Jave code examples for showing how to use getReader() of the org.alfresco.service.cmr.repository.ContentService class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: alfresco-remote-api   File: MoveMethod.java   Source Code and License Vote up 7 votes
private void copyContentOnly(FileInfo sourceFileInfo, FileInfo destFileInfo, FileFolderService fileFolderService) throws WebDAVServerException
{
	ContentService contentService = getContentService();
    ContentReader reader = contentService.getReader(sourceFileInfo.getNodeRef(), ContentModel.PROP_CONTENT);
    if (reader == null)
    {
        // There is no content for the node if it is a folder
        if (!sourceFileInfo.isFolder())
        {
            // Non-folders should have content available.
            logger.error("Unable to get ContentReader for source node " + sourceFileInfo.getNodeRef());
            throw new WebDAVServerException(HttpServletResponse.SC_NOT_FOUND);
        }
    }
    else
    {
        ContentWriter contentWriter = contentService.getWriter(destFileInfo.getNodeRef(), ContentModel.PROP_CONTENT, true);
        contentWriter.putContent(reader);
    }
}
 
Example 2
Project: alfresco-repository   File: ScriptNode.java   Source Code and License Vote up 6 votes
public Reader getReader()
{
    ContentService contentService = services.getContentService();
    ContentReader reader = contentService.getReader(nodeRef, property);
    
    if (reader != null && reader.exists())
    {
        try
        {
            return (contentData.getEncoding() == null) ? new InputStreamReader(reader.getContentInputStream()) : new InputStreamReader(reader.getContentInputStream(), contentData.getEncoding());
        }
        catch (IOException e)
        {
            // NOTE: fall-through
        }
    }
    return null;
}
 
Example 3
Project: alfresco-repository   File: ScriptNode.java   Source Code and License Vote up 6 votes
/**
 * Guess the mimetype for the given filename - uses the extension to match on system mimetype map
 */
public void guessMimetype(String filename)
{
    ContentService contentService = services.getContentService();
    ContentReader reader = contentService.getReader(nodeRef, property); 
    // MNT-12265 Browser sets a mimetype based on extension of file. But mimeType from browser can be
    // different as mapped in Alfresco for current extension. Therefore we need to guess a mimetype based on
    // map in Alfresco
    String typeByExt = services.getMimetypeService().guessMimetype(filename);
    if (reader != null && reader.getMimetype() != null && !typeByExt.equals(MimetypeMap.MIMETYPE_BINARY))
    {
        setMimetype(typeByExt);
    }
    else
    {
        setMimetype(services.getMimetypeService().guessMimetype(filename, reader));
    }
}
 
Example 4
Project: alfresco-repository   File: ScriptNode.java   Source Code and License Vote up 5 votes
/**
 * Generic method to transform Node content from one mimetype to another.
 * 
 * @param transformer   The Transformer delegate supplying the transformation logic
 * @param mimetype      Mimetype of the destination content
 * @param destination   Destination folder location for the resulting document
 * 
 * @return Node representing the transformed content - or null if the transform failed
 */
private ScriptNode transformNode(Transformer transformer, String mimetype, NodeRef destination)
{
    ScriptNode transformedNode = null;
    
    // get the content reader
    ContentService contentService = this.services.getContentService();
    ContentReader reader = contentService.getReader(this.nodeRef, ContentModel.PROP_CONTENT);
    
    // only perform the transformation if some content is available
    if (reader != null)
    {
        // Copy the content node to a new node
        String copyName = TransformActionExecuter.transformName(this.services.getMimetypeService(), getName(),
                mimetype, true);
        NodeRef copyNodeRef = this.services.getCopyService().copy(this.nodeRef, destination,
                ContentModel.ASSOC_CONTAINS,
                QName.createQName(ContentModel.PROP_CONTENT.getNamespaceURI(), QName.createValidLocalName(copyName)),
                false);
        
        // modify the name of the copy to reflect the new mimetype
        this.nodeService.setProperty(copyNodeRef, ContentModel.PROP_NAME, copyName);
        
        // get the writer and set it up
        ContentWriter writer = contentService.getWriter(copyNodeRef, ContentModel.PROP_CONTENT, true);
        writer.setMimetype(mimetype); // new mimetype
        writer.setEncoding(reader.getEncoding()); // original encoding
        
        // Try and transform the content using the supplied delegate
        transformedNode = transformer.transform(contentService, copyNodeRef, reader, writer);
    }
    
    return transformedNode;
}
 
Example 5
Project: alfresco-repository   File: BaseContentNode.java   Source Code and License Vote up 5 votes
/**
 * @return the content stream
 */
public String getContent()
{
    ContentService contentService = services.getContentService();
    ContentReader reader = contentService.getReader(getNodeRef(), property);
    
    return (reader != null && reader.exists()) ? reader.getContentString() : "";
}
 
Example 6
Project: alfresco-repository   File: BaseContentNode.java   Source Code and License Vote up 5 votes
/**
 * @return the content stream to the specified maximum length in characters
 */
public String getContentMaxLength(int length)
{
    ContentService contentService = services.getContentService();
    ContentReader reader = contentService.getReader(getNodeRef(), property);
    
    return (reader != null && reader.exists()) ? reader.getContentString(length) : "";
}
 
Example 7
Project: alfresco-repository   File: XMLUtil.java   Source Code and License Vote up 5 votes
/** utility function for parsing xml */
public static Document parse(final NodeRef nodeRef,
                             final ContentService contentService)
   throws SAXException,
   IOException
{
   final ContentReader contentReader = 
      contentService.getReader(nodeRef, ContentModel.TYPE_CONTENT);
   final InputStream in = contentReader.getContentInputStream();
   return XMLUtil.parse(in);
}
 
Example 8
Project: alfresco-repository   File: ScriptNode.java   Source Code and License Vote up 4 votes
public String getContent()
{
    ContentService contentService = services.getContentService();
    ContentReader reader = contentService.getReader(nodeRef, property);
    return (reader != null && reader.exists()) ? reader.getContentString() : "";
}
 
Example 9
Project: alfresco-repository   File: ScriptNode.java   Source Code and License Vote up 4 votes
public InputStream getInputStream()
{
    ContentService contentService = services.getContentService();
    ContentReader reader = contentService.getReader(nodeRef, property);
    return (reader != null && reader.exists()) ? reader.getContentInputStream() : null;
}
 
Example 10
Project: alfresco-repository   File: BaseContentNode.java   Source Code and License Vote up 4 votes
/**
 * @param length      Length of the character stream to return, or -1 for all
 * 
 * @return the binary content stream converted to text using any available transformer
 *         if fails to convert then null will be returned
 */
public String getContentAsText(int length)
{
    String result = null;
    
    if (MimetypeMap.MIMETYPE_TEXT_PLAIN.equals(getMimetype()))
    {
        result = getContentMaxLength(length);
    }
    else
    {
        // get the content reader
        ContentService contentService = services.getContentService();
        NodeRef nodeRef = getNodeRef();
        ContentReader reader = contentService.getReader(nodeRef, property);
        if (reader == null)
        {
            return ""; // Caller of this method returns "" if there is an IOException
        }
        
        // get the writer and set it up for text convert
        ContentWriter writer = contentService.getTempWriter();
        writer.setMimetype("text/plain"); 
        writer.setEncoding(reader.getEncoding());
        
        TransformationOptions options = new TransformationOptions();
        options.setSourceNodeRef(nodeRef);

        // try and transform the content
        try
        {
            contentService.transform(reader, writer, options);
            
            ContentReader resultReader = writer.getReader();
            if (resultReader != null && reader.exists())
            {
               if (length != -1)
               {
                   result = resultReader.getContentString(length);
               }
               else
               {
                   result = resultReader.getContentString();
               }
            }
        }
        catch (NoTransformerException|UnsupportedTransformationException e)
        {
            // ignore
        }
    }
    return result;
}