Alfresco webscripts with Groovy

After being a little bit disappointed about using Grails in Alfresco (see my post below), I decided to see how could Alfresco be integrated with Groovy.

Alfresco introduced a while ago the “webscripts”, with that they added a powerful scripting API based on JavaScript and the Rhino engine. When you add FreeMarker templates to that, what you could get is a nice output that you could customize to be some JSON data, XML or whatever suites you. That is amazing but I was not that happy as we have to wait for Alfresco to expose more of their services via the scripting API. Adding Groovy capabilities to any application and not only to Alfresco seems “natural” and I think that it helps in prototyping without having to recompile.

I took the groovy.servlet.GroovyServlet implementation and added a few lines that allows to configure a Spring bean as servlet parameters and to make them available in the groovy scripts.
The code is not yet published somewhere as a forge project but you can find it here. All you have to do is have Spring and Spring MVC on your classpath (Alfresco already has it) add Groovy libraries (groovy-all is a good solution, at least version 1.5.x) and configure your web.xml with the Groovy servlet that I am providing, an example could be found here. That example is using spring’s DispatcherServlet but you could use the GroovyServlet as any other servlet, directly in your web.xml. If your are also using the DispatcherServlet then you have to configure it correctly as shown here.

Once you are done with those configuration just add the desired beans to the servlet configuration and you are on “rails” with Alfresco.

The samples provided here are using the ServiceRegistry bean and it is mapped in Groovy as registry, so to get any other Alfresco service you have to start from there. But you are free to map whatever bean you want.

Although, some people are saying that there are problems using Alfresco beans with Groovy, I did not have those problems till now, but I am not saying it will happen.

Author : Daniel Gradecak

I am an Alfresco ECM consultant & Java developer from Belgium, currently living in Zagreb, Croatia. For the past 17 years, I've been developing Java and web applications. The last 15 years I have been primarily working with the Alfresco platform, along-side companies and organizations (all over Europe) to architect, develop and complete new applications. As an effective communicator I can normally create a solution that hurdles many obstacles to achieve a mutually accepted notion of success in terms of project delivery. I have specialized in tailor made application development using Javascript and Java/Alfresco. Each solution is highly accepted by the end users and that gives me a great confidence in my work.

2 thoughts on “Alfresco webscripts with Groovy

Leave a Reply

Your email address will not be published. Required fields are marked *