Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage phase != PhaseId.RENDER_RESPONSE #10

Open
jkrhub opened this issue May 7, 2020 · 2 comments
Open

Manage phase != PhaseId.RENDER_RESPONSE #10

jkrhub opened this issue May 7, 2020 · 2 comments

Comments

@jkrhub
Copy link

jkrhub commented May 7, 2020

Hi, I'm tryng your spring custom view scope.

In my project when I render an image with a p:graphicImage I use this method:

public StreamedContent getStreamGeneric(MyPicture myPic) throws Exception { FacesContext context = FacesContext.getCurrentInstance(); try{ if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) { // So, we're rendering the HTML. Return a stub StreamedContent so that it will generate right URL. return new DefaultStreamedContent(); } else { // So, browser is requesting the image. Return a real StreamedContent with the image bytes. return MyUtils.getStream(myPic); } }catch (Exception e) { return new DefaultStreamedContent(); } }

Using the view scope the render of the picture doesn't works.

I wrote this workaround in com.github.jneat.jsf.ViewScope :

` @OverRide
public Object get(String name, ObjectFactory objectFactory) {
UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
if(viewRoot!=null){
//this is true: FacesContext.getCurrentInstance().getCurrentPhaseId() == PhaseId.RENDER_RESPONSE

    	 Map<String, Object> viewMap = viewRoot.getViewMap();
         
         //noinspection SynchronizationOnLocalVariableOrMethodParameter
         if (viewMap.containsKey(name)) {
             return viewMap.get(name);
         } else {
             synchronized (viewMap) {
                 if (viewMap.containsKey(name)) {
                     return viewMap.get(name);
                 } else {
                     logger.trace("Creating bean " + name);
                     Object object = objectFactory.getObject();
                     viewMap.put(name, object);
                     return object;
                 }
             }
         }
    } else {
    	//this is false: FacesContext.getCurrentInstance().getCurrentPhaseId() == PhaseId.RENDER_RESPONSE
    	Object object = objectFactory.getObject();
        return object;
    }
   
}

`
In this way it works, it does not go into null pointer when FacesContext.getCurrentInstance().getViewRoot() is null (after the render response phase)

Are there any contraindications in my fix? If it is a good fix, you could implement it in the project?

Thanks.

@rumatoest
Copy link
Contributor

Hello. Probably you should submit it as a pull request because I can not see from your comment what changes you've made. And thus I can not review it properly.

@jkrhub
Copy link
Author

jkrhub commented May 7, 2020

I never do a pull request, I try now for first time: i got 403 error (I have made a branch and tryed to push it).

My fix is only inside the method

public Object get(String name, ObjectFactory objectFactory)

of your class com.github.jneat.jsf.ViewScope, if you replace this whole method with this you can see the differences:

` @OverRide
public Object get(String name, ObjectFactory objectFactory) {
UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
if(viewRoot!=null){
//this is true: FacesContext.getCurrentInstance().getCurrentPhaseId() == PhaseId.RENDER_RESPONSE

    	 Map<String, Object> viewMap = viewRoot.getViewMap();
         
         //noinspection SynchronizationOnLocalVariableOrMethodParameter
         if (viewMap.containsKey(name)) {
             return viewMap.get(name);
         } else {
             synchronized (viewMap) {
                 if (viewMap.containsKey(name)) {
                     return viewMap.get(name);
                 } else {
                     logger.trace("Creating bean " + name);
                     Object object = objectFactory.getObject();
                     viewMap.put(name, object);
                     return object;
                 }
             }
         }
    } else {
    	//this is false: FacesContext.getCurrentInstance().getCurrentPhaseId() == PhaseId.RENDER_RESPONSE
    	Object object = objectFactory.getObject();
        return object;
    }
   `

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants