Uploaded image for project: 'Tiles Request'
  1. Tiles Request
  2. TREQ-10

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

    Details

    • Type: Bug
    • Status: Closed
    • Priority: 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

        Hide
        nlebas 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
        nlebas 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
        michaelsembwever 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
        michaelsembwever 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
        michaelsembwever mck added a comment -

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

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

        committed. thank you Nicolas.

        Show
        michaelsembwever mck added a comment - - edited committed. thank you Nicolas.

          People

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

            Dates

            • Created:
              Updated:
              Resolved:

              Development