org.alfresco.service.cmr.repository.ContentIOException Java Examples
The following examples show how to use
org.alfresco.service.cmr.repository.ContentIOException.
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: MoveCapableCommonRoutingContentStore.java From alfresco-simple-content-stores with Apache License 2.0 | 6 votes |
/** * * {@inheritDoc} */ @Override public boolean delete(final String contentUrl) throws ContentIOException { boolean deleted = true; final List<ContentStore> stores = this.getAllStores(); /* * This operation has to be performed on all the stores in order to maintain the * {@link ContentStore#exists(String)} contract. * Still need to apply the isContentUrlSupported guard though. */ for (final ContentStore store : stores) { if (store.isContentUrlSupported(contentUrl) && store.isWriteSupported()) { deleted &= store.delete(contentUrl); } } LOGGER.debug("Deleted content URL from stores: \n\tStores: {}\n\tDeleted: {}", stores.size(), deleted); return deleted; }
Example #2
Source File: SpoofedTextContentReaderTest.java From alfresco-repository with GNU Lesser General Public License v3.0 | 6 votes |
public void testGetContentString_01() { // To URL String url = SpoofedTextContentReader.createContentUrl(Locale.ENGLISH, 12345L, 56L, "harry"); // To Reader ContentReader reader = new SpoofedTextContentReader(url); String readerText = reader.getContentString(); assertEquals("harry have voice the from countered growth invited ", readerText); // Cannot repeat try { reader.getContentString(); fail("Should not be able to reread content."); } catch (ContentIOException e) { // Expected } // Get a new Reader reader = reader.getReader(); // Get exactly the same text assertEquals(readerText, reader.getContentString()); }
Example #3
Source File: OOoContentTransformerHelper.java From alfresco-repository with GNU Lesser General Public License v3.0 | 6 votes |
/** * Populates a file with the content in the reader, but also converts the encoding to UTF-8. */ private void saveContentInUtf8File(ContentReader reader, File file) { String encoding = reader.getEncoding(); try { Reader in = new InputStreamReader(reader.getContentInputStream(), encoding); Writer out = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(file)), "UTF-8"); FileCopyUtils.copy(in, out); // both streams are closed } catch (IOException e) { throw new ContentIOException("Failed to copy content to file and convert "+encoding+" to UTF-8: \n" + " file: " + file, e); } }
Example #4
Source File: ContentReaderFacade.java From alfresco-simple-content-stores with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} */ @Override // same implementation as AbstractContentReader public String getContentString() throws ContentIOException { try { // read from the stream into a byte[] final InputStream is = this.getContentInputStream(); final ByteArrayOutputStream os = new ByteArrayOutputStream(); FileCopyUtils.copy(is, os); // both streams are closed final byte[] bytes = os.toByteArray(); // get the encoding for the string final String encoding = this.getEncoding(); // create the string from the byte[] using encoding if necessary final String systemEncoding = System.getProperty("file.encoding"); final String content = (encoding == null) ? new String(bytes, systemEncoding) : new String(bytes, encoding); // done return content; } catch (final IOException e) { throw new ContentIOException("Failed to copy content to string: \n" + " accessor: " + this, e); } }
Example #5
Source File: AbstractWritableContentStoreTest.java From alfresco-repository with GNU Lesser General Public License v3.0 | 6 votes |
/** * Checks that the writer can have a listener attached */ @Test public void testWriteStreamListener() throws Exception { ContentWriter writer = getWriter(); final boolean[] streamClosed = new boolean[] {false}; // has to be final ContentStreamListener listener = new ContentStreamListener() { public void contentStreamClosed() throws ContentIOException { streamClosed[0] = true; } }; writer.addListener(listener); // write some content writer.putContent("ABC"); // check that the listener was called assertTrue("Write stream listener was not called for the stream close", streamClosed[0]); }
Example #6
Source File: CachingContentStoreTest.java From alfresco-repository with GNU Lesser General Public License v3.0 | 6 votes |
@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(); }
Example #7
Source File: EncryptingContentWriterFacade.java From alfresco-simple-content-stores with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} */ @Override public ContentReader getReader() throws ContentIOException { final ContentReader reader; if (this.completedWrite) { reader = new DecryptingContentReaderFacade(super.getReader(), this.key, this.unencryptedSize); } else { reader = new EmptyContentReader(this.getContentUrl()); } return reader; }
Example #8
Source File: AbstractContentWriter.java From alfresco-repository with GNU Lesser General Public License v3.0 | 6 votes |
/** * @see Channels#newOutputStream(java.nio.channels.WritableByteChannel) */ public OutputStream getContentOutputStream() throws ContentIOException { try { WritableByteChannel channel = getWritableChannel(); OutputStream is = new BufferedOutputStream(Channels.newOutputStream(channel)); // done return is; } catch (Throwable e) { throw new ContentIOException("Failed to open stream onto channel: \n" + " writer: " + this, e); } }
Example #9
Source File: FileContentWriter.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
@Override protected WritableByteChannel getDirectWritableChannel() throws ContentIOException { try { // we may not write to an existing file - EVER!! if (file.exists() && file.length() > 0) { throw new IOException("File exists - overwriting not allowed"); } // create the channel WritableByteChannel channel = null; if (allowRandomAccess) { RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); // will create it channel = randomAccessFile.getChannel(); } else { OutputStream os = new FileOutputStream(file); channel = Channels.newChannel(os); } // done if (logger.isDebugEnabled()) { logger.debug("Opened write channel to file: \n" + " file: " + file + "\n" + " random-access: " + allowRandomAccess); } return channel; } catch (Throwable e) { throw new ContentIOException("Failed to open file channel: " + this, e); } }
Example #10
Source File: FileContentWriterImpl.java From alfresco-simple-content-stores with Apache License 2.0 | 5 votes |
/** * * {@inheritDoc} */ @Override protected WritableByteChannel getDirectWritableChannel() throws ContentIOException { try { if (this.file.exists() && this.file.length() > 0) { throw new IOException("File exists - overwriting not allowed"); } WritableByteChannel channel = null; if (this.allowRandomAccess) { @SuppressWarnings("resource") final RandomAccessFile randomAccessFile = new RandomAccessFile(this.file, "rw"); // will create it channel = randomAccessFile.getChannel(); } else { final OutputStream os = new FileOutputStream(this.file); channel = Channels.newChannel(os); } LOGGER.debug("Opened write channel to file: \n\tfile: {}\n\trandom-access: {}", this.file, this.allowRandomAccess); return channel; } catch (final Throwable e) { throw new ContentIOException("Failed to open file channel: " + this, e); } }
Example #11
Source File: MoveCapableCommonRoutingContentStore.java From alfresco-simple-content-stores with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public ContentReader getReader(final String contentUrl) throws ContentIOException { final ContentReader reader; if (this.isContentUrlSupported(contentUrl)) { final ContentStore store = this.selectReadStore(contentUrl); if (store != null) { LOGGER.debug("Getting reader from store: \n\tContent URL: {}\n\tStore: {}", contentUrl, store); reader = store.getReader(contentUrl); } else { LOGGER.debug("Getting empty reader for content URL: {}", contentUrl); reader = new EmptyContentReader(contentUrl); } } else { LOGGER.debug("Getting empty reader for unsupported content URL: {}", contentUrl); reader = new EmptyContentReader(contentUrl); } return reader; }
Example #12
Source File: DecompressingContentReader.java From alfresco-simple-content-stores with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public synchronized ReadableByteChannel getReadableChannel() throws ContentIOException { this.ensureDelegate(); final String mimetype = this.getMimetype(); LOGGER.debug("Determined mimetype {} as provided via setter / content data - mimetypes to compress are {}", mimetype, this.mimetypesToCompress); final boolean shouldCompress = this.mimetypesToCompress == null || this.mimetypesToCompress.isEmpty() || (mimetype != null && (this.mimetypesToCompress.contains(mimetype) || this.isMimetypeToCompressWildcardMatch(mimetype))); ReadableByteChannel channel; if (shouldCompress) { LOGGER.debug("Content will be decompressed from backing store (url={})", this.getContentUrl()); final String compressiongType = this.compressionType != null && !this.compressionType.trim().isEmpty() ? this.compressionType : CompressorStreamFactory.GZIP; try { final CompressorInputStream is = COMPRESSOR_STREAM_FACTORY.createCompressorInputStream(compressiongType, this.delegate.getContentInputStream()); channel = Channels.newChannel(is); } catch (final CompressorException e) { LOGGER.error("Failed to open decompressing channel", e); throw new ContentIOException("Failed to open channel: " + this, e); } } else { LOGGER.debug("Content will not be decompressed from backing store (url={})", this.getContentUrl()); channel = super.getReadableChannel(); } return channel; }
Example #13
Source File: DeletedContentBackupCleanerListener.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
public void beforeDelete(ContentStore sourceStore, String contentUrl) throws ContentIOException { if (store.isContentUrlSupported(contentUrl)) { ContentContext context = new ContentContext(null, contentUrl); ContentReader reader = sourceStore.getReader(contentUrl); if (!reader.exists()) { // Nothing to copy over return; } // write the content into the target store ContentWriter writer = store.getWriter(context); // copy across writer.putContent(reader); // done if (logger.isDebugEnabled()) { logger.debug( "Moved content before deletion: \n" + " URL: " + contentUrl + "\n" + " Source: " + sourceStore + "\n" + " Target: " + store); } } else { if (logger.isDebugEnabled()) { logger.debug( "Content cannot be moved during deletion. A backup will not be made: \n" + " URL: " + contentUrl + "\n" + " Source: " + sourceStore + "\n" + " Target: " + store); } } }
Example #14
Source File: StreamAwareContentWriterProxy.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
@Override public FileChannel getFileChannel(boolean truncate) throws ContentIOException { FileChannel result = delegatee.getFileChannel(truncate); if (null == releaseableResource) { releaseableResource = result; } return result; }
Example #15
Source File: FileWipingContentCleanerListener.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
public void beforeDelete(ContentStore sourceStore, String contentUrl) throws ContentIOException { // First check if the content is present at all ContentReader reader = sourceStore.getReader(contentUrl); if (reader != null && reader.exists()) { // Call to implementation's shred if (logger.isDebugEnabled()) { logger.debug( "About to shread: \n" + " URL: " + contentUrl + "\n" + " Source: " + sourceStore); } try { shred(reader); } catch (Throwable e) { logger.error( "Content shredding failed: \n" + " URL: " + contentUrl + "\n" + " Source: " + sourceStore + "\n" + " Reader: " + reader, e); } } else { logger.error( "Content no longer exists. Unable to shred: \n" + " URL: " + contentUrl + "\n" + " Source: " + sourceStore); } }
Example #16
Source File: AbstractContentWriter.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
public final void putContent(InputStream is) throws ContentIOException { try { OutputStream os = getContentOutputStream(); copyStreams(is, os); // both streams are closed // done } catch (IOException e) { throw new ContentIOException("Failed to copy content from input stream: \n" + " writer: " + this, e); } }
Example #17
Source File: AbstractContentReader.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
/** * Does a comparison of the binaries associated with two readers. Several shortcuts are assumed to be valid:<br/> * - if the readers are the same instance, then the binaries are the same<br/> * - if the size field is different, then the binaries are different<br/> * Otherwise the binaries are {@link EqualsHelper#binaryStreamEquals(InputStream, InputStream) compared}. * * @return Returns <tt>true</tt> if the underlying binaries are the same * @throws ContentIOException */ public static boolean compareContentReaders(ContentReader left, ContentReader right) throws ContentIOException { if (left == right) { return true; } else if (left == null || right == null) { return false; } else if (left.getSize() != right.getSize()) { return false; } InputStream leftIs = left.getContentInputStream(); InputStream rightIs = right.getContentInputStream(); try { return EqualsHelper.binaryStreamEquals(leftIs, rightIs); } catch (IOException e) { throw new ContentIOException( "Failed to compare content reader streams: \n" + " Left: " + left + "\n" + " right: " + right); } }
Example #18
Source File: FailoverUnsupportedSubtransformerTest.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
public void testExcelToPdfConversion() throws Exception { String[] quickFiles = getQuickFilenames(sourceMimeType); for (String quickFile : quickFiles) { String sourceExtension = quickFile.substring(quickFile.lastIndexOf('.') + 1); // is there a test file for this conversion? File sourceFile = AbstractContentTransformerTest.loadNamedQuickTestFile(quickFile); if (sourceFile == null) { continue; // no test file available for that extension } ContentReader sourceReader = new FileContentReader(sourceFile); // make a writer for the target file File targetFile = TempFileProvider.createTempFile(getClass().getSimpleName() + "_" + getName() + "_" + sourceExtension + "_", ".pdf"); ContentWriter targetWriter = new FileContentWriter(targetFile); // do the transformation sourceReader.setMimetype(sourceMimeType); targetWriter.setMimetype(targetMimeType); try { transformer.transform(sourceReader.getReader(), targetWriter); } catch (ContentIOException e) { // all transformers expected to fail for password protected MS office document } if (transformer.getTriggered().getValue()) { org.junit.Assert.fail("final AbstractContentTransformer2.transform was called for html2pdf"); } } }
Example #19
Source File: StreamAwareContentWriterProxy.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
@Override public WritableByteChannel getWritableChannel() throws ContentIOException { WritableByteChannel result = delegatee.getWritableChannel(); if (null == releaseableResource) { releaseableResource = result; } return result; }
Example #20
Source File: FileContentWriter.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
/** * The URL of the write is known from the start and this method contract states * that no consideration needs to be taken w.r.t. the stream state. */ @Override protected ContentReader createReader() throws ContentIOException { FileContentReader reader = new FileContentReader(this.file, getContentUrl()); reader.setAllowRandomAccess(this.allowRandomAccess); return reader; }
Example #21
Source File: FileContentWriterImpl.java From alfresco-simple-content-stores with Apache License 2.0 | 5 votes |
/** * * {@inheritDoc} */ @Override protected ContentReader createReader() throws ContentIOException { /* * The URL of the write is known from the start and this method contract states * that no consideration needs to be taken w.r.t. the stream state. */ final FileContentReaderImpl reader = new FileContentReaderImpl(this.file, this.getContentUrl()); reader.setAllowRandomAccess(this.allowRandomAccess); return reader; }
Example #22
Source File: ContentTransformServiceAdaptor.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
@Deprecated @Override public void transform(ContentReader reader, ContentWriter writer, Map<String, Object> legacyOptionsMap) throws NoTransformerException, ContentIOException { TransformationOptions transformationOptions = new TransformationOptions(legacyOptionsMap); Map<String, String> options = converter.getOptions(transformationOptions, null, null); synchronousTransformClient.transform(reader, writer, options, null, null); }
Example #23
Source File: FileContentStore.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
private void ensureFileInContentStore(File file) { String fileNormalizedAbsoultePath = FilenameUtils.normalize(file.getAbsolutePath()); String rootNormalizedAbsolutePath = FilenameUtils.normalize(rootAbsolutePath); if (!fileNormalizedAbsoultePath.startsWith(rootNormalizedAbsolutePath)) { throw new ContentIOException("Access to files outside of content store root is not allowed: " + file); } }
Example #24
Source File: FileContentStore.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
/** * Synchronized and retrying directory creation. Repeated attempts will be made to create the * directory, subject to a limit on the number of retries. * * @param dir the directory to create * @throws IOException if an IO error occurs */ private synchronized void makeDirectory(File dir) throws IOException { /* * Once in this method, the only contention will be from other file stores or processes. * This is OK as we have retrying to sort it out. */ if (dir.exists()) { // Beaten to it during synchronization return; } // 20 attempts with 20 ms wait each time for (int i = 0; i < 20; i++) { boolean created = dir.mkdirs(); if (created) { // Successfully created return; } // Wait try { this.wait(20L); } catch (InterruptedException e) {} // Did it get created in the meantime if (dir.exists()) { // Beaten to it while asleep return; } } // It still didn't succeed throw new ContentIOException("Failed to create directory for file storage: " + dir); }
Example #25
Source File: FileContentStore.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
/** * Creates a file for the specifically provided content URL. The URL may * not already be in use. * <p> * The store prefix is stripped off the URL and the rest of the URL * used directly to create a file. * * @param newContentUrl the specific URL to use, which may not be in use * @return Returns a new and unique file * @throws IOException * if the file or parent directories couldn't be created or if the URL is already in use. * @throws UnsupportedOperationException * if the store is read-only * * @see #setReadOnly(boolean) */ /*package*/ File createNewFile(String newContentUrl) throws IOException { if (readOnly) { throw new UnsupportedOperationException("This store is currently read-only: " + this); } File file = makeFile(newContentUrl); // create the directory, if it doesn't exist File dir = file.getParentFile(); if (!dir.exists()) { makeDirectory(dir); } // create a new, empty file boolean created = file.createNewFile(); if (!created) { throw new ContentIOException( "When specifying a URL for new content, the URL may not be in use already. \n" + " store: " + this + "\n" + " new URL: " + newContentUrl); } // done return file; }
Example #26
Source File: FileContentStore.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
/** * Private: for Spring-constructed instances only. * * @param rootDirectory * the root under which files will be stored. The directory will be created if it does not exist. */ private FileContentStore(File rootDirectory) { if (!rootDirectory.exists()) { if (!rootDirectory.mkdirs()) { throw new ContentIOException("Failed to create store root: " + rootDirectory, null); } } this.rootDirectory = rootDirectory.getAbsoluteFile(); rootAbsolutePath = rootDirectory.getAbsolutePath(); allowRandomAccess = true; readOnly = false; }
Example #27
Source File: FileContentReader.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
@Override protected ReadableByteChannel getDirectReadableChannel() throws ContentIOException { try { // the file must exist if (!file.exists()) { throw new IOException("File does not exist: " + file); } // create the channel ReadableByteChannel channel = null; if (allowRandomAccess) { RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r"); // won't create it channel = randomAccessFile.getChannel(); } else { InputStream is = new FileInputStream(file); channel = Channels.newChannel(is); } // done if (logger.isDebugEnabled()) { logger.debug("Opened read channel to file: \n" + " file: " + file + "\n" + " random-access: " + allowRandomAccess); } return channel; } catch (Throwable e) { throw new ContentIOException("Failed to open file channel: " + this, e); } }
Example #28
Source File: FileContentStore.java From alfresco-simple-content-stores with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override protected ContentWriter getWriterInternal(final ContentReader existingContentReader, final String newContentUrl) { String contentUrl = null; try { if (newContentUrl == null) { contentUrl = this.createNewFileStoreUrl(); } else { contentUrl = ContentUrlUtils.checkAndReplaceWildcardProtocol(newContentUrl, this.protocol); } final File file = this.createNewFile(contentUrl); final FileContentWriterImpl writer = new FileContentWriterImpl(file, contentUrl, existingContentReader); if (this.contentLimitProvider != null) { writer.setContentLimitProvider(this.contentLimitProvider); } writer.setAllowRandomAccess(this.allowRandomAccess); LOGGER.debug("Created content writer: \n writer: {}", writer); return writer; } catch (final Throwable e) { LOGGER.error("Error creating writer for {}", contentUrl, e); throw new ContentIOException("Failed to get writer for URL: " + contentUrl, e); } }
Example #29
Source File: ContentStoreContext.java From alfresco-simple-content-stores with Apache License 2.0 | 5 votes |
/** * Executes an operation within a new content store context. Code in the provided operation can call * {@link #setContextAttribute(String, Object) setContextAttribute} and be sure no {@link IllegalStateException} will be thrown. * * @param <R> * the return type of the operation to execute * @param operation * the operation to execute * @return the result of the operation */ public static <R> R executeInNewContext(final ContentStoreOperation<R> operation) { final Map<String, Object> oldMap = CONTEXT_ATTRIBUTES.get(); final Map<String, Object> newMap = new HashMap<>(); CONTEXT_ATTRIBUTES.set(newMap); try { LOGGER.trace("Running operation {} in new context", operation); final R result = operation.execute(); return result; } catch (final Exception ex) { if (ex instanceof RuntimeException) { throw (RuntimeException) ex; } LOGGER.debug("Unhandled exception in content store context operation", ex); throw new ContentIOException("Unhandled error in content store operation", ex); } finally { LOGGER.trace("Leaving context"); CONTEXT_ATTRIBUTES.set(oldMap); } }
Example #30
Source File: ContentWriterFacade.java From alfresco-simple-content-stores with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public void putContent(final InputStream is) throws ContentIOException { try { final OutputStream os = this.getContentOutputStream(); FileCopyUtils.copy(is, os); } catch (final IOException e) { LOGGER.error("Content writer {} failed to copy content from input stream", this, e); throw new ContentIOException("Failed to copy content from input stream: \n\twriter: " + this, e); } }