Interpolate response with IResponseFilter

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Interpolate response with IResponseFilter

guillaume.mary
Hi all,

Here is my use case: I need to fill variables, like {myVar}, that are rendered into the page. My approach is to use a specialized IResponseFilter for that. The important point is the filter should be applied to a certain Page of my application, and more of that, the variable values are picked up in this particular page (they are stored in a specific attribute of the page instance).

All was right in Wicket 1.4, I was able to check the targeted page in the IResponseFilter thanks to the code:
Page page = RequestCycle.get().getResponsePage();

Since Wicket 1.5, the getResponsePage() method disappeared, so I'm facing trouble to check (and get) my page.
I tried to replace the so simple getResponsePage() by some combination of overriden MountedMapper, WebPageRendered, BufferedResponseRequestHandler, or RequestCycle, etc, but I didn't manage to fill all the traps to ensure that all requests made by the user on the page are interpolated. Here is what I found:
- the first time the user opens the page seems to be treated by overriding WebPageRenderer.respond() and testing his renderPageRequestHandler.getPageClass()
- the Ajax case (user clicks on an ajax link on my page, I should interpolate the result response) seems to be treated by checking the result of PageInstanceMapper.mapRequest(..) to see if it's a IPageRequestHandler, then getting the page thru pageProvider
- the worst case is the redirecting one: the user click on a link that makes a setResponsePage(MyPage) then Wicket uses a temporary buffered response stored in WebApplication.storedResponse, then sends redirect to client, which recalls Wicket thats serves the cached response ! The second request doesn't contain the targeted Page because a BufferedResponseRequestHandler takes priority

Done all of that, the result is not always good, there are still unterpolated variables in my HTML.

It seems to be so complicated that I'm asking myself if i'm on the good way.
Does anyone can help me accomplish my use case ? (a good knowledge of wicket 1.5 request handling seems to be necessary !)

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Interpolate response with IResponseFilter

Martin Grigorov-4
Hi,

In Wicket 1.5.8 there is
org.apache.wicket.request.cycle.PageRequestHandlerTracker which you
can use to get the requested page and the response page. In your case
you need the latter.

PageRequestHandlerTracker.getLastHandler(cycle), then check whether it
is IPageRequestHandler and call #getPage() on it.

On Fri, Aug 24, 2012 at 4:32 PM, Guillaume Mary
<[hidden email]> wrote:

> Hi all,
>
> Here is my use case: I need to fill variables, like {myVar}, that are rendered into the page. My approach is to use a specialized IResponseFilter for that. The important point is the filter should be applied to a certain Page of my application, and more of that, the variable values are picked up in this particular page (they are stored in a specific attribute of the page instance).
>
> All was right in Wicket 1.4, I was able to check the targeted page in the IResponseFilter thanks to the code:
> Page page = RequestCycle.get().getResponsePage();
>
> Since Wicket 1.5, the getResponsePage() method disappeared, so I'm facing trouble to check (and get) my page.
> I tried to replace the so simple getResponsePage() by some combination of overriden MountedMapper, WebPageRendered, BufferedResponseRequestHandler, or RequestCycle, etc, but I didn't manage to fill all the traps to ensure that all requests made by the user on the page are interpolated. Here is what I found:
> - the first time the user opens the page seems to be treated by overriding WebPageRenderer.respond() and testing his renderPageRequestHandler.getPageClass()
> - the Ajax case (user clicks on an ajax link on my page, I should interpolate the result response) seems to be treated by checking the result of PageInstanceMapper.mapRequest(..) to see if it's a IPageRequestHandler, then getting the page thru pageProvider
> - the worst case is the redirecting one: the user click on a link that makes a setResponsePage(MyPage) then Wicket uses a temporary buffered response stored in WebApplication.storedResponse, then sends redirect to client, which recalls Wicket thats serves the cached response ! The second request doesn't contain the targeted Page because a BufferedResponseRequestHandler takes priority
>
> Done all of that, the result is not always good, there are still unterpolated variables in my HTML.
>
> It seems to be so complicated that I'm asking myself if i'm on the good way.
> Does anyone can help me accomplish my use case ? (a good knowledge of wicket 1.5 request handling seems to be necessary !)
>
> Thanks



--
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Interpolate response with IResponseFilter

Sven Meier
In reply to this post by guillaume.mary
Where do these {myVar}s come from?

Sven

On 08/24/2012 03:32 PM, Guillaume Mary wrote:

