eXo JCR :: Component :: Extension Service 1.15.0-CR2

org.exoplatform.services.jcr.ext.script.groovy
Class GroovyScript2RestLoader

java.lang.Object
  extended by org.exoplatform.services.jcr.ext.script.groovy.GroovyScript2RestLoader
All Implemented Interfaces:
org.picocontainer.Startable

public class GroovyScript2RestLoader
extends Object
implements org.picocontainer.Startable

Version:
$Id: GroovyScript2RestLoader.java 34445 2009-07-24 07:51:18Z dkatayev $
Author:
Andrey Parfonov

Nested Class Summary
protected static class GroovyScript2RestLoader.InnerGroovyJaxrsPublisher
           
static class GroovyScript2RestLoader.ScriptList
          Script list, used for pass script list as JSON.
static class GroovyScript2RestLoader.ScriptMetadata
          Script meta-data, used for pass script meta-data as JSON.
 
Field Summary
protected  List<GroovyScriptAddRepoPlugin> addRepoPlugins
           
protected  org.exoplatform.container.configuration.ConfigurationManager configurationManager
          See ConfigurationManager.
protected  org.exoplatform.services.rest.ext.groovy.GroovyJaxrsPublisher groovyPublisher
           
protected  org.exoplatform.container.xml.InitParams initParams
          See InitParams.
protected  List<GroovyScript2RestLoaderPlugin> loadPlugins
           
protected  RegistryService registryService
          See RegistryService.
protected  RepositoryService repositoryService
          See RepositoryService.
protected  ThreadLocalSessionProviderService sessionProviderService
          See SessionProviderService
 
Constructor Summary
GroovyScript2RestLoader(org.exoplatform.services.rest.impl.ResourceBinder binder, org.exoplatform.services.script.groovy.GroovyScriptInstantiator groovyScriptInstantiator, RepositoryService repositoryService, ThreadLocalSessionProviderService sessionProviderService, org.exoplatform.container.configuration.ConfigurationManager configurationManager, Handler jcrUrlHandler, org.exoplatform.container.xml.InitParams params)
           
GroovyScript2RestLoader(org.exoplatform.services.rest.impl.ResourceBinder binder, org.exoplatform.services.script.groovy.GroovyScriptInstantiator groovyScriptInstantiator, RepositoryService repositoryService, ThreadLocalSessionProviderService 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)
           
GroovyScript2RestLoader(org.exoplatform.services.rest.impl.ResourceBinder binder, org.exoplatform.services.script.groovy.GroovyScriptInstantiator groovyScriptInstantiator, RepositoryService repositoryService, ThreadLocalSessionProviderService sessionProviderService, org.exoplatform.container.configuration.ConfigurationManager configurationManager, RegistryService registryService, Handler jcrUrlHandler, org.exoplatform.container.xml.InitParams params)
           
 
Method Summary
 void addPlugin(org.exoplatform.container.component.ComponentPlugin cp)
           
 javax.ws.rs.core.Response addScript(InputStream stream, 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.
 javax.ws.rs.core.Response addScript(Iterator<org.apache.commons.fileupload.FileItem> items, 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.
protected  void addScripts()
          Add scripts that specified in configuration.
 javax.ws.rs.core.Response autoload(String repository, String workspace, String path, boolean state)
          Change exo:autoload property.
protected  javax.jcr.Node createScript(javax.jcr.Node parent, String name, boolean autoload, InputStream stream)
          Create JCR node.
 javax.ws.rs.core.Response deleteScript(String repository, String workspace, String path)
          Remove node that contains groovy script.
protected  String getAttributeSmart(Element element, String attr)
          Get attribute value.
protected static String getName(String fullPath)
          Extract node's name from full node path.
 String getNodeType()
          Get node type for store scripts, may throw IllegalStateException if nodeType not initialized yet.
protected static String getPath(String fullPath)
          Extract path to node's parent from full path.
 javax.ws.rs.core.Response getScript(String repository, String workspace, String path)
          Get source code of groovy script.
 javax.ws.rs.core.Response getScriptMetadata(String repository, String workspace, String path)
          Get groovy script's meta-information.
 javax.ws.rs.core.Response list(String repository, String workspace, String name)
          Returns the list of all groovy-scripts found in workspace.
 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.
 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.
protected  void readParamsFromFile()
          Read parameters from file.
protected  void readParamsFromRegistryService(SessionProvider sessionProvider)
          Read parameters from RegistryService.
protected  void setAttributeSmart(Element element, String attr, String value)
          Set attribute value.
 void start()
           
 void stop()
           
 javax.ws.rs.core.Response updateScript(InputStream stream, 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.
 javax.ws.rs.core.Response updateScript(Iterator<org.apache.commons.fileupload.FileItem> items, 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.
 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.
 void validateScript(String name, InputStream script, org.exoplatform.services.rest.ext.groovy.SourceFolder[] src, org.exoplatform.services.rest.ext.groovy.SourceFile[] files)
          Check is specified source script contains valid Groovy source code.
protected  void writeParamsToRegistryService(SessionProvider sessionProvider)
          Write parameters to RegistryService.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

groovyPublisher

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

initParams

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


repositoryService

protected RepositoryService repositoryService
See RepositoryService.


configurationManager

protected org.exoplatform.container.configuration.ConfigurationManager configurationManager
See ConfigurationManager.


registryService

protected RegistryService registryService
See RegistryService.


sessionProviderService

protected ThreadLocalSessionProviderService sessionProviderService
See SessionProviderService


loadPlugins

protected List<GroovyScript2RestLoaderPlugin> loadPlugins

addRepoPlugins

protected List<GroovyScriptAddRepoPlugin> addRepoPlugins
Constructor Detail

GroovyScript2RestLoader

public GroovyScript2RestLoader(org.exoplatform.services.rest.impl.ResourceBinder binder,
                               org.exoplatform.services.script.groovy.GroovyScriptInstantiator groovyScriptInstantiator,
                               RepositoryService repositoryService,
                               ThreadLocalSessionProviderService 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,
                               RepositoryService repositoryService,
                               ThreadLocalSessionProviderService 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,
                               RepositoryService repositoryService,
                               ThreadLocalSessionProviderService 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

writeParamsToRegistryService

protected void writeParamsToRegistryService(SessionProvider sessionProvider)
                                     throws IOException,
                                            SAXException,
                                            ParserConfigurationException,
                                            javax.jcr.RepositoryException
Write parameters to RegistryService.

Parameters:
sessionProvider - the SessionProvider
Throws:
ParserConfigurationException
SAXException
IOException
javax.jcr.RepositoryException

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 UriInfo
repository - repository name
workspace - workspace name
path - path to resource to be created
Returns:
Response with status 'created'

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 FileItem
uriInfo - see UriInfo
repository - repository name
workspace - workspace name
path - path to resource to be created
Returns:
Response with status 'created'

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

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

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 UriInfo
repository - repository name
workspace - workspace name
path - path to resource to be created
Returns:
Response with status 'created'

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 FileItem
uriInfo - see UriInfo
repository - repository name
workspace - workspace name
path - path to resource to be created
Returns:
Response with status 'created'

load

@RolesAllowed(value="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

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

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

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

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

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

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

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

eXo JCR :: Component :: Extension Service 1.15.0-CR2

Copyright © 2012 eXo Platform SAS. All Rights Reserved.