addResource to add CSS JS - Only working on PostBack

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

addResource to add CSS JS - Only working on PostBack

fischman_98
To all,

I still have a MyFaces 1.1.6/Tomahawk 1.1.10 in production, client not giving the time to migrate to 2.X.

That said, I'm trying to add javascript to specific pages <head></head section (security code to stop the page from being included in frames; <a href="https://www.owasp.org/index.php/Cross_Frame_Scripting">Cross_Frame_Scripting).

Instead of just adding it to the pages between <f:verbatim></f:verbatim> tags, I am trying to use org.apache.myfaces.renderkit.html.util.AddResource to inject the CSS and JS (analyze the directories and add it to specific pages).

I thought to do it in a phaseListener.

Problem is, I can't get it to work on the initial request, only on PostBacks (is it done in the Apply Requests phase?).

Seems like I should be able to intercept the response and add this...I have the Extensions Filter (Tomahawk) set up and have added ADD_RESOURCE_CLASS (DefaultAddResource) to the context in web.xml.

Anyone have any thoughts?

I haven't posted a question in a long while...I would love to hear a solution for this (relatively) Old School set up!

Thanks.



Reply | Threaded
Open this post in threaded view
|

Re: addResource to add CSS JS - Only working on PostBack

Leonardo Uribe
Hi

"apply request value" phase is invoked on postback only. In the initial
request, only 2 phases are executed: restore view phase and render view
phase. Probably you only need to adjust the code a bit.

regards,

Leonardo Uribe

2016-04-14 21:31 GMT-05:00 fischman_98 <[hidden email]>:

> To all,
>
> I still have a MyFaces 1.1.6/Tomahawk 1.1.10 in production, client not
> giving the time to migrate to 2.X.
>
> That said, I'm trying to add javascript to specific pages <head></head
> section (security code to stop the page from being included in frames; <a
> href="https://www.owasp.org/index.php/Cross_Frame_Scripting
> ">Cross_Frame_Scripting
> ).
>
> Instead of just adding it to the pages between <f:verbatim></f:verbatim>
> tags, I am trying to use org.apache.myfaces.renderkit.html.util.AddResource
> to inject the CSS and JS (/analyze the directories and add it to specific
> pages/).
>
> I thought to do it in a phaseListener.
>
> Problem is, I can't get it to work on the *initial reques*t, only on
> *PostBacks* (/is it done in the *Apply Requests* phase/?).
>
> Seems like I should be able to intercept the response and add this...I have
> the Extensions Filter (Tomahawk) set up and have added ADD_RESOURCE_CLASS
> (DefaultAddResource) to the context in web.xml.
>
> Anyone have any thoughts?
>
> I haven't posted a question in a long while...I would love to hear a
> solution for this (/relatively/) Old School set up!
>
> Thanks.
>
>
>
>
>
>
>
> --
> View this message in context:
> http://myfaces.10567.n7.nabble.com/addResource-to-add-CSS-JS-Only-working-on-PostBack-tp121593.html
> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|

Re: addResource to add CSS JS - Only working on PostBack

fischman_98
So are you confirming/agreeing that the result of an addResource call is handled in the Apply Request phase?

And as to only need to adjust the code a bit, what code and where?  I was trying in a phaselistener, are you suggesting a custom ViewHandler, custom component or ?

Matt
Reply | Threaded
Open this post in threaded view
|

Re: addResource to add CSS JS - Only working on PostBack

Leonardo Uribe
Hi

AddResource API in its default implementation use a buffer to render
resources, but on render response phase, so you should aim to add your
resources in that phase. A custom phase listener or a custom component
using the code in encodeXXX should work.

Regards,

Leonardo
On Apr 19, 2016 8:05 AM, "fischman_98" <[hidden email]>
wrote:

> So are you confirming/agreeing that the result of an addResource call is
> handled in the Apply Request phase?
>
> And as to /only need to adjust the code a bit/, what code and where?  I was
> trying in a phaselistener, are you suggesting a custom ViewHandler, custom
> component or ?
>
> Matt
>
>
>
> --
> View this message in context:
> http://myfaces.10567.n7.nabble.com/addResource-to-add-CSS-JS-Only-working-on-PostBack-tp121593p121611.html
> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|

Re: addResource to add CSS JS - Only working on PostBack

fischman_98
I created this simple test phaseListener before my original post and it only works an a postback, not an initial request.  Put the code in both phases just in case.  No Luck.

package com.ngsimages.cart.phaselistener;

import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

import org.apache.myfaces.renderkit.html.util.AddResource;
import org.apache.myfaces.renderkit.html.util.AddResourceFactory;

/**
 * @author mfischer
 *
 */
