You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2023/10/04 07:46:54 UTC

[struts] 01/01: WW-5338 Removes deprecated OgnTool

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

lukaszlenart pushed a commit to branch feature/WW-5338-remove-ognltool
in repository https://gitbox.apache.org/repos/asf/struts.git

commit debcb541e33f113287cb3d9c13e8400122adf677
Author: Lukasz Lenart <lu...@apache.org>
AuthorDate: Wed Oct 4 09:46:40 2023 +0200

    WW-5338 Removes deprecated OgnTool
---
 .../java/org/apache/struts2/util/StrutsUtil.java   | 15 ++----
 .../struts2/views/freemarker/FreemarkerResult.java | 16 +++---
 .../org/apache/struts2/views/jsp/ui/OgnlTool.java  | 60 ----------------------
 core/src/main/resources/struts-beans.xml           |  3 --
 .../views/freemarker/PortletFreemarkerResult.java  | 10 ++--
 5 files changed, 15 insertions(+), 89 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 727d67a26..13c574fbd 100644
--- a/core/src/main/java/org/apache/struts2/util/StrutsUtil.java
+++ b/core/src/main/java/org/apache/struts2/util/StrutsUtil.java
@@ -18,13 +18,10 @@
  */
 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;
@@ -62,7 +59,6 @@ public class StrutsUtil {
     protected HttpServletRequest request;
     protected HttpServletResponse response;
     protected Map<String, Class<?>> classes = new HashMap<>();
-    protected OgnlUtil ognl;
     protected ValueStack stack;
 
     private final UrlHelper urlHelper;
@@ -72,7 +68,6 @@ public class StrutsUtil {
         this.stack = stack;
         this.request = request;
         this.response = response;
-        this.ognl = stack.getActionContext().getContainer().getInstance(OgnlUtil.class);
         this.urlHelper = stack.getActionContext().getContainer().getInstance(UrlHelper.class);
         this.objectFactory = stack.getActionContext().getContainer().getInstance(ObjectFactory.class);
     }
@@ -125,12 +120,10 @@ public class StrutsUtil {
 
     public Object findValue(String expr, Object context) {
         try {
-            return ognl.getValue(expr, ActionContext.getContext().getContextMap(), context);
-        } catch (OgnlException e) {
-            if (e.getReason() instanceof SecurityException) {
-                LOG.error(format("Could not evaluate this expression due to security constraints: [{0}]", expr), e);
-            }
-            return null;
+            stack.push(context);
+            return stack.findValue(expr, true);
+        } finally {
+            stack.pop();
         }
     }
 
diff --git a/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerResult.java b/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerResult.java
index 7d594569b..4b7fd4bc0 100644
--- a/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerResult.java
+++ b/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerResult.java
@@ -33,7 +33,6 @@ import freemarker.template.TemplateModelException;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
-import org.apache.struts2.ServletActionContext;
 import org.apache.struts2.StrutsStatics;
 import org.apache.struts2.result.StrutsResultSupport;
 
@@ -207,7 +206,7 @@ public class FreemarkerResult extends StrutsResultSupport {
      * @throws TemplateException in case of freemarker configuration errors
      */
     protected Configuration getConfiguration() throws TemplateException {
-        return freemarkerManager.getConfiguration(ServletActionContext.getServletContext());
+        return freemarkerManager.getConfiguration(ActionContext.getContext().getServletContext());
     }
 
     /**
@@ -244,7 +243,7 @@ public class FreemarkerResult extends StrutsResultSupport {
         if (writer != null) {
             return writer;
         }
-        return ServletActionContext.getResponse().getWriter();
+        return ActionContext.getContext().getServletResponse().getWriter();
     }
 
     /**
@@ -261,7 +260,6 @@ public class FreemarkerResult extends StrutsResultSupport {
      * <li>request - the HttpServletRequst object for direct access
      * <li>response - the HttpServletResponse object for direct access
      * <li>stack - the OgnLValueStack instance for direct access
-     * <li>ognl - the instance of the OgnlTool
      * <li>action - the action itself
      * <li>exception - optional : the JSP or Servlet exception as per the servlet spec (for JSP Exception pages)
      * <li>struts - instance of the StrutsUtil class
@@ -271,9 +269,9 @@ public class FreemarkerResult extends StrutsResultSupport {
      * @throws TemplateModelException in case of errors during creating the model
      */
     protected TemplateModel createModel() throws TemplateModelException {
-        ServletContext servletContext = ServletActionContext.getServletContext();
-        HttpServletRequest request = ServletActionContext.getRequest();
-        HttpServletResponse response = ServletActionContext.getResponse();
+        ServletContext servletContext = ActionContext.getContext().getServletContext();
+        HttpServletRequest request = ActionContext.getContext().getServletRequest();
+        HttpServletResponse response = ActionContext.getContext().getServletResponse();
         ValueStack stack = ActionContext.getContext().getValueStack();
 
         Object action = null;
@@ -321,7 +319,7 @@ public class FreemarkerResult extends StrutsResultSupport {
     protected boolean preTemplateProcess(Template template, TemplateModel model) throws IOException {
         Object attrContentType = template.getCustomAttribute("content_type");
 
-        HttpServletResponse response = ServletActionContext.getResponse();
+        HttpServletResponse response = ActionContext.getContext().getServletResponse();
         if (response.getContentType() == null) {
             if (attrContentType != null) {
                 response.setContentType(attrContentType.toString());
@@ -349,7 +347,7 @@ public class FreemarkerResult extends StrutsResultSupport {
     }
 
     private boolean isInsideActionTag() {
-        Object attribute = ServletActionContext.getRequest().getAttribute(StrutsStatics.STRUTS_ACTION_TAG_INVOCATION);
+        Object attribute = ActionContext.getContext().getServletRequest().getAttribute(StrutsStatics.STRUTS_ACTION_TAG_INVOCATION);
         return (Boolean) ObjectUtils.defaultIfNull(attribute, Boolean.FALSE);
     }
 
diff --git a/core/src/main/java/org/apache/struts2/views/jsp/ui/OgnlTool.java b/core/src/main/java/org/apache/struts2/views/jsp/ui/OgnlTool.java
deleted file mode 100644
index bb9d2b5b8..000000000
--- a/core/src/main/java/org/apache/struts2/views/jsp/ui/OgnlTool.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.struts2.views.jsp.ui;
-
-import com.opensymphony.xwork2.ActionContext;
-import com.opensymphony.xwork2.inject.Inject;
-import com.opensymphony.xwork2.ognl.OgnlUtil;
-import ognl.OgnlException;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
-
-/**
- * @deprecated since 6.3.0. Use {@link org.apache.struts2.util.StrutsUtil} instead.
- */
-@Deprecated
-public class OgnlTool {
-
-    private static final Logger LOG = LogManager.getLogger(OgnlTool.class);
-
-    private OgnlUtil ognlUtil;
-
-    public OgnlTool() {
-    }
-
-    @Inject
-    public void setOgnlUtil(OgnlUtil ognlUtil) {
-        this.ognlUtil = ognlUtil;
-    }
-
-    /**
-     * @deprecated since 6.3.0. Use {@link org.apache.struts2.util.StrutsUtil#findValue(String, Object)} instead.
-     */
-    @Deprecated
-    public Object findValue(String expr, Object context) {
-        try {
-            return ognlUtil.getValue(expr, ActionContext.getContext().getContextMap(), context);
-        } catch (OgnlException e) {
-            if (e.getReason() instanceof SecurityException) {
-                LOG.error("Could not evaluate this expression due to security constraints: [{}]", expr, e);
-            }
-            return null;
-        }
-    }
-}
diff --git a/core/src/main/resources/struts-beans.xml b/core/src/main/resources/struts-beans.xml
index 3dcd60197..91fb65db7 100644
--- a/core/src/main/resources/struts-beans.xml
+++ b/core/src/main/resources/struts-beans.xml
@@ -200,9 +200,6 @@
     <bean type="ognl.MethodAccessor" name="com.opensymphony.xwork2.util.CompoundRoot"
           class="com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor"/>
 
-    <!-- Deprecated since 6.3.0, inject OgnlUtil directly instead, or use StrutsUtil -->
-    <bean class="org.apache.struts2.views.jsp.ui.OgnlTool"/>
-
     <bean type="org.apache.struts2.dispatcher.StaticContentLoader"
           class="org.apache.struts2.dispatcher.DefaultStaticContentLoader" name="struts"/>
     <bean type="com.opensymphony.xwork2.UnknownHandlerManager"
diff --git a/plugins/portlet/src/main/java/org/apache/struts2/views/freemarker/PortletFreemarkerResult.java b/plugins/portlet/src/main/java/org/apache/struts2/views/freemarker/PortletFreemarkerResult.java
index 91f3c0865..ed75bd8ba 100644
--- a/plugins/portlet/src/main/java/org/apache/struts2/views/freemarker/PortletFreemarkerResult.java
+++ b/plugins/portlet/src/main/java/org/apache/struts2/views/freemarker/PortletFreemarkerResult.java
@@ -71,7 +71,7 @@ public class PortletFreemarkerResult extends StrutsResultSupport {
     public PortletFreemarkerResult(String location) {
         super(location);
     }
-    
+
     @Inject
     public void setFreemarkerManager(FreemarkerManager mgr) {
         this.freemarkerManager = mgr;
@@ -157,7 +157,7 @@ public class PortletFreemarkerResult extends StrutsResultSupport {
             }
         }
     }
-    
+
     private void executeResourceResult(String location, ActionInvocation invocation)
              throws TemplateException, IOException, PortletException {
          this.location = location;
@@ -243,7 +243,6 @@ public class PortletFreemarkerResult extends StrutsResultSupport {
      * <li>request - the HttpServletRequst object for direct access
      * <li>response - the HttpServletResponse object for direct access
      * <li>stack - the OgnLValueStack instance for direct access
-     * <li>ognl - the instance of the OgnlTool
      * <li>action - the action itself
      * <li>exception - optional : the JSP or Servlet exception as per the
      * servlet spec (for JSP Exception pages)
@@ -255,11 +254,10 @@ public class PortletFreemarkerResult extends StrutsResultSupport {
      * @throws TemplateModelException in case of template model errors
      */
     protected TemplateModel createModel() throws TemplateModelException {
-        ServletContext servletContext = ServletActionContext
-                .getServletContext();
+        ServletContext servletContext = ServletActionContext.getServletContext();
         HttpServletRequest request = ServletActionContext.getRequest();
         HttpServletResponse response = ServletActionContext.getResponse();
-        ValueStack stack = ServletActionContext.getContext().getValueStack();
+        ValueStack stack = ServletActionContext.getActionContext().getValueStack();
         return freemarkerManager.buildTemplateModel(stack, invocation.getAction(), servletContext, request, response, wrapper);
     }