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

The following examples show how to use org.alfresco.service.cmr.repository.ContentReader#getContentInputStream() . 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: MimetypeMap.java    From alfresco-data-model with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Uses Tika to try to identify the mimetype of the file, falling back on
 * {@link #guessMimetype(String)} for an extension based one if Tika can't
 * help.
 */
public String guessMimetype(String filename, ContentReader reader)
{
    // ALF-10813: MimetypeMap.guessMimetype consumes 30% of file upload time
    // Let's only 'guess' if we need to
    if (reader != null && reader.getMimetype() != null && !reader.getMimetype().equals(MimetypeMap.MIMETYPE_BINARY))
    {
        // It was set to something other than the default.
        // Possibly someone used this method before (like the UI does) or
        // they just
        // know what their files are.
        return reader.getMimetype();
    }
    InputStream input = (reader != null ? reader.getContentInputStream() : null);
    return guessMimetype(filename, input);
}
 
Example 2
Source File: RepoTransferProgressMonitorImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
public InputStream getLogInputStream(String transferId)
        throws TransferException
{
    NodeRef transferRecord = getTransferRecord(transferId);
    
    ContentReader reader = contentService.getReader(transferRecord, ContentModel.PROP_CONTENT); 
    
    if(reader != null)
    {
        return reader.getContentInputStream();
    }
    else
    {
        return null;
    }
}
 
Example 3
Source File: SpoofedTextContentReaderTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testGetContentBinary_01() throws Exception
{
    // To URL
    String url = SpoofedTextContentReader.createContentUrl(Locale.ENGLISH, 12345L, 56L, "harry");
    // To Reader
    ContentReader reader = new SpoofedTextContentReader(url);
    InputStream is = reader.getContentInputStream();
    try
    {
        byte[] bytes = FileCopyUtils.copyToByteArray(is);
        assertEquals(56L, bytes.length);
    }
    finally
    {
        is.close();
    }
    // Compare readers
    ContentReader copyOne = reader.getReader();
    ContentReader copyTwo = reader.getReader();
    // Get exactly the same binaries
    assertTrue(AbstractContentReader.compareContentReaders(copyOne, copyTwo));
}
 
Example 4
Source File: AbstractContentWriter.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * @see ContentReader#getContentInputStream()
 * @see #putContent(InputStream) 
 */
public void putContent(ContentReader reader) throws ContentIOException
{
    try
    {
        // get the stream to read from
        InputStream is = reader.getContentInputStream();
        // put the content
        putContent(is);
    }
    catch (Throwable e)
    {
        throw new ContentIOException("Failed to copy reader content to writer: \n" +
                "   writer: " + this + "\n" +
                "   source reader: " + reader,
                e);
    }
}
 
Example 5
Source File: AbstractContentWriter.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void doGuessEncoding()
{
    ContentCharsetFinder charsetFinder = mimetypeService.getContentCharsetFinder();
    
    ContentReader reader = getReader();
    InputStream is = reader.getContentInputStream();
    Charset charset = charsetFinder.getCharset(is, getMimetype());
    try
    {
        is.close();
    }
    catch(IOException e)
    {}
    
    setEncoding(charset.name());
}
 
Example 6
Source File: ClassPathRepoTemplateLoader.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected InputStream getRelativeResource(String name)
{
    InputStream stream = null;
    NodeRef parentRef = nodeService.getPrimaryParent(nodeRef).getParentRef();
    NodeRef child = nodeService.getChildByName(parentRef, ContentModel.ASSOC_CONTAINS, name);
    if (child != null)
    {
        ContentReader contentReader = contentService.getReader(child, ContentModel.PROP_CONTENT);
        if (contentReader.exists())
        {
            stream = contentReader.getContentInputStream();
        }
    }
    return stream;
}
 
Example 7
Source File: ScriptNode.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
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 8
Source File: CustomContentModelIT.java    From alfresco-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Read text content for passed in file Node Reference
 *
 * @param nodeRef the node reference for a file containing text
 * @return the text content
 */
private String readTextContent(NodeRef nodeRef) {
    ContentReader reader = getServiceRegistry().getContentService().getReader(nodeRef, ContentModel.PROP_CONTENT);
    if (reader == null) {
        return ""; // Maybe it was a folder after all
    }

    InputStream is = reader.getContentInputStream();
    try {
        return IOUtils.toString(is, "UTF-8");
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }
}
 
Example 9
Source File: EncryptingContentWriterFacade.java    From alfresco-simple-content-stores with Apache License 2.0 5 votes vote down vote up
/**
 *
 * {@inheritDoc}
 */
@Override
public void guessEncoding()
{
    if (this.completedWrite)
    {
        if (this.mimetypeService == null)
        {
            LOGGER.warn("MimetypeService not supplied, but required for content guessing");
            return;
        }

        final ContentCharsetFinder charsetFinder = this.mimetypeService.getContentCharsetFinder();

        final ContentReader reader = this.getReader();
        final InputStream is = reader.getContentInputStream();
        final Charset charset = charsetFinder.getCharset(is, this.getMimetype());
        try
        {
            is.close();
        }
        catch (final IOException e)
        {
            LOGGER.trace("Error closing input stream");
        }

        this.setEncoding(charset.name());
    }
    else
    {
        // no point in delegating to backing writer - it can't guess from encrypted content
        this.guessEncoding = true;
    }
}
 
Example 10
Source File: TransferServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void dumpToSystemOut(NodeRef nodeRef) throws IOException
{
    ContentReader reader2 = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
    assertNotNull("transfer reader is null", reader2);
    InputStream is = reader2.getContentInputStream();

    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    String s = br.readLine();
    while(s != null)
    {
        System.out.println(s);   
        s = br.readLine();
    }
}
 
Example 11
Source File: XMLUtil.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** 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 12
Source File: MessageServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public ResourceBundle getRepoResourceBundle(
        final StoreRef storeRef,
        final String path,
        final Locale locale) throws IOException
{   
    // TODO - need to replace basic strategy with a more complete
    // search & instantiation strategy similar to ResourceBundle.getBundle()
    // Consider search query with basename* and then apply strategy ...
    
    // Avoid permission exceptions
    RunAsWork<ResourceBundle> getBundleWork = new RunAsWork<ResourceBundle>()
    {
        @Override
        public ResourceBundle doWork() throws Exception
        {
            NodeRef rootNode = nodeService.getRootNode(storeRef);

            // first attempt - with locale        
            NodeRef nodeRef = getNode(rootNode, path+"_"+locale+PROPERTIES_FILE_SUFFIX);
            
            if (nodeRef == null)
            {
                // second attempt - basename 
                nodeRef = getNode(rootNode, path+PROPERTIES_FILE_SUFFIX);
            }
            
            if (nodeRef == null)
            {
                logger.debug("Could not find message resource bundle " + storeRef + "/" + path);
                return null;
            }
            
            ContentReader cr = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
            ResourceBundle resBundle = new MessagePropertyResourceBundle(
                    new InputStreamReader(cr.getContentInputStream(), cr.getEncoding()));
            return resBundle;
        }
    };
    return AuthenticationUtil.runAs(getBundleWork, AuthenticationUtil.getSystemUserName());
}
 
Example 13
Source File: DictionaryRepositoryBootstrap.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 14
Source File: AbstractContentReader.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Does a comparison of the binaries associated with two readers.  Several shortcuts are assumed to be valid:<br/>
 *  - if the readers are the same instance, then the binaries are the same<br/>
 *  - if the size field is different, then the binaries are different<br/>
 * Otherwise the binaries are {@link EqualsHelper#binaryStreamEquals(InputStream, InputStream) compared}.
 * 
 * @return          Returns <tt>true</tt> if the underlying binaries are the same
 * @throws ContentIOException
 */
public static boolean compareContentReaders(ContentReader left, ContentReader right) throws ContentIOException
{
    if (left == right)
    {
        return true;
    }
    else if (left == null || right == null)
    {
        return false;
    }
    else if (left.getSize() != right.getSize())
    {
        return false;
    }
    InputStream leftIs = left.getContentInputStream();
    InputStream rightIs = right.getContentInputStream();
    try
    {
        return EqualsHelper.binaryStreamEquals(leftIs, rightIs);
    }
    catch (IOException e)
    {
        throw new ContentIOException(
                "Failed to compare content reader streams: \n" +
                "   Left:  " + left + "\n" +
                "   right: " + right);
    }
}
 
Example 15
Source File: SignableDocumentBehavior.java    From CounterSign with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * When the "signable" aspect is applied, extract the signature fields and add them
 * to the multivalue property
 */
public void onAddAspect(NodeRef nodeRef, QName aspectTypeQName) 
{
	try 
	{
		// when the aspect is added, extract the signature fields from the PDF
		ArrayList<String> signatureFields = new ArrayList<String>();
		ContentReader pdfReader = serviceRegistry.getContentService().getReader(nodeRef, ContentModel.PROP_CONTENT);
        PdfReader reader = new PdfReader(pdfReader.getContentInputStream());
        AcroFields form = reader.getAcroFields();
        Map<String, Item> fields = form.getFields();
        Iterator<String> it = fields.keySet().iterator();
        while(it.hasNext())
        {
        	String fieldName = it.next();
        	if(form.getFieldType(fieldName) == AcroFields.FIELD_TYPE_SIGNATURE)
        	{
        		// add this signature field to the list of available fields
        		signatureFields.add(fieldName);
        	}
        }
   		serviceRegistry.getNodeService().setProperty(nodeRef, CounterSignSignatureModel.PROP_SIGNATUREFIELDS, signatureFields);

	}
	catch(IOException ex)
	{
		logger.error("Error extracting PDF signature fields from document: " + ex);
	}
}
 
Example 16
Source File: LocalPassThroughTransform.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void transformImpl(ContentReader reader, ContentWriter writer, Map<String, String> transformOptions,
                             String sourceMimetype, String targetMimetype, String sourceExtension,
                             String targetExtension, String renditionName, NodeRef sourceNodeRef)
        throws UnsupportedTransformationException, ContentIOException
{
    if (isToText(sourceMimetype, targetMimetype))
    {
        // Set the encodings if specified.
        String sourceEncoding = reader.getEncoding();
        try (Reader charReader = sourceEncoding == null
                ? new InputStreamReader(reader.getContentInputStream())
                : new InputStreamReader(reader.getContentInputStream(), sourceEncoding))
        {
            String targetEncoding = writer.getEncoding();
            try (Writer charWriter = targetEncoding == null
                ? new OutputStreamWriter(writer.getContentOutputStream())
                : new OutputStreamWriter(writer.getContentOutputStream(), targetEncoding))
            {
                char[] buffer = new char[8192];
                int readCount = 0;
                while (readCount > -1)
                {
                    // write the last read count number of bytes
                    charWriter.write(buffer, 0, readCount);
                    // fill the buffer again
                    readCount = charReader.read(buffer);
                }
            }
        }
        catch (IOException e)
        {
            log.error(e);
        }
    }
    else // simple pass through
    {
        writer.putContent(reader.getContentInputStream());
    }
}
 
Example 17
Source File: CustomModelServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private M2Model getM2Model(final NodeRef modelNodeRef)
{
    ContentReader reader = contentService.getReader(modelNodeRef, ContentModel.PROP_CONTENT);
    if (reader == null)
    {
        return null;
    }
    InputStream in = reader.getContentInputStream();
    try
    {
        return M2Model.createModel(in);
    }
    finally
    {
        if (in != null)
        {
            try
            {
                in.close();
            }
            catch (IOException e)
            {
                logger.error("Failed to close input stream for " + modelNodeRef);
            }
        }
    }
}
 
Example 18
Source File: RepoUrlConfigSource.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public InputStream getInputStream(String sourceUrl)
{
    // determine the config source       
    try
    {
        return super.getInputStream(sourceUrl);
    } 
    catch (ConfigException ce)
    {     
        int idx = sourceUrl.indexOf(StoreRef.URI_FILLER); 
        if (idx != -1)
        {
            // assume this is a repository location
            int idx2 = sourceUrl.indexOf("/", idx+3);
                            
            String store = sourceUrl.substring(0, idx2);
            String path = sourceUrl.substring(idx2);
            
            StoreRef storeRef = tenantService.getName(new StoreRef(store));    
            NodeRef rootNode = null;
            
            try 
            {
                rootNode = nodeService.getRootNode(storeRef);
            } 
            catch (InvalidStoreRefException e)
            {
                throw ce;
            }
            
            List<NodeRef> nodeRefs = searchService.selectNodes(rootNode, path, null, namespaceService, false);
            
            if (nodeRefs.size() == 0)
            {
                // if none found, then simply skip
                return null;
            }
            else if (nodeRefs.size() > 1)
            {
                // unexpected
                throw new ConfigException("Found duplicate config sources in the repository " + sourceUrl);
            }

            NodeRef nodeRef = nodeRefs.get(0);
            
            ContentReader cr = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
                               
            return cr.getContentInputStream();                                                  
        } 
        else
        {
            // not a repository url
            throw ce;
        }
    }
}
 
Example 19
Source File: CompressingContentWriter.java    From alfresco-simple-content-stores with Apache License 2.0 4 votes vote down vote up
protected void writeToBackingStore()
{
    String mimetype = this.getMimetype();
    LOGGER.debug("Determined mimetype {} from write into temporary store - mimetypes to compress are {}", mimetype,
            this.mimetypesToCompress);

    if ((this.mimetypesToCompress != null && !this.mimetypesToCompress.isEmpty()) && this.mimetypeService != null
            && (mimetype == null || MimetypeMap.MIMETYPE_BINARY.equals(mimetype)))
    {
        mimetype = this.mimetypeService.guessMimetype(null, this.createReader());
        LOGGER.debug("Determined mimetype {} from MimetypeService.guessMimetype()", mimetype);

        if (mimetype == null || MimetypeMap.MIMETYPE_BINARY.equals(mimetype))
        {
            this.setMimetype(mimetype);
        }
    }

    final boolean shouldCompress = this.mimetypesToCompress == null || this.mimetypesToCompress.isEmpty()
            || (mimetype != null && (this.mimetypesToCompress.contains(mimetype) || this.isMimetypeToCompressWildcardMatch(mimetype)));

    if (shouldCompress)
    {
        LOGGER.debug("Content will be compressed to backing store (url={})", this.getContentUrl());
        final String compressiongType = this.compressionType != null && !this.compressionType.trim().isEmpty() ? this.compressionType
                : CompressorStreamFactory.GZIP;
        try (final OutputStream contentOutputStream = this.backingWriter.getContentOutputStream())
        {
            try (OutputStream compressedOutputStream = COMPRESSOR_STREAM_FACTORY.createCompressorOutputStream(compressiongType,
                    contentOutputStream))
            {
                final ContentReader reader = this.temporaryWriter.getReader();
                final InputStream contentInputStream = reader.getContentInputStream();
                FileCopyUtils.copy(contentInputStream, compressedOutputStream);
                this.properSize = this.temporaryWriter.getSize();
            }
        }
        catch (final IOException | CompressorException ex)
        {
            throw new ContentIOException("Error writing compressed content", ex);
        }
    }
    else
    {
        LOGGER.debug("Content will not be compressed to backing store (url={})", this.getContentUrl());
        this.backingWriter.putContent(this.createReader());
    }

    this.writtenToBackingWriter = true;

    final String finalContentUrl = this.backingWriter.getContentUrl();
    // we don't expect a different content URL, but just to make sure
    this.setContentUrl(finalContentUrl);

    this.cleanupTemporaryContent();
}
 
Example 20
Source File: MimetypeMapContentTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testGuessMimetypeForFile() throws Exception
{
    // Correct ones
    assertEquals(
            "application/msword", 
            mimetypeService.guessMimetype("something.doc", openQuickTestFile("quick.doc"))
    );
    assertEquals(
            "application/msword", 
            mimetypeService.guessMimetype("SOMETHING.DOC", openQuickTestFile("quick.doc"))
    );
    
    // Incorrect ones, Tika spots the mistake
    assertEquals(
            "application/msword", 
            mimetypeService.guessMimetype("something.pdf", openQuickTestFile("quick.doc"))
    );
    assertEquals(
            "application/pdf", 
            mimetypeService.guessMimetype("something.doc", openQuickTestFile("quick.pdf"))
    );
    
    // Ones where we use a different mimetype to the canonical one
    assertEquals(
            "image/bmp", // Officially image/x-ms-bmp 
            mimetypeService.guessMimetype("image.bmp", openQuickTestFile("quick.bmp"))
    );
    
    // Ones where we know about the parent, and Tika knows about the details
    assertEquals(
          "application/dita+xml", // Full version:  application/dita+xml;format=concept
          mimetypeService.guessMimetype("concept.dita", openQuickTestFile("quickConcept.dita"))
    );
    
    // Alfresco Specific ones, that Tika doesn't know about
    assertEquals(
          "application/acp", 
          mimetypeService.guessMimetype("something.acp", openQuickTestFile("quick.acp"))
    );

    
    // Where the file is corrupted
    File tmp = File.createTempFile("alfresco", ".tmp");
    ContentReader reader = openQuickTestFile("quick.doc");
    InputStream inp = reader.getContentInputStream();
    byte[] trunc = new byte[512+256];
    IOUtils.readFully(inp, trunc);
    inp.close();
    FileOutputStream out = new FileOutputStream(tmp);
    out.write(trunc);
    out.close();
    ContentReader truncReader = new FileContentReader(tmp);
    
    // Because the file is truncated, Tika won't be able to process the contents
    //  of the OLE2 structure
    // So, it'll fall back to just OLE2, but it won't fail
    assertEquals(
            "application/x-tika-msoffice", 
            mimetypeService.guessMimetype(null, truncReader)
    );
    // But with the filename it'll be able to use the .doc extension
    //  to guess at it being a .Doc file
    assertEquals(
          "application/msword", 
          mimetypeService.guessMimetype("something.doc", truncReader)
    );
    
    // Lotus notes EML files (ALF-16381 / TIKA-1042)
    assertEquals(
          "message/rfc822", 
          mimetypeService.guessMimetype("something.eml", openQuickTestFile("quickLotus.eml"))
    );
}