use of org.alfresco.scripts.ScriptException in project alfresco-repository by Alfresco.
the class RhinoScriptProcessor method execute.
/**
* @see org.alfresco.service.cmr.repository.ScriptProcessor#execute(org.alfresco.service.cmr.repository.ScriptLocation, java.util.Map)
*/
public Object execute(ScriptLocation location, Map<String, Object> model) {
try {
// test the cache for a pre-compiled script matching our path
Script script = null;
String path = location.getPath();
if (this.compile && location.isCachable()) {
script = this.scriptCache.get(path);
}
if (script == null) {
if (logger.isDebugEnabled())
logger.debug("Resolving and compiling script path: " + path);
// retrieve script content and resolve imports
ByteArrayOutputStream os = new ByteArrayOutputStream();
// both streams are closed
FileCopyUtils.copy(location.getInputStream(), os);
byte[] bytes = os.toByteArray();
String source = new String(bytes, "UTF-8");
source = resolveScriptImports(new String(bytes));
// compile the script and cache the result
Context cx = Context.enter();
try {
script = cx.compileString(source, location.toString(), 1, null);
// multi-threaded access to the common cache.
if (this.compile && location.isCachable()) {
this.scriptCache.put(path, script);
}
} finally {
Context.exit();
}
}
String debugScriptName = null;
if (callLogger.isDebugEnabled()) {
int i = path.lastIndexOf('/');
debugScriptName = (i != -1) ? path.substring(i + 1) : path;
}
return executeScriptImpl(script, model, location.isSecure(), debugScriptName);
} catch (Throwable err) {
throw new ScriptException("Failed to execute script '" + location.toString() + "': " + err.getMessage(), err);
}
}
use of org.alfresco.scripts.ScriptException in project alfresco-repository by Alfresco.
the class RhinoScriptProcessor method execute.
/**
* @see org.alfresco.service.cmr.repository.ScriptProcessor#execute(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, java.util.Map)
*/
public Object execute(NodeRef nodeRef, QName contentProp, Map<String, Object> model) {
try {
if (this.services.getNodeService().exists(nodeRef) == false) {
throw new AlfrescoRuntimeException("Script Node does not exist: " + nodeRef);
}
if (contentProp == null) {
contentProp = ContentModel.PROP_CONTENT;
}
ContentReader cr = this.services.getContentService().getReader(nodeRef, contentProp);
if (cr == null || cr.exists() == false) {
throw new AlfrescoRuntimeException("Script Node content not found: " + nodeRef);
}
// compile the script based on the node content
Script script;
Context cx = Context.enter();
try {
script = cx.compileString(resolveScriptImports(cr.getContentString()), nodeRef.toString(), 1, null);
} finally {
Context.exit();
}
return executeScriptImpl(script, model, false, nodeRef.toString());
} catch (Throwable err) {
throw new ScriptException("Failed to execute script '" + nodeRef.toString() + "': " + err.getMessage(), err);
}
}
use of org.alfresco.scripts.ScriptException in project alfresco-repository by Alfresco.
the class ScriptServiceImpl method translateProcessingException.
protected ScriptException translateProcessingException(String scriptInfo, Throwable err) {
ScriptException result = null;
String msg = "Failed to execute script " + (scriptInfo == null ? "" : scriptInfo);
if (logger.isWarnEnabled()) {
logger.warn(msg, err);
}
if (ScriptException.class.isAssignableFrom(err.getClass())) {
result = (ScriptException) err;
} else {
result = new ScriptException(msg, err);
}
return result;
}
use of org.alfresco.scripts.ScriptException in project alfresco-repository by Alfresco.
the class ScriptNodeTest method testCreateFolderPath.
@Test
public void testCreateFolderPath() {
Repository repositoryHelper = (Repository) APP_CONTEXT_INIT.getApplicationContext().getBean("repositoryHelper");
NodeRef companyHome = repositoryHelper.getCompanyHome();
NodeRef folderNodeRef = testNodes.createNode(companyHome, "foldertest1", ContentModel.TYPE_FOLDER, AuthenticationUtil.getFullyAuthenticatedUser());
assertNotNull(folderNodeRef);
ScriptNode folderNode = new ScriptNode(folderNodeRef, SERVICE_REGISTRY);
// create a simple path of depth one - does not exist yet
assertNotNull(folderNode.createFolderPath("One"));
// create a simple path of depth one - does exist (which should be ignored and continue - createFolderPath() emulates 'mkdir -p' behaviour)
assertNotNull(folderNode.createFolderPath("One"));
// create depth path - none of which exists
assertNotNull(folderNode.createFolderPath("A/B"));
// create depth path - all of which exists
assertNotNull(folderNode.createFolderPath("A/B"));
// create depth path - some of which exists
assertNotNull(folderNode.createFolderPath("A/B/C"));
// test last child is returned as the result
NodeRef folderARef = NODE_SERVICE.getChildByName(folderNodeRef, ContentModel.ASSOC_CONTAINS, "A");
NodeRef folderBRef = NODE_SERVICE.getChildByName(folderARef, ContentModel.ASSOC_CONTAINS, "B");
assertEquals(folderBRef, folderNode.createFolderPath("A/B").getNodeRef());
// test case where folder should not should be created - under a content node
NodeRef contentNodeRef = testNodes.createNode(folderNodeRef, "CONTENT", ContentModel.TYPE_CONTENT, AuthenticationUtil.getFullyAuthenticatedUser());
assertNotNull(contentNodeRef);
try {
folderNode.createFolderPath("CONTENT/A");
fail("Should not be able to create folder path when all nodes are not subtypes of cm:folder");
} catch (ScriptException se1) {
// expected
}
// test string edge cases
try {
assertNotNull(folderNode.createFolderPath("/A/B"));
fail("Leading slash not expected");
} catch (Throwable e1) {
// expected
}
try {
assertNotNull(folderNode.createFolderPath("A/B/"));
fail("Trailing slash not expected");
} catch (Throwable e2) {
// expected
}
}
Aggregations