6.2. Using Groovy REST service

Besides using Jax-RS, you can also develop a REST service as a Groovy script in your own extension that will be loaded at startup by the REST engine.

To do this, you first need to have your own extension project as described in Creating your extension project. The below process will instruct how to write a simple Groovy REST script in your own extension and make it work in eXo Platform.

  1. Create a Groovy script named custom-extension.war!/WEB-INF/groovy/demo/GroovyRest.groovy with the simple content:

    
    // simple groovy script
    import javax.ws.rs.Path
    import javax.ws.rs.GET
    import javax.ws.rs.PathParam

    @Path("/groovyrest")
    public class GroovyRest {
      @GET
      @Path("helloworld/{name}")
      public String hello(@PathParam("name") String name) {
        return "Hello " + name
      }
    }
  2. Declare the Groovy file in the custom-extension.war!/WEB-INF/conf/configuration.xml file.

    
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <configuration>
        <external-component-plugins>
            <target-component>org.exoplatform.platform.gadget.services.GroovyScript2RestLoader.GroovyScript2RestLoaderExt</target-component>
            <component-plugin>
                <name>test</name>
                <set-method>addPlugin</set-method>
                <type>org.exoplatform.services.jcr.ext.script.groovy.GroovyScript2RestLoaderPlugin</type>
                <init-params>
                    <value-param>
                        <name>workspace</name>
                        <value>portal-system</value>
                    </value-param>
                    <value-param>
                        <name>node</name>
                        <value>/exo:gadget-groovy</value>
                    </value-param>
                    <properties-param>
                        <name>GroovyRest.groovy</name>
                        <property name="autoload" value="true" />
                        <property name="path" value="war:/groovy/demo/GroovyRest.groovy" />
                    </properties-param>
                </init-params>
            </component-plugin>
        </external-component-plugins>
    </configuration>

    The war custom-extension now looks like:

  3. Deploy your custom-extension into eXo Platform by putting custom-extension.war in the webapps folder and custom-extension-config.jar in the lib folder. See How to for both Tomcat and JBoss.

  4. Start eXo Platform, then go to http://mycompany.com:8080/portal/rest/groovyrest/helloworld/eXo. The output (Hello eXo) will be shown.

Tip

To change your Groovy script, follow a good practice that allows you to update it from a local file onto remote server so that you do not need to redeploy your entire package. First, make your needed changes in the GroovyRest.groovy file, for example, by putting an additional string as below:


// simple groovy script
import javax.ws.rs.Path
import javax.ws.rs.GET
import javax.ws.rs.PathParam

@Path("/groovyrest")
public class GroovyRest {
  @GET
  @Path("/helloworld/{name}")
  public String hello(@PathParam("name") String name) {
    return "Hello " + name + ". Nice to meet you!"
  }
}

Then, use this following curl command to load your script via HTTP requests:


$curl -uroot:gtn \
-X POST \
-'Content-type:script/groovy' \
--data-binary @GroovyRest.groovy \
http://mycompany.com:8080/rest/private/script/groovy/update/repository/portal-system/exo:gadget-groovy/GroovyRest.groovy

Now you can go to http://mycompany.com:8080/portal/rest/groovyrest/helloworld/eXo. You will see the message "Hello eXo. Nice to meet you!" printed out.

To read more about curl command, refer here.

Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus