Scala and Restful Web Services

80 views
Skip to first unread message

Ilya Klyuchnikov

unread,
May 12, 2008, 9:39:35 AM5/12/08
to liftweb
There is an example of development RPC-like web services using lift.

Is there any example of development Restful web services using lift in
paradigm similar to Restlet ( http://restlet.org )?

David Pollak

unread,
May 12, 2008, 5:54:30 PM5/12/08
to lif...@googlegroups.com
Ilya,

SteveJ gave a presentation on REST in lift at the Scala lift off.  His slides will be online soon.

The net-net is that you can intercept URLs for REST services with pattern matching (one pattern per URL that you want to service) and Scala's built-in XML to respond.

For example:
    LiftRules.addDispatchBefore{
      case RequestMatcher(_, ParsePath("game_xml" :: id :: _, _, _),_,  _) =>  req => Game.dumpGame(id)
    }

And in Game.dumpGame:
def dumpGame(id: String) = for (game <- Game.find(id)) game.toXml

Thanks,

David
--
Scala lift off unconference, May 10th 2008, San Francisco http://scalaliftoff.com
lift, the secure, simple, powerful web framework http://liftweb.net
Collaborative Task Management http://much4.us

Tim Perrett

unread,
May 13, 2008, 4:45:38 AM5/13/08
to liftweb
Not to be picky, but wouldn't that scenario be just for GET REST?
(i.e. reading data)? Wouldn't you need to be able to specify actions
for GET, PUT, etc etc to be proper rest? Just spitballing here, but
that was always my understanding. The REST pattern is a complex one -
you only need look at the JAX-RS spec to see that!

It would be really interesting to see the slides from liftoff
however :)

Cheers

Tim

Marius

unread,
May 13, 2008, 6:10:56 AM5/13/08
to liftweb
Hi,

You can also use RewritePf to transform the post request into a URI
fitful for DispatchPf and then just pattern match the RequestMatcher.

Br's,
Marius

Marius

unread,
May 13, 2008, 6:13:15 AM5/13/08
to liftweb


On May 13, 1:10 pm, Marius <marius.dan...@gmail.com> wrote:
> Hi,
>
> You can also use RewritePf to transform the post request into a URI
> fitful for DispatchPf and then just pattern match the RequestMatcher.

You know if you have some POST request and parameter denominates the
action and arguments they can be easily transformed (using functional
style should be piece of cake and very little code) into and URI
sequence and then handle it in DispatchPf.

Ilya Klyuchnikov

unread,
May 13, 2008, 6:34:28 AM5/13/08
to liftweb


On May 13, 2:13 pm, Marius <marius.dan...@gmail.com> wrote:

> > You can also use RewritePf to transform the post request into a URI
> > fitful for DispatchPf and then just pattern match the RequestMatcher.
>
> You know if you have some POST request and parameter denominates the
> action and arguments they can be easily transformed (using functional
> style should be piece of cake and very little code) into and URI
> sequence and then handle it in DispatchPf.

Transforming POST (ant PUT) into URI seems to be quite cumbersome in
case when structure of object in request is not flat. Consider for
example PUT request with following content:

<placements>
<Placement id="12" name="Campaign1" typeUri="http://example.com/
Type1">
<property key="geo" value="us"/>
<property key="auditory" value="adult"/>
</inventory:Placement>
<Placement id="13" name="Campaign2" typeUri="http://example.com/
Type1">
<property key="auditory" value="children"/>
<property key="geo" value="us"/>
</Placement>
<placements>

Transforming this XML into URI and then parsing URI is quite
unnatural.

Ilya.

Marius

unread,
May 13, 2008, 8:36:47 AM5/13/08
to liftweb
Hi,

The discussion was about REST I believe. In your specific example you
have a specific protocol and if you want your lift application to
process it, your DispatchPf needs to read the xml content, parse it,
process it and return response.
RequestMatcher is a nice mechanism to pattern match URI fragments and
it also provide necessary information to process pretty much any HTTP
request.

For full blown XML/text/binary protocols sent by clients one needs to
parse them "manually".

Br's,
Marius

On May 13, 1:34 pm, Ilya Klyuchnikov <ilya.klyuchni...@gmail.com>
wrote:

David Pollak

unread,
May 13, 2008, 12:54:05 PM5/13/08
to lif...@googlegroups.com
On Tue, May 13, 2008 at 1:45 AM, Tim Perrett <he...@timperrett.com> wrote:

Not to be picky, but wouldn't that scenario be just for GET REST?
(i.e. reading data)? Wouldn't you need to be able to specify actions
for GET, PUT, etc etc to be proper rest? Just spitballing here, but
that was always my understanding. The REST pattern is a complex one -
you only need look at the JAX-RS spec to see that!

Tim,

You can pattern match against the request type as well (GET, POST, PUT, DELETE, other) and dispatch accordingly.

Thanks,

David
 


It would be really interesting to see the slides from liftoff
however :)

Cheers

Tim

Steve Jenson

unread,
May 13, 2008, 11:52:31 PM5/13/08
to lif...@googlegroups.com
I just checked in my presentation into SVN.

http://liftweb.googlecode.com/svn/trunk/liftweb/docs/presentations/lift-web-services/

I was hoping it'd be viewable directly out of SVN but it sends out the
wrong MIME type for html.

Please let me know if you have any questions.

Steve

PS You may have to resize your browser window when viewing the
presentation for all the text to appear. S5 is touchy that way.

TylerWeir

unread,
May 14, 2008, 8:39:38 AM5/14/08
to liftweb
> I just checked in my presentation into SVN.

I know what's going in to a few of my apps.

Thanks Steve!

David Pollak

