WW-4846 Adds constant to control if include/traverse proxy info in JSONResult


Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/682d7a88
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/682d7a88
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/682d7a88

Branch: refs/heads/master
Commit: 682d7a88886b16a3d6d150f9d8069a090158a682
Parents: fbe0994
Author: Yasser Zamani <yasser.zam...@live.com>
Authored: Tue Sep 5 11:51:14 2017 +0430
Committer: Yasser Zamani <yasser.zam...@live.com>
Committed: Tue Sep 5 11:51:14 2017 +0430

----------------------------------------------------------------------
 .../org/apache/struts2/json/JSONConstants.java  | 29 ++++++++++++++++++++
 .../java/org/apache/struts2/json/JSONUtil.java  | 13 ++++++---
 .../org/apache/struts2/json/JSONWriter.java     | 10 ++++++-
 .../json/src/main/resources/struts-plugin.xml   |  3 ++
 .../spring/src/main/resources/struts-plugin.xml |  1 +
 5 files changed, 51 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/682d7a88/plugins/json/src/main/java/org/apache/struts2/json/JSONConstants.java
----------------------------------------------------------------------
diff --git 
a/plugins/json/src/main/java/org/apache/struts2/json/JSONConstants.java 
b/plugins/json/src/main/java/org/apache/struts2/json/JSONConstants.java
new file mode 100644
index 0000000..2f46769
--- /dev/null
+++ b/plugins/json/src/main/java/org/apache/struts2/json/JSONConstants.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2017 The Apache Software Foundation.
+ *
+ * Licensed 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.json;
+
+/**
+ * <p>Class consisting of various constant values being used controlling
+ * JSON plugin behaviour</p>
+ *
+ * <p>
+ * These values can be overridden using struts.xml file by providing custom 
values.
+ * </p>
+ */
+public class JSONConstants {
+
+    public static final String RESULT_EXCLUDE_PROXY_PROPERTIES = 
"struts.json.result.excludeProxyProperties";
+}

