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

The following examples show how to use org.alfresco.service.cmr.repository.ContentWriter#setLocale() . 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: RoutingContentServiceTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Check that a valid writer into the content store can be retrieved and used.
 */
public void testSimpleNonTempWriter() throws Exception
{
    ContentWriter writer = contentService.getWriter(null, null, false);
    assertNotNull("Writer should not be null", writer);
    assertNotNull("Content URL should not be null", writer.getContentUrl());
    
    // write some content
    writer.putContent(SOME_CONTENT);
    writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
    writer.setEncoding("UTF-16");
    writer.setLocale(Locale.CHINESE);

    // get the reader
    ContentReader reader = contentService.getReader(contentNodeRef, ContentModel.PROP_CONTENT);
    assertNotNull("Reader should not be null", reader);
    assertNotNull("Content URL should not be null", reader.getContentUrl());
    assertEquals("Content Encoding was not set", "UTF-16", reader.getEncoding());
    assertEquals("Content Locale was not set", Locale.CHINESE, reader.getLocale());
}
 
Example 2
Source File: RepoPrimaryManifestProcessorImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @param nodeToUpdate NodeRef
 * @param contentProps Map<QName, Serializable>
 * @return true if any content property has been updated for the needToUpdate node
 */
private boolean writeContent(NodeRef nodeToUpdate, Map<QName, Serializable> contentProps)
{
    boolean contentUpdated = false;
    File stagingDir = getStagingFolder();
    for (Map.Entry<QName, Serializable> contentEntry : contentProps.entrySet())
    {
        ContentData contentData = (ContentData) contentEntry.getValue();
        String contentUrl = contentData.getContentUrl();
        if(contentUrl == null || contentUrl.isEmpty())
        {
            log.debug("content data is null or empty:" + nodeToUpdate);
            ContentData cd = new ContentData(null, null, 0, null);
            nodeService.setProperty(nodeToUpdate, contentEntry.getKey(), cd);
            contentUpdated = true;
        }
        else
        {
            String fileName = TransferCommons.URLToPartName(contentUrl);
            File stagedFile = new File(stagingDir, fileName);
            if (!stagedFile.exists())
            {
                error(MSG_REFERENCED_CONTENT_FILE_MISSING);
            }
            ContentWriter writer = contentService.getWriter(nodeToUpdate, contentEntry.getKey(), true);
            writer.setEncoding(contentData.getEncoding());
            writer.setMimetype(contentData.getMimetype());
            writer.setLocale(contentData.getLocale());
            writer.putContent(stagedFile);
            contentUpdated = true;
        }
    }
    return contentUpdated;
}
 
Example 3
Source File: TransferReporterImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public NodeRef writeDestinationReport(String transferName,
        TransferTarget target,
        File tempFile)
{
   
    String title = transferName + "_destination";
    String description = "Transfer Destination Report - target: " + target.getName();
    String name = title + ".xml";
    
    logger.debug("writing destination transfer report " + title);
    logger.debug("parent node ref " + target.getNodeRef());
    
    Map<QName, Serializable> properties = new HashMap<QName, Serializable> ();
    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_DEST, 
            properties);
    
    ContentWriter writer = contentService.getWriter(ref.getChildRef(), 
            ContentModel.PROP_CONTENT, true);
    writer.setLocale(Locale.getDefault());
    writer.setMimetype(MimetypeMap.MIMETYPE_XML);
    writer.setEncoding(DEFAULT_ENCODING);
    writer.putContent(tempFile);
    
    logger.debug("written " + name + ", " + ref.getChildRef());
    
    return ref.getChildRef();
}
 
Example 4
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 5
Source File: RoutingContentServiceTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void setUp() throws Exception
{
    ctx = ApplicationContextHelper.getApplicationContext();
    transactionService = (TransactionService) ctx.getBean("TransactionService");
    nodeService = (NodeService) ctx.getBean("NodeService");
    contentService = (ContentService) ctx.getBean(ServiceRegistry.CONTENT_SERVICE.getLocalName());
    copyService = (CopyService) ctx.getBean("CopyService");
    this.policyComponent = (PolicyComponent) ctx.getBean("policyComponent");
    this.authenticationComponent = (AuthenticationComponent) ctx.getBean("authenticationComponent");
    
    // authenticate
    this.authenticationComponent.setSystemUserAsCurrentUser();
    
    // start the transaction
    txn = getUserTransaction();
    txn.begin();
    
    // create a store and get the root node
    StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, getName());
    if (!nodeService.exists(storeRef))
    {
        storeRef = nodeService.createStore(storeRef.getProtocol(), storeRef.getIdentifier());
    }
    rootNodeRef = nodeService.getRootNode(storeRef);

    ChildAssociationRef assocRef = nodeService.createNode(
            rootNodeRef,
            ContentModel.ASSOC_CHILDREN,
            QName.createQName(TEST_NAMESPACE, GUID.generate()),
            ContentModel.TYPE_CONTENT);
    contentNodeRef = assocRef.getChildRef();

    ContentWriter writer = contentService.getWriter(contentNodeRef, ContentModel.PROP_CONTENT, true);
    writer.setEncoding("UTF-16");
    writer.setLocale(Locale.CHINESE);
    writer.setMimetype("text/plain");
    writer.putContent("sample content");
}
 
