Author: tmjee Date: Mon Jul 31 07:18:50 2006 New Revision: 427126 URL: http://svn.apache.org/viewvc?rev=427126&view=rev Log: WW-1390 - added capability to add request parameter to redirect-action result type
Added: struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/ServletActionRedirectResultTest.java (with props) Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/ServletActionRedirectResult.java struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/util/UrlHelper.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/ServletActionRedirectResult.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/ServletActionRedirectResult.java?rev=427126&r1=427125&r2=427126&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/ServletActionRedirectResult.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/ServletActionRedirectResult.java Mon Jul 31 07:18:50 2006 @@ -17,10 +17,18 @@ */ package org.apache.struts2.dispatcher; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + import org.apache.struts2.dispatcher.mapper.ActionMapper; import org.apache.struts2.dispatcher.mapper.ActionMapperFactory; import org.apache.struts2.dispatcher.mapper.ActionMapping; +import org.apache.struts2.views.util.UrlHelper; +import com.opensymphony.xwork2.config.entities.ResultConfig; import com.opensymphony.xwork2.ActionInvocation; /** @@ -89,7 +97,11 @@ protected String namespace; protected String method; - /* (non-Javadoc) + protected List<String> prohibitedResultParam = Arrays.asList(new String[] { + DEFAULT_PARAM, "namespace", "method", "encode", "parse", "location", + "prependServletContext" }); + + /** * @see com.opensymphony.xwork2.Result#execute(com.opensymphony.xwork2.ActionInvocation) */ public void execute(ActionInvocation invocation) throws Exception { @@ -106,9 +118,25 @@ method = conditionalParse(method, invocation); } + Map<String, String> requestParameters = new HashMap<String, String>(); + ResultConfig resultConfig = invocation.getProxy().getConfig().getResults().get( + invocation.getResultCode()); + Map resultConfigParams = resultConfig.getParams(); + for (Iterator i = resultConfigParams.entrySet().iterator(); i.hasNext(); ) { + Map.Entry e = (Map.Entry) i.next(); + if (! prohibitedResultParam.contains(e.getKey())) { + requestParameters.put(e.getKey().toString(), + e.getValue() == null ? "": + conditionalParse(e.getValue().toString(), invocation)); + } + } + ActionMapper mapper = ActionMapperFactory.getMapper(); - location = mapper.getUriFromActionMapping(new ActionMapping(actionName, namespace, method, null)); - + StringBuffer tmpLocation = new StringBuffer(mapper.getUriFromActionMapping(new ActionMapping(actionName, namespace, method, null))); + UrlHelper.buildParametersString(requestParameters, tmpLocation, "&"); + + location = tmpLocation.toString(); + super.execute(invocation); } Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/util/UrlHelper.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/util/UrlHelper.java?rev=427126&r1=427125&r2=427126&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/util/UrlHelper.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/util/UrlHelper.java Mon Jul 31 07:18:50 2006 @@ -166,11 +166,15 @@ } public static void buildParametersString(Map params, StringBuffer link) { + buildParametersString(params, link, AMP); + } + + public static void buildParametersString(Map params, StringBuffer link, String paramSeparator) { if ((params != null) && (params.size() > 0)) { if (link.toString().indexOf("?") == -1) { link.append("?"); } else { - link.append(AMP); + link.append(paramSeparator); } // Set params @@ -200,12 +204,12 @@ } if (i < (values.length - 1)) { - link.append(AMP); + link.append(paramSeparator); } } if (iter.hasNext()) { - link.append(AMP); + link.append(paramSeparator); } } } Added: struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/ServletActionRedirectResultTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/ServletActionRedirectResultTest.java?rev=427126&view=auto ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/ServletActionRedirectResultTest.java (added) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/ServletActionRedirectResultTest.java Mon Jul 31 07:18:50 2006 @@ -0,0 +1,159 @@ +/* + * $Id: ServletActionRedirectResult.java 421742 2006-07-13 23:48:46Z 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.struts2.dispatcher; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.struts2.ServletActionContext; +import org.apache.struts2.StrutsTestCase; +import org.easymock.EasyMock; +import org.easymock.IMocksControl; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionInvocation; +import com.opensymphony.xwork2.ActionProxy; +import com.opensymphony.xwork2.config.entities.ActionConfig; +import com.opensymphony.xwork2.config.entities.ResultConfig; +import com.opensymphony.xwork2.util.OgnlValueStack; + + +/** + * @version $Date$ $Id$ + */ +public class ServletActionRedirectResultTest extends StrutsTestCase { + + public void testIncludeParameterInResultWithConditionParseOn() throws Exception { + + ResultConfig resultConfig = new ResultConfig(); + resultConfig.addParam("actionName", "someActionName"); + resultConfig.addParam("namespace", "someNamespace"); + resultConfig.addParam("encode", "true"); + resultConfig.addParam("parse", "true"); + resultConfig.addParam("location", "someLocation"); + resultConfig.addParam("prependServletContext", "true"); + resultConfig.addParam("method", "someMethod"); + resultConfig.addParam("param1", "${#value1}"); + resultConfig.addParam("param2", "${#value2}"); + resultConfig.addParam("param3", "${#value3}"); + + + + ActionContext context = ActionContext.getContext(); + OgnlValueStack stack = context.getValueStack(); + context.getContextMap().put("value1", "value 1"); + context.getContextMap().put("value2", "value 2"); + context.getContextMap().put("value3", "value 3"); + MockHttpServletRequest req = new MockHttpServletRequest(); + MockHttpServletResponse res = new MockHttpServletResponse(); + context.put(ServletActionContext.HTTP_REQUEST, req); + context.put(ServletActionContext.HTTP_RESPONSE, res); + + + Map<String, ResultConfig> results= new HashMap<String, ResultConfig>(); + results.put("myResult", resultConfig); + + ActionConfig actionConfig = new ActionConfig(); + actionConfig.setResults(results); + + ServletActionRedirectResult result = new ServletActionRedirectResult(); + result.setActionName("myAction"); + result.setNamespace("/myNamespace"); + result.setParse(true); + result.setEncode(false); + result.setPrependServletContext(false); + + IMocksControl control = EasyMock.createControl(); + ActionProxy mockActionProxy = control.createMock(ActionProxy.class); + ActionInvocation mockInvocation = control.createMock(ActionInvocation.class); + mockInvocation.getProxy(); + control.andReturn(mockActionProxy); + mockInvocation.getResultCode(); + control.andReturn("myResult"); + mockActionProxy.getConfig(); + control.andReturn(actionConfig); + mockInvocation.getInvocationContext(); + control.andReturn(context); + mockInvocation.getStack(); + control.andReturn(stack); + control.anyTimes(); + + control.replay(); + + result.execute(mockInvocation); + assertEquals("/myNamespace/myAction.action?param2=value+2¶m1=value+1¶m3=value+3", res.getRedirectedUrl()); + + control.verify(); + } + + public void testIncludeParameterInResult() throws Exception { + + ResultConfig resultConfig = new ResultConfig(); + resultConfig.addParam("actionName", "someActionName"); + resultConfig.addParam("namespace", "someNamespace"); + resultConfig.addParam("encode", "true"); + resultConfig.addParam("parse", "true"); + resultConfig.addParam("location", "someLocation"); + resultConfig.addParam("prependServletContext", "true"); + resultConfig.addParam("method", "someMethod"); + resultConfig.addParam("param1", "value 1"); + resultConfig.addParam("param2", "value 2"); + resultConfig.addParam("param3", "value 3"); + + ActionContext context = ActionContext.getContext(); + MockHttpServletRequest req = new MockHttpServletRequest(); + MockHttpServletResponse res = new MockHttpServletResponse(); + context.put(ServletActionContext.HTTP_REQUEST, req); + context.put(ServletActionContext.HTTP_RESPONSE, res); + + + Map<String, ResultConfig> results= new HashMap<String, ResultConfig>(); + results.put("myResult", resultConfig); + + ActionConfig actionConfig = new ActionConfig(); + actionConfig.setResults(results); + + ServletActionRedirectResult result = new ServletActionRedirectResult(); + result.setActionName("myAction"); + result.setNamespace("/myNamespace"); + result.setParse(false); + result.setEncode(false); + result.setPrependServletContext(false); + + IMocksControl control = EasyMock.createControl(); + ActionProxy mockActionProxy = control.createMock(ActionProxy.class); + ActionInvocation mockInvocation = control.createMock(ActionInvocation.class); + mockInvocation.getProxy(); + control.andReturn(mockActionProxy); + mockInvocation.getResultCode(); + control.andReturn("myResult"); + mockActionProxy.getConfig(); + control.andReturn(actionConfig); + mockInvocation.getInvocationContext(); + control.andReturn(context); + + control.replay(); + + result.execute(mockInvocation); + assertEquals("/myNamespace/myAction.action?param2=value+2¶m1=value+1¶m3=value+3", res.getRedirectedUrl()); + + control.verify(); + } +} Propchange: struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/ServletActionRedirectResultTest.java ------------------------------------------------------------------------------ svn:eol-style = native