@Override protected void persistNode(NodeRef nodeRef, FormData data) { // let superclass persist properties super.persistNode(nodeRef, data); // implements File field persistance int fileFieldCount = 0; for (FieldData fieldData : data) { // NOTE: ignore file fields for now, not supported yet! if (fieldData.isFile() == true && fieldData instanceof CustomFormData.FieldData) { CustomFormData.FieldData cfd = (CustomFormData.FieldData) fieldData; if (fileFieldCount == 0) { InputStream inputStream = cfd.getInputStream(); try { if (inputStream.available() > 0) { ContentWriter writer = this.contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true); String mimetype = cfd.getMimetype(); logger.debug("write content in :" + nodeRef); logger.debug("mimeType :" + mimetype); logger.debug("encoding :" + writer.getEncoding()); writer.setMimetype(mimetype); writer.putContent(inputStream); } } catch (InvalidTypeException e1) { logger.error(e1); } catch (ContentIOException e1) { logger.error(e1); } catch (InvalidNodeRefException e1) { logger.error(e1); } catch (IOException e1) { logger.error(e1); } finally { try { inputStream.close(); } catch (IOException e) { logger.error("trying to close inputStream fail", e); } } } else { // TODO multi file upload not implemented yet } fileFieldCount++; } } }
/** * @see * org.alfresco.repo.content.transform.AbstractContentTransformer2#transformInternal(org.alfresco.service.cmr.repository.ContentReader, * org.alfresco.service.cmr.repository.ContentWriter, * org.alfresco.service.cmr.repository.TransformationOptions) */ @Override public void transformInternal( ContentReader reader, ContentWriter writer, TransformationOptions options) throws Exception { final String outputMimetype = writer.getMimetype(); final String outputFileExt = getMimetypeService().getExtension(outputMimetype); // We need to keep a reference to thrown exceptions as we're going to catch them and // then move on to the next transformer. In the event that they all fail, we will throw // the final exception. Exception transformationException = null; for (int i = 0; i < transformers.size(); i++) { int oneBasedCount = i + 1; ContentTransformer transf = transformers.get(i); ContentWriter currentWriter = null; File tempFile = null; try { if (logger.isDebugEnabled()) { logger.debug( "Transformation attempt " + oneBasedCount + " of " + transformers.size() + ": " + transf); } if (!transf.isTransformable( reader.getMimetype(), reader.getSize(), outputMimetype, options)) { throw new UnsupportedTransformationException( "Unsupported transformation: " + reader.getMimetype() + " to " + outputMimetype); } // We can't know in advance which transformer in the sequence will work - if any. // Therefore we can't write into the ContentWriter stream. // So make a temporary file writer with the current transformer name. tempFile = TempFileProvider.createTempFile( "FailoverTransformer_intermediate_" + transf.getClass().getSimpleName() + "_", "." + outputFileExt); currentWriter = new FileContentWriter(tempFile); currentWriter.setMimetype(outputMimetype); currentWriter.setEncoding(writer.getEncoding()); // attempt to transform transf.transform(reader, currentWriter, options); // TODO Could add a check for zero-length output and treat that as a failure // final long writtenSize = currentWriter.getSize(); } catch (Exception are) { if (transformationException == null) { transformationException = are; } if (logger.isDebugEnabled()) { logger.debug("Transformation " + oneBasedCount + " was unsuccessful."); if (i != transformers.size() - 1) { // We don't log the last exception as we're going to throw it. logger.debug("The below exception is provided for information purposes only.", are); } } // Set a new reader to refresh the input stream. reader = reader.getReader(); // and move to the next transformer continue; } // No need to close input or output streams // At this point the current transformation was successful i.e. it did not throw an exception. // Now we must copy the content from the temporary file into the ContentWriter stream. if (tempFile != null) { writer.putContent(tempFile); } if (logger.isInfoEnabled()) { logger.info("Transformation was successful"); } return; } // At this point we have tried all transformers in the sequence without apparent success. if (transformationException != null) { transformerDebug.debug(" No more transformations to failover to"); if (logger.isDebugEnabled()) { logger.debug( "All transformations were unsuccessful. Throwing first exception.", transformationException); } throw transformationException; } }