Merged V2.1 to HEAD

6374: AR-1639 Web Script Content Upload
         Patch merge: Schema target 71 changed to 101


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@6404 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2007-08-02 23:44:15 +00:00
parent feca4b296a
commit 9f7c5d6443
10 changed files with 360 additions and 19 deletions

View File

@@ -24,6 +24,9 @@
*/
package org.alfresco.repo.jscript;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
@@ -51,14 +54,13 @@ import org.alfresco.service.cmr.model.FileNotFoundException;
import org.alfresco.service.cmr.repository.AssociationRef;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.service.cmr.repository.ContentIOException;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.InvalidNodeRefException;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.repository.TemplateException;
import org.alfresco.service.cmr.repository.TemplateImageResolver;
import org.alfresco.service.cmr.search.QueryParameterDefinition;
import org.alfresco.service.cmr.security.AccessPermission;
@@ -77,6 +79,7 @@ import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.Wrapper;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
/**
@@ -2093,10 +2096,63 @@ public class ScriptNode implements Serializable, Scopeable
// ------------------------------------------------------------------------------
// Inner Classes
/**
* Inner class for representing content
*/
public static abstract class ScriptContent implements Serializable
{
/**
* @return the content stream as a string
*/
public abstract String getContent();
public String jsGet_content()
{
return getContent();
}
/**
* @return the content mimetype
*/
public abstract String getMimetype();
public String jsGet_mimetype()
{
return getMimetype();
}
/**
* @return the content encoding
*/
public abstract String getEncoding();
public String jsGet_encoding()
{
return getEncoding();
}
/**
* @return the content size
*/
public abstract long getSize();
public long jsGet_size()
{
return getSize();
}
/**
* @return input stream onto content
*/
/*package*/ abstract InputStream getInputStream();
}
/**
* Inner class wrapping and providing access to a ContentData property
*/
public class ScriptContentData implements Serializable
public class ScriptContentData extends ScriptContent implements Serializable
{
private static final long serialVersionUID = -7819328543933312278L;
@@ -2111,9 +2167,9 @@ public class ScriptNode implements Serializable, Scopeable
this.contentData = contentData;
this.property = property;
}
/**
* @return the content stream
/* (non-Javadoc)
* @see org.alfresco.repo.jscript.ScriptNode.ScriptContent#getContent()
*/
public String getContent()
{
@@ -2123,9 +2179,12 @@ public class ScriptNode implements Serializable, Scopeable
return (reader != null && reader.exists()) ? reader.getContentString() : "";
}
public String jsGet_content()
/*package*/ InputStream getInputStream()
{
return getContent();
ContentService contentService = services.getContentService();
ContentReader reader = contentService.getReader(nodeRef, property);
return (reader != null && reader.exists()) ? reader.getContentInputStream() : null;
}
/**
@@ -2148,7 +2207,24 @@ public class ScriptNode implements Serializable, Scopeable
{
setContent(content);
}
/**
* Set the content stream from another content object
*
* @param content ScriptContent to set
*/
public void write(ScriptContent content)
{
ContentService contentService = services.getContentService();
ContentWriter writer = contentService.getWriter(nodeRef, this.property, true);
writer.setMimetype(content.getMimetype());
writer.setEncoding(content.getEncoding());
writer.putContent(content.getInputStream());
// update cached variables after putContent()
this.contentData = (ContentData) services.getNodeService().getProperty(nodeRef, this.property);
}
/**
* @return download URL to the content
*/
@@ -2203,28 +2279,22 @@ public class ScriptNode implements Serializable, Scopeable
{
return getDownloadUrl();
}
public long getSize()
{
return contentData.getSize();
}
public long jsGet_size()
{
return getSize();
}
public String getMimetype()
{
return contentData.getMimetype();
}
public String jsGet_mimetype()
public String getEncoding()
{
return getMimetype();
return contentData.getEncoding();
}
public void setMimetype(String mimetype)
{
this.contentData = ContentData.setMimetype(this.contentData, mimetype);
@@ -2244,6 +2314,79 @@ public class ScriptNode implements Serializable, Scopeable
private QName property;
}
/**
* Inner class wrapping and providing access to a Content stream
*/
public static class ScriptContentStream extends ScriptContent implements Serializable
{
private static final long serialVersionUID = -7819328543933312278L;
/**
* Constructor
*
* @param stream content input stream
* @param mimetype content mimetype
*/
public ScriptContentStream(InputStream stream, String mimetype, String encoding)
{
this.stream = stream;
this.mimetype = mimetype;
this.encoding = encoding;
}
/* (non-Javadoc)
* @see org.alfresco.repo.jscript.ScriptNode.ScriptContent#getContent()
*/
public String getContent()
{
try
{
ByteArrayOutputStream os = new ByteArrayOutputStream();
FileCopyUtils.copy(stream, os); // both streams are closed
byte[] bytes = os.toByteArray();
// get the encoding for the string
String encoding = getEncoding();
// create the string from the byte[] using encoding if necessary
String content = (encoding == null) ? new String(bytes) : new String(bytes, encoding);
// done
return content;
}
catch (IOException e)
{
throw new ContentIOException("Failed to copy content to string", e);
}
}
/*package*/ InputStream getInputStream()
{
return stream;
}
public long getSize()
{
return -1;
}
public String getMimetype()
{
return mimetype;
}
public String getEncoding()
{
return encoding;
}
private InputStream stream;
private String mimetype;
private String encoding;
}
/**
* Interface contract for simple anonymous classes that implement document transformations
*/