users@jersey.java.net

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

From: James Strachan <james.strachan_at_gmail.com>
Date: Thu, 23 Apr 2009 09:26:15 +0100

2009/4/23 Paul Sandoz <Paul.Sandoz_at_sun.com>:
> 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.

Yeah. Fingers crossed it works out OK - failing that we could always
just fork the code and rip out the Ajax bits and just do a simple
version of Lift's templates; thats compatible with the snippets stuff
but doesn't include the whole thing. Though hopefully we can just
reuse lift; plus then folks can always use 'real lift' and JAXRS side
by side using the same codebase.


>> 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 ? :-)

It could be :). When I've something more interesting than the previous
bit of code I'll probably pop it up on github first; being neither a
committer in Jersey or Lift - its then up to other folks to pull it
into Jersey/Lift as they see fit.

Any plans to move Jersey to git BTW? :) It'd make contributing add ons
much easier...

-- 
James
-------
http://macstrac.blogspot.com/
Open Source Integration
http://fusesource.com/