Example 6
Source File: RoutingContentServiceTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testWriteToNodeWithoutAnyContentProperties() throws Exception
{
    NodeRef nodeRef = nodeService.createNode(
            rootNodeRef,
            ContentModel.ASSOC_CHILDREN,
            QName.createQName(TEST_NAMESPACE, GUID.generate()),
            ContentModel.TYPE_CONTENT).getChildRef();

    ContentWriter writer = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);

    assertNull(writer.getMimetype());
    assertEquals("UTF-8", writer.getEncoding());
    assertEquals(Locale.getDefault(), writer.getLocale());
    
    // now set it on the writer
    writer.setMimetype("text/plain");
    writer.setEncoding("UTF-16");
    writer.setLocale(Locale.FRENCH);
    
    String content = "The quick brown fox ...";
    writer.putContent(content);
    
    // the properties should have found their way onto the node
    ContentData contentData = (ContentData) nodeService.getProperty(nodeRef, ContentModel.PROP_CONTENT);
    assertEquals("metadata didn't get onto node", writer.getContentData(), contentData);
    
    // check that the reader's metadata is set
    ContentReader reader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
    assertEquals("Metadata didn't get set on reader", writer.getContentData(), reader.getContentData());
}
 
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();
        }
    }
}
 
Example 8
Source File: AbstractWritableContentStoreTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Checks that the various methods of obtaining a reader are supported.
 */
@Test
public synchronized void testGetReader() throws Exception
{
    ContentStore store = getStore();
    ContentWriter writer = store.getWriter(ContentStore.NEW_CONTENT_CONTEXT);
    String contentUrl = writer.getContentUrl();
    
    // Check that a reader is available from the store
    ContentReader readerFromStoreBeforeWrite = store.getReader(contentUrl);
    assertNotNull("A reader must always be available from the store", readerFromStoreBeforeWrite);
    
    // check that a reader is available from the writer
    ContentReader readerFromWriterBeforeWrite = writer.getReader();
    assertNotNull("A reader must always be available from the writer", readerFromWriterBeforeWrite);
    
    String content = "Content for testGetReader";
    
    // write some content
    long before = System.currentTimeMillis();
    this.wait(1000L);
    writer.setMimetype("text/plain");
    writer.setEncoding("UTF-8");
    writer.setLocale(Locale.CHINESE);
    writer.putContent(content);
    this.wait(1000L);
    long after = System.currentTimeMillis();
    
    // get a reader from the store
    ContentReader readerFromStore = store.getReader(contentUrl);
    assertNotNull(readerFromStore);
    assertTrue(readerFromStore.exists());
    // Store-provided readers don't have context other than URLs
    // assertEquals(writer.getContentData(), readerFromStore.getContentData());
    assertEquals(content, readerFromStore.getContentString());
    
    // get a reader from the writer
    ContentReader readerFromWriter = writer.getReader();
    assertNotNull(readerFromWriter);
    assertTrue(readerFromWriter.exists());
    assertEquals(writer.getContentData(), readerFromWriter.getContentData());
    assertEquals(content, readerFromWriter.getContentString());
    
    // get another reader from the reader
    ContentReader readerFromReader = readerFromWriter.getReader();
    assertNotNull(readerFromReader);
    assertTrue(readerFromReader.exists());
    assertEquals(writer.getContentData(), readerFromReader.getContentData());
    assertEquals(content, readerFromReader.getContentString());
    
    // check that the length is correct
    int length = content.getBytes(writer.getEncoding()).length;
    assertEquals("Reader content length is incorrect", length, readerFromWriter.getSize());

    // check that the last modified time is correct
    long modifiedTimeCheck = readerFromWriter.getLastModified();

    // On some versionms of Linux (e.g. Centos) this test won't work as the 
    // modified time accuracy is only to the second.
    long beforeSeconds = before/1000L;
    long afterSeconds = after/1000L;
    long modifiedTimeCheckSeconds = modifiedTimeCheck/1000L;

    assertTrue("Reader last modified is incorrect", beforeSeconds <= modifiedTimeCheckSeconds);
    assertTrue("Reader last modified is incorrect", modifiedTimeCheckSeconds <= afterSeconds);
}