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;
}
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);
}
}
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());
}
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);
}
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;
}
Aggregations