users@jersey.java.net

Re: custom TemplateProcessor not having its constructor injected?

From: James Strachan <james.strachan_at_gmail.com>
Date: Wed, 22 Apr 2009 12:18:45 +0100

2009/4/22 James Strachan <james.strachan_at_gmail.com>:
> 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...)
>
> My class declaration mirrors that of JSPTemplateProcessor.... Yet the
> constructor parameters are all null. Any ideas what I'm doing wrong? I
> notice JSPTemplateProcessor isn't annotated with @Provider - so is
> this special injection only available to JSPTemplateProcessor?
>
>
> @Provider
> public class LiftTemplateProcessor implements TemplateProcessor {
>    @Context
>    HttpContext hc;
>
>    @Context
>    ServletContext servletContext;
>
>    @Context
>    UriInfo ui;
>
>    private final ThreadLocal<HttpServletRequest> requestInvoker;
>
>    private final ThreadLocal<HttpServletResponse> responseInvoker;
>
>    private final String basePath;
>
>    public LiftTemplateProcessor(
>            ResourceConfig resourceConfig,
>            ThreadLocal<HttpServletRequest> requestInvoker,
>            ThreadLocal<HttpServletResponse> responseInvoker) {


Just to answer my own question - DOH - not enough coffee! (Note to
self - have another cup before posting...)

The trick is not to use the JSPTemplateProcessor's style of
constructor, but just inject the required attributes...

    @Context
    HttpServletRequest request;

    @Context
    HttpServletResponse response;

I wonder if its worth adding a little comment to the constructor of
JSPTemplateProcessor to help avoid other folks not having had much
coffee and copy and pasting that constructor code then scratching
their heads when all the parameters are null?

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