ContentWriter contentWriter = contentService.getWriter(noderef, ContentModel.PROP_CONTENT, true); contentWriter.setMimetype("text/csv"); FileChannel fileChannel = contentWriter.getFileChannel(false); ByteBuffer bf = ByteBuffer.wrap(logLine.getBytes()); try { fileChannel.position(contentWriter.getSize()); fileChannel.write(bf); fileChannel.force(false); fileChannel.close(); } catch (IOException e){ e.printStackTrace(); }
@Override public NodeRef execute() throws Exception { final NodeRef nodeRef = createDownloadTypeNode(moduleId + SHARE_EXT_MODULE_SUFFIX); ContentWriter writer = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true); writer.setMimetype(MimetypeMap.MIMETYPE_XML); writer.setEncoding("UTF-8"); writer.putContent(moduleFile); return nodeRef; } });
@Override public void addListener(ContentStreamListener listener) { delegatee.addListener(listener); }
@Override public ContentData getContentData() { if (!this.cacheWriter.getReader().exists()) { return this.bsWriter.getContentData(); } return this.cacheWriter.getContentData(); }
@Test public void encodingAttrsCopiedToBackingStoreWriter() { cachingStore = new CachingContentStore(backingStore, cache, true); ContentContext ctx = ContentContext.NULL_CONTEXT; ContentWriter bsWriter = mock(ContentWriter.class); when(backingStore.getWriter(ctx)).thenReturn(bsWriter); when(bsWriter.getContentUrl()).thenReturn("url"); ContentWriter cacheWriter = mock(ContentWriter.class); when(cache.getWriter("url")).thenReturn(cacheWriter); ContentReader readerFromCacheWriter = mock(ContentReader.class); when(cacheWriter.getReader()).thenReturn(readerFromCacheWriter); when(cacheWriter.getEncoding()).thenReturn("UTF-8"); when(cacheWriter.getLocale()).thenReturn(Locale.UK); when(cacheWriter.getMimetype()).thenReturn("not/real/mimetype"); cachingStore.getWriter(ctx); // Get the stream listener and trigger it ArgumentCaptor<ContentStreamListener> arg = ArgumentCaptor.forClass(ContentStreamListener.class); verify(cacheWriter).addListener(arg.capture()); // Simulate a stream close arg.getValue().contentStreamClosed(); verify(bsWriter).setEncoding("UTF-8"); verify(bsWriter).setLocale(Locale.UK); verify(bsWriter).setMimetype("not/real/mimetype"); }
@Test(expected=RuntimeException.class) // Check that exceptions raised by the backing store's putContent(ContentReader) // aren't swallowed and can therefore cause the transaction to fail. public void exceptionRaisedWhenCopyingTempToBackingStoreIsPropogatedCorrectly() throws ContentIOException, IOException { cachingStore = new CachingContentStore(backingStore, cache, true); ContentContext ctx = ContentContext.NULL_CONTEXT; ContentWriter bsWriter = mock(ContentWriter.class); when(backingStore.getWriter(ctx)).thenReturn(bsWriter); when(bsWriter.getContentUrl()).thenReturn("url"); ContentWriter cacheWriter = mock(ContentWriter.class); when(cache.getWriter("url")).thenReturn(cacheWriter); ContentReader readerFromCacheWriter = mock(ContentReader.class); when(cacheWriter.getReader()).thenReturn(readerFromCacheWriter); doThrow(new RuntimeException()).when(bsWriter).putContent(any(ContentReader.class)); cachingStore.getWriter(ctx); // Get the stream listener and trigger it ArgumentCaptor<ContentStreamListener> arg = ArgumentCaptor.forClass(ContentStreamListener.class); verify(cacheWriter).addListener(arg.capture()); // Simulate a stream close arg.getValue().contentStreamClosed(); }
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, writer.getMimetype()); assertEquals("UTF-16", writer.getEncoding()); assertEquals(Locale.CHINESE, writer.getLocale()); assertNull(writer.getMimetype()); assertEquals("UTF-8", writer.getEncoding()); assertEquals(Locale.getDefault(), writer.getLocale()); writer.setMimetype("text/plain"); writer.setEncoding("UTF-16"); writer.setLocale(Locale.FRENCH); writer.putContent(content); assertEquals("metadata didn't get onto node", writer.getContentData(), contentData); assertEquals("Metadata didn't get set on reader", writer.getContentData(), reader.getContentData());
@Override public ContentData doWork() throws Exception { InputStream contentStream = handler.importStream(sourceContentUrl); ContentWriter writer = contentService.getWriter(null, null, false); writer.setEncoding(sourceContentData.getEncoding()); writer.setMimetype(sourceContentData.getMimetype()); writer.putContent(contentStream); return writer.getContentData(); } }, TenantService.DEFAULT_DOMAIN);
eagerContentStoreCleaner.registerNewContentUrl(writer.getContentUrl()); ContentWriter writer = store.getWriter(ctx); eagerContentStoreCleaner.registerNewContentUrl(writer.getContentUrl()); writer.setMimetype(contentData.getMimetype()); writer.setEncoding(contentData.getEncoding()); writer.setLocale(contentData.getLocale()); writer.addListener(listener);
writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN); writer.putContent("INITIAL CONTENT"); contentData = writer.getContentData();
/** * 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); // set the content property manually nodeService.setProperty(contentNodeRef, ContentModel.PROP_CONTENT, writer.getContentData()); // 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()); }
contentWriter.setEncoding(getMailPartContentEncoding(part)); contentWriter.setMimetype(MimetypeMap.MIMETYPE_HTML); contentWriter.putContent(mailPartContent); extractor.setLinks(false); extractor.setReplaceNonBreakingSpaces(false); extractor.setURL(tempHtmlFile, contentWriter.getEncoding()); sb.append(extractor.getStrings());
/** * {@inheritDoc} * <p> * This implementation creates some content in the store and returns the new content URL. */ protected String getExistingContentUrl() { ContentWriter writer = getWriter(); writer.putContent("Content for getExistingContentUrl"); return writer.getContentUrl(); }
assertNotNull("Content URL should not be null", writer.getContentUrl()); assertNotNull("Content mimetype should not be null", writer.getMimetype()); assertNotNull("Content encoding should not be null", writer.getEncoding()); assertNotNull("Content locale should not be null", writer.getLocale()); writer.putContent(SOME_CONTENT);
@Override protected Writer createUnderlyingLogWriter(String transferId) { NodeRef node = new NodeRef(transferId); ContentWriter contentWriter = contentService.getWriter(node, ContentModel.PROP_CONTENT, true); contentWriter.setMimetype(MimetypeMap.MIMETYPE_XML); contentWriter.setEncoding("UTF-8"); return Channels.newWriter(contentWriter.getWritableChannel(), "UTF-8"); } }
/** * 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); }
writer.putContent(content); assertFalse("Overlimit content should be deleted from cache", writer.getReader().exists()); assertEquals("Cache writer should still return actual size", overLimitSize, writer.getSize()); assertEquals("Cache writer should still return actual size", overLimitSize, writer.getContentData().getSize());
@Override public Void execute() throws Throwable { ContentWriter writer = contentService.getWriter(testContext.contentNodeRef, ContentModel.PROP_CONTENT, true); writer.setLocale(CONTENT_LOCALE); writer.setEncoding(CONTENT_ENCODING); writer.putContent(testContext.contentString); return null; } };
public String execute() throws Throwable { ContentWriter writer = contentService.getWriter(contentNodeRef, ContentModel.PROP_CONTENT, true); writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN); writer.putContent("CONTENT FOR FAIL"); // This will have updated the metadata, so we can fail now urlsToMiss.add(writer.getContentUrl()); // Done throw new RuntimeException("FAIL"); } };