Return-Path: X-Original-To: apmail-struts-commits-archive@minotaur.apache.org Delivered-To: apmail-struts-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D830E10C3D for ; Thu, 25 Jul 2013 10:04:12 +0000 (UTC) Received: (qmail 17229 invoked by uid 500); 25 Jul 2013 10:04:12 -0000 Delivered-To: apmail-struts-commits-archive@struts.apache.org Received: (qmail 17160 invoked by uid 500); 25 Jul 2013 10:04:11 -0000 Mailing-List: contact commits-help@struts.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@struts.apache.org Delivered-To: mailing list commits@struts.apache.org Received: (qmail 17148 invoked by uid 99); 25 Jul 2013 10:04:10 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 25 Jul 2013 10:04:10 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 25 Jul 2013 10:04:05 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 7299C238888A; Thu, 25 Jul 2013 10:03:43 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1506872 - in /struts/struts2/trunk: core/src/main/java/org/apache/struts2/ core/src/main/java/org/apache/struts2/config/ core/src/main/java/org/apache/struts2/factory/ core/src/main/resources/ core/src/test/java/org/apache/struts2/ core/sr... Date: Thu, 25 Jul 2013 10:03:43 -0000 To: commits@struts.apache.org From: lukaszlenart@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20130725100343.7299C238888A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: lukaszlenart Date: Thu Jul 25 10:03:42 2013 New Revision: 1506872 URL: http://svn.apache.org/r1506872 Log: WW-4144 Defines ParamNameAwareResult interfaces and ResultBuilder to be used with Added: struts/struts2/trunk/core/src/main/java/org/apache/struts2/factory/ struts/struts2/trunk/core/src/main/java/org/apache/struts2/factory/StrutsResultBuilder.java struts/struts2/trunk/core/src/test/java/org/apache/struts2/factory/ struts/struts2/trunk/core/src/test/java/org/apache/struts2/factory/MyResultBuilder.java struts/struts2/trunk/core/src/test/java/org/apache/struts2/factory/StrutsResultBuilderTest.java struts/struts2/trunk/core/src/test/resources/struts-object-factory-result-builder.xml - copied, changed from r1506116, struts/struts2/trunk/core/src/test/resources/struts.xml struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/result/ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/result/ParamNameAwareResult.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/StrutsConstants.java struts/struts2/trunk/core/src/main/java/org/apache/struts2/config/BeanSelectionProvider.java struts/struts2/trunk/core/src/main/resources/struts-default.xml struts/struts2/trunk/core/src/test/java/org/apache/struts2/StrutsTestCase.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/StrutsConstants.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/StrutsConstants.java?rev=1506872&r1=1506871&r2=1506872&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/StrutsConstants.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/StrutsConstants.java Thu Jul 25 10:03:42 2013 @@ -64,6 +64,8 @@ public final class StrutsConstants { /** The com.opensymphony.xwork2.ObjectFactory implementation class */ public static final String STRUTS_OBJECTFACTORY = "struts.objectFactory"; + public static final String STRUTS_OBJECTFACTORY_RESULTBUILDER = "struts.objectFactory.resultBuilder"; + /** The com.opensymphony.xwork2.util.FileManager implementation class */ public static final String STRUTS_FILE_MANAGER_FACTORY = "struts.fileManagerFactory"; Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/config/BeanSelectionProvider.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/config/BeanSelectionProvider.java?rev=1506872&r1=1506871&r2=1506872&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/config/BeanSelectionProvider.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/config/BeanSelectionProvider.java Thu Jul 25 10:03:42 2013 @@ -61,6 +61,7 @@ import org.apache.struts2.components.Url import org.apache.struts2.dispatcher.StaticContentLoader; import org.apache.struts2.dispatcher.mapper.ActionMapper; import org.apache.struts2.dispatcher.multipart.MultiPartRequest; +import com.opensymphony.xwork2.factory.ResultBuilder; import org.apache.struts2.views.freemarker.FreemarkerManager; import org.apache.struts2.views.util.UrlHelper; import org.apache.struts2.views.velocity.VelocityManager; @@ -92,6 +93,12 @@ import java.util.StringTokenizer; * Creates actions, results, and interceptors * * + * com.opensymphony.xwork2.factory.ResultBuilder + * struts.objectFactory.resultBuilder + * singleton + * Dedicated builder to create Results, you can implement/extend existing one instead of defining new ObjectFactory + * + * * com.opensymphony.xwork2.ActionProxyFactory * struts.actionProxyFactory * singleton @@ -325,6 +332,8 @@ public class BeanSelectionProvider imple public void register(ContainerBuilder builder, LocatableProperties props) { alias(ObjectFactory.class, StrutsConstants.STRUTS_OBJECTFACTORY, builder, props); + alias(ResultBuilder.class, StrutsConstants.STRUTS_OBJECTFACTORY_RESULTBUILDER, builder, props); + alias(FileManagerFactory.class, StrutsConstants.STRUTS_FILE_MANAGER_FACTORY, builder, props, Scope.SINGLETON); alias(XWorkConverter.class, StrutsConstants.STRUTS_XWORKCONVERTER, builder, props); Added: struts/struts2/trunk/core/src/main/java/org/apache/struts2/factory/StrutsResultBuilder.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/factory/StrutsResultBuilder.java?rev=1506872&view=auto ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/factory/StrutsResultBuilder.java (added) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/factory/StrutsResultBuilder.java Thu Jul 25 10:03:42 2013 @@ -0,0 +1,71 @@ +package org.apache.struts2.factory; + +import com.opensymphony.xwork2.ObjectFactory; +import com.opensymphony.xwork2.Result; +import com.opensymphony.xwork2.config.entities.ResultConfig; +import com.opensymphony.xwork2.factory.ResultBuilder; +import com.opensymphony.xwork2.inject.Inject; +import com.opensymphony.xwork2.util.reflection.ReflectionException; +import com.opensymphony.xwork2.util.reflection.ReflectionExceptionHandler; +import com.opensymphony.xwork2.util.reflection.ReflectionProvider; +import com.opensymphony.xwork2.result.ParamNameAwareResult; + +import java.util.Map; + +/** + * Default implementation which uses {@link com.opensymphony.xwork2.result.ParamNameAwareResult} to accept or throwaway parameters + */ +public class StrutsResultBuilder implements ResultBuilder { + + protected ObjectFactory objectFactory; + protected ReflectionProvider reflectionProvider; + + @Inject + public void setObjectFactory(ObjectFactory objectFactory) { + this.objectFactory = objectFactory; + } + + @Inject + public void setReflectionProvider(ReflectionProvider provider) { + this.reflectionProvider = provider; + } + + public Result buildResult(ResultConfig resultConfig, Map extraContext) throws Exception { + String resultClassName = resultConfig.getClassName(); + Result result = null; + + if (resultClassName != null) { + result = (Result) objectFactory.buildBean(resultClassName, extraContext); + Map params = resultConfig.getParams(); + if (params != null) { + setParameters(extraContext, result, params); + } + } + return result; + } + + protected void setParameters(Map extraContext, Result result, Map params) { + for (Map.Entry paramEntry : params.entrySet()) { + try { + String name = paramEntry.getKey(); + String value = paramEntry.getValue(); + setParameter(result, name, value, extraContext); + } catch (ReflectionException ex) { + if (result instanceof ReflectionExceptionHandler) { + ((ReflectionExceptionHandler) result).handle(ex); + } + } + } + } + + protected void setParameter(Result result, String name, String value, Map extraContext) { + if (result instanceof ParamNameAwareResult) { + if (((ParamNameAwareResult) result).acceptParamName(name, value)) { + reflectionProvider.setProperty(name, value, result, extraContext, true); + } + } else { + reflectionProvider.setProperty(name, value, result, extraContext, true); + } + } + +} Modified: struts/struts2/trunk/core/src/main/resources/struts-default.xml URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/resources/struts-default.xml?rev=1506872&r1=1506871&r2=1506872&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/resources/struts-default.xml (original) +++ struts/struts2/trunk/core/src/main/resources/struts-default.xml Thu Jul 25 10:03:42 2013 @@ -28,6 +28,8 @@ + + Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/StrutsTestCase.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/StrutsTestCase.java?rev=1506872&r1=1506871&r2=1506872&view=diff ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/StrutsTestCase.java (original) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/StrutsTestCase.java Thu Jul 25 10:03:42 2013 @@ -21,23 +21,22 @@ package org.apache.struts2; +import com.opensymphony.xwork2.XWorkTestCase; +import com.opensymphony.xwork2.util.logging.LoggerFactory; +import com.opensymphony.xwork2.util.logging.jdk.JdkLoggerFactory; +import org.apache.struts2.dispatcher.Dispatcher; +import org.apache.struts2.util.StrutsTestCaseHelper; +import org.springframework.mock.web.MockServletContext; + import java.text.SimpleDateFormat; import java.util.Date; +import java.util.HashMap; import java.util.Map; import java.util.logging.ConsoleHandler; import java.util.logging.Formatter; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; -import java.util.logging.SimpleFormatter; - -import org.apache.struts2.dispatcher.Dispatcher; -import org.apache.struts2.util.StrutsTestCaseHelper; -import org.springframework.mock.web.MockServletContext; - -import com.opensymphony.xwork2.XWorkTestCase; -import com.opensymphony.xwork2.util.logging.LoggerFactory; -import com.opensymphony.xwork2.util.logging.jdk.JdkLoggerFactory; /** * Base test case for JUnit testing Struts. @@ -90,6 +89,19 @@ public abstract class StrutsTestCase ext return du; } + /** + * Init Dispatcher with provided comma delimited list of xml configs to use, ie: + * initDispatcherWithConfigs("struts-default.xml,test-struts-config.xml") + * + * @param configs comma delimited list of config files + * @return instance of {@see Dispatcher} + */ + protected Dispatcher initDispatcherWithConfigs(String configs) { + Map params = new HashMap(); + params.put("config", configs); + return initDispatcher(params); + } + protected void tearDown() throws Exception { super.tearDown(); StrutsTestCaseHelper.tearDown(); Added: struts/struts2/trunk/core/src/test/java/org/apache/struts2/factory/MyResultBuilder.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/factory/MyResultBuilder.java?rev=1506872&view=auto ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/factory/MyResultBuilder.java (added) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/factory/MyResultBuilder.java Thu Jul 25 10:03:42 2013 @@ -0,0 +1,4 @@ +package org.apache.struts2.factory; + +public class MyResultBuilder extends StrutsResultBuilder { +} Added: struts/struts2/trunk/core/src/test/java/org/apache/struts2/factory/StrutsResultBuilderTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/factory/StrutsResultBuilderTest.java?rev=1506872&view=auto ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/factory/StrutsResultBuilderTest.java (added) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/factory/StrutsResultBuilderTest.java Thu Jul 25 10:03:42 2013 @@ -0,0 +1,75 @@ +package org.apache.struts2.factory; + +import com.opensymphony.xwork2.ActionInvocation; +import com.opensymphony.xwork2.Result; +import com.opensymphony.xwork2.config.entities.ResultConfig; +import com.opensymphony.xwork2.factory.ResultBuilder; +import org.apache.struts2.StrutsTestCase; +import com.opensymphony.xwork2.result.ParamNameAwareResult; + +import java.util.HashMap; +import java.util.Map; + +public class StrutsResultBuilderTest extends StrutsTestCase { + + public void testAcceptParams() throws Exception { + // given + initDispatcherWithConfigs("struts-default.xml"); + StrutsResultBuilder builder = (StrutsResultBuilder) container.getInstance(ResultBuilder.class); + + Map params = new HashMap(); + params.put("accept", "ok"); + params.put("reject", "bad"); + ResultConfig config = new ResultConfig.Builder("struts", MyResult.class.getName()).addParams(params).build(); + Map context = new HashMap(); + + // when + Result result = builder.buildResult(config, context); + + // then + assertEquals("ok", ((MyResult)result).getAccept()); + assertEquals("ok", ((MyResult)result).getReject()); + } + + public void testUseCustomResultBuilder() throws Exception { + // given + initDispatcherWithConfigs("struts-default.xml,struts-object-factory-result-builder.xml"); + + // when + ResultBuilder actual = container.getInstance(ResultBuilder.class); + + // then + assertTrue(actual instanceof MyResultBuilder); + } + + public static class MyResult implements Result, ParamNameAwareResult { + + private String accept; + private String reject = "ok"; + + public boolean acceptParamName(String name, String value) { + return "accept".equals(name); + } + + public void execute(ActionInvocation invocation) throws Exception { + + } + + public String getAccept() { + return accept; + } + + public void setAccept(String accept) { + this.accept = accept; + } + + public String getReject() { + return reject; + } + + public void setReject(String reject) { + this.reject = reject; + } + } + +} Copied: struts/struts2/trunk/core/src/test/resources/struts-object-factory-result-builder.xml (from r1506116, struts/struts2/trunk/core/src/test/resources/struts.xml) URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/resources/struts-object-factory-result-builder.xml?p2=struts/struts2/trunk/core/src/test/resources/struts-object-factory-result-builder.xml&p1=struts/struts2/trunk/core/src/test/resources/struts.xml&r1=1506116&r2=1506872&rev=1506872&view=diff ============================================================================== --- struts/struts2/trunk/core/src/test/resources/struts.xml (original) +++ struts/struts2/trunk/core/src/test/resources/struts-object-factory-result-builder.xml Thu Jul 25 10:03:42 2013 @@ -21,78 +21,14 @@ */ --> - - - - hello.jsp - /tutorial/test.action - - - - - - - - - - sitegraph/guess-input.ftl - - - - - org/apache/struts2/views/freemarker/callActionFreeMarker.ftl - - - - - org/apache/struts2/views/freemarker/nested.ftl - + "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" + "http://struts.apache.org/dtds/struts-2.3.dtd"> - - - org/apache/struts2/views/freemarker/nested.ftl - - - - - - org/apache/struts2/views/freemarker/callActionFreeMarker2.ftl - - - - - - org/apache/struts2/views/freemarker/dynaAttributes.ftl - - - - - - org/apache/struts2/views/freemarker/manual-list.ftl - - - - - - org/apache/struts2/views/freemarker/customTextField.ftl - - + - + - - - - + - - guess-success.jsp - guess-input.ftl - guess-error.ftl - - Added: struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/result/ParamNameAwareResult.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/result/ParamNameAwareResult.java?rev=1506872&view=auto ============================================================================== --- struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/result/ParamNameAwareResult.java (added) +++ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/result/ParamNameAwareResult.java Thu Jul 25 10:03:42 2013 @@ -0,0 +1,10 @@ +package com.opensymphony.xwork2.result; + +/** + * Accept parameter name/value to be set on {@link com.opensymphony.xwork2.Result} + */ +public interface ParamNameAwareResult { + + boolean acceptParamName(String name, String value); + +}