Author: tmjee
Date: Sun May 21 02:16:25 2006
New Revision: 408150

URL: http://svn.apache.org/viewvc?rev=408150&view=rev
Log:
WW-1315


Added:
    
struts/action2/trunk/core/src/test/java/org/apache/struts/action2/dispatcher/StrutsResultSupportTest.java
   (with props)
Modified:
    
struts/action2/trunk/core/src/main/java/org/apache/struts/action2/dispatcher/StrutsResultSupport.java

Modified: 
struts/action2/trunk/core/src/main/java/org/apache/struts/action2/dispatcher/StrutsResultSupport.java
URL: 
http://svn.apache.org/viewvc/struts/action2/trunk/core/src/main/java/org/apache/struts/action2/dispatcher/StrutsResultSupport.java?rev=408150&r1=408149&r2=408150&view=diff
==============================================================================
--- 
struts/action2/trunk/core/src/main/java/org/apache/struts/action2/dispatcher/StrutsResultSupport.java
 (original)
+++ 
struts/action2/trunk/core/src/main/java/org/apache/struts/action2/dispatcher/StrutsResultSupport.java
 Sun May 21 02:16:25 2006
@@ -17,6 +17,11 @@
  */
 package org.apache.struts.action2.dispatcher;
 
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.struts.action2.StrutsStatics;
 import com.opensymphony.xwork.ActionInvocation;
 import com.opensymphony.xwork.Result;
