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

This commit is contained in:
Yasser Zamani
2017-09-05 11:51:14 +04:30
parent fbe09949c7
commit 682d7a8888
5 changed files with 51 additions and 5 deletions
@@ -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";
}
@@ -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);
@@ -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)
@@ -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>
@@ -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>