Author: lukaszlenart Date: Wed Oct 23 10:32:02 2013 New Revision: 1534977 URL: http://svn.apache.org/r1534977 Log: WW-4223 Uses safe toString method to avoid exception during generating debug info
Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/AttributeMap.java struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/StrutsUtil.java struts/struts2/trunk/core/src/main/resources/template/simple/debug.ftl Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/AttributeMap.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/AttributeMap.java?rev=1534977&r1=1534976&r2=1534977&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/AttributeMap.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/AttributeMap.java Wed Oct 23 10:32:02 2013 @@ -21,15 +21,14 @@ package org.apache.struts2.util; +import org.apache.struts2.ServletActionContext; + +import javax.servlet.jsp.PageContext; import java.util.Collection; import java.util.Collections; import java.util.Map; import java.util.Set; -import javax.servlet.jsp.PageContext; - -import org.apache.struts2.ServletActionContext; - /** * A Map that holds 4 levels of scope. @@ -48,15 +47,12 @@ public class AttributeMap implements Map protected static final String UNSUPPORTED = "method makes no sense for a simplified map"; - Map context; - public AttributeMap(Map context) { this.context = context; } - public boolean isEmpty() { throw new UnsupportedOperationException(UNSUPPORTED); } @@ -135,4 +131,25 @@ public class AttributeMap implements Map private PageContext getPageContext() { return (PageContext) context.get(ServletActionContext.PAGE_CONTEXT); } + + @Override + public String toString() { + return "AttributeMap {" + + "request=" + toStringSafe(context.get("request")) + + ", session=" + toStringSafe(context.get("session")) + + ", application=" + toStringSafe(context.get("application")) + + '}'; + } + + private String toStringSafe(Object obj) { + try { + if (obj != null) { + return String.valueOf(obj); + } + return ""; + } catch (Exception e) { + return "Exception thrown: " + e; + } + } + } Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/StrutsUtil.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/StrutsUtil.java?rev=1534977&r1=1534976&r2=1534977&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/StrutsUtil.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/util/StrutsUtil.java Wed Oct 23 10:32:02 2013 @@ -250,6 +250,16 @@ public class StrutsUtil { return Integer.toString(anInt); } + public String toStringSafe(Object obj) { + try { + if (obj != null) { + return String.valueOf(obj); + } + return ""; + } catch (Exception e) { + return "Exception thrown: " + e; + } + } static class ResponseWrapper extends HttpServletResponseWrapper { StringWriter strout; Modified: struts/struts2/trunk/core/src/main/resources/template/simple/debug.ftl URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/resources/template/simple/debug.ftl?rev=1534977&r1=1534976&r2=1534977&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/resources/template/simple/debug.ftl (original) +++ struts/struts2/trunk/core/src/main/resources/template/simple/debug.ftl Wed Oct 23 10:32:02 2013 @@ -43,7 +43,7 @@ <p /> <h3>Value Stack Contents</h3> -<table border="0" cellpadding="2" cellspacing="0" width="400" bgcolor="#DDDDDD"> +<table border="0" cellpadding="2" cellspacing="0" bgcolor="#DDDDDD"> <tr><th>Object</th><th>Property Name</th><th>Property Value</th></tr> <#assign index=1> @@ -65,7 +65,7 @@ <h3>Stack Context</h3> <i>These items are available using the #key notation</i> -<table border="0" cellpadding="2" cellspacing="0" width="400" bgcolor="#DDDDDD"> +<table border="0" cellpadding="2" cellspacing="0" bgcolor="#DDDDDD"> <tr> <th>Key</th><th>Value</th> </tr> @@ -73,9 +73,10 @@ <#assign index=1> <#list stack.context.keySet() as contextKey> <tr bgcolor="<#if (index % 2) == 0>#BBBBBB<#else>#CCCCCC</#if>"> - <td>${contextKey}</td><td><#if stack.context.get(contextKey)??>${stack.context.get(contextKey).toString()?html}<#else>null</#if></td> + <td>${contextKey}</td> + <td><#if stack.context.get(contextKey)??>${struts.toStringSafe(stack.context.get(contextKey))?html}<#else>null</#if></td> </tr> <#assign index= index + 1> </#list> </table> -</div> \ No newline at end of file +</div>