Search in sources :

Example 36 with ContentWriter

use of org.alfresco.service.cmr.repository.ContentWriter in project records-management by Alfresco.

the class TransferReportPost method fileTransferReport.

/**
 * Files the given transfer report as a record in the given record folder.
 *
 * @param report Report to file
 * @param destination The destination record folder
 * @return NodeRef of the created record
 */
protected NodeRef fileTransferReport(File report, NodeRef destination) {
    ParameterCheck.mandatory("report", report);
    ParameterCheck.mandatory("destination", destination);
    NodeRef record = null;
    Map<QName, Serializable> properties = new HashMap<QName, Serializable>(1);
    properties.put(ContentModel.PROP_NAME, report.getName());
    // file the transfer report as an undeclared record
    record = this.nodeService.createNode(destination, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, QName.createValidLocalName(report.getName())), ContentModel.TYPE_CONTENT, properties).getChildRef();
    // Set the content
    ContentWriter writer = contentService.getWriter(record, ContentModel.PROP_CONTENT, true);
    writer.setMimetype(MimetypeMap.MIMETYPE_HTML);
    writer.setEncoding("UTF-8");
    writer.putContent(report);
    return record;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Serializable(java.io.Serializable) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName)

Example 37 with ContentWriter

use of org.alfresco.service.cmr.repository.ContentWriter in project SearchServices by Alfresco.

the class SolrContentStore method storeDocOnSolrContentStore.

/**
 * Stores a {@link SolrInputDocument} into Alfresco solr content store.
 * @param tenant
 * @param dbId
 * @param doc
 * @throws IOException
 */
public void storeDocOnSolrContentStore(String tenant, long dbId, SolrInputDocument doc) throws IOException {
    ContentContext contentContext = SolrContentUrlBuilder.start().add(SolrContentUrlBuilder.KEY_TENANT, tenant).add(SolrContentUrlBuilder.KEY_DB_ID, String.valueOf(dbId)).getContentContext();
    this.delete(contentContext.getContentUrl());
    ContentWriter writer = this.getWriter(contentContext);
    if (log.isDebugEnabled()) {
        log.debug("Writing doc to " + contentContext.getContentUrl());
    }
    try (OutputStream contentOutputStream = writer.getContentOutputStream();
        // Compresses the document
        GZIPOutputStream gzip = new GZIPOutputStream(contentOutputStream)) {
        JavaBinCodec codec = new JavaBinCodec(resolver);
        codec.marshal(doc, gzip);
    } catch (Exception e) {
        // A failure to write to the store is acceptable as long as it's logged
        log.warn("Failed to write to store using URL: " + contentContext.getContentUrl(), e);
    }
}
Also used : ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ContentContext(org.alfresco.repo.content.ContentContext) IOException(java.io.IOException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec)

Example 38 with ContentWriter

use of org.alfresco.service.cmr.repository.ContentWriter in project SearchServices by Alfresco.

the class SolrContentStoreTest method contentByString.

@Test
public void contentByString() {
    SolrContentStore store = new SolrContentStore(solrHome);
    ContentContext ctx = createContentContext("abc");
    ContentWriter writer = store.getWriter(ctx);
    File file = new File(store.getRootLocation() + "/" + writer.getContentUrl().replace("solr://", ""));
    Assert.assertFalse("File was created before anything was written", file.exists());
    String content = "Quick brown fox jumps over the lazy dog.";
    writer.putContent(content);
    Assert.assertTrue("File was not created.", file.exists());
    try {
        writer.putContent("Should not work");
    } catch (IllegalStateException e) {
    // Expected
    }
    // Now get the reader
    ContentReader reader = store.getReader(ctx.getContentUrl());
    Assert.assertNotNull(reader);
    Assert.assertTrue(reader.exists());
    Assert.assertEquals(content, reader.getContentString());
}
Also used : ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) ContentReader(org.alfresco.service.cmr.repository.ContentReader) ContentContext(org.alfresco.repo.content.ContentContext) File(java.io.File) Test(org.junit.Test)

Example 39 with ContentWriter

use of org.alfresco.service.cmr.repository.ContentWriter in project SearchServices by Alfresco.

the class SolrContentStoreTest method getWriter.

