diff --git a/core/src/main/java/org/apache/struts2/action/ParametersAware.java b/core/src/main/java/org/apache/struts2/action/ParametersAware.java new file mode 100644 index 000000000..475d603c0 --- /dev/null +++ b/core/src/main/java/org/apache/struts2/action/ParametersAware.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.action; + +import org.apache.struts2.dispatcher.HttpParameters; + +/** + * This interface gives actions an alternative way of receiving input parameters. The parameters will + * contain all input parameters as implementation of {@link org.apache.struts2.dispatcher.Parameter}. + * Actions that need this should simply implement it. + * + * One common use for this is to have the action propagate parameters to internally instantiated data + * objects. + * + * @since 2.6 + */ +public interface ParametersAware { + + /** + * Sets the HTTP parameters in the implementing class. + * + * @param parameters an instance of {@link HttpParameters}. + */ + void withParameters(HttpParameters parameters); +} diff --git a/core/src/main/java/org/apache/struts2/interceptor/HttpParametersAware.java b/core/src/main/java/org/apache/struts2/interceptor/HttpParametersAware.java index 66f2a1d44..c27a05694 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/HttpParametersAware.java +++ b/core/src/main/java/org/apache/struts2/interceptor/HttpParametersAware.java @@ -31,13 +31,19 @@ import org.apache.struts2.dispatcher.HttpParameters; * One common use for this is to have the action propagate parameters to internally instantiated data * objects. *

+ * + * @deprecated please use {@link org.apache.struts2.action.ParametersAware} instead */ +@Deprecated public interface HttpParametersAware { /** * Sets the HTTP parameters in the implementing class. * * @param parameters an instance of {@link HttpParameters}. + * + * @deprecated please use {@link org.apache.struts2.action.ParametersAware#withParameters(HttpParameters)} instead */ + @Deprecated void setParameters(HttpParameters parameters); } diff --git a/core/src/main/java/org/apache/struts2/interceptor/ParameterAware.java b/core/src/main/java/org/apache/struts2/interceptor/ParameterAware.java index 28eb7491f..9689e36b9 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/ParameterAware.java +++ b/core/src/main/java/org/apache/struts2/interceptor/ParameterAware.java @@ -36,7 +36,7 @@ import java.util.Map; * the map is java.lang.String[]. *

* - * @deprecated please use {@link HttpParametersAware} instead + * @deprecated please use {@link org.apache.struts2.action.ParametersAware} instead */ @Deprecated public interface ParameterAware { diff --git a/core/src/main/java/org/apache/struts2/interceptor/RequestAware.java b/core/src/main/java/org/apache/struts2/interceptor/RequestAware.java index 8a7e87072..e8e73eee5 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/RequestAware.java +++ b/core/src/main/java/org/apache/struts2/interceptor/RequestAware.java @@ -18,6 +18,8 @@ */ package org.apache.struts2.interceptor; +import org.apache.struts2.dispatcher.HttpParameters; + import java.util.Map; /** @@ -28,13 +30,17 @@ import java.util.Map; *

* This interface is only relevant if the Action is used in a servlet environment. *

+ * @deprecated please use {@link org.apache.struts2.action.ParametersAware} instead */ +@Deprecated public interface RequestAware { /** * Sets the Map of request attributes in the implementing class. * * @param request a Map of HTTP request attribute name/value pairs. + * @deprecated please use {@link org.apache.struts2.action.ParametersAware#withParameters(HttpParameters)} instead */ + @Deprecated public void setRequest(Map request); } diff --git a/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java index 302fcba66..0312da9ff 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java +++ b/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java @@ -25,6 +25,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.StrutsStatics; +import org.apache.struts2.action.ParametersAware; import org.apache.struts2.interceptor.servlet.ServletPrincipalProxy; import org.apache.struts2.util.ServletContextAware; @@ -103,7 +104,7 @@ import com.opensymphony.xwork2.interceptor.AbstractInterceptor; * @see ServletRequestAware * @see ServletResponseAware * @see ParameterAware - * @see HttpParametersAware + * @see ParametersAware * @see SessionAware * @see ApplicationAware * @see PrincipalAware @@ -151,6 +152,10 @@ public class ServletConfigInterceptor extends AbstractInterceptor implements Str ((HttpParametersAware) action).setParameters(context.getParameters()); } + if (action instanceof ParametersAware) { + ((ParametersAware) action).withParameters(context.getParameters()); + } + if (action instanceof ApplicationAware) { ((ApplicationAware) action).setApplication(context.getApplication()); } diff --git a/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java b/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java index 4306aaa8b..4bb8684d9 100644 --- a/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java +++ b/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java @@ -23,6 +23,7 @@ import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.mock.MockActionInvocation; import org.apache.struts2.StrutsInternalTestCase; import org.apache.struts2.StrutsStatics; +import org.apache.struts2.action.ParametersAware; import org.apache.struts2.dispatcher.HttpParameters; import org.apache.struts2.interceptor.servlet.ServletPrincipalProxy; import org.apache.struts2.util.ServletContextAware; @@ -145,6 +146,22 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase { verify(mock); } + public void testActionParametersAware() throws Exception { + ParametersAware mock = createMock(ParametersAware.class); + + MockActionInvocation mai = createActionInvocation(mock); + + HttpParameters params = HttpParameters.create().build(); + mai.getInvocationContext().setParameters(params); + + mock.withParameters(params); + expectLastCall().times(1); + + replay(mock); + interceptor.intercept(mai); + verify(mock); + } + public void testSessionAware() throws Exception { SessionAware mock = (SessionAware) createMock(SessionAware.class);