Search in sources :

Example 6 with ScriptException

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);
    }
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) ScriptException(org.alfresco.scripts.ScriptException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 7 with ScriptException

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);
    }
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) ScriptException(org.alfresco.scripts.ScriptException) ContentReader(org.alfresco.service.cmr.repository.ContentReader) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException)

Example 8 with ScriptException

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;
}
Also used : ScriptException(org.alfresco.scripts.ScriptException)

Example 9 with ScriptException

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
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ScriptException(org.alfresco.scripts.ScriptException) Repository(org.alfresco.repo.model.Repository) ActivitiScriptNode(org.alfresco.repo.workflow.activiti.ActivitiScriptNode) Test(org.junit.Test)

Aggregations

ScriptException (org.alfresco.scripts.ScriptException)9 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)5 UnimportantTransformException (org.alfresco.repo.content.transform.UnimportantTransformException)3 NodeRef (org.alfresco.service.cmr.repository.NodeRef)3 Context (org.mozilla.javascript.Context)3 Script (org.mozilla.javascript.Script)3 IOException (java.io.IOException)2 UnsupportedTransformationException (org.alfresco.repo.content.transform.UnsupportedTransformationException)2 InvalidTypeException (org.alfresco.repo.model.filefolder.FileFolderServiceImpl.InvalidTypeException)2 FileExistsException (org.alfresco.service.cmr.model.FileExistsException)2 FileNotFoundException (org.alfresco.service.cmr.model.FileNotFoundException)2 ContentIOException (org.alfresco.service.cmr.repository.ContentIOException)2 NoTransformerException (org.alfresco.service.cmr.repository.NoTransformerException)2 NamespaceException (org.alfresco.service.namespace.NamespaceException)2 JSONException (org.json.JSONException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 Serializable (java.io.Serializable)1