@Test
public void getWriter() {
    SolrContentStore store = new SolrContentStore(solrHome);
    ContentContext ctx = createContentContext("abc");
    ContentWriter writer = store.getWriter(ctx);
    String url = writer.getContentUrl();
    Assert.assertNotNull(url);
    Assert.assertEquals("URL of the context does not match the writer URL. ", ctx.getContentUrl(), url);
}
Also used : ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) ContentContext(org.alfresco.repo.content.ContentContext) Test(org.junit.Test)

Example 40 with ContentWriter

use of org.alfresco.service.cmr.repository.ContentWriter in project acs-community-packaging by Alfresco.

the class BaseContentWizard method saveContent.

// ------------------------------------------------------------------------------
// Helper methods
/**
 * Save the specified content using the currently set wizard attributes
 *
 * @param fileContent      File content to save
 * @param strContent       String content to save
 */
protected void saveContent(File fileContent, String strContent) throws Exception {
    // get the node ref of the node that will contain the content
    NodeRef containerNodeRef;
    String nodeId = this.navigator.getCurrentNodeId();
    if (nodeId == null) {
        containerNodeRef = this.getNodeService().getRootNode(Repository.getStoreRef());
    } else {
        containerNodeRef = new NodeRef(Repository.getStoreRef(), nodeId);
    }
    // apply the inline editable aspect (by adding the property at create time)
    Map<QName, Serializable> editProps = new HashMap<QName, Serializable>(6);
    if (this.inlineEdit == true) {
        editProps.put(ApplicationModel.PROP_EDITINLINE, this.inlineEdit);
        if (logger.isDebugEnabled())
            logger.debug("Added inlineeditable aspect with properties: " + editProps);
    }
    // set the name
    editProps.put(ContentModel.PROP_NAME, this.fileName);
    // set the author property
    editProps.put(ContentModel.PROP_AUTHOR, this.author);
    // apply the titled aspect - title and description
    editProps.put(ContentModel.PROP_TITLE, this.title);
    editProps.put(ContentModel.PROP_DESCRIPTION, this.description);
    if (logger.isDebugEnabled())
        logger.debug("Added titled aspect with properties: " + this.title + ", " + this.description);
    // create the node
    NodeRef fileNodeRef = this.getNodeService().createNode(containerNodeRef, ContentModel.ASSOC_CONTAINS, QName.createQNameWithValidLocalName(NamespaceService.CONTENT_MODEL_1_0_URI, this.fileName), Repository.resolveToQName(this.objectType), editProps).getChildRef();
    if (logger.isDebugEnabled())
        logger.debug("Created file node for file: " + this.fileName);
    // get a writer for the content and put the file
    ContentWriter writer = getContentService().getWriter(fileNodeRef, ContentModel.PROP_CONTENT, true);
    // set the mimetype and encoding
    writer.setMimetype(this.mimeType);
    writer.setEncoding(getEncoding());
    if (fileContent != null) {
        writer.putContent(fileContent);
    } else {
        writer.putContent(strContent == null ? "" : strContent);
    }
    // remember the created node now
    this.createdNode = fileNodeRef;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Serializable(java.io.Serializable) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName)

Aggregations

ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)76 NodeRef (org.alfresco.service.cmr.repository.NodeRef)50 HashMap (java.util.HashMap)31 Serializable (java.io.Serializable)22 QName (org.alfresco.service.namespace.QName)21 ContentReader (org.alfresco.service.cmr.repository.ContentReader)20 Test (org.junit.Test)18 FileContentWriter (org.alfresco.repo.content.filestore.FileContentWriter)14 InputStream (java.io.InputStream)13 AlfrescoDocument (org.alfresco.cmis.client.AlfrescoDocument)13 AlfrescoFolder (org.alfresco.cmis.client.AlfrescoFolder)13 VersionableAspectTest (org.alfresco.repo.version.VersionableAspectTest)13 TestNetwork (org.alfresco.rest.api.tests.RepoService.TestNetwork)13 CmisSession (org.alfresco.rest.api.tests.client.PublicApiClient.CmisSession)13 RequestContext (org.alfresco.rest.api.tests.client.RequestContext)13 ContentStreamImpl (org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl)13 TestPerson (org.alfresco.rest.api.tests.RepoService.TestPerson)12 CmisUpdateConflictException (org.apache.chemistry.opencmis.commons.exceptions.CmisUpdateConflictException)12 PublicApiException (org.alfresco.rest.api.tests.client.PublicApiException)11 CmisConstraintException (org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException)11