3.4.5. Getting node UUID example

Now you are going to try a simple example of Groovy RESTfull service. There is one limitation to your Groovy script that you should use Java style code and decline to use dynamic types anywhere except in private methods and fields.

  1. Create a script with the following code and save it to your home directory: /home/andrew/JcrGroovyTest.groovy.

    import javax.jcr.Node
    
    import javax.jcr.Session
    import javax.ws.rs.GET
    import javax.ws.rs.Path
    import javax.ws.rs.PathParam
    import org.exoplatform.services.jcr.RepositoryService
    import org.exoplatform.services.jcr.ext.app.ThreadLocalSessionProviderService
    @Path("groovy/test/{repository}/{workspace}")
    public class JcrGroovyTest {
      private RepositoryService                 repositoryService
      private ThreadLocalSessionProviderService sessionProviderService
      public JcrGroovyTest(RepositoryService repositoryService,
                           ThreadLocalSessionProviderService sessionProviderService) {
        this.repositoryService = repositoryService
        this.sessionProviderService = sessionProviderService
      }
      @GET
      @Path("{path:.*}")
      public String nodeUUID(@PathParam("repository") String repository,
                             @PathParam("workspace") String workspace,
                             @PathParam("path") String path) {
        Session ses = null
        try {
          ses = sessionProviderService.getSessionProvider(null).getSession(workspace, repositoryService.getRepository(repository))
          Node node = (Node) ses.getItem("/" + path)
          return node.getUUID() + "\n"
        } finally {
          if (ses != null)
            ses.logout()
        }
      }
  2. Configure GroovyScript2RestLoaderPlugin as described in Loading script at startup time, then start the server. If configuration is correct and script does not have syntax error, you should see the service is deployed:

  3. Create a folder 'test' in the repository "production" via WebDAV. Now, you can try to access this service. Open another console and type command:

    andrew@ossl:~> curl -u root:exo \
    http://localhost:8080/rest/groovy/test/repository/production/test

    At this time it should have exception, because the JCR node "/test" is not referenceable and has not UUID. You will fix it in next steps by adding the mixin mix:referenceable.

  4. Open the script from local source code: /home/andrew/JcrGroovyTest.groovy, then add the following code and save:

    @POST
    
    @Path("{path:.*}")
    public void addReferenceableMixin(@PathParam("repository") String repository,
    @PathParam("workspace") String workspace,
    @PathParam("path") String path) {
    Session ses = null
    try {
    ses = sessionProviderService.getSessionProvider(null).getSession(workspace, repositoryService.getRepository(repository))
    Node node = (Node) ses.getItem("/" + path)
    node.addMixin("mix:referenceable")
    ses.save()
    } finally {
    if (ses != null)
    ses.logout()
    }
    }
  5. Upload the new source code to the server by the following command:

    andrew@ossl:~> curl -i -v -u root:exo \
    -X POST \
    --data-binary @JcrGroovyTest.groovy \
    -H 'Content-type:script/groovy' \
    http://localhost:8080/rest/script/groovy/update/repository/production/script/groovy/JcrGroovyTest.groovy

    The /script/groovy/JcrGroovyTest.groovy node has the exo:autoload=true property, so the script will be re-deployed automatically and you will see:

  6. Access the newly created method by the command:

    andrew@ossl:~> curl -u root:exo \
    -X POST \
    http://localhost:8080/rest/groovy/test/repository/production/test

    The method should run quietly, without any logs. Now, get the UUID node:

    andrew@ossl:~> curl -u root:exo \
    http://localhost:8080/rest/groovy/test/repository/production/test
    1b8c88d37f0000020084433d3af4941f

    The UUID node: 1b8c88d37f0000020084433d3af4941f

  7. You do not need this script any more, so remove it from JCR:

    andrew@ossl:~> curl -u root:exo \
    http://localhost:8080/rest/script/groovy/delete/repository/production/script/groovy/JcrGroovyTest.groovy
Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus