velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cbris...@apache.org
Subject svn commit: r1801740 - /velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ViewToolContext.java
Date Wed, 12 Jul 2017 14:42:51 GMT
Author: cbrisson
Date: Wed Jul 12 14:42:51 2017
New Revision: 1801740

URL: http://svn.apache.org/viewvc?rev=1801740&view=rev
Log:
[tools] Protection from NPEs in view context initialization whenever request is null

There may only be a servlet context scope in some cases, for instance in a webapp background
thread.


Modified:
    velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ViewToolContext.java

Modified: velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ViewToolContext.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ViewToolContext.java?rev=1801740&r1=1801739&r2=1801740&view=diff
==============================================================================
--- velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ViewToolContext.java
(original)
+++ velocity/tools/trunk/velocity-tools-view/src/main/java/org/apache/velocity/tools/view/ViewToolContext.java
Wed Jul 12 14:42:51 2017
@@ -109,7 +109,10 @@ public class ViewToolContext extends Too
         putToolProperty(RESPONSE, getResponse());
         putToolProperty(SESSION, getSession());
         putToolProperty(SERVLET_CONTEXT_KEY, getServletContext());
-        putToolProperty(PATH_KEY, ServletUtils.getPath(getRequest()));
+        if (getRequest() != null)
+        {
+            putToolProperty(PATH_KEY, ServletUtils.getPath(getRequest()));
+        }
     }
 
     protected List<Toolbox> getToolboxes()
@@ -129,12 +132,14 @@ public class ViewToolContext extends Too
 
     protected void addToolboxesUnderKey(String toolboxKey)
     {
-        Toolbox reqTools = (Toolbox)getRequest().getAttribute(toolboxKey);
-        if (reqTools != null)
+        if (getRequest() != null)
         {
-            addToolbox(reqTools);
+            Toolbox reqTools = (Toolbox)getRequest().getAttribute(toolboxKey);
+            if (reqTools != null)
+            {
+                addToolbox(reqTools);
+            }
         }
-
         if (getSession() != null)
         {
             Toolbox sessTools = (Toolbox)getSession().getAttribute(toolboxKey);
@@ -214,11 +219,11 @@ public class ViewToolContext extends Too
     {
         if (key.equals(REQUEST))
         {
-            return request;
+            return getRequest();
         }
         else if(key.equals(RESPONSE))
         {
-            return response;
+            return getResponse();
         }
         else if (key.equals(SESSION))
         {
@@ -241,7 +246,11 @@ public class ViewToolContext extends Too
      */
     public Object getAttribute(String key)
     {
-        Object o = request.getAttribute(key);
+        Object o = null;
+        if (getRequest() != null)
+        {
+            getRequest().getAttribute(key);
+        }
         if (o == null)
         {
             if (getSession() != null)
@@ -324,8 +333,8 @@ public class ViewToolContext extends Too
      {
          return super.containsKey(key)
            || getAttribute(key) != null
-           || key.equals(REQUEST) && request != null
-           || key.equals(RESPONSE) && response != null
+           || key.equals(REQUEST) && getRequest() != null
+           || key.equals(RESPONSE) && getResponse() != null
            || key.equals(SESSION) && getSession() != null
            || key.equals(APPLICATION) && application != null;
      }



Mime
View raw message