ROUTING REFERENCE

Basic syntax rules

As mentioned elsewhere, the routing configuration syntax is essentially a program written in the Groovy language.

However, don't let that scare you away with the thought of having to learn yet another language, since you get can a long way without knowing Groovy.

You can pick up the syntax by looking at some examples, such as this:

builder.router {

   restlet(uri: "/helloworld1", ofClass: "org.mycomp.HelloWorld1")

   restlet(uri: "/hello/world2", ofClass: org.mycomp.HelloWorld2")

   router(uri: "/hello") {
     restlet(uri: "/world3", ofClass: org.mycomp.HelloWorld3")
   }
}

In case you're wondering, this example will react on URIs starting with:

/helloworld1
/hello/world2
/hello/world3

From this example you can see the basic rules:

  • components names are optionally followed by a set of parameters between brackets
  • the parameters are a comma-separated list of name value-pairs, the name and value separated by a colon
  • components nested inside another component are listed within curly brackets following the component
  • the top-level component (usually a 'router') is prefixed with 'builder.'

Setting generic properties on routing components

The documentation of the individual routing components describes the parameters supported by those components. Besides what is documented, the system will automatically try to call a setter method on the routing component for any other parameters you specify.

For example, if you would have a look at the API documentation of Restlet's Router class, you would see it has the following setter method:

public void setRetryDelay(long retryDelay)

You can set this parameter as follows:

builder.router(retryDelay: 50) {
    [...]
}

In general, most of the commonly useful parameters can be found in Kauri's documentation, so you only need to know this for advanced usage.

Variables available in the script

[TODO]

moduleContext: the Restlet context of the Kauri module.

confRegistry: the ConfRegistry associated to the module. (see Configuration )

inSourceMode: a boolean indicating if the module in which the routing is created was loaded in source mode or not.

Advanced things

Since the router configuration is Groovy code, you can write any control structures in it as you like. We advise however to keep the routing configuration as close to static data as possible. This will make it easier to understand for users with no or less knowledge about Groovy.

But just for fun and general knowledge, let us look at some examples.

The following example will create 10 restlets attached to 10 different URIs on the router: "/helloworld0", "/helloworld1", ..., "/helloworld9". The loop is executed during the building of the routing structure, thus not during actual routing.

builder.router {
    10.times { i ->
        restletRoute = restlet(uri: "/helloworld" + i,
                               handle: { request, response ->
            response.setEntity("Hello world from inside the router " + i,
                                mediaType.TEXT_PLAIN)
        })
    }
}

The following example shows two things: the currently constructed component is available inside its following {} part as a variable named current, and it shows how to print things to the console. Again, the print is executed only once, during the building of the routing structure.

builder.router {
     print("The current routing component is: ${current}")
}