> Hi all,
>
> Here is my use case: I need to fill variables, like {myVar}, that are rendered into the page. My approach is to use a specialized IResponseFilter for that. The important point is the filter should be applied to a certain Page of my application, and more of that, the variable values are picked up in this particular page (they are stored in a specific attribute of the page instance).
>
> All was right in Wicket 1.4, I was able to check the targeted page in the IResponseFilter thanks to the code:
> Page page = RequestCycle.get().getResponsePage();
>
> Since Wicket 1.5, the getResponsePage() method disappeared, so I'm facing trouble to check (and get) my page.
> I tried to replace the so simple getResponsePage() by some combination of overriden MountedMapper, WebPageRendered, BufferedResponseRequestHandler, or RequestCycle, etc, but I didn't manage to fill all the traps to ensure that all requests made by the user on the page are interpolated. Here is what I found:
> - the first time the user opens the page seems to be treated by overriding WebPageRenderer.respond() and testing his renderPageRequestHandler.getPageClass()
> - the Ajax case (user clicks on an ajax link on my page, I should interpolate the result response) seems to be treated by checking the result of PageInstanceMapper.mapRequest(..) to see if it's a IPageRequestHandler, then getting the page thru pageProvider
> - the worst case is the redirecting one: the user click on a link that makes a setResponsePage(MyPage) then Wicket uses a temporary buffered response stored in WebApplication.storedResponse, then sends redirect to client, which recalls Wicket thats serves the cached response ! The second request doesn't contain the targeted Page because a BufferedResponseRequestHandler takes priority
>
> Done all of that, the result is not always good, there are still unterpolated variables in my HTML.
>
> It seems to be so complicated that I'm asking myself if i'm on the good way.
> Does anyone can help me accomplish my use case ? (a good knowledge of wicket 1.5 request handling seems to be necessary !)
>
> Thanks
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Interpolate response with IResponseFilter

guillaume.mary
In reply to this post by Martin Grigorov-4
Thanks Martin, it's easier to find the actual page with PageRequestHandlerTracker.

Nevertheless, the redirecting case is still not filled because as I debugged, PageRequestHandlerTracker returns null for both first and last handler when my filter is called (am i wrong ??). That's almost the same problem as wicket 1.5.7: there's no more IRequestHandler during call of IResponseFilter.filter(..) on the second request. It's handle by a BufferedResponseRequestHandler at the beggining but it disappears !

@Sven: my {myVar}s comes from HTML, or even model of components
Reply | Threaded
Open this post in threaded view
|

Re: Interpolate response with IResponseFilter

Sven Meier
> my {myVar}s comes from HTML, or even model of components

If these placeholders are in your HTML, why don't you just use an AbstractTransformerBehavior to fill them?

Sven


On 08/24/2012 04:51 PM, guillaume.mary wrote:

> Thanks Martin, it's easier to find the actual page with
> PageRequestHandlerTracker.
>
> Nevertheless, the redirecting case is still not filled because as I
> debugged, PageRequestHandlerTracker returns null for both first and last
> handler when my filter is called (am i wrong ??). That's almost the same
> problem as wicket 1.5.7: there's no more IRequestHandler during call of
> IResponseFilter.filter(..) on the second request. It's handle by a
> BufferedResponseRequestHandler at the beggining but it disappears !
>
> @Sven: my {myVar}s comes from HTML, or even model of components
>
>
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/Interpolate-response-with-IResponseFilter-tp4651476p4651480.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Interpolate response with IResponseFilter

guillaume.mary
Because I didn't know AbstractTransformerBehavior  ! :)
Many thanks !
I put it on my Page instance so it fullfills the last case of post + redirect (but the filter is still necessary for components refreshed by Ajax, otherwise I should add AbstractTransformerBehavior to all components of my Page)

Reply | Threaded
Open this post in threaded view
|

Re: Interpolate response with IResponseFilter

Sven Meier
Just an idea:

You could use an AjaxRequestTarget.IListener which adds a temporary
behavior to the updated components.

Sven

On 08/24/2012 06:19 PM, guillaume.mary wrote:

> Because I didn't know AbstractTransformerBehavior  ! :)
> Many thanks !
> I put it on my Page instance so it fullfills the last case of post +
> redirect (but the filter is still necessary for components refreshed by
> Ajax, otherwise I should add AbstractTransformerBehavior to all components
> of my Page)
>
>
>
>
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/Interpolate-response-with-IResponseFilter-tp4651476p4651486.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Interpolate response with IResponseFilter

guillaume.mary
Thanks for the tip Sven, I will give it a try.
For now I have another issue about the AbstractTransformerBehavior: as soon as I add it to my Page, I get encoding problems of characters into my page, this is weird. Here is my very simple transformer:
new AbstractTransformerBehavior() {
    public CharSequence transform(Component component, CharSequence output) throws Exception {
        return output.toString();
    }
}

You should ask "why are you using output.toString() since returning output is sufficient ?". Well, the begining of the story is that I really need a String instance to be interpolated by Wicket MapVariableInterpolator, I encountered encoding problems so I removed interpolation and came to the above code.

The encoding problem is that accent characters are replaced by ? in my browser.

So, I realized that response headers are very different (in my client browser) with and without an AbstractTransformerBehavior. I think it shouldn't and I'm guessing that's the cause of my charset problems.
AbstractTransformerBehavior seems to lose the initial headers, but I didn't see anything for that in Wicket code.
Any idea ?
Reply | Threaded
Open this post in threaded view
|

Re: Interpolate response with IResponseFilter

guillaume.mary
After searching a bit, I found that AbstractTransformerBehavior replaces the RequestCycle.response temporarly to put its own, between beforeRender en afterRender methods, whereas other classes manipulates the response during that phase, so when AbstractTransformerBehavior puts back the "original" response, it is no more the "official" response.
In fact I'm not sure that AbstractTransformerBehavior is for me, i'm going to write my own behavior which doesn't keep the "orginal" response. First tests seem promising.