From 8d2f8a159d0b4d2e18538d8af2091986e0e697fe Mon Sep 17 00:00:00 2001 From: Kevin Roast Date: Mon, 10 Oct 2011 14:34:21 +0000 Subject: [PATCH] Fix for ALF-7654 - Share content upload performs several updates to cm:content property - added specialised write(content) method to ScriptNode to allow the mimetype, encoding etc. to all be calculated and applied in a single call. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@31088 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../org/alfresco/repo/jscript/ScriptNode.java | 43 ++++++++++++++++--- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/source/java/org/alfresco/repo/jscript/ScriptNode.java b/source/java/org/alfresco/repo/jscript/ScriptNode.java index f5af05fb3e..0573aa1db4 100644 --- a/source/java/org/alfresco/repo/jscript/ScriptNode.java +++ b/source/java/org/alfresco/repo/jscript/ScriptNode.java @@ -3454,7 +3454,7 @@ public class ScriptNode implements Scopeable, NamespacePrefixResolverProvider } /** - * Set the content stream from another content object + * Set the content stream from another content object. * * @param content ScriptContent to set */ @@ -3469,9 +3469,39 @@ public class ScriptNode implements Scopeable, NamespacePrefixResolverProvider // update cached variables after putContent() this.contentData = (ContentData) services.getNodeService().getProperty(nodeRef, this.property); } + + /** + * Set the content stream from another content object. + * + * @param content ScriptContent to set + * @param applyMimetype If true, apply the mimetype from the Content object, else leave the original mimetype + * @param guessEncoding If true, guess the encoding from the underlying input stream, else use encoding set in + * the Content object as supplied. + */ + public void write(Content content, boolean applyMimetype, boolean guessEncoding) + { + ContentService contentService = services.getContentService(); + ContentWriter writer = contentService.getWriter(nodeRef, this.property, true); + if (applyMimetype) + { + writer.setMimetype(content.getMimetype()); + } + if (guessEncoding) + { + writer.setEncoding(guessEncoding(content.getInputStream())); + } + else + { + writer.setEncoding(content.getEncoding()); + } + writer.putContent(content.getInputStream()); + + // update cached variables after putContent() + this.contentData = (ContentData) services.getNodeService().getProperty(nodeRef, this.property); + } /** - * Set the content stream from another input stream + * Set the content stream from another input stream. * * @param inputStream */ @@ -3587,12 +3617,15 @@ public class ScriptNode implements Scopeable, NamespacePrefixResolverProvider * the appropriate encoding (such as UTF-16 or similar) will be appiled if detected. */ public void guessEncoding() + { + setEncoding(guessEncoding(getInputStream())); + } + + private String guessEncoding(InputStream in) { String encoding = "UTF-8"; - InputStream in = null; try { - in = getInputStream(); if (in != null) { Charset charset = services.getMimetypeService().getContentCharsetFinder().getCharset(in, getMimetype()); @@ -3612,7 +3645,7 @@ public class ScriptNode implements Scopeable, NamespacePrefixResolverProvider { } } - setEncoding(encoding); + return encoding; } private ContentData contentData;