This is an automated email from the ASF dual-hosted git repository.

kusal pushed a commit to branch WW-5336-deprecate-ognltool
in repository https://gitbox.apache.org/repos/asf/struts.git

commit 0c524a37791ad9d9239a971c03e418972895a400
Author: Kusal Kithul-Godage <g...@kusal.io>
AuthorDate: Mon Aug 21 19:08:12 2023 +1000

    WW-5336 Clean up StrutsUtil
---
 .../java/org/apache/struts2/util/StrutsUtil.java   | 126 ++++++++-------------
 .../org/apache/struts2/util/StrutsUtilTest.java    |   5 +
 2 files changed, 55 insertions(+), 76 deletions(-)

diff --git a/core/src/main/java/org/apache/struts2/util/StrutsUtil.java 
b/core/src/main/java/org/apache/struts2/util/StrutsUtil.java
index f9fc43bf3..9946cd944 100644
--- a/core/src/main/java/org/apache/struts2/util/StrutsUtil.java
+++ b/core/src/main/java/org/apache/struts2/util/StrutsUtil.java
@@ -18,14 +18,16 @@
  */
 package org.apache.struts2.util;
 
+import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.ObjectFactory;
+import com.opensymphony.xwork2.ognl.OgnlUtil;
 import com.opensymphony.xwork2.util.ClassLoaderUtil;
 import com.opensymphony.xwork2.util.TextParseUtil;
 import com.opensymphony.xwork2.util.ValueStack;
+import ognl.OgnlException;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.apache.struts2.StrutsException;
-import org.apache.struts2.views.jsp.ui.OgnlTool;
 import org.apache.struts2.views.util.UrlHelper;
 
 import javax.servlet.RequestDispatcher;
