Tiles Request
  1. Tiles Request
  2. TREQ-10

NPE while reading the session scope when no session has been created

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.0.0
    • Fix Version/s: 1.0.0
    • Labels:
      None

      Description

      If no HttpSession has been created, the following code leads to an exception:

      Map<String, Object> scopes = new HashMap<String, Object>();
      Map<String, Object> data = request.getContext("session");
      scopes.putAll(data);

      java.lang.NullPointerException
      org.apache.tiles.request.util.RequestUtil.enumerationSize(RequestUtil.java:64)
      org.apache.tiles.request.collection.ReadOnlyEnumerationMap.size(ReadOnlyEnumerationMap.java:128)
      java.util.HashMap.putAll(HashMap.java:498)

        Activity

        Nicolas Le Bas created issue -
        Hide
        Nicolas Le Bas added a comment -

        The following patch solves it (AttributeExtractor.getKeys should never return null)

        — a/tiles-request/tiles-request-servlet/src/main/java/org/apache/tiles/request/servlet/extractor/SessionScopeExtractor.java
        +++ b/tiles-request/tiles-request-servlet/src/main/java/org/apache/tiles/request/servlet/extractor/SessionScopeExtractor.java
        @@ -21,6 +21,7 @@
        package org.apache.tiles.request.servlet.extractor;

        import java.util.Enumeration;
        +import java.util.Collections;

        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpSession;
        @@ -68,7 +69,7 @@ public class SessionScopeExtractor implements AttributeExtractor {
        if (session != null)

        { return session.getAttributeNames(); }
        • return null;
          + return Collections.enumeration(Collections.EMPTY_SET);
          }

        @Override

        Show
        Nicolas Le Bas added a comment - The following patch solves it (AttributeExtractor.getKeys should never return null) — a/tiles-request/tiles-request-servlet/src/main/java/org/apache/tiles/request/servlet/extractor/SessionScopeExtractor.java +++ b/tiles-request/tiles-request-servlet/src/main/java/org/apache/tiles/request/servlet/extractor/SessionScopeExtractor.java @@ -21,6 +21,7 @@ package org.apache.tiles.request.servlet.extractor; import java.util.Enumeration; +import java.util.Collections; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; @@ -68,7 +69,7 @@ public class SessionScopeExtractor implements AttributeExtractor { if (session != null) { return session.getAttributeNames(); } return null; + return Collections.enumeration(Collections.EMPTY_SET); } @Override
        Nicolas Le Bas made changes -
        Field Original Value New Value
        Priority Major [ 3 ] Minor [ 4 ]
        Hide
        mck added a comment -

        this patch breaks SessionScopeExtractorTest.testGetKeysNoSession(..)

        but i agree that returning null should always be avoided. especially when an empty collection is an alternative.

        Show
        mck added a comment - this patch breaks SessionScopeExtractorTest.testGetKeysNoSession(..) but i agree that returning null should always be avoided. especially when an empty collection is an alternative.
        Hide
        mck added a comment -

        New patch including required test changes.
        Nicolas: does this look ok to you?

        Show
        mck added a comment - New patch including required test changes. Nicolas: does this look ok to you?
        mck made changes -
        Attachment TREQ-10.patch [ 12501521 ]
        Hide
        Nicolas Le Bas added a comment -

        I have been a bit hasty indeed.
        You patch looks great, except that Collections.emptyEnumeration() exists since 1.7, and Tiles is supposed to run on java 1.5 according to the POM.
        My solution is not ideal either, probably Collections.enumeration(Collections.<String>emptySet()) is best.

        Show
        Nicolas Le Bas added a comment - I have been a bit hasty indeed. You patch looks great, except that Collections.emptyEnumeration() exists since 1.7, and Tiles is supposed to run on java 1.5 according to the POM. My solution is not ideal either, probably Collections.enumeration(Collections.<String>emptySet()) is best.
        Hide
        mck added a comment - - edited

        committed. thank you Nicolas.

        Show
        mck added a comment - - edited committed. thank you Nicolas.
        mck made changes -
        Assignee Mck SembWever [ michaelsembwever ]
        mck made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 1.0.0 [ 12316845 ]
        Resolution Fixed [ 1 ]
        Nicolas Le Bas made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Transition Time In Source Status Execution Times Last Executer Last Execution Date
        Open Open Resolved Resolved
        11d 19h 17m 1 mck 08/Nov/11 13:51
        Resolved Resolved Closed Closed
        215d 8h 48m 1 Nicolas Le Bas 10/Jun/12 23:39

          People

          • Assignee:
            mck
            Reporter:
            Nicolas Le Bas
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development