Java Code Examples for org.alfresco.service.cmr.repository.ContentWriter#getContentOutputStream()

The following examples show how to use org.alfresco.service.cmr.repository.ContentWriter#getContentOutputStream() . 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: ScriptNode.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Delete the content stream
 */
public void delete()
{
    ContentService contentService = services.getContentService();
    ContentWriter writer = contentService.getWriter(nodeRef, this.property, true);
    OutputStream output = writer.getContentOutputStream();
    try
    {
        output.close();
    }
    catch (IOException e)
    {
        // NOTE: fall-through
    }
    writer.setMimetype(null);
    writer.setEncoding(null);
    
    // update cached variables after putContent()
    updateContentData(true);
}
 
Example 2
Source File: FileFolderServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testAlf6560MimetypeSetting() throws Exception
{
    FileInfo fileInfo = fileFolderService.create(workingRootNodeRef, "Something.html", ContentModel.TYPE_CONTENT);
    NodeRef fileNodeRef = fileInfo.getNodeRef();
    
    // Write the content but without setting the mimetype
    ContentWriter writer = fileFolderService.getWriter(fileNodeRef);
    writer.putContent("CONTENT");
    
    ContentReader reader = fileFolderService.getReader(fileNodeRef);
    assertEquals("Mimetype was not automatically set", MimetypeMap.MIMETYPE_HTML, reader.getMimetype());
    
    
    // Now ask for encoding too
    writer = fileFolderService.getWriter(fileNodeRef);
    writer.guessEncoding();
    OutputStream out = writer.getContentOutputStream();
    out.write( "<html><body>hall\u00e5 v\u00e4rlden</body></html>".getBytes("UnicodeBig") );
    out.close();
    
    reader = fileFolderService.getReader(fileNodeRef);
    assertEquals("Mimetype was not automatically set", MimetypeMap.MIMETYPE_HTML, reader.getMimetype());
    assertEquals("Encoding was not automatically set", "UTF-16BE", reader.getEncoding());
}
 
Example 3
Source File: AbstractWritableContentStoreTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testReadAndWriteStreamByPush() throws Exception
{
    ContentWriter writer = getWriter();

    String content = "Some Random Content";
    // get the content output stream
    OutputStream os = writer.getContentOutputStream();
    os.write(content.getBytes());
    assertFalse("Stream has not been closed", writer.isClosed());
    // close the stream and check again
    os.close();
    assertTrue("Stream close not detected", writer.isClosed());
    
    // pull the content from a stream
    ContentReader reader = writer.getReader();
    InputStream is = reader.getContentInputStream();
    byte[] buffer = new byte[100];
    int count = is.read(buffer);
    assertEquals("No content read", content.length(), count);
    is.close();
    String check = new String(buffer, 0, count);
    
    assertEquals("Write out of and read into files failed", content, check);
}
 
Example 4
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 5
Source File: RepoRemoteService.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public OutputStream createFile(NodeRef base, String path) 
{
    Pair<NodeRef, String> parentChild = getParentChildRelative(base, path);
    FileInfo info = fFileFolderService.create(parentChild.getFirst(), 
                                              parentChild.getSecond(), 
                                              ContentModel.TYPE_CONTENT);
    // TODO is update supposed to be true.
    ContentWriter writer = fContentService.getWriter(info.getNodeRef(), ContentModel.PROP_CONTENT, true);
    return writer.getContentOutputStream();
}
 
Example 6
Source File: AbstractWritableContentStoreTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testMimetypAndEncodingAndLocale() throws Exception
{
    ContentWriter writer = getWriter();
    // set mimetype and encoding
    writer.setMimetype("text/plain");
    writer.setEncoding("UTF-16");
    writer.setLocale(Locale.CHINESE);
    
    // create a UTF-16 string
    String content = "A little bit o' this and a little bit o' that";
    byte[] bytesUtf16 = content.getBytes("UTF-16");
    // write the bytes directly to the writer
    OutputStream os = writer.getContentOutputStream();
    os.write(bytesUtf16);
    os.close();
    
    // now get a reader from the writer
    ContentReader reader = writer.getReader();
    assertEquals("Writer -> Reader content URL mismatch", writer.getContentUrl(), reader.getContentUrl());
    assertEquals("Writer -> Reader mimetype mismatch", writer.getMimetype(), reader.getMimetype());
    assertEquals("Writer -> Reader encoding mismatch", writer.getEncoding(), reader.getEncoding());
    assertEquals("Writer -> Reader locale mismatch", writer.getLocale(), reader.getLocale());
    
    // now get the string directly from the reader
    String contentCheck = reader.getContentString();     // internally it should have taken care of the encoding
    assertEquals("Encoding and decoding of strings failed", content, contentCheck);
}
 
Example 7
Source File: TransferReporterImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Write exception transfer report
 * 
 * @return NodeRef the node ref of the new transfer report
 */
public NodeRef createTransferReport(String transferName,
            Exception e, 
            TransferTarget target,
            TransferDefinition definition, 
            List<TransferEvent> events, 
            File snapshotFile)
{
    Map<QName, Serializable> properties = new HashMap<QName, Serializable> ();
    
    String title = transferName;
    String description = "Transfer Report - target: " + target.getName();
    String name = transferName + ".xml";
    
    properties.put(ContentModel.PROP_NAME, name);
    properties.put(ContentModel.PROP_TITLE, title);
    properties.put(ContentModel.PROP_DESCRIPTION, description);
    ChildAssociationRef ref = nodeService.createNode(target.getNodeRef(), ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, name), TransferModel.TYPE_TRANSFER_REPORT, properties);
    ContentWriter writer = contentService.getWriter(ref.getChildRef(), ContentModel.PROP_CONTENT, true);
    writer.setLocale(Locale.getDefault());
    writer.setMimetype(MimetypeMap.MIMETYPE_XML);
    writer.setEncoding(DEFAULT_ENCODING);
    
    XMLTransferReportWriter reportWriter = new XMLTransferReportWriter();
    
    BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(writer.getContentOutputStream()));

    try
    {
        reportWriter.startTransferReport(DEFAULT_ENCODING, bufferedWriter);
        
        reportWriter.writeTarget(target);
        
        reportWriter.writeDefinition(definition);
        
        reportWriter.writeException(e);
        
        reportWriter.writeTransferEvents(events);
        
        reportWriter.endTransferReport();
        
        return ref.getChildRef();
    }
    
    catch (SAXException se)
    {
        return null;
    }
    finally
    {
        try
        {
            bufferedWriter.close();
        }
        catch (IOException error)
        {
            error.printStackTrace();
        }
    }
}