REST

How to activate groovy REST services in GateIn Beta5

By Jérémi Joslin · February 16, 2010 · Tags: , ,

By default GateIn come with an implementation of JAX-RS(JSR-311). You can write your REST services in java using this spec, and deploy your jar in GateIn. But at eXo, system admins don't let us access to the filesystem of the GateIn servers in the cloud, so it's not an option. But we can write our services in groovy and deploy them in the portal without having to restart the portal or access to the filesystem, and this, admin sys let us do it.

UPDATE: This is no more needed since GateIn 3.0-GA

In it's Beta5 version, GateIn doesn't come with the needed configuration to be able to deploy groovy REST services.

  • Download GateIn (I'm going to use the tomcat version on this article)
  • Start it a first time to decompress the war and stop it (./bin/gatein.sh run)
  • Add the jar exo.core.component.script.groovy-2.3.0-Beta05.jar to the lib directory of your tomcat
  • Edit repository-configuration.xml (webapps/portal/WEB-INF/conf/jcr/repository-configuration.xml) to add a JCR Workspace to store the files and name it "groovy-rest" (if you don't want to do it yourself, copy this file: repository-configuration.xml)
  • In the file "webapps/portal/WEB-INF/conf/jcr/jcr-configuration.xml", at the end of the file after the tag "" insert(if you don't want to do it yourself, copy this file: jcr-configuration.xml):
    <component>
      <type>org.exoplatform.services.jcr.ext.script.groovy.GroovyScript2RestLoader</type>
      <init-params>
        <object-param>
          <name>observation.config</name>
          <object type="org.exoplatform.services.jcr.ext.script.groovy.ObservationListenerConfiguration">
            <field name="repository">
              <string>repository</string>
            </field>
            <field name="workspaces">
              <collection type="java.util.ArrayList">
                <value>
                  <string>groovy-rest</string>
                </value>
              </collection>
            </field>
          </object>
        </object-param>
      </init-params>
    </component>
  • Remove the directory temp to force the creation of the new workspace: rm -rf temp/

Your GateIn is now ready to deploy groovy REST service.

Let's write our first REST service called test.groovy:

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

@Path("pub")
public class HelloWorld {
  @GET
  @Path("helloworld/{name}")
  public String hello(@PathParam("name") String name) {
    return "Hello $name\n"
  }
}

To push this script, we simply have to do a POST on our server. We want our script to be loaded automatically so we add "autoload=true". Here is how to do with CURL:

$ curl -u root:gtn -X POST -F "file=@test.groovy;name=test" -F "autoload=true" http://localhost:8080/rest/private/script/groovy/add/repository/groovy-rest/test.groovy
$

the URL is composed as :

  • http://localhost:8080: The url of our server
  • /rest/private/: The private access of the REST API
  • script/groovy/add: The name of the webservice we acces
  • repository/groovy-rest/: our repository name followed by the workspace name
  • test.groovy: The path to our groovy service

You can test your service:

$ curl http://localhost:8080/rest/pub/helloworld/jeremiHello jeremi
$

the URL is composed as :

  • http://localhost:8080: The url of our server
  • rest: The public access of the REST API
  • pub: The @Path of our class
  • helloworld: the @Path of our function
  • jeremi: the parameter of our function

To undeploy your service:

$ curl -u root:gtn -X POST http://localhost:8080/rest/private/script/groovy/load/repository/groovy-rest/test.groovy?state=false

If you want to make it easier to manage your groovy files I wrote a simple wrapper around the REST API used here: gatein-groovy-tools. To update your code:

$ exo.groovy update
PUSHING: test.groovy
$

Resources:

JAX-RS service to proxy an HTTP request in groovy

By Jérémi Joslin · January 26, 2010 · Tags: , , ,

I needed a simple way to proxy an HTTP request and add an authorization header to it, so I created a REST service using JAX-RS and using groovy to call the server. I put the code on our eXo server thanks to eXo IDE, and click deploy and that's it :)

If you need something similar, here is the code:

import javax.ws.rs.Path
import javax.ws.rs.POST
import javax.ws.rs.FormParam

@Path("proxy")
public class Proxy {
  @POST
  @Path("basic_auth")
  public String basic_auth(@FormParam("url") String url, @FormParam("login") String login,
                      @FormParam("password") String password) {
    def encoded = "$login:$password".getBytes().encodeBase64().toString()
    def c= new URL(url).openConnection()
    c.setRequestProperty("Authorization", "Basic $encoded")
    return c.content.text
  }
}

To test it:

curl -d "url=http://TheUrl.com/test&login=my_login&password=my_pwd" \
http://YourDomain.com/rest/proxy/basic_auth/
Archive