javaserverfaces
  1. javaserverfaces
  2. JAVASERVERFACES-2638

Invalidated session causes NPE with ViewScoped beans

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.1.16
    • Fix Version/s: 2.2.0-m08, 2.1.17
    • Component/s: managed bean
    • Labels:
      None

      Description

      Create a view with view-scoped beans. If that view sends a request that invalidates the session, you will get a NullPointerException in com.sun.faces.mgbean.BeanManager.ViewScopeHandler.handle().

      The simple fix is to add a guard clause and just return when getViewMap() is null.

          private static class ViewScopeHandler implements ScopeHandler {
      
                  public void handle(String name, Object bean, FacesContext context) {
                      if (context.getViewRoot().getViewMap() == null) return;
      
                      context.getViewRoot().getViewMap().put(name, bean);
      
                  }
      

        Issue Links

          Activity

          Hide
          Manfred Riem added a comment -

          Looking at the code that could certainly happen, but once the request invalidates the session it by virtue of the view being stored in the session it also invalidates any view scoped beans. Would a ViewExpiredException be more appropriate here?

          Show
          Manfred Riem added a comment - Looking at the code that could certainly happen, but once the request invalidates the session it by virtue of the view being stored in the session it also invalidates any view scoped beans. Would a ViewExpiredException be more appropriate here?
          Hide
          Manfred Riem added a comment -

          The code in question will not return null as the UIView.getViewMap() call will create a view map if it not already exists. If the session was invalidate it will recreate an empty view map. If you have an example that does reproduce a NullPointerException in any particular scenario feel free to create a new issue and attach a reproducer and we'll have a look.

          Show
          Manfred Riem added a comment - The code in question will not return null as the UIView.getViewMap() call will create a view map if it not already exists. If the session was invalidate it will recreate an empty view map. If you have an example that does reproduce a NullPointerException in any particular scenario feel free to create a new issue and attach a reproducer and we'll have a look.
          Hide
          ssilvert added a comment -

          Please reopen. UIView.getViewMap() will indeed return null in this case, which may be part of the problem.

          I'll try to get you some sample code soon. The test code I have contains some classes that are proprietary so I can't give you what I have right now.

          BTW, ViewExpiredException is not appropriate. I'm talking about the request that does the invalidate(). So the view is not expired yet.

          Show
          ssilvert added a comment - Please reopen. UIView.getViewMap() will indeed return null in this case, which may be part of the problem. I'll try to get you some sample code soon. The test code I have contains some classes that are proprietary so I can't give you what I have right now. BTW, ViewExpiredException is not appropriate. I'm talking about the request that does the invalidate(). So the view is not expired yet.
          Hide
          Manfred Riem added a comment -

          Where you able to get some sample code?

          Show
          Manfred Riem added a comment - Where you able to get some sample code?
          Hide
          Manfred Riem added a comment -

          Applied to 2.1 branch,

          svn commit -m "Fixes http://java.net/jira/browse/JAVASERVERFACES-2638, make sure if the session is invalidated the BeanManager works correctly."
          Sending jsf-ri\src\main\java\com\sun\faces\mgbean\BeanManager.java
          Adding test\agnostic\scope\view\src\main\java\com\sun\faces\test\agnostic\scope\view\Invalidated2Bean.java
          Adding test\agnostic\scope\view\src\main\webapp\invalidatedSession2.xhtml
          Adding test\agnostic\scope\view\src\test\java\com\sun\faces\test\agnostic\scope\view\Issue2638IT.java
          Transmitting file data ....
          Committed revision 11319.

          Show
          Manfred Riem added a comment - Applied to 2.1 branch, svn commit -m "Fixes http://java.net/jira/browse/JAVASERVERFACES-2638 , make sure if the session is invalidated the BeanManager works correctly." Sending jsf-ri\src\main\java\com\sun\faces\mgbean\BeanManager.java Adding test\agnostic\scope\view\src\main\java\com\sun\faces\test\agnostic\scope\view\Invalidated2Bean.java Adding test\agnostic\scope\view\src\main\webapp\invalidatedSession2.xhtml Adding test\agnostic\scope\view\src\test\java\com\sun\faces\test\agnostic\scope\view\Issue2638IT.java Transmitting file data .... Committed revision 11319.
          Hide
          Manfred Riem added a comment -

          Applied to 2.2 trunk,

          svn commit -m "Fixes http://java.net/jira/browse/JAVASERVERFACES-2638, make sure if the session is invalidated the BeanManager works correctly."
          Sending jsf-ri\src\main\java\com\sun\faces\mgbean\BeanManager.java
          Adding test\agnostic\scope\view\src\main\java\com\sun\faces\test\agnostic\scope\view\Invalidated2Bean.java
          Adding test\agnostic\scope\view\src\main\webapp\invalidatedSession2.xhtml
          Adding test\agnostic\scope\view\src\test\java\com\sun\faces\test\agnostic\scope\view\Issue2638IT.java
          Transmitting file data ....
          Committed revision 11320.

          Show
          Manfred Riem added a comment - Applied to 2.2 trunk, svn commit -m "Fixes http://java.net/jira/browse/JAVASERVERFACES-2638 , make sure if the session is invalidated the BeanManager works correctly." Sending jsf-ri\src\main\java\com\sun\faces\mgbean\BeanManager.java Adding test\agnostic\scope\view\src\main\java\com\sun\faces\test\agnostic\scope\view\Invalidated2Bean.java Adding test\agnostic\scope\view\src\main\webapp\invalidatedSession2.xhtml Adding test\agnostic\scope\view\src\test\java\com\sun\faces\test\agnostic\scope\view\Issue2638IT.java Transmitting file data .... Committed revision 11320.
          Hide
          ssilvert added a comment -

          Sorry I've been slow to get back to this. I've had a lot of pressing issues lately. I see you've fixed it though. If you still need sample code I can provide it.

          Show
          ssilvert added a comment - Sorry I've been slow to get back to this. I've had a lot of pressing issues lately. I see you've fixed it though. If you still need sample code I can provide it.
          Hide
          Manfred Riem added a comment -

          If you feel like verifying the fix, please do so.

          Show
          Manfred Riem added a comment - If you feel like verifying the fix, please do so.

            People

            • Assignee:
              Manfred Riem
              Reporter:
              ssilvert
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: