Class GroovyScript2RestLoader

  • All Implemented Interfaces:
    org.picocontainer.Startable

    public class GroovyScript2RestLoader
    extends Object
    implements org.picocontainer.Startable
    GroovyScript2RestLoader allows to save a Groovy script in JCR.
    Version:
    $Id: GroovyScript2RestLoader.java 34445 2009-07-24 07:51:18Z dkatayev $
    Author:
    Andrey Parfonov
    • Field Detail

      • groovyPublisher

        protected org.exoplatform.services.rest.ext.groovy.GroovyJaxrsPublisher groovyPublisher
      • initParams

        protected org.exoplatform.container.xml.InitParams initParams
        See InitParams.
      • repositoryService

        protected org.exoplatform.services.jcr.RepositoryService repositoryService
        See RepositoryService.
      • configurationManager

        protected org.exoplatform.container.configuration.ConfigurationManager configurationManager
        See ConfigurationManager.
    • Constructor Detail

      • GroovyScript2RestLoader

        public GroovyScript2RestLoader​(org.exoplatform.services.rest.impl.ResourceBinder binder,
                                       org.exoplatform.services.script.groovy.GroovyScriptInstantiator groovyScriptInstantiator,
                                       org.exoplatform.services.jcr.RepositoryService repositoryService,
                                       SessionProviderService sessionProviderService,
                                       org.exoplatform.container.configuration.ConfigurationManager configurationManager,
                                       Handler jcrUrlHandler,
                                       org.exoplatform.container.xml.InitParams params)
        Parameters:
        binder - binder for RESTful services
        groovyScriptInstantiator - instantiate groovy scripts
        repositoryService - See RepositoryService
        sessionProviderService - See SessionProviderService
        configurationManager - for solve resource loading issue in common way
        params - initialized parameters
      • GroovyScript2RestLoader

        public GroovyScript2RestLoader​(org.exoplatform.services.rest.impl.ResourceBinder binder,
                                       org.exoplatform.services.script.groovy.GroovyScriptInstantiator groovyScriptInstantiator,
                                       org.exoplatform.services.jcr.RepositoryService repositoryService,
                                       SessionProviderService sessionProviderService,
                                       org.exoplatform.container.configuration.ConfigurationManager configurationManager,
                                       RegistryService registryService,
                                       Handler jcrUrlHandler,
                                       org.exoplatform.container.xml.InitParams params)
        Parameters:
        binder - binder for RESTful services
        groovyScriptInstantiator - instantiates Groovy scripts
        repositoryService - See RepositoryService
        sessionProviderService - See SessionProviderService
        configurationManager - for solve resource loading issue in common way
        registryService - See RegistryService
        params - initialized parameters
      • GroovyScript2RestLoader

        public GroovyScript2RestLoader​(org.exoplatform.services.rest.impl.ResourceBinder binder,
                                       org.exoplatform.services.script.groovy.GroovyScriptInstantiator groovyScriptInstantiator,
                                       org.exoplatform.services.jcr.RepositoryService repositoryService,
                                       SessionProviderService sessionProviderService,
                                       org.exoplatform.container.configuration.ConfigurationManager configurationManager,
                                       RegistryService registryService,
                                       org.exoplatform.services.rest.ext.groovy.GroovyJaxrsPublisher groovyPublisher,
                                       Handler jcrUrlHandler,
                                       org.exoplatform.container.xml.InitParams params)
    • Method Detail

      • getNodeType

        public String getNodeType()
        Get node type for store scripts, may throw IllegalStateException if nodeType not initialized yet.
        Returns:
        return node type
      • start

        public void start()
        Specified by:
        start in interface org.picocontainer.Startable
        See Also:
        Startable.start()
      • stop

        public void stop()
        Specified by:
        stop in interface org.picocontainer.Startable
        See Also:
        Startable.stop()
      • addPlugin

        public void addPlugin​(org.exoplatform.container.component.ComponentPlugin cp)
      • addScripts

        protected void addScripts()
        Add scripts that specified in configuration.
      • createScript

        protected javax.jcr.Node createScript​(javax.jcr.Node parent,
                                              String name,
                                              boolean autoload,
                                              InputStream stream)
                                       throws Exception
        Create JCR node.
        Parameters:
        parent - parent node
        name - name of node to be created
        stream - data stream for property jcr:data
        Returns:
        newly created node
        Throws:
        Exception - if any errors occurs
      • readParamsFromRegistryService

        protected void readParamsFromRegistryService​(SessionProvider sessionProvider)
                                              throws javax.jcr.PathNotFoundException,
                                                     javax.jcr.RepositoryException
        Read parameters from RegistryService.
        Parameters:
        sessionProvider - the SessionProvider
        Throws:
        javax.jcr.RepositoryException
        javax.jcr.PathNotFoundException
      • getAttributeSmart

        protected String getAttributeSmart​(Element element,
                                           String attr)
        Get attribute value.
        Parameters:
        element - The element to get attribute value
        attr - The attribute name
        Returns:
        Value of attribute if present and null in other case
      • setAttributeSmart

        protected void setAttributeSmart​(Element element,
                                         String attr,
                                         String value)
        Set attribute value. If value is null the attribute will be removed.
        Parameters:
        element - The element to set attribute value
        attr - The attribute name
        value - The value of attribute
      • readParamsFromFile

        protected void readParamsFromFile()
        Read parameters from file.
      • addScript

        public javax.ws.rs.core.Response addScript​(InputStream stream,
                                                   @Context
                                                   javax.ws.rs.core.UriInfo uriInfo,
                                                   String repository,
                                                   String workspace,
                                                   String path)
        This method is useful for clients that can send script in request body without form-data. At required to set specific Content-type header 'script/groovy'.
        Parameters:
        stream - the stream that contains groovy source code
        uriInfo - see javax.ws.rs.core.UriInfo
        repository - repository name
        workspace - workspace name
        path - path to resource to be created
        Returns:
        Response with status 'created'
        eXo level API
        Provisional
        eXo anchor for generated doc on REST API
        {code} "stream" : the input stream that contains groovy source code {code}
      • addScript

        public javax.ws.rs.core.Response addScript​(Iterator<org.apache.commons.fileupload.FileItem> items,
                                                   @Context
                                                   javax.ws.rs.core.UriInfo uriInfo,
                                                   String repository,
                                                   String workspace,
                                                   String path)
        This method is useful for clients that send scripts as file in 'multipart/*' request body. NOTE even we use iterator item should be only one, rule one address - one script. This method is created just for comfort loading script from HTML form. NOT use this script for uploading few files in body of 'multipart/form-data' or other type of multipart.
        Parameters:
        items - iterator org.apache.commons.fileupload.FileItem
        uriInfo - javax.ws.rs.core.UriInfo
        repository - repository name
        workspace - workspace name
        path - path to resource to be created
        Returns:
        Response with status 'created'
        eXo level API
        Provisional
        eXo anchor for generated doc on REST API
        {code} "items" : the sending data with HTML form ('multipart/form-data') {code}
      • validateScript

        public javax.ws.rs.core.Response validateScript​(String name,
                                                        InputStream script,
                                                        List<String> sources,
                                                        List<String> files)
        Check is specified source script contains valid Groovy source code.
        Parameters:
        name - script name. This name will be used by GroovyClassLoader to identify script, e.g. specified name will be used in error message in compilation of Groovy fails. If this parameter is null then GroovyClassLoader will use automatically generated name
        script - Groovy source stream
        sources - locations (string representation of URL) of source folders that should be add in class path when compile Groovy script. NOTE : To be able load Groovy source files from specified folders the following rules must be observed: - Groovy source files must be located in folder with respect to package structure - Name of Groovy source files must be the same as name of class located in file - Groovy source file must have extension '.groovy' Example: If source stream that we want validate contains the following code: package c.b.a import a.b.c.A class B extends A { // Do something. } Assume we store dependencies in JCR then URL of folder with Groovy sources may be like this: jcr://repository/workspace#/groovy-library. Then absolute path to JCR node that contains Groovy source must be as following: /groovy-library/a/b/c/A.groovy
        files - locations (string representation of URL) of source files that should be add in class path when compile Groovy script. Each location must point directly to file that contains Groovy source. Source file can have any name and extension
        Returns:
        Response with corresponded status. 200 if source code is valid
        eXo level API
        Provisional
        eXo anchor for generated doc on REST API
        {code } "script" : the Groovy source stream. {code}
      • validateScript

        public void validateScript​(String name,
                                   InputStream script,
                                   org.exoplatform.services.rest.ext.groovy.SourceFolder[] src,
                                   org.exoplatform.services.rest.ext.groovy.SourceFile[] files)
                            throws org.exoplatform.services.rest.ext.groovy.MalformedScriptException
        Check is specified source script contains valid Groovy source code.
        Parameters:
        name - script name. This name will be used by GroovyClassLoader to identify script, e.g. specified name will be used in error message in compilation of Groovy fails. If this parameter is null then GroovyClassLoader will use automatically generated name
        script - Groovy source stream
        src - set of folders that contains Groovy source files that should be add in class-path when validate script, see ClassPathEntry.getPath(). NOTE To be able load Groovy source files from specified folders the following rules must be observed:
        • Groovy source files must be located in folder with respect to package structure
        • Name of Groovy source files must be the same as name of class located in file
        • Groovy source file must have extension '.groovy'
        files - set of groovy source files that should be add in class-path when validate script. Each item must point directly to file that contains Groovy source, see ClassPathEntry.getPath() . Source file can have any name and extension
        Throws:
        org.exoplatform.services.rest.ext.groovy.MalformedScriptException - if script contains not valid source code
        eXo level API
        Provisional
      • updateScript

        public javax.ws.rs.core.Response updateScript​(InputStream stream,
                                                      @Context
                                                      javax.ws.rs.core.UriInfo uriInfo,
                                                      String repository,
                                                      String workspace,
                                                      String path)
        This method is useful for clients that can send script in request body without form-data. At required to set specific Content-type header 'script/groovy'.
        Parameters:
        stream - the stream that contains groovy source code
        uriInfo - see javax.ws.rs.core.UriInfo
        repository - repository name
        workspace - workspace name
        path - path to resource to be created
        Returns:
        Response with status 'created'
        eXo level API
        Provisional
        eXo anchor for generated doc on REST API
        {code } "stream" : the input stream that contains groovy source code. {code}
      • updateScript

        public javax.ws.rs.core.Response updateScript​(Iterator<org.apache.commons.fileupload.FileItem> items,
                                                      @Context
                                                      javax.ws.rs.core.UriInfo uriInfo,
                                                      String repository,
                                                      String workspace,
                                                      String path)
        This method is useful for clients that send scripts as file in 'multipart/*' request body. NOTE even we use iterator item should be only one, rule one address - one script. This method is created just for comfort loading script from HTML form. NOT use this script for uploading few files in body of 'multipart/form-data' or other type of multipart.
        Parameters:
        items - iterator org.apache.commons.fileupload.FileItem
        uriInfo - see javax.ws.rs.core.UriInfo
        repository - repository name
        workspace - workspace name
        path - path to resource to be created
        Returns:
        Response with status 'created'
        eXo level API
        Provisional
        eXo anchor for generated doc on REST API
        {code} "items" : the sending data with HTML form ('multipart/form-data') {code}
      • load

        @RolesAllowed("administrators")
        public javax.ws.rs.core.Response load​(String repository,
                                              String workspace,
                                              String path,
                                              boolean state,
                                              List<String> sources,
                                              List<String> files,
                                              javax.ws.rs.core.MultivaluedMap<String,​String> properties)
        Deploy groovy script as REST service. If this property set to 'true' then script will be deployed as REST service if 'false' the script will be undeployed. NOTE is script already deployed and state is true script will be re-deployed.
        Parameters:
        repository - repository name
        workspace - workspace name
        path - the path to JCR node that contains groovy script to be deployed
        state - true if resource should be loaded and false otherwise. If this attribute is not present in HTTP request then it will be considered as true
        sources - locations (string representation of URL) of source folders that should be add in class path when compile Groovy script. NOTE: To be able load Groovy source files from specified folders the following rules must be observed: - Groovy source files must be located in folder with respect to package structure - Name of Groovy source files must be the same as name of class located in file - Groovy source file must have extension '.groovy' Example: If source stream that we want validate contains the following code: package c.b.a import a.b.c.A class B extends A { // Do something. } Assume we store dependencies in JCR then URL of folder with Groovy sources may be like this: jcr://repository/workspace#/groovy-library. Then absolute path to JCR node that contains Groovy source must be as following: /groovy-library/a/b/c/A.groovy
        files - locations (string representation of URL) of source files that should be add in class path when compile Groovy script. Each location must point directly to file that contains Groovy source. Source file can have any name and extension
        properties - optional properties to be applied to loaded resource. Ignored if state parameter is false
        eXo level API
        Provisional
      • load

        public javax.ws.rs.core.Response load​(String repository,
                                              String workspace,
                                              String path,
                                              boolean state,
                                              javax.ws.rs.core.MultivaluedMap<String,​String> properties,
                                              org.exoplatform.services.rest.ext.groovy.SourceFolder[] src,
                                              org.exoplatform.services.rest.ext.groovy.SourceFile[] files)
        Deploy groovy script as REST service. If this property set to 'true' then script will be deployed as REST service if 'false' the script will be undeployed. NOTE is script already deployed and state is true script will be re-deployed.
        Parameters:
        repository - repository name
        workspace - workspace name
        path - the path to JCR node that contains groovy script to be deployed
        state - true if resource should be loaded and false otherwise. If this attribute is not present in HTTP request then it will be considered as true
        properties - optional properties to be applied to loaded resource. Ignored if state parameter is false
        src - set of folders that contains Groovy source files that should be add in class-path when compile file located at path . NOTE To be able load Groovy source files from specified folders the following rules must be observed:
        • Groovy source files must be located in folder with respect to package structure
        • Name of Groovy source files must be the same as name of class located in file
        • Groovy source file must have extension '.groovy'
        files - set of groovy source files that should be add in class-path when compile file located at path. Each item must point directly to file that contains Groovy source, see ClassPathEntry.getPath() . Source file can have any name and extension
        eXo level API
        Provisional
      • deleteScript

        public javax.ws.rs.core.Response deleteScript​(String repository,
                                                      String workspace,
                                                      String path)
        Remove node that contains groovy script.
        Parameters:
        repository - repository name
        workspace - workspace name
        path - JCR path to node that contains script
        eXo level API
        Provisional
      • autoload

        public javax.ws.rs.core.Response autoload​(String repository,
                                                  String workspace,
                                                  String path,
                                                  boolean state)
        Change exo:autoload property. If this property is 'true' script will be deployed automatically when JCR repository startup and automatically re-deployed when script source code changed.
        Parameters:
        repository - repository name
        workspace - workspace name
        path - JCR path to node that contains script
        state - value for property exo:autoload, if it is not specified then 'true' will be used as default. Example: .../scripts/groovy/test1.groovy/load is the same to .../scripts/groovy/test1.groovy/load?state=true
        eXo level API
        Provisional
      • getScript

        public javax.ws.rs.core.Response getScript​(String repository,
                                                   String workspace,
                                                   String path)
        Get source code of groovy script.
        Parameters:
        repository - repository name
        workspace - workspace name
        path - JCR path to node that contains script
        Returns:
        groovy script as stream
        eXo level API
        Provisional
        eXo response for generated doc on REST API
        {code} "scriptsource" : the source code of groovy script. {code}
      • getScriptMetadata

        public javax.ws.rs.core.Response getScriptMetadata​(String repository,
                                                           String workspace,
                                                           String path)
        Get groovy script's meta-information.
        Parameters:
        repository - repository name
        workspace - workspace name
        path - JCR path to node that contains script
        Returns:
        groovy script's meta-information
        eXo level API
        Provisional
        eXo response for generated doc on REST API
        {code:json} "scriptList" : the groovy script's meta-information {code}
      • list

        public javax.ws.rs.core.Response list​(String repository,
                                              String workspace,
                                              String name)
        Returns the list of all groovy-scripts found in workspace.
        Parameters:
        repository - repository name
        workspace - workspace name
        name - additional search parameter. If not empty method returns the list of script names matching wildcard else returns all the scripts found in workspace.
        Returns:
        list of groovy services
        eXo level API
        Provisional
        eXo response for generated doc on REST API
        {code:json} "scriptList" : the list of all groovy scripts found in workspace. {code}
      • getPath

        protected static String getPath​(String fullPath)
        Extract path to node's parent from full path.
        Parameters:
        fullPath - full path to node
        Returns:
        node's parent path
      • getName

        protected static String getName​(String fullPath)
        Extract node's name from full node path.
        Parameters:
        fullPath - full path to node
        Returns:
        node's name