http://git-wip-us.apache.org/repos/asf/struts/blob/682d7a88/plugins/json/src/main/java/org/apache/struts2/json/JSONUtil.java
----------------------------------------------------------------------
diff --git a/plugins/json/src/main/java/org/apache/struts2/json/JSONUtil.java 
b/plugins/json/src/main/java/org/apache/struts2/json/JSONUtil.java
index f44719f..c650e06 100644
--- a/plugins/json/src/main/java/org/apache/struts2/json/JSONUtil.java
+++ b/plugins/json/src/main/java/org/apache/struts2/json/JSONUtil.java
@@ -41,6 +41,7 @@ import java.util.zip.GZIPOutputStream;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import com.opensymphony.xwork2.inject.Inject;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
@@ -58,7 +59,14 @@ public class JSONUtil {
     public static final boolean CACHE_BEAN_INFO_DEFAULT = true;
     
     private static final Logger LOG = LogManager.getLogger(JSONUtil.class);
-          
+
+    private static JSONWriter writer = new JSONWriter();
+
+    @Inject
+    public static void setWriter(JSONWriter writer) {
+        JSONUtil.writer = writer;
+    }
+
     /**
      * Serializes an object into JSON.
      *
@@ -70,7 +78,6 @@ public class JSONUtil {
      * @throws JSONException in case of error during serialize
      */
     public static String serialize(Object object, boolean cacheBeanInfo) 
throws JSONException {
-        JSONWriter writer = new JSONWriter();
         writer.setCacheBeanInfo(cacheBeanInfo);
         return writer.write(object);
     }
@@ -124,7 +131,6 @@ public class JSONUtil {
             Collection<Pattern> includeProperties, boolean ignoreHierarchy, 
boolean excludeNullProperties,
             boolean cacheBeanInfo)
             throws JSONException {
-        JSONWriter writer = new JSONWriter();
         writer.setIgnoreHierarchy(ignoreHierarchy);
         writer.setCacheBeanInfo(cacheBeanInfo);
         return writer.write(object, excludeProperties, includeProperties, 
excludeNullProperties);
@@ -186,7 +192,6 @@ public class JSONUtil {
     public static String serialize(Object object, Collection<Pattern> 
excludeProperties,
                                    Collection<Pattern> includeProperties, 
boolean ignoreHierarchy, boolean enumAsBean,
                                    boolean excludeNullProperties, String 
defaultDateFormat, boolean cacheBeanInfo) throws JSONException {
-        JSONWriter writer = new JSONWriter();
         writer.setIgnoreHierarchy(ignoreHierarchy);
         writer.setEnumAsBean(enumAsBean);
         writer.setDateFormatter(defaultDateFormat);

http://git-wip-us.apache.org/repos/asf/struts/blob/682d7a88/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java
----------------------------------------------------------------------
diff --git a/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java 
b/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java
index 0e10ccf..00396c5 100644
--- a/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java
+++ b/plugins/json/src/main/java/org/apache/struts2/json/JSONWriter.java
@@ -20,6 +20,7 @@
  */
 package org.apache.struts2.json;
 
+import com.opensymphony.xwork2.inject.Inject;
 import com.opensymphony.xwork2.util.ProxyUtil;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
@@ -76,6 +77,12 @@ public class JSONWriter {
     private boolean enumAsBean = ENUM_AS_BEAN_DEFAULT;
     private boolean excludeNullProperties;
     private boolean cacheBeanInfo = true;
+    private boolean excludeProxyProperties = false;
+
+    @Inject(value = JSONConstants.RESULT_EXCLUDE_PROXY_PROPERTIES, required = 
false)
+    public void setExcludeProxyProperties(String excludeProxyProperties) {
+        this.excludeProxyProperties = 
Boolean.parseBoolean(excludeProxyProperties);
+    }
 
     /**
      * @param object Object to be serialized into JSON
@@ -102,6 +109,7 @@ public class JSONWriter {
                         Collection<Pattern> includeProperties, boolean 
excludeNullProperties) throws JSONException {
         this.excludeNullProperties = excludeNullProperties;
         this.buf.setLength(0);
+        this.stack.clear();
         this.root = object;
         this.exprStack = "";
         this.buildExpr = ((excludeProperties != null) && 
!excludeProperties.isEmpty())
@@ -211,7 +219,7 @@ public class JSONWriter {
         BeanInfo info;
 
         try {
-            Class clazz = ProxyUtil.ultimateTargetClass(object);
+            Class clazz = excludeProxyProperties ? 
ProxyUtil.ultimateTargetClass(object) : object.getClass();
 
             info = ((object == this.root) && this.ignoreHierarchy)
                     ? getBeanInfoIgnoreHierarchy(clazz)

http://git-wip-us.apache.org/repos/asf/struts/blob/682d7a88/plugins/json/src/main/resources/struts-plugin.xml
----------------------------------------------------------------------
diff --git a/plugins/json/src/main/resources/struts-plugin.xml 
b/plugins/json/src/main/resources/struts-plugin.xml
index e6aa1de..01fc1bd 100644
--- a/plugins/json/src/main/resources/struts-plugin.xml
+++ b/plugins/json/src/main/resources/struts-plugin.xml
@@ -5,6 +5,9 @@
        "http://struts.apache.org/dtds/struts-2.5.dtd";>
 
 <struts>
+    <bean class="org.apache.struts2.json.JSONWriter"/>
+    <bean class="org.apache.struts2.json.JSONUtil" static="true"/>
+
     <package name="json-default" extends="struts-default">
 
         <result-types>

http://git-wip-us.apache.org/repos/asf/struts/blob/682d7a88/plugins/spring/src/main/resources/struts-plugin.xml
----------------------------------------------------------------------
diff --git a/plugins/spring/src/main/resources/struts-plugin.xml 
b/plugins/spring/src/main/resources/struts-plugin.xml
index cc13bca..4198ecc 100644
--- a/plugins/spring/src/main/resources/struts-plugin.xml
+++ b/plugins/spring/src/main/resources/struts-plugin.xml
@@ -36,6 +36,7 @@
     <constant name="struts.class.reloading.reloadConfig" value="false" />
 
     <constant name="struts.disallowProxyMemberAccess" value="true" />
+    <constant name="struts.json.result.excludeProxyProperties" value="true" />
 
     <package name="spring-default">
         <interceptors>

Reply via email to