unread,
May 14, 2008, 9:40:59 AM5/14/08
to lif...@googlegroups.com
Very, very sweet

David Bernard

unread,
May 14, 2008, 5:08:35 PM5/14/08
to lif...@googlegroups.com
Why putting the doc to svn, instead of the wiki ?
thanks for the slides.

/davidB

Steve Jenson

unread,
May 15, 2008, 10:39:09 AM5/15/08
to lif...@googlegroups.com
I didn't want to spend more time converting them into wiki format
(unless you know of a way to put S5 slides directly into the wiki).
I'll find a home for them that makes them show up properly in your
browser.

TylerWeir

unread,
May 15, 2008, 11:06:28 AM5/15/08
to liftweb
Did someone say Wiki? :)

http://liftweb.net/index.php/HowTo_do_Web_Services

On May 15, 10:39 am, "Steve Jenson" <ste...@gmail.com> wrote:
> I didn't want to spend more time converting them into wiki format
> (unless you know of a way to put S5 slides directly into the wiki).
> I'll find a home for them that makes them show up properly in your
> browser.
>
> On Wed, May 14, 2008 at 2:08 PM, David Bernard
>

Steve Jenson

unread,
May 15, 2008, 12:23:49 PM5/15/08
to lif...@googlegroups.com
woah. thanks!

TylerWeir

unread,
May 15, 2008, 12:58:13 PM5/15/08
to liftweb
np, it's a great resource.

On May 15, 12:23 pm, "Steve Jenson" <ste...@gmail.com> wrote:
> woah. thanks!
>

Bjarte Stien Karlsen

unread,
May 17, 2008, 12:30:28 PM5/17/08
to liftweb
I took the liberty of fixing a broken <code> tag in the wiki to make
it look prettier.

Thanks a bunch to Steve for the work and to Tyler for the
wikification.

Steve Jenson

unread,
May 17, 2008, 1:45:10 PM5/17/08
to lif...@googlegroups.com
Thanks!

TylerWeir

unread,
May 17, 2008, 2:41:01 PM5/17/08
to liftweb
Word up!

On May 17, 12:30 pm, Bjarte Stien Karlsen

Robert Spychala

unread,
May 21, 2008, 11:09:57 PM5/21/08
to liftweb
this is great! one question: I'm having trouble compiling the example
cause i can't find SimpleController. i'm using lift. 0.8? What is this
class and where can I find it?

thanks!

r.S.

Tim Perrett

unread,
May 22, 2008, 7:17:43 AM5/22/08
to liftweb
Sorry for my late pick up of this thread, but that is seriously cool!

Kudos Steve, very, very nice work.
Props also to Tyler for the Wikification - very groovy indeed.

TylerWeir

unread,
May 22, 2008, 8:45:19 AM5/22/08
to liftweb
> Kudos Steve, very, very nice work.
> Props also to Tyler for the Wikification - very groovy indeed.

It's quite obvious that *I* did the hard part. :)

On May 22, 7:17 am, Tim Perrett <he...@timperrett.com> wrote:
> Sorry for my late pick up of this thread, but that is seriously cool!
>
>

Ilya Klyuchnikov

unread,
May 22, 2008, 6:30:09 AM5/22/08
to liftweb
As far as I understand SimpleController is a trait supposed to be
defined in this example:

trait SimpleController SimpleController {
def index: ResponseIt
def get(id: String): ResponseIt
def create: ResponseIt
def delete(id: String): ResponseIt
}

The rationale behind this trait is that you are able to defined many
controllers and setup dispatching in unified way in Boot class.

Steve Jenson

unread,
May 22, 2008, 12:58:02 PM5/22/08
to lif...@googlegroups.com
I didn't realize that SimpleController had vanished from lift! So,
gang, what's the preferred way to handle Web Services now?

David Pollak

unread,
May 22, 2008, 1:15:08 PM5/22/08
to lif...@googlegroups.com
On Thu, May 22, 2008 at 9:58 AM, Steve Jenson <ste...@gmail.com> wrote:

I didn't realize that SimpleController had vanished from lift! So,
gang, what's the preferred way to handle Web Services now?

Dunno... maybe we should bring SimpleController back.
 


On Thu, May 22, 2008 at 3:30 AM, Ilya Klyuchnikov
> As far as I understand SimpleController is a trait supposed to be
> defined in this example:
>
>    trait SimpleController SimpleController {
>        def index: ResponseIt
>        def get(id: String): ResponseIt
>        def create: ResponseIt
>        def delete(id: String): ResponseIt
>     }
>
> The rationale behind this trait is that you are able to defined many
> controllers and setup dispatching in unified way in Boot class.
>
>
> On May 22, 7:09 am, Robert Spychala <robspych...@gmail.com> wrote:
>> this is great! one question: I'm having trouble compiling the example
>> cause i can't find SimpleController. i'm using lift. 0.8? What is this
>> class and where can I find it?
>>
>> thanks!
>
> >
>





--
lift, the simply functional web framework http://liftweb.net

Marius

unread,
May 22, 2008, 1:19:29 PM5/22/08
to liftweb
I think DispatchPf or implementing dispatch_& from LiftView trait is
quite sufficient.

Br's,
Marius

On May 22, 7:58 pm, "Steve Jenson" <ste...@gmail.com> wrote:
> I didn't realize that SimpleController had vanished from lift! So,
> gang, what's the preferred way to handle Web Services now?
>
> On Thu, May 22, 2008 at 3:30 AM, Ilya Klyuchnikov
>

Steve Jenson

unread,
May 22, 2008, 2:05:08 PM5/22/08
to lif...@googlegroups.com
I'll give this a try. Thanks.
Reply all
Reply to author
Forward
0 new messages