public class AddResourceListener implements PhaseListener {
        /**
         *
         */
        private static final long serialVersionUID = -7584339550737064360L;

        @Override
        public void afterPhase(PhaseEvent arg0) {
                AddResource ar = AddResourceFactory.getInstance(arg0.getFacesContext());
                ar.addInlineScriptAtPosition(arg0.getFacesContext(), AddResource.HEADER_BEGIN, "window.open()");
        }
       
        @Override
        public void beforePhase(PhaseEvent arg0) {
                AddResource ar = AddResourceFactory.getInstance(arg0.getFacesContext());
                ar.addInlineScriptAtPosition(arg0.getFacesContext(), AddResource.HEADER_BEGIN, "window.open()");
               
        }
       
        @Override
        public PhaseId getPhaseId() {
                return PhaseId.RENDER_RESPONSE;
        }
}

Shame to have to create a component just to add javascript.

Any other ideas on how to dynamically add a script to the page header?
Reply | Threaded
Open this post in threaded view
|

Re: addResource to add CSS JS - Only working on PostBack

fischman_98
FYI: When added the call to addResource in encodeEnd method of a Custom Component it works on both initial request and postback .

Here's the initial request;

RESTORE_VIEW(1) :: Before
RESTORE_VIEW(1) :: After
RENDER_RESPONSE(6) :: Before
AddResource Here!
RENDER_RESPONSE(6) :: After

Same code I had in the listener in the encodeEnd;

        AddResource ar = AddResourceFactory.getInstance(facesContext);
        ar.addInlineScriptAtPosition(facesContext, AddResource.HEADER_BEGIN, "window.open()");
        System.out.println("AddResource Here!");

So....any thoughts why it works from the component and not from the phase listener?

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: addResource to add CSS JS - Only working on PostBack

Mike Kienenberger
There is only a RENDER_RESPONSE phase for the initial request in a
phase listener, but all of the phases in a postback.

Does that help?


On Tue, Apr 19, 2016 at 2:09 PM, fischman_98
<[hidden email]> wrote:

> *FYI*: When added the call to addResource in encodeEnd method of a Custom
> Component it works on both /*initial request*/ and /*postback */.
>
> Here's the initial request;
>
> RESTORE_VIEW(1) :: Before
> RESTORE_VIEW(1) :: After
> RENDER_RESPONSE(6) :: Before
> *AddResource Here!*
> RENDER_RESPONSE(6) :: After
>
> Same code I had in the listener in the encodeEnd;
>
>         AddResource ar = AddResourceFactory.getInstance(facesContext);
>         ar.addInlineScriptAtPosition(facesContext, AddResource.HEADER_BEGIN,
> "window.open()");
>         System.out.println("AddResource Here!");
>
> So....any thoughts why it works from the component and not from the phase
> listener?
>
> Thanks.
>
>
>
>
> --
> View this message in context: http://myfaces.10567.n7.nabble.com/addResource-to-add-CSS-JS-Only-working-on-PostBack-tp121593p121619.html
> Sent from the MyFaces - Users mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|

Re: addResource to add CSS JS - Only working on PostBack

Mike Kienenberger
I guess it probably doesn't help -- it looks like your phase listener
was already using RENDER_RESPONSE.

On Tue, Apr 19, 2016 at 3:13 PM, Mike Kienenberger <[hidden email]> wrote:

> There is only a RENDER_RESPONSE phase for the initial request in a
> phase listener, but all of the phases in a postback.
>
> Does that help?
>
>
> On Tue, Apr 19, 2016 at 2:09 PM, fischman_98
> <[hidden email]> wrote:
>> *FYI*: When added the call to addResource in encodeEnd method of a Custom
>> Component it works on both /*initial request*/ and /*postback */.
>>
>> Here's the initial request;
>>
>> RESTORE_VIEW(1) :: Before
>> RESTORE_VIEW(1) :: After
>> RENDER_RESPONSE(6) :: Before
>> *AddResource Here!*
>> RENDER_RESPONSE(6) :: After
>>
>> Same code I had in the listener in the encodeEnd;
>>
>>         AddResource ar = AddResourceFactory.getInstance(facesContext);
>>         ar.addInlineScriptAtPosition(facesContext, AddResource.HEADER_BEGIN,
>> "window.open()");
>>         System.out.println("AddResource Here!");
>>
>> So....any thoughts why it works from the component and not from the phase
>> listener?
>>
>> Thanks.
>>
>>
>>
>>
>> --
>> View this message in context: http://myfaces.10567.n7.nabble.com/addResource-to-add-CSS-JS-Only-working-on-PostBack-tp121593p121619.html
>> Sent from the MyFaces - Users mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|

Re: addResource to add CSS JS - Only working on PostBack