@@ -39,7 +41,15 @@ import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+
+import static java.text.MessageFormat.format;
+import static java.util.Collections.singletonList;
 
 /**
  * Struts base utility class, for use in Velocity and Freemarker templates
@@ -50,13 +60,12 @@ public class StrutsUtil {
 
     protected HttpServletRequest request;
     protected HttpServletResponse response;
-    protected Map<String, Class> classes = new Hashtable<>();
-    protected OgnlTool ognl;
+    protected Map<String, Class<?>> classes = new Hashtable<>();
     protected OgnlUtil ognl;
     protected ValueStack stack;
 
-    private UrlHelper urlHelper;
-    private ObjectFactory objectFactory;
+    private final UrlHelper urlHelper;
+    private final ObjectFactory objectFactory;
 
     public StrutsUtil(ValueStack stack, HttpServletRequest request, 
HttpServletResponse response) {
         this.stack = stack;
@@ -67,16 +76,10 @@ public class StrutsUtil {
         this.objectFactory = 
stack.getActionContext().getContainer().getInstance(ObjectFactory.class);
     }
 
-    public Object bean(Object aName) throws Exception {
-        String name = aName.toString();
-        Class c = classes.get(name);
-
-        if (c == null) {
-            c = ClassLoaderUtil.loadClass(name, StrutsUtil.class);
-            classes.put(name, c);
-        }
-
-        return objectFactory.buildBean(c, stack.getContext());
+    public Object bean(Object name) throws Exception {
+        String className = name.toString();
+        Class<?> clazz = classes.putIfAbsent(className, 
ClassLoaderUtil.loadClass(className, StrutsUtil.class));
+        return objectFactory.buildBean(clazz, stack.getContext());
     }
 
     public boolean isTrue(String expression) {
@@ -89,30 +92,20 @@ public class StrutsUtil {
     }
 
     public String include(Object aName) throws Exception {
-        try {
-            RequestDispatcher dispatcher = 
request.getRequestDispatcher(aName.toString());
-
-            if (dispatcher == null) {
-                throw new IllegalArgumentException("Cannot find included file 
" + aName);
-            }
-
-            ResponseWrapper responseWrapper = new ResponseWrapper(response);
-
-            dispatcher.include(request, responseWrapper);
-
-            return responseWrapper.getData();
-        }
-        catch (Exception e) {
-            LOG.debug("Cannot include {}", aName, e);
-            throw e;
+        RequestDispatcher dispatcher = 
request.getRequestDispatcher(aName.toString());
+        if (dispatcher == null) {
+            throw new IllegalArgumentException("Cannot find included file " + 
aName);
         }
+        ResponseWrapper responseWrapper = new ResponseWrapper(response);
+        dispatcher.include(request, responseWrapper);
+        return responseWrapper.getData();
     }
 
     public String urlEncode(String s) {
         try {
             return URLEncoder.encode(s, "UTF-8");
         } catch (UnsupportedEncodingException e) {
-            LOG.debug("Cannot encode URL [{}]", s, e);
+            LOG.debug(format("Cannot encode URL [{0}]", s), e);
             return s;
         }
     }
@@ -144,7 +137,7 @@ public class StrutsUtil {
      * @return the url ContextPath. An empty string if one does not exist.
      */
     public String getContext() {
-        return (request == null)? "" : request.getContextPath();
+        return request == null ? "" : request.getContextPath();
     }
 
     public String translateVariables(String expression) {
@@ -168,8 +161,13 @@ public class StrutsUtil {
      *                     to use as the value of the ListEntry
      * @return a List of ListEntry
      */
-    public List makeSelectList(String selectedList, String list, String 
listKey, String listValue) {
-        List selectList = new ArrayList();
+    public List<ListEntry> makeSelectList(String selectedList, String list, 
String listKey, String listValue) {
+        List<ListEntry> selectList = new ArrayList<>();
+
+        Collection items = (Collection) stack.findValue(list);
+        if (items == null) {
+            return selectList;
+        }
 
         Collection selectedItems = null;
 
@@ -181,42 +179,27 @@ public class StrutsUtil {
             } else if (i instanceof Collection) {
                 selectedItems = (Collection) i;
             } else {
-                // treat it is a single item
-                selectedItems = new ArrayList();
-                selectedItems.add(i);
+                selectedItems = singletonList(i);
             }
         }
 
-        Collection items = (Collection) stack.findValue(list);
-
-        if (items != null) {
-            for (Object element : items) {
-                Object key;
-
-                if ((listKey == null) || (listKey.length() == 0)) {
-                    key = element;
-                } else {
-                    key = ognl.findValue(listKey, element);
-                }
+        for (Object element : items) {
+            Object key;
+            if (listKey == null || listKey.isEmpty()) {
+                key = element;
+            } else {
                 key = findValue(listKey, element);
+            }
 
-                Object value = null;
-
-                if ((listValue == null) || (listValue.length() == 0)) {
-                    value = element;
-                } else {
-                    value = ognl.findValue(listValue, element);
-                }
-
-                boolean isSelected = false;
-
-                if ((value != null) && (selectedItems != null) && 
selectedItems.contains(value)) {
-                    isSelected = true;
-                }
+            Object value;
+            if (listValue == null || listValue.isEmpty()) {
+                value = element;
+            } else {
                 value = findValue(listValue, element);
-
-                selectList.add(new ListEntry(key, value, isSelected));
             }
+            boolean isSelected = value != null && selectedItems != null && 
selectedItems.contains(value);
+
+            selectList.add(new ListEntry(key, value, isSelected));
         }
 
         return selectList;
@@ -227,14 +210,13 @@ public class StrutsUtil {
     }
 
     public long toLong(int anInt) {
-        return (long) anInt;
+        return anInt;
     }
 
     public long toLong(String aLong) {
         if (aLong == null) {
             return 0;
         }
-
         return Long.parseLong(aLong);
     }
 
@@ -247,14 +229,7 @@ public class StrutsUtil {
     }
 
     public String toStringSafe(Object obj) {
-        try {
-            if (obj != null) {
-                return String.valueOf(obj);
-            }
-            return "";
-        } catch (Exception e) {
-            return "Exception thrown: " + e;
-        }
+        return obj == null ? "" : obj.toString();
     }
 
     static class ResponseWrapper extends HttpServletResponseWrapper {
@@ -271,7 +246,6 @@ public class StrutsUtil {
 
         public String getData() {
             writer.flush();
-
             return strout.toString();
         }
 
diff --git a/core/src/test/java/org/apache/struts2/util/StrutsUtilTest.java 
b/core/src/test/java/org/apache/struts2/util/StrutsUtilTest.java
index 4cf0abb82..4a2dcd1c9 100644
--- a/core/src/test/java/org/apache/struts2/util/StrutsUtilTest.java
+++ b/core/src/test/java/org/apache/struts2/util/StrutsUtilTest.java
@@ -184,6 +184,11 @@ public class StrutsUtilTest extends StrutsInternalTestCase 
{
         assertEquals(strutsUtil.toString(11L), "11");
     }
 
+    public void testToStringSafe() {
+        assertEquals("1", strutsUtil.toStringSafe(1));
+        assertEquals("", strutsUtil.toStringSafe(null));
+    }
+
     public void testTranslateVariables() {
         stack.push(new Object() {
             public String getFoo() {

Reply via email to