Configuring the ServiceRegistry as a Javascript Root Object

cancel
Showing results for 
Search instead for 
Did you mean: 

Configuring the ServiceRegistry as a Javascript Root Object

resplin
Intermediate
0 0 2,344

Obsolete Pages{{Obsolete}}

The official documentation is at: http://docs.alfresco.com



Web Scripts
JavaScript API
Examples



Introduction


In some cases you may have a need to call the Alfresco Foundation Services API from a Javascript script running within the Alfresco server.

One way to expose this object to Javascript scripts is to use the technique described at Adding Custom Script APIs to inject a proxy of the ServiceRegistry as a root scope Javascript object.


WARNING!


This technique creates a serious security risk and as a result this code should not be installed in any Alfresco instance where security is a concern.  Specifically it allows any Javascript script in the system (including ones uploaded by users) to access low level Alfresco APIs that could be used to irreparably damage content in the system.


Configuring the Service Registry as a Javascript Root Object


Because the ServiceRegistry is not suitable for direct injection as a Javascript root object, the following proxy class (which simply delegates all calls to the real ServiceRegistry) can be used instead, providing equivalent behaviour:

public class JavascriptCompatibleServiceRegistry
    extends BaseProcessorExtension
    implements ServiceRegistry
{
    private ServiceRegistry impl;

    public void setServiceRegistry(final ServiceRegistry impl)
    {
        this.impl = impl;
    }

    /**
     * @see org.alfresco.service.ServiceRegistry#getAVMLockingAwareService()
     */
    public AVMService getAVMLockingAwareService()
    {
        return(impl.getAVMLockingAwareService());
    }

    // ...
    // Other getters in ServiceRegistry go here
    // ...

    /**
     * @see org.alfresco.service.ServiceRegistry#isServiceProvided(org.alfresco.service.namespace.QName)
     */
    public boolean isServiceProvided(final QName qname)
    {
        return(impl.isServiceProvided(qname));
    }
}

The new root scope object is registered with the scripting framework in a custom Spring application context:

<bean id='javascriptServiceRegistry' parent='baseJavaScriptExtension' class='org.alfresco.repo.jscript.JavascriptCompatibleServiceRegistry'>
  <property name='serviceRegistry' ref='ServiceRegistry' />
  <property name='extensionName'   value='serviceRegistry' />
</bean>

After this is done, scripts will have a new root scope object called 'serviceRegistry' available that can be used to interact with the Alfresco Foundation Services API:

var avmService = serviceRegistry.getAVMService();
var stores     = avmService.getStores();
var result     = 'List of AVM stores and versions:\n';

for (var i = 0; i Return to JavaScript API