@@ -24,6 +29,8 @@
 
 
 /**
+ * <!-- START SNIPPET: javadoc -->
+ * 
  * A base class for all Struts action execution results.
  * The "location" param is the default parameter, meaning the most common 
usage of this result would be:
  * <p/>
@@ -34,7 +41,18 @@
  * [EMAIL PROTECTED] TextParseUtil#translateVariables(java.lang.String, 
com.opensymphony.xwork.util.OgnlValueStack) translateVariables}
  * method</li>
  * <li>parse - true by default. If set to false, the location param will not 
be parsed for expressions</li>
+ * <li>encode - false by default. If set to false, the location param will not 
be url encoded. This only have effect when parse is true</li>
  * </ul>
+ * 
+ * <b>NOTE:</b>
+ * The encode param will only have effect when parse is true
+ * 
+ * <!-- END SNIPPET: javadoc -->
+ * 
+ * <p/>
+ * 
+ * <!-- START SNIPPET: example -->
+ * 
  * <p/>
  * In the xwork.xml configuration file, these would be included as:
  * <p/>
@@ -48,7 +66,8 @@
  * <pre>
  *  &lt;result name="success" type="redirect" &gt;
  *      &lt;param name="<b>location</b>"&gt;foo.jsp&lt;/param&gt;
- *      &lt;param name="<b>parse</b>"&gt;false&lt;/param&gt;
+ *      &lt;param name="<b>parse</b>"&gt;true&lt;/param&gt;
+ *      &lt;param name="<b>encode</b>"&gt;false&lt;/param&gt;
  *  &lt;/result&gt;</pre>
  * <p/>
  * or when using the default parameter feature
@@ -70,12 +89,18 @@
  * <p/>
  * Please see the [EMAIL PROTECTED] com.opensymphony.xwork.Result} class for 
more info on Results in general.
  *
+ * <!-- END SNIPPET: example -->
+ * 
  * @see com.opensymphony.xwork.Result
  */
 public abstract class StrutsResultSupport implements Result, StrutsStatics {
+       
+       private static final Log _log = 
LogFactory.getLog(StrutsResultSupport.class);
+       
     public static final String DEFAULT_PARAM = "location";
 
     protected boolean parse = true;
+    protected boolean encode = false;
     protected String location;
 
     /**
@@ -99,6 +124,16 @@
     public void setParse(boolean parse) {
         this.parse = parse;
     }
+    
+    /**
+     * Set encode to <tt>true</tt> to indicate that the location should be url 
encoded. This is set to
+     * <tt>true</tt> by default
+     * 
+     * @param encode <tt>true</tt> if the location parameter should be url 
encode, <tt>false</tt> otherwise.
+     */
+    public void setEncode(boolean encode) {
+       this.encode = encode;
+    }
 
     /**
      * Implementation of the <tt>execute</tt> method from the <tt>Result</tt> 
interface. This will call
@@ -114,9 +149,26 @@
 
     protected String conditionalParse(String param, ActionInvocation 
invocation) {
         if (parse && param != null && invocation != null) {
-            return TextParseUtil.translateVariables(param, 
invocation.getStack());
+            return TextParseUtil.translateVariables(param, 
invocation.getStack(), 
+                       new TextParseUtil.ParsedValueEvaluator() {
+                                               public Object evaluate(Object 
parsedValue) {
+                                                       if (encode) {
+                                                               if (parsedValue 
!= null) {
+                                                                       try {
+                                                                               
// use UTF-8 as this is the recommended encoding by W3C to 
+                                                                               
// avoid incompatibilities.
+                                                                               
return URLEncoder.encode(parsedValue.toString(), "UTF-8");
+                                                                       }
+                                                                       
catch(UnsupportedEncodingException e) {
+                                                                               
_log.warn("error while trying to encode ["+parsedValue+"]", e);
+                                                                       }
+                                                               }
+                                                       }
+                                                       return parsedValue;
+                                               }
+            });
         } else {
-            return param;
+               return param;
         }
     }
 

Added: 
struts/action2/trunk/core/src/test/java/org/apache/struts/action2/dispatcher/StrutsResultSupportTest.java
URL: 
http://svn.apache.org/viewvc/struts/action2/trunk/core/src/test/java/org/apache/struts/action2/dispatcher/StrutsResultSupportTest.java?rev=408150&view=auto
==============================================================================
--- 
struts/action2/trunk/core/src/test/java/org/apache/struts/action2/dispatcher/StrutsResultSupportTest.java
 (added)
+++ 
struts/action2/trunk/core/src/test/java/org/apache/struts/action2/dispatcher/StrutsResultSupportTest.java
 Sun May 21 02:16:25 2006
@@ -0,0 +1,136 @@
+/*
+ * $Id: StrutsResultSupport.java 394468 2006-04-16 12:16:03Z tmjee $
+ *
+ * Copyright 2006 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.struts.action2.dispatcher;
+
+import org.apache.struts.action2.StrutsTestCase;
+/*
+ * $Id: StrutsResultSupport.java 394468 2006-04-16 12:16:03Z tmjee $
+ *
+ * Copyright 2006 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.
+ */
+import org.easymock.EasyMock;
+
+import com.opensymphony.xwork.ActionInvocation;
+import com.opensymphony.xwork.ActionSupport;
+import com.opensymphony.xwork.util.OgnlValueStack;
+
+/**
+ * Test case for StrutsResultSupport.
+ */
+public class StrutsResultSupportTest extends StrutsTestCase {
+
+       
+       public void testParse() throws Exception {
+               OgnlValueStack stack = new OgnlValueStack();
+               stack.push(new ActionSupport() {
+                       public String getMyLocation() {
+                               return "ThisIsMyLocation";
+                       }
+               });
+               
+               ActionInvocation mockActionInvocation = 
EasyMock.createNiceMock(ActionInvocation.class);
+               mockActionInvocation.getStack();
+               EasyMock.expectLastCall().andReturn(stack);
+               EasyMock.replay(mockActionInvocation);
+               
+               InternalStrutsResultSupport result = new 
InternalStrutsResultSupport();
+               result.setParse(true);
+               result.setEncode(false);
+               result.setLocation("/pages/myJsp.jsp?location=${myLocation}");
+               
+               result.execute(mockActionInvocation);
+               
+               assertNotNull(result.getInternalLocation());
+               assertEquals("/pages/myJsp.jsp?location=ThisIsMyLocation", 
result.getInternalLocation());
+               EasyMock.verify(mockActionInvocation);
+       }
+       
+       public void testParseAndEncode() throws Exception {
+               OgnlValueStack stack = new OgnlValueStack();
+               stack.push(new ActionSupport() {
+                       public String getMyLocation() {
+                               return "/myPage?param=value&param1=value1";
+                       }
+               });
+               
+               ActionInvocation mockActionInvocation = 
EasyMock.createNiceMock(ActionInvocation.class);
+               mockActionInvocation.getStack();
+               EasyMock.expectLastCall().andReturn(stack);
+               EasyMock.replay(mockActionInvocation);
+               
+               InternalStrutsResultSupport result = new 
InternalStrutsResultSupport();
+               result.setParse(true);
+               result.setEncode(true);
+               result.setLocation("/pages/myJsp.jsp?location=${myLocation}");
+               
+               result.execute(mockActionInvocation);
+               
+               assertNotNull(result.getInternalLocation());
+               
assertEquals("/pages/myJsp.jsp?location=%2FmyPage%3Fparam%3Dvalue%26param1%3Dvalue1",
 result.getInternalLocation());
+               EasyMock.verify(mockActionInvocation);
+       }
+       
+       
+       public void testNoParseAndEncode() throws Exception {
+               OgnlValueStack stack = new OgnlValueStack();
+               stack.push(new ActionSupport() {
+                       public String getMyLocation() {
+                               return "myLocation.jsp";
+                       }
+               });
+               
+               ActionInvocation mockActionInvocation = 
EasyMock.createNiceMock(ActionInvocation.class);
+               EasyMock.replay(mockActionInvocation);
+               
+               InternalStrutsResultSupport result = new 
InternalStrutsResultSupport();
+               result.setParse(false);
+               result.setEncode(false); // don't really need this, as encode 
is only valid when parse is true.
+               result.setLocation("/pages/myJsp.jsp?location=${myLocation}");
+               
+               result.execute(mockActionInvocation);
+               
+               assertNotNull(result.getInternalLocation());
+               assertEquals("/pages/myJsp.jsp?location=${myLocation}", 
result.getInternalLocation());
+               EasyMock.verify(mockActionInvocation);
+       }
+       
+       
+       public static class InternalStrutsResultSupport extends 
StrutsResultSupport {
+               private String _internalLocation = null;
+               
+               protected void doExecute(String finalLocation, ActionInvocation 
invocation) throws Exception {
+                       _internalLocation = finalLocation;
+               }
+               
+               public String getInternalLocation() {
+                       return _internalLocation;
+               }
+       }
+}

Propchange: 
struts/action2/trunk/core/src/test/java/org/apache/struts/action2/dispatcher/StrutsResultSupportTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to