accessing request during page parsing

View: New views
3 Messages — Rating Filter:   Alert me  

accessing request during page parsing

by shamsian :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I am following the method outlined in http://java.dzone.com/news/using-sitemesh-page-modification-not-just-decoration to use SiteMesh for page modification. While processing a tag int the "public void process(Tag tag)" function of my extened BasicRule class, I need to look and see what page I am in by accessing the HttpServletRequest request object. Any pointers as to how I can do that. thanks - ben

Re: accessing request during page parsing

by shamsian :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

After looking at the code I noticed the Page class has getRequest method.  I tried saving the PageBuilder parameter passed to addUserDefinedRules and using the getRequest method after casting it to Page.  This did not work since this call always returns null. I then noticed this method is deprecated.  

Is there any way to access the HttpServletRequest object while processing a tag?

Re: accessing request during page parsing

by Joe Walnes-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It's great to see you using the feature.

The bad news: I'm afraid the PageParser API does not allow the HttpServletRequest to be passed down to the TagRule. This is a short coming in SiteMesh that I plan to address in the future.

The good news: It's possible to work around this. You can use a static ThreadLocal to act as intermediate storage between the SiteMeshFilter and the TagRule (the Wormhole Pattern).

Disclaimer: I'm not usually an advocate of ThreadLocals as they usually suggest a hole elsewhere in the design. In this case, the hole is in SiteMesh's design, so you have no choice. This solution is dirty, but will work.

  public class MySiteMeshFilter extends SiteMeshFilter {

    private static final ThreadLocal<HttpServletRequest> requestStorage
        = new ThreadLocal<HttpServletRequest>();

    @Override
    public void doFilter(ServletRequest rq, ServletResponse rs, FilterChain chain)
            throws IOException, ServletException {
      requestStorage.set((HttpServletRequest) rq);
      super.doFilter(rq, rs, chain);
      requestStorage.remove();
    }

    public static HttpServletRequest getCurrentRequest() {
      return requestStorage.get();
    }
  }

Then, in web.xml, deploy your new MySiteMeshFilter, instead of the standard SiteMeshFilter.

Now, from your TagRule implementation, you should be able to call the static method MySiteMeshFilter.getCurrentRequest() to access the HttpServletRequest.

Again, it's not pretty, but works :)

cheers
-Joe


On Sat, Jun 13, 2009 at 3:35 AM, shamsian <bshamsian@...> wrote:

After looking at the code I noticed the Page class has getRequest method.  I
tried saving the PageBuilder parameter passed to addUserDefinedRules and
using the getRequest method after casting it to Page.  This did not work
since this call always returns null. I then noticed this method is
deprecated.

Is there any way to access the HttpServletRequest object while processing a
tag?
--
View this message in context: http://www.nabble.com/accessing-request-during-page-parsing-tp24008440p24008586.html
Sent from the OpenSymphony - SiteMesh mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...