SiteMap, PathInfo and REST controllers

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

SiteMap, PathInfo and REST controllers

Guillaume Laforge-2
Hi,

In GRAILS-13 (http://jira.codehaus.org/browse/GRAILS-13), there's a
nice concept of a SiteMap class which would be used to provide some
paths mappings.

For example:

class ExampleSitemap {
    @Property default = [ "/some/path" : "/controllerOne/actionOne" ]
    @Property another = [ "/another/path" : "/controllerTwo/actionTwo" ]
}

It allows one to create custom URLs not restricted to /controller/action

Inspired by RIFE's lovely "pathinfo" feature, I'd like we support
something similar that could be used for normal controllers as well as
with a new REST controller type.

First of all, read some background on pathinfo:
http://rifers.org/blogs/gbevin/2005/12/13/rife_1_3_released#1_3_highlight01

We could allow interpolated variables in the URL definition:

class MySitemap {
    @Property archives = [ "/archives/#year/#month/#day/#postTitle",
"/archives/show" ]
}

For instance, /archives/2005/12/25/merry-christmas would be remapped to:
/archives/show?year=2005&month=12&day=25&postTitle=merry-christmas

And it'd call the ArchivesController's show closure action.

If the controller to be mapped only indicate a controller name, then
the controller'd better have a default action defined.

But it also made me think about a special kind of controllers I'd like
to have in Grails: REST controllers for restish APIs. The URL mapping
of sitemaps could also be used to map the "meaningful" URLs REST fans
love.

REST controllers would only contain one type of action but supporting
different HTTP methods. A REST controller would look like:

class AirbusRestController {
    @Property get = {}
    @Property post = {}
    @Property put = {}
    @Property delete = {}
}

One closure per HTTP method we want to support. If certain methods are
missing, that means the service doesn't support them.

Also note that controllers are named with the following pattern as
convention: *RestController

With the sitemap, we could map to a REST controller the following URLs:

http://www.airbus.com/aircraft/A380
http://www.airbus.com/engine/rollsroyce/RR345
http://www.airbus.com/engine/snecma/SN876

class AirbusSitemap {
    @Property aircraft = [ "/aircraft/#aircraftId", "/aircraft" ]
    @Property engine = [ "/engine/#make/#motorId", "/engine" ]
}

And again, the variables could be passed as parameters as explained at
the top of the email in each controller method.

class EngineRestController {
    @Property get = {
        def make = this.request["make"]
        def motorId = this.request["motorId"]
    }
}

What do you think?

--
Guillaume Laforge
Groovy Project Manager
http://glaforge.free.fr/blog/groovy
Reply | Threaded
Open this post in threaded view
|

Re: SiteMap, PathInfo and REST controllers

Steven Devijver
That sounds really cool. I had a chat with Geert Bevin at JavaPolis,
he's a really nice guy.


On 12/16/05, Guillaume Laforge <[hidden email]> wrote:

> Hi,
>
> In GRAILS-13 (http://jira.codehaus.org/browse/GRAILS-13), there's a
> nice concept of a SiteMap class which would be used to provide some
> paths mappings.
>
> For example:
>
> class ExampleSitemap {
>     @Property default = [ "/some/path" : "/controllerOne/actionOne" ]
>     @Property another = [ "/another/path" : "/controllerTwo/actionTwo" ]
> }
>
> It allows one to create custom URLs not restricted to /controller/action
>
> Inspired by RIFE's lovely "pathinfo" feature, I'd like we support
> something similar that could be used for normal controllers as well as
> with a new REST controller type.
>
> First of all, read some background on pathinfo:
> http://rifers.org/blogs/gbevin/2005/12/13/rife_1_3_released#1_3_highlight01
>
> We could allow interpolated variables in the URL definition:
>
> class MySitemap {
>     @Property archives = [ "/archives/#year/#month/#day/#postTitle",
> "/archives/show" ]
> }
>
> For instance, /archives/2005/12/25/merry-christmas would be remapped to:
> /archives/show?year=2005&month=12&day=25&postTitle=merry-christmas
>
> And it'd call the ArchivesController's show closure action.
>
> If the controller to be mapped only indicate a controller name, then
> the controller'd better have a default action defined.
>
> But it also made me think about a special kind of controllers I'd like
> to have in Grails: REST controllers for restish APIs. The URL mapping
> of sitemaps could also be used to map the "meaningful" URLs REST fans
> love.
>
> REST controllers would only contain one type of action but supporting
> different HTTP methods. A REST controller would look like:
>
> class AirbusRestController {
>     @Property get = {}
>     @Property post = {}
>     @Property put = {}
>     @Property delete = {}
> }
>
> One closure per HTTP method we want to support. If certain methods are
> missing, that means the service doesn't support them.
>
> Also note that controllers are named with the following pattern as
> convention: *RestController
>
> With the sitemap, we could map to a REST controller the following URLs:
>
> http://www.airbus.com/aircraft/A380
> http://www.airbus.com/engine/rollsroyce/RR345
> http://www.airbus.com/engine/snecma/SN876
>
> class AirbusSitemap {
>     @Property aircraft = [ "/aircraft/#aircraftId", "/aircraft" ]
>     @Property engine = [ "/engine/#make/#motorId", "/engine" ]
> }
>
> And again, the variables could be passed as parameters as explained at
> the top of the email in each controller method.
>
> class EngineRestController {
>     @Property get = {
>         def make = this.request["make"]
>         def motorId = this.request["motorId"]
>     }
> }
>
> What do you think?
>
> --
> Guillaume Laforge
> Groovy Project Manager
> http://glaforge.free.fr/blog/groovy
>


--
"If you want to be a different fish, you gotta jump out of the school."
-- Captain Beefheart
Reply | Threaded
Open this post in threaded view
|

Re: SiteMap, PathInfo and REST controllers

graemer
+1 This is a great idea, probably for implementation after the first
release... thanks for the suggestions Guillaume

Graeme

On 17/12/05, Steven Devijver <[hidden email]> wrote:

> That sounds really cool. I had a chat with Geert Bevin at JavaPolis,
> he's a really nice guy.
>
>
> On 12/16/05, Guillaume Laforge <[hidden email]> wrote:
> > Hi,
> >
> > In GRAILS-13 (http://jira.codehaus.org/browse/GRAILS-13), there's a
> > nice concept of a SiteMap class which would be used to provide some
> > paths mappings.
> >
> > For example:
> >
> > class ExampleSitemap {
> >     @Property default = [ "/some/path" : "/controllerOne/actionOne" ]
> >     @Property another = [ "/another/path" : "/controllerTwo/actionTwo" ]
> > }
> >
> > It allows one to create custom URLs not restricted to /controller/action
> >
> > Inspired by RIFE's lovely "pathinfo" feature, I'd like we support
> > something similar that could be used for normal controllers as well as
> > with a new REST controller type.
> >
> > First of all, read some background on pathinfo:
> > http://rifers.org/blogs/gbevin/2005/12/13/rife_1_3_released#1_3_highlight01
> >
> > We could allow interpolated variables in the URL definition:
> >
> > class MySitemap {
> >     @Property archives = [ "/archives/#year/#month/#day/#postTitle",
> > "/archives/show" ]
> > }
> >
> > For instance, /archives/2005/12/25/merry-christmas would be remapped to:
> > /archives/show?year=2005&month=12&day=25&postTitle=merry-christmas
> >
> > And it'd call the ArchivesController's show closure action.
> >
> > If the controller to be mapped only indicate a controller name, then
> > the controller'd better have a default action defined.
> >
> > But it also made me think about a special kind of controllers I'd like
> > to have in Grails: REST controllers for restish APIs. The URL mapping
> > of sitemaps could also be used to map the "meaningful" URLs REST fans
> > love.
> >
> > REST controllers would only contain one type of action but supporting
> > different HTTP methods. A REST controller would look like:
> >
> > class AirbusRestController {
> >     @Property get = {}
> >     @Property post = {}
> >     @Property put = {}
> >     @Property delete = {}
> > }
> >
> > One closure per HTTP method we want to support. If certain methods are
> > missing, that means the service doesn't support them.
> >
> > Also note that controllers are named with the following pattern as
> > convention: *RestController
> >
> > With the sitemap, we could map to a REST controller the following URLs:
> >
> > http://www.airbus.com/aircraft/A380
> > http://www.airbus.com/engine/rollsroyce/RR345
> > http://www.airbus.com/engine/snecma/SN876
> >
> > class AirbusSitemap {
> >     @Property aircraft = [ "/aircraft/#aircraftId", "/aircraft" ]
> >     @Property engine = [ "/engine/#make/#motorId", "/engine" ]
> > }
> >
> > And again, the variables could be passed as parameters as explained at
> > the top of the email in each controller method.
> >
> > class EngineRestController {
> >     @Property get = {
> >         def make = this.request["make"]
> >         def motorId = this.request["motorId"]
> >     }
> > }
> >
> > What do you think?
> >
> > --
> > Guillaume Laforge
> > Groovy Project Manager
> > http://glaforge.free.fr/blog/groovy
> >
>
>
> --
> "If you want to be a different fish, you gotta jump out of the school."
> -- Captain Beefheart
>