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)

      1. TREQ-10.patch
        2 kB
        Mck SembWever

        Activity

        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
        Hide
        Mck SembWever 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 SembWever 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 SembWever added a comment -

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

        Show
        Mck SembWever added a comment - New patch including required test changes. Nicolas: does this look ok to you?
        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 SembWever added a comment - - edited

        committed. thank you Nicolas.

        Show
        Mck SembWever added a comment - - edited committed. thank you Nicolas.

          People

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

            Dates

            • Created:
              Updated:
              Resolved:

              Development