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: Wed, 22 Apr 2009 16:32:17 +0100

2009/4/22 Paul Sandoz <Paul.Sandoz_at_sun.com>:
>
> On Apr 22, 2009, at 1:30 PM, James Strachan wrote:
>
>> Figured changing the subject might be an idea... :)
>>
>> 2009/4/22 Paul Sandoz <Paul.Sandoz_at_sun.com>:
>>>
>>> On Apr 22, 2009, at 1:11 PM, James Strachan wrote:
>>>
>>>> I tried creating a custom TemplateProcessor instance like the
>>>> JSPTemplateProcessor using a similar constructor... (I'm experimenting
>>>> using LiftWeb.net's templates with Jersey 1.0.3 - I'm slowly being
>>>> drawn to scala...)
>>>>
>>>
>>> I have been drawn to Scala too and wondered if the lift type
>>> templates/snippets could be integrated.
>>
>> Yeah; the lift templates could be an awesome alternative to JSP + JSTL
>> + EL + custom tags + JSP fragments + SiteMesh/Tiles...
>>
>
> I agree.
>
>
>>
>>> 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.

>> I've managed to get a TemplateProcessor find the Lift template using
>> implicit views (so your resources can be Java and it can find a
>> related lift template); I've just not grokked the internals of Lift
>> enough yet to figure out how to actually render it yet :)
>>
>
> :-)
>
>
>> Though a simpler model would be just to use scala for the resource and
>> return a template instantiation for HTML/XML views - but having both
>> options would rock.
>>
>
> Indeed!
>
> 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#

Contributions welcome :)

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 {}.

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