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

The following examples show how to use org.alfresco.service.cmr.repository.ContentService#getReader() . These examples are extracted from open source projects. 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
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 2
/**
 * 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 3
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 4
/**
 * 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(synchronousTransformClient, copyNodeRef, reader, writer);
    }
    
    return transformedNode;
}
 
Example 5
/**
 * @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
/**
 * @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
/** 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
public String getContent()
{
    ContentService contentService = services.getContentService();
    ContentReader reader = contentService.getReader(nodeRef, property);
    return (reader != null && reader.exists()) ? reader.getContentString() : "";
}
 
Example 9
public InputStream getInputStream()
{
    ContentService contentService = services.getContentService();
    ContentReader reader = contentService.getReader(nodeRef, property);
    return (reader != null && reader.exists()) ? reader.getContentInputStream() : null;
}
 
Example 10
/**
 * @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();
        SynchronousTransformClient synchronousTransformClient = services.getSynchronousTransformClient();
        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
        {
            try
            {
                synchronousTransformClient.transform(reader, writer, Collections.emptyMap(), null, nodeRef);

                ContentReader resultReader = writer.getReader();
                if (resultReader != null && reader.exists())
                {
                    if (length != -1)
                    {
                        result = resultReader.getContentString(length);
                    }
                    else
                    {
                        result = resultReader.getContentString();
                    }
                }
            }
            catch (UnsupportedTransformationException ignore)
            {
            }
        }
        catch (NoTransformerException|UnsupportedTransformationException| ContentIOException e)
        {
            // ignore
        }
    }
    return result;
}
 
Example 11
/**
   * Validates a single signature, passed in as a nodeRef String
   * 
   * @param nodeRef
   * @return {signatureValid:[validity],hashValid:[validity]}
   */
  public JSONObject validateSignature(String nodeRef)
  {
  	// get the node, make sure it exists
  	NodeService ns = serviceRegistry.getNodeService();
  	ContentService cs = serviceRegistry.getContentService();
  	NodeRef sigNode = new NodeRef(nodeRef);
  	boolean signatureValid = false;
  	boolean hashValid = false;
  	
  	if(ns.exists(sigNode))
  	{
  		try
  		{
   		// get the doc has from the time of the sig and the sig itself
   		String docHash = String.valueOf(ns.getProperty(sigNode, CounterSignSignatureModel.PROP_DOCHASH));
   		ContentReader sigReader = cs.getReader(sigNode, ContentModel.PROP_CONTENT);
   		InputStream sigStream = sigReader.getContentInputStream();
   		
   		ByteArrayOutputStream baos = new ByteArrayOutputStream();				
   		byte[] buffer = new byte[1024];
   		int read = 0;
   		while ((read = sigStream.read(buffer, 0, buffer.length)) != -1) {
   			baos.write(buffer, 0, read);
   		}		
   		baos.flush();
   		
   		// get the signing user's public key
   		String person = String.valueOf(ns.getProperty(sigNode, CounterSignSignatureModel.PROP_EXTERNALSIGNER));
   		SignatureProvider prov = signatureProviderFactory.getSignatureProvider(person);	
   		
   		// validate the sig using the public key
   		signatureValid = prov.validateSignature(baos.toByteArray(), docHash.getBytes());
   		
  			// get the document associated with this sig, and compute the hash
  			NodeRef signedDoc = ns.getParentAssocs(sigNode).get(0).getParentRef();
  			ContentReader docReader = cs.getReader(signedDoc, ContentModel.PROP_CONTENT);
  			String contentHash = new String(prov.computeHash(docReader.getContentInputStream()));
  			if(docHash.equals(contentHash))
  			{
  				hashValid = true;
  			}
  			else
  			{
  				signatureValid = false;
  			}
  		}
  		catch(IOException ioex)
  		{
  			throw new AlfrescoRuntimeException("IOException reading signature: " + ioex.getMessage());
  		}
  	}
  	else
  	{
  		throw new AlfrescoRuntimeException("Node: " + nodeRef + " does not exist");
  	}
  	
// create the JSONObject to return
JSONObject valid = new JSONObject();
valid.put(signatureValidName, signatureValid);
valid.put(hashValidName,  hashValid);

  	return valid;
  }