users@jersey.java.net

Re: [Jersey] using Lift templates with Jersey (was Re: [Jersey] custom TemplateProcessor not having its constructor injected?)

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 23 Apr 2009 10:13:45 +0200

On Apr 22, 2009, at 5:32 PM, James Strachan wrote:
>>>
>>>> I think there is some potential to provide some nice Scala
>>>> wrappers on
>>>> some
>>>> stuff, and use of partial functions for resource methods.
>>>
>>> Agreed! My thinking too...
>>>
>>>> The only current
>>>> down side is a compiler bug that stops the client side being
>>>> used :-(
>>>
>>> Really? I've not hit that one - what is it?
>>>
>>
>> http://lampsvn.epfl.ch/trac/scala/ticket/1539
>
>> Any reference to WebResource causes the compiler to vomit.
>
> Thanks for the heads up.
>

It means one cannot write Jersey-based unit tests in Scala :-( i have
the sample ready, and have been waiting for ages so i can add unit
tests but alas it seems that the bug is not that important.


>>> I would love to demo something like this at the Jersey BOF @
>>> JavaOne :-)
>
> BTW rather than scratching my head much more I figured I'd ask the
> lift folks for help - fingers crossed...
> http://groups.google.com/group/liftweb/browse_thread/thread/dba9a11251aa5067#
>

Saw it! i have been lurking on lift list for a while.


> Contributions welcome :)
>

It looks like they are willing to support the abstraction your
require. I am not currently sure how this will work with some of the
lift features that seem quite integrated with the templates. I
remember scratching my head wondering how the snippet concept could be
integrated with resource classes.


> BTW the following helper class makes it easy to use Scala's XML syntax
> with JAXRS... (though it renders XML as is, without evaluating any
> embedded Lift templates)
>
>
> import scala.xml.NodeSeq;
>
> import javax.ws.rs.WebApplicationException;
> import javax.ws.rs.core.MediaType;
> import javax.ws.rs.core.MultivaluedMap;
> import javax.ws.rs.ext.MessageBodyWriter;
> import javax.ws.rs.ext.Provider;
> import java.io.IOException;
> import java.io.OutputStream;
> import java.lang.annotation.Annotation;
> import java.lang.reflect.Type;
>
> /**
> * @version $Revision: 1.1 $
> */
> @Provider
> public class NodeWriter implements MessageBodyWriter<NodeSeq> {
> public boolean isWriteable(Class<?> aClass, Type type,
> Annotation[] annotations, MediaType mediaType) {
> return NodeSeq.class.isAssignableFrom(aClass);
> }
>
> public long getSize(NodeSeq nodeSeq, Class<?> aClass, Type type,
> Annotation[] annotations, MediaType mediaType) {
> return -1;
> }
>
> public void writeTo(NodeSeq nodeSeq, Class<?> aClass, Type type,
> Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,
> Object> stringObjectMultivaluedMap, OutputStream outputStream) throws
> IOException, WebApplicationException {
> String answer = nodeSeq.toString();
> outputStream.write(answer.getBytes());
> }
>
>
> You can then write a resource like this...
>
> import javax.ws.rs.{Produces, GET, Path}
> import scala.xml.NodeSeq
>
> @Path("/bar")
> class BarResource {
>
> @GET
> @Produces(Array("text/html"))
> def view() = <html><body><h1>Hello World!</h1></body></html>
> }
>
> (within the <html> you can use Scala expressions and so forth within
> {}.
>

Cool!

Is this this the start of a jersey-scala module ? :-)

Paul.