Leonardo Uribe
Hi

Add the resource in afterPhase(...) is too late in the lifecycle, because
if the response was flushed the script could not be rendered. Add it in
beforePhase(...)  of render response should work.

Just FYI, ExtensionsFilter class is the one responsible to create the
buffer and then parse the response and insert the code.

The API was meant to be used in components, but I do not see any reason why
it should not work. It could be an old bug that was fixed at some point in
time.

regards,

Leonardo Uribe

2016-04-19 14:14 GMT-05:00 Mike Kienenberger <[hidden email]>:

> I guess it probably doesn't help -- it looks like your phase listener
> was already using RENDER_RESPONSE.
>
> On Tue, Apr 19, 2016 at 3:13 PM, Mike Kienenberger <[hidden email]>
> wrote:
> > There is only a RENDER_RESPONSE phase for the initial request in a
> > phase listener, but all of the phases in a postback.
> >
> > Does that help?
> >
> >
> > On Tue, Apr 19, 2016 at 2:09 PM, fischman_98
> > <[hidden email]> wrote:
> >> *FYI*: When added the call to addResource in encodeEnd method of a
> Custom
> >> Component it works on both /*initial request*/ and /*postback */.
> >>
> >> Here's the initial request;
> >>
> >> RESTORE_VIEW(1) :: Before
> >> RESTORE_VIEW(1) :: After
> >> RENDER_RESPONSE(6) :: Before
> >> *AddResource Here!*
> >> RENDER_RESPONSE(6) :: After
> >>
> >> Same code I had in the listener in the encodeEnd;
> >>
> >>         AddResource ar = AddResourceFactory.getInstance(facesContext);
> >>         ar.addInlineScriptAtPosition(facesContext,
> AddResource.HEADER_BEGIN,
> >> "window.open()");
> >>         System.out.println("AddResource Here!");
> >>
> >> So....any thoughts why it works from the component and not from the
> phase
> >> listener?
> >>
> >> Thanks.
> >>
> >>
> >>
> >>
> >> --
> >> View this message in context:
> http://myfaces.10567.n7.nabble.com/addResource-to-add-CSS-JS-Only-working-on-PostBack-tp121593p121619.html
> >> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|

Re: addResource to add CSS JS - Only working on PostBack

fischman_98
In reply to this post by Mike Kienenberger
Mike, Thanks...yeah were all good with the understanding of the Lifecycle...

I'm just curious why the addResource call WORKS in an encodeEnd of a Custom Component, which is fired in the Render Response phase,

but...

DOES NOT WORK when the addResource is called directly from a PhaseListener during the same Render Response phase...

Any thoughts?
Reply | Threaded
Open this post in threaded view
|

Re: addResource to add CSS JS - Only working on PostBack

Mike Kienenberger
You've probably already figured it out, but for anyone reading this
thread later, what Leonardo said explains it.

Component encodeEnd is called during the time that the response is
rendered (between before and after phase listeners).   "After" is too
late.

On Tue, Apr 19, 2016 at 2:41 PM, fischman_98
<[hidden email]> wrote:

> Mike, Thanks...yeah were all good with the understanding of the Lifecycle...
>
> I'm just curious why the addResource call *WORKS* in an /encodeEnd /of a
> Custom Component, which is fired in the */Render Response/* phase,
>
> but...
>
> *DOES NOT WORK* when the addResource is called directly from a PhaseListener
> during the same */Render Response/* phase...
>
> Any thoughts?
>
>
>
> --
> View this message in context: http://myfaces.10567.n7.nabble.com/addResource-to-add-CSS-JS-Only-working-on-PostBack-tp121593p121623.html
> Sent from the MyFaces - Users mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|

Re: addResource to add CSS JS - Only working on PostBack

fischman_98
Gentlemen,

After all this (My initial attempt at doing this was using a phaselistener after all), I now see WHEN the phaselistener solution is not working.  The question now is why?

When I use the URL: http://localhost:8080/<context>/index.jsf - IT WORKS

When I use the URL: http://localhost:8080/<context>/ - IT DOES NOT WORK

So when Tomcat defaults to the index.jsf, IT DOES NOT WORK.  When I specify the index.jsf (or any other jsf page for that matter), IT WORKS!

Any thoughts.

Thank you for your assistance.  

Matt
Reply | Threaded
Open this post in threaded view
|

Re: addResource to add CSS JS - Only working on PostBack

fischman_98
OK.

Moved the org.apache.myfaces.webapp.filter.WelcomeFileFilter after the Extensions Filter in the web.xml and the PhaseListener approach to addResource seems to be working.  More testing to follow and I will report any issues.

That was a lot of investigation due to Filter Order/etc.!!  

Thanks for the input!