diff --git a/apps/showcase/src/main/java/org/apache/struts2/showcase/source/ViewSourceAction.java b/apps/showcase/src/main/java/org/apache/struts2/showcase/source/ViewSourceAction.java index 3b71fa68e..56dbf9bfb 100644 --- a/apps/showcase/src/main/java/org/apache/struts2/showcase/source/ViewSourceAction.java +++ b/apps/showcase/src/main/java/org/apache/struts2/showcase/source/ViewSourceAction.java @@ -23,7 +23,7 @@ package org.apache.struts2.showcase.source; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.util.ClassLoaderUtil; import org.apache.struts2.ServletActionContext; -import org.apache.struts2.util.ServletContextAware; +import org.apache.struts2.action.ServletContextAware; import javax.servlet.ServletContext; import java.io.BufferedReader; @@ -227,7 +227,7 @@ public class ViewSourceAction extends ActionSupport implements ServletContextAwa return snippet; } - public void setServletContext(ServletContext arg0) { + public void withServletContext(ServletContext arg0) { this.servletContext = arg0; } diff --git a/bundles/admin/src/main/java/org/apache/struts2/osgi/admin/actions/BundlesAction.java b/bundles/admin/src/main/java/org/apache/struts2/osgi/admin/actions/BundlesAction.java index 5d81de896..cf6edb07f 100644 --- a/bundles/admin/src/main/java/org/apache/struts2/osgi/admin/actions/BundlesAction.java +++ b/bundles/admin/src/main/java/org/apache/struts2/osgi/admin/actions/BundlesAction.java @@ -28,7 +28,7 @@ import com.opensymphony.xwork2.inject.Inject; import org.apache.struts2.osgi.BundleAccessor; import org.apache.struts2.osgi.host.OsgiHost; import org.apache.struts2.osgi.StrutsOsgiListener; -import org.apache.struts2.util.ServletContextAware; +import org.apache.struts2.action.ServletContextAware; import org.osgi.framework.Bundle; import org.osgi.framework.BundleException; @@ -197,7 +197,7 @@ public class BundlesAction extends ActionSupport implements ServletContextAware this.bundleAccessor = bundleAccessor; } - public void setServletContext(ServletContext servletContext) { + public void withServletContext(ServletContext servletContext) { osgiHost = (OsgiHost) servletContext.getAttribute(StrutsOsgiListener.OSGI_HOST); } } diff --git a/bundles/demo/src/main/java/actions/osgi/BundlesAction.java b/bundles/demo/src/main/java/actions/osgi/BundlesAction.java index eff79ccce..9c5464fcb 100644 --- a/bundles/demo/src/main/java/actions/osgi/BundlesAction.java +++ b/bundles/demo/src/main/java/actions/osgi/BundlesAction.java @@ -22,7 +22,7 @@ package actions.osgi; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.ResultPath; -import org.apache.struts2.osgi.interceptor.BundleContextAware; +import org.apache.struts2.osgi.action.BundleContextAware; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; @@ -33,7 +33,7 @@ import org.osgi.framework.BundleContext; public class BundlesAction extends ActionSupport implements BundleContextAware { private BundleContext bundleContext; - public void setBundleContext(BundleContext bundleContext) { + public void withBundleContext(BundleContext bundleContext) { this.bundleContext = bundleContext; } diff --git a/core/src/main/java/org/apache/struts2/action/ApplicationAware.java b/core/src/main/java/org/apache/struts2/action/ApplicationAware.java new file mode 100644 index 000000000..3748e60f2 --- /dev/null +++ b/core/src/main/java/org/apache/struts2/action/ApplicationAware.java @@ -0,0 +1,38 @@ +/* + * 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 java.util.Map; + +/** + * Actions that want to be aware of the application Map object should implement this interface. + * This will give them access to a Map where they can put objects that should be available + * to other parts of the application. + * + * Typical uses are configuration objects and caches. + */ +public interface ApplicationAware { + + /** + * Applies the map of application properties in the implementing class. + * + * @param application a Map of application properties. + */ + void withApplication(Map application); +} diff --git a/core/src/main/java/org/apache/struts2/action/CookiesAware.java b/core/src/main/java/org/apache/struts2/action/CookiesAware.java new file mode 100644 index 000000000..ba753869a --- /dev/null +++ b/core/src/main/java/org/apache/struts2/action/CookiesAware.java @@ -0,0 +1,40 @@ +/* + * 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.interceptor.CookieInterceptor; + +import java.util.Map; + +/** + * Actions implementing the CookiesAware interface will receive + * a Map of filtered cookies via the setCookiesMap method. + * + * Please note that the {@link CookieInterceptor} needs to be + * activated to receive a cookies map. + * + * @since 2.6 + */ +public interface CookiesAware { + /** + * Sets a map of filtered cookies. + * @param cookies the cookies + */ + void withCookies(Map cookies); +} \ No newline at end of file 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/action/PrincipalAware.java b/core/src/main/java/org/apache/struts2/action/PrincipalAware.java new file mode 100644 index 000000000..9bff42beb --- /dev/null +++ b/core/src/main/java/org/apache/struts2/action/PrincipalAware.java @@ -0,0 +1,34 @@ +/* + * 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.interceptor.PrincipalProxy; + +/** + * Actions that want access to the Principal information from HttpServletRequest object + * should implement this interface. + * + * This interface is only relevant if the Action is used in a servlet environment. + * By using this interface you will not become tied to servlet environment. + */ +public interface PrincipalAware { + + void withPrincipalProxy(PrincipalProxy principalProxy); + +} diff --git a/core/src/main/java/org/apache/struts2/action/ServletContextAware.java b/core/src/main/java/org/apache/struts2/action/ServletContextAware.java new file mode 100644 index 000000000..09a99d081 --- /dev/null +++ b/core/src/main/java/org/apache/struts2/action/ServletContextAware.java @@ -0,0 +1,31 @@ +/* + * 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 javax.servlet.ServletContext; + +/** + * For components that have a dependence on the Servlet context. + * + * @since 2.6 + */ +public interface ServletContextAware { + + void withServletContext(ServletContext context); +} diff --git a/core/src/main/java/org/apache/struts2/action/ServletRequestAware.java b/core/src/main/java/org/apache/struts2/action/ServletRequestAware.java new file mode 100644 index 000000000..115bff35e --- /dev/null +++ b/core/src/main/java/org/apache/struts2/action/ServletRequestAware.java @@ -0,0 +1,39 @@ +/* + * 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 javax.servlet.http.HttpServletRequest; + +/** + * All Actions that want to have access to the servlet request object must implement this interface. + * + * This interface is only relevant if the Action is used in a servlet environment. + * + * Note that using this interface makes the Action tied to a servlet environment, so it should be + * avoided if possible since things like unit testing will become more difficult. + */ +public interface ServletRequestAware { + + /** + * Applies the HTTP request object in implementing classes. + * + * @param request the HTTP request. + */ + void withServletRequest(HttpServletRequest request); +} diff --git a/core/src/main/java/org/apache/struts2/action/ServletResponseAware.java b/core/src/main/java/org/apache/struts2/action/ServletResponseAware.java new file mode 100644 index 000000000..0403f1f9c --- /dev/null +++ b/core/src/main/java/org/apache/struts2/action/ServletResponseAware.java @@ -0,0 +1,39 @@ +/* + * 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 javax.servlet.http.HttpServletResponse; + +/** + * All Actions that want to have access to the servlet response object must implement this interface. + * + * This interface is only relevant if the Action is used in a servlet environment. + * + * Note that using this interface makes the Action tied to a servlet environment, so it should be + * avoided if possible since things like unit testing will become more difficult. + */ +public interface ServletResponseAware { + + /** + * Applies the HTTP response object in implementing classes. + * + * @param response the HTTP response. + */ + void withServletResponse(HttpServletResponse response); +} diff --git a/core/src/main/java/org/apache/struts2/action/SessionAware.java b/core/src/main/java/org/apache/struts2/action/SessionAware.java new file mode 100644 index 000000000..039fcca00 --- /dev/null +++ b/core/src/main/java/org/apache/struts2/action/SessionAware.java @@ -0,0 +1,40 @@ +/* + * 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 java.util.Map; + +/** + * Actions that want access to the user's HTTP session attributes should implement this interface. + * + * This will give them access to a Map where they can put objects that can be made available + * to subsequent requests. + * + * Typical uses may be cached user data such as name, or a shopping cart. + */ +public interface SessionAware { + + /** + * Applies the Map of session attributes in the implementing class. + * + * @param session a Map of HTTP session attribute name/value pairs. + */ + void withSession(Map session); + +} diff --git a/core/src/main/java/org/apache/struts2/interceptor/ApplicationAware.java b/core/src/main/java/org/apache/struts2/interceptor/ApplicationAware.java index 4cdd4785e..c240fa8cb 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/ApplicationAware.java +++ b/core/src/main/java/org/apache/struts2/interceptor/ApplicationAware.java @@ -29,13 +29,17 @@ import java.util.Map; *

* Typical uses are configuration objects and caches. *

+ * @deprecated please use {@link org.apache.struts2.action.ApplicationAware} instead */ +@Deprecated public interface ApplicationAware { /** * Sets the map of application properties in the implementing class. * * @param application a Map of application properties. + * @deprecated please use {@link org.apache.struts2.action.ApplicationAware#withApplication(Map)} instead */ + @Deprecated public void setApplication(Map application); } diff --git a/core/src/main/java/org/apache/struts2/interceptor/CookieInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/CookieInterceptor.java index 86578e77b..eb40c3a53 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/CookieInterceptor.java +++ b/core/src/main/java/org/apache/struts2/interceptor/CookieInterceptor.java @@ -348,8 +348,8 @@ public class CookieInterceptor extends AbstractInterceptor { } /** - * Hook that set the cookiesMap into action that implements - * {@link CookiesAware}. + * Hook that set the cookiesMap into action that implements {@link CookiesAware} + * or {@link org.apache.struts2.action.CookiesAware}. * * @param action action object * @param cookiesMap map of cookies @@ -359,5 +359,9 @@ public class CookieInterceptor extends AbstractInterceptor { LOG.debug("Action [{}] implements CookiesAware, injecting cookies map [{}]", action, cookiesMap); ((CookiesAware)action).setCookiesMap(cookiesMap); } + if (action instanceof org.apache.struts2.action.CookiesAware) { + LOG.debug("Action [{}] implements CookiesAware, injecting cookies map [{}]", action, cookiesMap); + ((org.apache.struts2.action.CookiesAware)action).withCookies(cookiesMap); + } } } diff --git a/core/src/main/java/org/apache/struts2/interceptor/CookiesAware.java b/core/src/main/java/org/apache/struts2/interceptor/CookiesAware.java index 0632e8326..d3a0a02b1 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/CookiesAware.java +++ b/core/src/main/java/org/apache/struts2/interceptor/CookiesAware.java @@ -26,11 +26,15 @@ import java.util.Map; * * Please note that the {@link CookieInterceptor} needs to be * activated to receive a cookies map. + * + * @deprecated please use {@link org.apache.struts2.action.CookiesAware} instead */ +@Deprecated public interface CookiesAware { /** * Sets a map of filtered cookies. * @param cookies the cookies + * @deprecated please use {@link org.apache.struts2.action.CookiesAware#withCookies(Map)} instead */ void setCookiesMap(Map cookies); } \ No newline at end of file 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/PrincipalAware.java b/core/src/main/java/org/apache/struts2/interceptor/PrincipalAware.java index 7bf418ec9..ac1e3296b 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/PrincipalAware.java +++ b/core/src/main/java/org/apache/struts2/interceptor/PrincipalAware.java @@ -25,7 +25,14 @@ package org.apache.struts2.interceptor; *

This interface is only relevant if the Action is used in a servlet environment. * By using this interface you will not become tied to servlet environment.

* + * @deprecated please use {@link org.apache.struts2.action.PrincipalAware} instead */ +@Deprecated public interface PrincipalAware { + + /** + * @deprecated please use {@link org.apache.struts2.action.PrincipalAware#withPrincipalProxy(PrincipalProxy)} instead + */ + @Deprecated void setPrincipalProxy(PrincipalProxy principalProxy); } 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 813a87d91..970789623 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 @@ -128,11 +129,21 @@ public class ServletConfigInterceptor extends AbstractInterceptor implements Str ((ServletRequestAware) action).setServletRequest(request); } + if (action instanceof org.apache.struts2.action.ServletRequestAware) { + HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST); + ((org.apache.struts2.action.ServletRequestAware) action).withServletRequest(request); + } + if (action instanceof ServletResponseAware) { HttpServletResponse response = (HttpServletResponse) context.get(HTTP_RESPONSE); ((ServletResponseAware) action).setServletResponse(response); } + if (action instanceof org.apache.struts2.action.ServletResponseAware) { + HttpServletResponse response = (HttpServletResponse) context.get(HTTP_RESPONSE); + ((org.apache.struts2.action.ServletResponseAware) action).withServletResponse(response); + } + if (action instanceof ParameterAware) { context.getParameters().applyParameters((ParameterAware) action); } @@ -141,14 +152,26 @@ 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()); } + if (action instanceof org.apache.struts2.action.ApplicationAware) { + ((org.apache.struts2.action.ApplicationAware) action).withApplication(context.getApplication()); + } + if (action instanceof SessionAware) { ((SessionAware) action).setSession(context.getSession()); } + if (action instanceof org.apache.struts2.action.SessionAware) { + ((org.apache.struts2.action.SessionAware) action).withSession(context.getSession()); + } + if (action instanceof RequestAware) { ((RequestAware) action).setRequest((Map) context.get("request")); } @@ -160,10 +183,25 @@ public class ServletConfigInterceptor extends AbstractInterceptor implements Str ((PrincipalAware) action).setPrincipalProxy(new ServletPrincipalProxy(request)); } } + + if (action instanceof org.apache.struts2.action.PrincipalAware) { + HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST); + if(request != null) { + // We are in servlet environment, so principal information resides in HttpServletRequest + ((org.apache.struts2.action.PrincipalAware) action).withPrincipalProxy(new ServletPrincipalProxy(request)); + } + } + if (action instanceof ServletContextAware) { ServletContext servletContext = (ServletContext) context.get(SERVLET_CONTEXT); ((ServletContextAware) action).setServletContext(servletContext); } + + if (action instanceof org.apache.struts2.action.ServletContextAware) { + ServletContext servletContext = (ServletContext) context.get(SERVLET_CONTEXT); + ((org.apache.struts2.action.ServletContextAware) action).withServletContext(servletContext); + } + return invocation.invoke(); } } diff --git a/core/src/main/java/org/apache/struts2/interceptor/ServletRequestAware.java b/core/src/main/java/org/apache/struts2/interceptor/ServletRequestAware.java index 20a6039dd..7c9da3032 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/ServletRequestAware.java +++ b/core/src/main/java/org/apache/struts2/interceptor/ServletRequestAware.java @@ -33,13 +33,17 @@ import javax.servlet.http.HttpServletRequest; * Note that using this interface makes the Action tied to a servlet environment, so it should be * avoided if possible since things like unit testing will become more difficult. *

+ * @deprecated please use {@link org.apache.struts2.action.ServletRequestAware} instead */ +@Deprecated public interface ServletRequestAware { /** * Sets the HTTP request object in implementing classes. * * @param request the HTTP request. + * @deprecated please use {@link org.apache.struts2.action.ServletRequestAware#withServletRequest(HttpServletRequest)} */ + @Deprecated public void setServletRequest(HttpServletRequest request); } diff --git a/core/src/main/java/org/apache/struts2/interceptor/ServletResponseAware.java b/core/src/main/java/org/apache/struts2/interceptor/ServletResponseAware.java index e70c1af72..9043b4434 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/ServletResponseAware.java +++ b/core/src/main/java/org/apache/struts2/interceptor/ServletResponseAware.java @@ -31,13 +31,17 @@ import javax.servlet.http.HttpServletResponse; * Note that using this interface makes the Action tied to a servlet environment, so it should be * avoided if possible since things like unit testing will become more difficult. *

+ * @deprecated please use {@link org.apache.struts2.action.ServletResponseAware} instead */ +@Deprecated public interface ServletResponseAware { /** * Sets the HTTP response object in implementing classes. * * @param response the HTTP response. + * @deprecated please use {@link org.apache.struts2.action.ServletResponseAware#withServletResponse(HttpServletResponse)} instead */ + @Deprecated public void setServletResponse(HttpServletResponse response); } diff --git a/core/src/main/java/org/apache/struts2/interceptor/SessionAware.java b/core/src/main/java/org/apache/struts2/interceptor/SessionAware.java index db553889e..0380c9702 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/SessionAware.java +++ b/core/src/main/java/org/apache/struts2/interceptor/SessionAware.java @@ -31,13 +31,17 @@ import java.util.Map; *

* Typical uses may be cached user data such as name, or a shopping cart. *

+ * @deprecated use {@link org.apache.struts2.action.SessionAware} */ +@Deprecated public interface SessionAware { /** * Sets the Map of session attributes in the implementing class. * * @param session a Map of HTTP session attribute name/value pairs. + * @deprecated please use {@link org.apache.struts2.action.SessionAware#withSession(Map)} */ + @Deprecated public void setSession(Map session); } diff --git a/core/src/main/java/org/apache/struts2/util/ServletContextAware.java b/core/src/main/java/org/apache/struts2/util/ServletContextAware.java index 7fed260c6..13a717cc7 100644 --- a/core/src/main/java/org/apache/struts2/util/ServletContextAware.java +++ b/core/src/main/java/org/apache/struts2/util/ServletContextAware.java @@ -22,8 +22,15 @@ import javax.servlet.ServletContext; /** * For components that have a dependence on the Servlet context. + * + * @deprecated please use {@link org.apache.struts2.action.ServletContextAware} instead */ +@Deprecated public interface ServletContextAware { + /** + * @deprecated please use {@link org.apache.struts2.action.ServletContextAware#withServletContext(ServletContext)} instead + */ + @Deprecated public void setServletContext(ServletContext context); } diff --git a/core/src/test/java/org/apache/struts2/interceptor/CookieInterceptorTest.java b/core/src/test/java/org/apache/struts2/interceptor/CookieInterceptorTest.java index fd374aee5..69d36ce4f 100644 --- a/core/src/test/java/org/apache/struts2/interceptor/CookieInterceptorTest.java +++ b/core/src/test/java/org/apache/struts2/interceptor/CookieInterceptorTest.java @@ -426,6 +426,49 @@ public class CookieInterceptorTest extends StrutsInternalTestCase { assertFalse(excludedName.get(reqCookieName)); } + public void testActionCookieAwareWithStrutsInternalsAccess() throws Exception { + MockHttpServletRequest request = new MockHttpServletRequest(); + String sessionCookieName = "session.userId"; + String sessionCookieValue = "session.userId=1"; + String appCookieName = "application.userId"; + String appCookieValue = "application.userId=1"; + String reqCookieName = "request.userId"; + String reqCookieValue = "request.userId=1"; + + request.setCookies( + new Cookie(sessionCookieName, "1"), + new Cookie("1", sessionCookieValue), + new Cookie(appCookieName, "1"), + new Cookie("1", appCookieValue), + new Cookie(reqCookieName, "1"), + new Cookie("1", reqCookieValue) + ); + ServletActionContext.setRequest(request); + + final Map excludedName = new HashMap<>(); + + CookieInterceptor interceptor = new CookieInterceptor() { + @Override + protected boolean isAcceptableName(String name) { + boolean accepted = super.isAcceptableName(name); + excludedName.put(name, accepted); + return accepted; + } + }; + interceptor.setExcludedPatternsChecker(new DefaultExcludedPatternsChecker()); + interceptor.setAcceptedPatternsChecker(new DefaultAcceptedPatternsChecker()); + interceptor.setCookiesName("*"); + + MockActionInvocation invocation = new MockActionInvocation(); + invocation.setAction(new MockActionWithActionCookieAware()); + + interceptor.intercept(invocation); + + assertFalse(excludedName.get(sessionCookieName)); + assertFalse(excludedName.get(appCookieName)); + assertFalse(excludedName.get(reqCookieName)); + } + public static class MockActionWithCookieAware extends ActionSupport implements CookiesAware { private static final long serialVersionUID = -6202290616812813386L; @@ -453,4 +496,29 @@ public class CookieInterceptorTest extends StrutsInternalTestCase { public void setCookie3(String cookie3) { this.cookie3 = cookie3; } } + public static class MockActionWithActionCookieAware extends ActionSupport implements org.apache.struts2.action.CookiesAware { + + private Map cookies = Collections.EMPTY_MAP; + private String cookie1; + private String cookie2; + private String cookie3; + + public void withCookies(Map cookies) { + this.cookies = cookies; + } + + public Map getCookiesMap() { + return this.cookies; + } + + public String getCookie1() { return cookie1; } + public void setCookie1(String cookie1) { this.cookie1 = cookie1; } + + public String getCookie2() { return cookie2; } + public void setCookie2(String cookie2) { this.cookie2 = cookie2; } + + public String getCookie3() { return cookie3; } + public void setCookie3(String cookie3) { this.cookie3 = cookie3; } + } + } 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 c394d2b90..20057e235 100644 --- a/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java +++ b/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java @@ -18,31 +18,33 @@ */ package org.apache.struts2.interceptor; -import java.util.HashMap; -import java.util.Map; - -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - +import com.opensymphony.xwork2.Action; +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.interceptor.servlet.ServletPrincipalProxy; +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; - -import static org.easymock.EasyMock.*; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockServletContext; -import com.opensymphony.xwork2.Action; -import com.opensymphony.xwork2.ActionContext; -import com.opensymphony.xwork2.mock.MockActionInvocation; +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.HashMap; +import java.util.Map; + +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expectLastCall; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; /** * Unit test for {@link ServletConfigInterceptor}. - * */ public class ServletConfigInterceptorTest extends StrutsInternalTestCase { @@ -64,6 +66,22 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase { verify(mock); } + public void testActionServletRequestAware() throws Exception { + org.apache.struts2.action.ServletRequestAware mock = createMock(org.apache.struts2.action.ServletRequestAware.class); + + MockHttpServletRequest req = new MockHttpServletRequest(); + + MockActionInvocation mai = createActionInvocation(mock); + mai.getInvocationContext().put(StrutsStatics.HTTP_REQUEST, req); + + mock.withServletRequest(req); + expectLastCall(); + + replay(mock); + interceptor.intercept(mai); + verify(mock); + } + public void testServletResponseAware() throws Exception { ServletResponseAware mock = (ServletResponseAware) createMock(ServletResponseAware.class); @@ -80,6 +98,22 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase { verify(mock); } + public void testActionServletResponseAware() throws Exception { + org.apache.struts2.action.ServletResponseAware mock = createMock(org.apache.struts2.action.ServletResponseAware.class); + + MockHttpServletResponse res = new MockHttpServletResponse(); + + MockActionInvocation mai = createActionInvocation(mock); + mai.getInvocationContext().put(StrutsStatics.HTTP_RESPONSE, res); + + mock.withServletResponse(res); + expectLastCall().times(1); + + replay(mock); + interceptor.intercept(mai); + verify(mock); + } + public void testParameterAware() throws Exception { ParameterAware mock = createMock(ParameterAware.class); @@ -112,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); @@ -128,6 +178,22 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase { verify(mock); } + public void testActionSessionAware() throws Exception { + org.apache.struts2.action.SessionAware mock = createMock(org.apache.struts2.action.SessionAware.class); + + MockActionInvocation mai = createActionInvocation(mock); + + Map session = new HashMap(); + mai.getInvocationContext().setSession(session); + + mock.withSession(session); + expectLastCall().times(1); + + replay(mock); + interceptor.intercept(mai); + verify(mock); + } + public void testApplicationAware() throws Exception { ApplicationAware mock = (ApplicationAware) createMock(ApplicationAware.class); @@ -144,6 +210,22 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase { verify(mock); } + public void testActionApplicationAware() throws Exception { + org.apache.struts2.action.ApplicationAware mock = createMock(org.apache.struts2.action.ApplicationAware.class); + + MockActionInvocation mai = createActionInvocation(mock); + + Map app = new HashMap<>(); + mai.getInvocationContext().setApplication(app); + + mock.withApplication(app); + expectLastCall().times(1); + + replay(mock); + interceptor.intercept(mai); + verify(mock); + } + public void testPrincipalAware() throws Exception { MockHttpServletRequest req = new MockHttpServletRequest(); req.setUserPrincipal(null); @@ -152,7 +234,7 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase { MockActionInvocation mai = createActionInvocation(mock); mai.getInvocationContext().put(StrutsStatics.HTTP_REQUEST, req); - + MockServletContext ctx = new MockServletContext(); mai.getInvocationContext().put(StrutsStatics.SERVLET_CONTEXT, ctx); @@ -164,6 +246,26 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase { verify(mock); } + public void testActionPrincipalAware() throws Exception { + MockHttpServletRequest req = new MockHttpServletRequest(); + req.setUserPrincipal(null); + req.setRemoteUser("Santa"); + org.apache.struts2.action.PrincipalAware mock = createMock(org.apache.struts2.action.PrincipalAware.class); + + MockActionInvocation mai = createActionInvocation(mock); + mai.getInvocationContext().put(StrutsStatics.HTTP_REQUEST, req); + + MockServletContext ctx = new MockServletContext(); + mai.getInvocationContext().put(StrutsStatics.SERVLET_CONTEXT, ctx); + + mock.withPrincipalProxy(anyObject(ServletPrincipalProxy.class)); // less strict match is needed for this unit test to be conducted using mocks + expectLastCall().times(1); + + replay(mock); + interceptor.intercept(mai); + verify(mock); + } + public void testPrincipalProxy() throws Exception { // uni test that does not use mock, but an Action so we also get code coverage for the PrincipalProxy class MockHttpServletRequest req = new MockHttpServletRequest(); @@ -180,8 +282,30 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase { PrincipalProxy proxy = action.getProxy(); assertNull(proxy.getUserPrincipal()); - assertTrue(! proxy.isRequestSecure()); - assertTrue(! proxy.isUserInRole("no.role")); + assertTrue(!proxy.isRequestSecure()); + assertTrue(!proxy.isUserInRole("no.role")); + assertEquals("Santa", proxy.getRemoteUser()); + + } + + public void testActionPrincipalProxy() throws Exception { + // unit test that does not use mock, but an Action so we also get code coverage for the PrincipalProxy class + MockHttpServletRequest req = new MockHttpServletRequest(); + req.setUserPrincipal(null); + req.setRemoteUser("Santa"); + + MyNewPrincipalAction action = new MyNewPrincipalAction(); + MockActionInvocation mai = createActionInvocation(action); + mai.getInvocationContext().put(StrutsStatics.HTTP_REQUEST, req); + + assertNull(action.getProxy()); + interceptor.intercept(mai); + assertNotNull(action.getProxy()); + + PrincipalProxy proxy = action.getProxy(); + assertNull(proxy.getUserPrincipal()); + assertFalse(proxy.isRequestSecure()); + assertFalse(proxy.isUserInRole("no.role")); assertEquals("Santa", proxy.getRemoteUser()); } @@ -202,6 +326,22 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase { verify(mock); } + public void testActionServletContextAware() throws Exception { + org.apache.struts2.action.ServletContextAware mock = createMock(org.apache.struts2.action.ServletContextAware.class); + + MockActionInvocation mai = createActionInvocation(mock); + + MockServletContext ctx = new MockServletContext(); + mai.getInvocationContext().put(StrutsStatics.SERVLET_CONTEXT, ctx); + + mock.withServletContext(ctx); + expectLastCall().times(1); + + replay(mock); + interceptor.intercept(mai); + verify(mock); + } + private MockActionInvocation createActionInvocation(Object mock) { MockActionInvocation mai = new MockActionInvocation(); mai.setResultCode("success"); @@ -240,4 +380,21 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase { } } + private class MyNewPrincipalAction implements Action, org.apache.struts2.action.PrincipalAware { + + private PrincipalProxy proxy; + + public String execute() throws Exception { + return SUCCESS; + } + + public void withPrincipalProxy(PrincipalProxy proxy) { + this.proxy = proxy; + } + + public PrincipalProxy getProxy() { + return proxy; + } + } + } diff --git a/plugins/osgi/src/main/java/org/apache/struts2/osgi/action/BundleContextAware.java b/plugins/osgi/src/main/java/org/apache/struts2/osgi/action/BundleContextAware.java new file mode 100644 index 000000000..c2be80aaa --- /dev/null +++ b/plugins/osgi/src/main/java/org/apache/struts2/osgi/action/BundleContextAware.java @@ -0,0 +1,31 @@ +/* + * 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.osgi.action; + +import org.osgi.framework.BundleContext; + +/** + * Actions implementing this interface will receive an instance of the BundleContext, + * the OsgiInterceptor must be applied to the action. + */ +public interface BundleContextAware { + + void withBundleContext(BundleContext bundleContext); + +} diff --git a/plugins/osgi/src/main/java/org/apache/struts2/osgi/action/ServiceAware.java b/plugins/osgi/src/main/java/org/apache/struts2/osgi/action/ServiceAware.java new file mode 100644 index 000000000..45d356610 --- /dev/null +++ b/plugins/osgi/src/main/java/org/apache/struts2/osgi/action/ServiceAware.java @@ -0,0 +1,34 @@ +/* + * 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.osgi.action; + +import java.util.List; + +/** + * Classes implementing this interface, will be injected a list of services + * registered with the type of the parameterized type + * + * @param The type of the service + * @since 2.6 + */ +public interface ServiceAware { + + void withServices(List services); + +} diff --git a/plugins/osgi/src/main/java/org/apache/struts2/osgi/interceptor/BundleContextAware.java b/plugins/osgi/src/main/java/org/apache/struts2/osgi/interceptor/BundleContextAware.java index d9eaf60e4..d4ad98d3c 100644 --- a/plugins/osgi/src/main/java/org/apache/struts2/osgi/interceptor/BundleContextAware.java +++ b/plugins/osgi/src/main/java/org/apache/struts2/osgi/interceptor/BundleContextAware.java @@ -23,9 +23,16 @@ import org.osgi.framework.BundleContext; /** * Actions implementing this interface will receive an instance of the BundleContext, * the OsgiInterceptor must be applied to the action. + * + * @deprecated please use {@link org.apache.struts2.osgi.action.BundleContextAware} instead */ +@Deprecated public interface BundleContextAware { + /** + * @deprecated please use {@link org.apache.struts2.osgi.action.BundleContextAware#withBundleContext(BundleContext)} instead + */ + @Deprecated void setBundleContext(BundleContext bundleContext); } diff --git a/plugins/osgi/src/main/java/org/apache/struts2/osgi/interceptor/OsgiInterceptor.java b/plugins/osgi/src/main/java/org/apache/struts2/osgi/interceptor/OsgiInterceptor.java index 09b976927..62c676ce0 100644 --- a/plugins/osgi/src/main/java/org/apache/struts2/osgi/interceptor/OsgiInterceptor.java +++ b/plugins/osgi/src/main/java/org/apache/struts2/osgi/interceptor/OsgiInterceptor.java @@ -21,10 +21,11 @@ package org.apache.struts2.osgi.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.inject.Inject; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.apache.struts2.osgi.host.OsgiHost; import org.osgi.framework.BundleContext; +import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceReference; import javax.servlet.ServletContext; @@ -46,35 +47,85 @@ public class OsgiInterceptor extends AbstractInterceptor { public String intercept(ActionInvocation invocation) throws Exception { if (bundleContext != null) { Object action = invocation.getAction(); + injectBundleContext(action); + injectServicesUsingDeprecatedInterface(action); + injectServices(action); + } else if (LOG.isWarnEnabled()) { + LOG.warn("The OSGi interceptor was not able to find the BundleContext in the ServletContext"); + } - //inject BundleContext - if (action instanceof BundleContextAware) - ((BundleContextAware)action).setBundleContext(bundleContext); + return invocation.invoke(); + } - //inject service implementations - if (action instanceof ServiceAware) { - Type[] types = action.getClass().getGenericInterfaces(); - if (types != null) { - for (Type type : types) { - if (type instanceof ParameterizedType) { - ParameterizedType parameterizedType = (ParameterizedType) type; - if (parameterizedType.getRawType() instanceof Class) { - Class clazz = (Class) parameterizedType.getRawType(); - if (ServiceAware.class.equals(clazz)) { - Class serviceClass = (Class) parameterizedType.getActualTypeArguments()[0]; - ServiceReference[] refs = bundleContext.getAllServiceReferences(serviceClass.getName(), null); - //get the services - if (refs != null) { - List services = new ArrayList(refs.length); - for (ServiceReference ref : refs) { - Object service = bundleContext.getService(ref); - //wow, that's a lot of nested ifs - if (service != null) - services.add(service); - } + private void injectBundleContext(Object action) { + if (action instanceof BundleContextAware) + ((BundleContextAware) action).setBundleContext(bundleContext); - if (!services.isEmpty()) - ((ServiceAware)action).setServices(services); + if (action instanceof org.apache.struts2.osgi.action.BundleContextAware) { + ((org.apache.struts2.osgi.action.BundleContextAware) action).withBundleContext(bundleContext); + } + } + + @Deprecated + private void injectServicesUsingDeprecatedInterface(Object action) throws InvalidSyntaxException { + //inject service implementations + if (action instanceof ServiceAware) { + Type[] types = action.getClass().getGenericInterfaces(); + if (types != null) { + for (Type type : types) { + if (type instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) type; + if (parameterizedType.getRawType() instanceof Class) { + Class clazz = (Class) parameterizedType.getRawType(); + if (ServiceAware.class.equals(clazz)) { + Class serviceClass = (Class) parameterizedType.getActualTypeArguments()[0]; + ServiceReference[] refs = bundleContext.getAllServiceReferences(serviceClass.getName(), null); + //get the services + if (refs != null) { + List services = new ArrayList(refs.length); + for (ServiceReference ref : refs) { + Object service = bundleContext.getService(ref); + //wow, that's a lot of nested ifs + if (service != null) + services.add(service); + } + + if (!services.isEmpty()) + ((ServiceAware) action).setServices(services); + } + } + } + } + } + } + } + } + + private void injectServices(Object action) throws InvalidSyntaxException { + //inject service implementations + if (action instanceof org.apache.struts2.osgi.action.ServiceAware) { + Type[] types = action.getClass().getGenericInterfaces(); + if (types != null) { + for (Type type : types) { + if (type instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) type; + if (parameterizedType.getRawType() instanceof Class) { + Class clazz = (Class) parameterizedType.getRawType(); + if (org.apache.struts2.osgi.action.ServiceAware.class.equals(clazz)) { + Class serviceClass = (Class) parameterizedType.getActualTypeArguments()[0]; + ServiceReference[] refs = bundleContext.getAllServiceReferences(serviceClass.getName(), null); + //get the services + if (refs != null) { + List services = new ArrayList<>(refs.length); + for (ServiceReference ref : refs) { + Object service = bundleContext.getService(ref); + //wow, that's a lot of nested ifs + if (service != null) + services.add(service); + } + + if (!services.isEmpty()) { + ((org.apache.struts2.osgi.action.ServiceAware) action).withServices(services); } } } @@ -82,11 +133,7 @@ public class OsgiInterceptor extends AbstractInterceptor { } } } - } else if (LOG.isWarnEnabled()){ - LOG.warn("The OSGi interceptor was not able to find the BundleContext in the ServletContext"); } - - return invocation.invoke(); } @Inject diff --git a/plugins/osgi/src/main/java/org/apache/struts2/osgi/interceptor/ServiceAware.java b/plugins/osgi/src/main/java/org/apache/struts2/osgi/interceptor/ServiceAware.java index bc6e916fe..c5c1c8e83 100644 --- a/plugins/osgi/src/main/java/org/apache/struts2/osgi/interceptor/ServiceAware.java +++ b/plugins/osgi/src/main/java/org/apache/struts2/osgi/interceptor/ServiceAware.java @@ -24,9 +24,15 @@ import java.util.List; * Classes implementing this interface, will be injected a list of services * registered with the type of the parameterized type * @param The type of the service + * @deprecated please use {@link org.apache.struts2.osgi.action.ServiceAware} instead */ +@Deprecated public interface ServiceAware { + /** + * @deprecated please use {@link org.apache.struts2.osgi.action.ServiceAware#withServices(List)} instead + */ + @Deprecated void setServices(List services); } diff --git a/plugins/osgi/src/test/java/org/apache/struts2/osgi/action/ServiceAction.java b/plugins/osgi/src/test/java/org/apache/struts2/osgi/action/ServiceAction.java new file mode 100644 index 000000000..df6c2588a --- /dev/null +++ b/plugins/osgi/src/test/java/org/apache/struts2/osgi/action/ServiceAction.java @@ -0,0 +1,34 @@ +/* + * 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.osgi.action; + +import java.util.List; + +public class ServiceAction implements ServiceAware { + private List services; + + + public List getServices() { + return services; + } + + public void withServices(List services) { + this.services = services; + } +} diff --git a/plugins/osgi/src/test/java/org/apache/struts2/osgi/interceptor/OsgiInterceptorTest.java b/plugins/osgi/src/test/java/org/apache/struts2/osgi/interceptor/OsgiInterceptorTest.java index 9c4228079..e464d405e 100644 --- a/plugins/osgi/src/test/java/org/apache/struts2/osgi/interceptor/OsgiInterceptorTest.java +++ b/plugins/osgi/src/test/java/org/apache/struts2/osgi/interceptor/OsgiInterceptorTest.java @@ -18,6 +18,7 @@ */ package org.apache.struts2.osgi.interceptor; +import org.apache.struts2.osgi.action.ServiceAction; import org.easymock.EasyMock; import org.apache.struts2.osgi.host.OsgiHost; import org.osgi.framework.BundleContext; @@ -53,6 +54,28 @@ public class OsgiInterceptorTest extends TestCase { EasyMock.verify(bundleContextAware); } + public void testActionBundleContextAware() throws Exception { + ServletContext servletContext = EasyMock.createStrictMock(ServletContext.class); + BundleContext bundleContext = EasyMock.createStrictMock(BundleContext.class); + ActionInvocation actionInvocation = EasyMock.createStrictMock(ActionInvocation.class); + org.apache.struts2.osgi.action.BundleContextAware bundleContextAware = EasyMock.createStrictMock(org.apache.struts2.osgi.action.BundleContextAware.class); + + EasyMock.expect(servletContext.getAttribute(OsgiHost.OSGI_BUNDLE_CONTEXT)).andReturn(bundleContext); + EasyMock.expect(actionInvocation.getAction()).andReturn(bundleContextAware); + bundleContextAware.withBundleContext(bundleContext); + EasyMock.expect(actionInvocation.invoke()).andReturn(""); + + EasyMock.replay(bundleContextAware); + EasyMock.replay(servletContext); + EasyMock.replay(actionInvocation); + + OsgiInterceptor osgiInterceptor = new OsgiInterceptor(); + osgiInterceptor.setServletContext(servletContext); + osgiInterceptor.intercept(actionInvocation); + + EasyMock.verify(bundleContextAware); + } + public void testBundleContextAwareNegative() throws Exception { ServletContext servletContext = EasyMock.createStrictMock(ServletContext.class); ActionInvocation actionInvocation = EasyMock.createStrictMock(ActionInvocation.class); @@ -72,6 +95,25 @@ public class OsgiInterceptorTest extends TestCase { EasyMock.verify(bundleContextAware); } + public void testActionBundleContextAwareNegative() throws Exception { + ServletContext servletContext = EasyMock.createStrictMock(ServletContext.class); + ActionInvocation actionInvocation = EasyMock.createStrictMock(ActionInvocation.class); + org.apache.struts2.osgi.action.BundleContextAware bundleContextAware = EasyMock.createStrictMock(org.apache.struts2.osgi.action.BundleContextAware.class); + + EasyMock.expect(servletContext.getAttribute(OsgiHost.OSGI_BUNDLE_CONTEXT)).andReturn(null); + EasyMock.expect(actionInvocation.invoke()).andReturn(""); + + EasyMock.replay(bundleContextAware); + EasyMock.replay(servletContext); + EasyMock.replay(actionInvocation); + + OsgiInterceptor osgiInterceptor = new OsgiInterceptor(); + osgiInterceptor.setServletContext(servletContext); + osgiInterceptor.intercept(actionInvocation); + + EasyMock.verify(bundleContextAware); + } + public void testServiceAware() throws Exception { ServletContext servletContext = EasyMock.createStrictMock(ServletContext.class); BundleContext bundleContext = EasyMock.createStrictMock(BundleContext.class); @@ -100,4 +142,33 @@ public class OsgiInterceptorTest extends TestCase { assertNotNull(objects); assertSame(someObject, objects.get(0)); } + + public void testActionServiceAware() throws Exception { + ServletContext servletContext = EasyMock.createStrictMock(ServletContext.class); + BundleContext bundleContext = EasyMock.createStrictMock(BundleContext.class); + ActionInvocation actionInvocation = EasyMock.createStrictMock(ActionInvocation.class); + ServiceAction serviceAction = new ServiceAction(); + + //service refs + ServiceReference objectRef = EasyMock.createNiceMock(ServiceReference.class); + Object someObject = new Object(); + + EasyMock.expect(servletContext.getAttribute(OsgiHost.OSGI_BUNDLE_CONTEXT)).andReturn(bundleContext); + EasyMock.expect(actionInvocation.getAction()).andReturn(serviceAction); + EasyMock.expect(actionInvocation.invoke()).andReturn(""); + EasyMock.expect(bundleContext.getAllServiceReferences(Object.class.getName(), null)).andReturn(new ServiceReference[] {objectRef}); + EasyMock.expect(bundleContext.getService(objectRef)).andReturn(someObject); + + EasyMock.replay(bundleContext); + EasyMock.replay(servletContext); + EasyMock.replay(actionInvocation); + + OsgiInterceptor osgiInterceptor = new OsgiInterceptor(); + osgiInterceptor.setServletContext(servletContext); + osgiInterceptor.intercept(actionInvocation); + + List objects = serviceAction.getServices(); + assertNotNull(objects); + assertSame(someObject, objects.get(0)); + } } diff --git a/plugins/portlet/src/main/java/org/apache/struts2/portlet/action/PortletContextAware.java b/plugins/portlet/src/main/java/org/apache/struts2/portlet/action/PortletContextAware.java new file mode 100644 index 000000000..c34039794 --- /dev/null +++ b/plugins/portlet/src/main/java/org/apache/struts2/portlet/action/PortletContextAware.java @@ -0,0 +1,30 @@ +/* + * 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.portlet.action; + +import javax.portlet.PortletContext; + +/** + * @since 2.6 + */ +public interface PortletContextAware { + + void withPortletContext(PortletContext portletContext); + +} diff --git a/plugins/portlet/src/main/java/org/apache/struts2/portlet/action/PortletPreferencesAware.java b/plugins/portlet/src/main/java/org/apache/struts2/portlet/action/PortletPreferencesAware.java new file mode 100644 index 000000000..da20c023b --- /dev/null +++ b/plugins/portlet/src/main/java/org/apache/struts2/portlet/action/PortletPreferencesAware.java @@ -0,0 +1,39 @@ +/* + * 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.portlet.action; + +import javax.portlet.PortletPreferences; + + +/** + * All Actions that want to have access to the portlet preferences should + * implement this interface. If running in a servlet environment, an + * appropriate testing implementation will be provided. + * + * @since 2.6 + */ +public interface PortletPreferencesAware { + + /** + * Sets the HTTP request object in implementing classes. + * + * @param preferences the portlet preferences. + */ + void withPortletPreferences(PortletPreferences preferences); +} diff --git a/plugins/portlet/src/main/java/org/apache/struts2/portlet/action/PortletRequestAware.java b/plugins/portlet/src/main/java/org/apache/struts2/portlet/action/PortletRequestAware.java new file mode 100644 index 000000000..b7537fe87 --- /dev/null +++ b/plugins/portlet/src/main/java/org/apache/struts2/portlet/action/PortletRequestAware.java @@ -0,0 +1,30 @@ +/* + * 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.portlet.action; + +import javax.portlet.PortletRequest; + +/** + * @since 2.6 + */ +public interface PortletRequestAware { + + void withPortletRequest(PortletRequest request); + +} diff --git a/plugins/portlet/src/main/java/org/apache/struts2/portlet/action/PortletResponseAware.java b/plugins/portlet/src/main/java/org/apache/struts2/portlet/action/PortletResponseAware.java new file mode 100644 index 000000000..007a65bb5 --- /dev/null +++ b/plugins/portlet/src/main/java/org/apache/struts2/portlet/action/PortletResponseAware.java @@ -0,0 +1,30 @@ +/* + * 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.portlet.action; + +import javax.portlet.PortletResponse; + +/** + * @since 2.6 + */ +public interface PortletResponseAware { + + void withPortletResponse(PortletResponse response); + +} diff --git a/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptor.java b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptor.java index 63ac9c913..420042e1d 100644 --- a/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptor.java +++ b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptor.java @@ -21,8 +21,8 @@ package org.apache.struts2.portlet.interceptor; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.apache.struts2.StrutsStatics; import org.apache.struts2.interceptor.PrincipalAware; import org.apache.struts2.portlet.PortletConstants; @@ -33,11 +33,11 @@ import javax.portlet.PortletResponse; public class PortletAwareInterceptor extends AbstractInterceptor implements StrutsStatics { - private static final long serialVersionUID = 2476509721059587700L; - - private static final Logger LOG = LogManager.getLogger(PortletAwareInterceptor.class); + private static final long serialVersionUID = 2476509721059587700L; - /** + private static final Logger LOG = LogManager.getLogger(PortletAwareInterceptor.class); + + /** * Sets action properties based on the interfaces an action implements. Things like application properties, * parameters, session attributes, etc are set based on the implementing interface. * @@ -53,29 +53,65 @@ public class PortletAwareInterceptor extends AbstractInterceptor implements Stru ((PortletRequestAware) action).setPortletRequest(request); } + if (action instanceof org.apache.struts2.portlet.action.PortletRequestAware) { + PortletRequest request = (PortletRequest) context.get(PortletConstants.REQUEST); + ((org.apache.struts2.portlet.action.PortletRequestAware) action).withPortletRequest(request); + } + if (action instanceof PortletResponseAware) { PortletResponse response = (PortletResponse) context.get(PortletConstants.RESPONSE); ((PortletResponseAware) action).setPortletResponse(response); } + + if (action instanceof org.apache.struts2.portlet.action.PortletResponseAware) { + PortletResponse response = (PortletResponse) context.get(PortletConstants.RESPONSE); + ((org.apache.struts2.portlet.action.PortletResponseAware) action).withPortletResponse(response); + } + if (action instanceof PrincipalAware) { PortletRequest request = (PortletRequest) context.get(PortletConstants.REQUEST); ((PrincipalAware) action).setPrincipalProxy(new PortletPrincipalProxy(request)); } + + if (action instanceof org.apache.struts2.action.PrincipalAware) { + PortletRequest request = (PortletRequest) context.get(PortletConstants.REQUEST); + ((org.apache.struts2.action.PrincipalAware) action).withPrincipalProxy(new PortletPrincipalProxy(request)); + } + if (action instanceof PortletContextAware) { PortletContext portletContext = (PortletContext) context.get(StrutsStatics.STRUTS_PORTLET_CONTEXT); ((PortletContextAware) action).setPortletContext(portletContext); } + + if (action instanceof org.apache.struts2.portlet.action.PortletContextAware) { + PortletContext portletContext = (PortletContext) context.get(StrutsStatics.STRUTS_PORTLET_CONTEXT); + ((org.apache.struts2.portlet.action.PortletContextAware) action).withPortletContext(portletContext); + } + if (action instanceof PortletPreferencesAware) { - PortletRequest request = (PortletRequest) context.get(PortletConstants.REQUEST); - + PortletRequest request = (PortletRequest) context.get(PortletConstants.REQUEST); + // Check if running in a servlet environment if (request == null) { LOG.warn("This portlet preferences implementation should only be used during development"); - ((PortletPreferencesAware)action).setPortletPreferences(new ServletPortletPreferences(ActionContext.getContext().getSession())); + ((PortletPreferencesAware) action).setPortletPreferences(new ServletPortletPreferences(ActionContext.getContext().getSession())); } else { - ((PortletPreferencesAware)action).setPortletPreferences(request.getPreferences()); + ((PortletPreferencesAware) action).setPortletPreferences(request.getPreferences()); } } + + if (action instanceof org.apache.struts2.portlet.action.PortletPreferencesAware) { + PortletRequest request = (PortletRequest) context.get(PortletConstants.REQUEST); + + // Check if running in a servlet environment + if (request == null) { + LOG.warn("This portlet preferences implementation should only be used during development"); + ((org.apache.struts2.portlet.action.PortletPreferencesAware) action).withPortletPreferences(new ServletPortletPreferences(ActionContext.getContext().getSession())); + } else { + ((org.apache.struts2.portlet.action.PortletPreferencesAware) action).withPortletPreferences(request.getPreferences()); + } + } + return invocation.invoke(); } } diff --git a/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletContextAware.java b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletContextAware.java index c917a2a4c..5ea01856a 100644 --- a/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletContextAware.java +++ b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletContextAware.java @@ -20,8 +20,15 @@ package org.apache.struts2.portlet.interceptor; import javax.portlet.PortletContext; +/** + * @deprecated please use {@link org.apache.struts2.portlet.action.PortletContextAware} instead + */ +@Deprecated public interface PortletContextAware { - void setPortletContext(PortletContext portletContext); + /** + * @deprecated please use {@link org.apache.struts2.portlet.action.PortletContextAware#withPortletContext(PortletContext)} instead + */ + void setPortletContext(PortletContext portletContext); } diff --git a/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletPreferencesAware.java b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletPreferencesAware.java index e7b61d9c4..de39b25eb 100644 --- a/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletPreferencesAware.java +++ b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletPreferencesAware.java @@ -25,13 +25,18 @@ import javax.portlet.PortletPreferences; * All Actions that want to have access to the portlet preferences should * implement this interface. If running in a servlet environment, an * appropriate testing implementation will be provided. + * + * @deprecated please use {@link org.apache.struts2.portlet.action.PortletPreferencesAware} instead */ +@Deprecated public interface PortletPreferencesAware { /** * Sets the HTTP request object in implementing classes. * * @param prefs the portlet preferences. + * @deprecated please use {@link org.apache.struts2.portlet.action.PortletPreferencesAware#withPortletPreferences(PortletPreferences)} instead */ - public void setPortletPreferences(PortletPreferences prefs); + @Deprecated + void setPortletPreferences(PortletPreferences prefs); } diff --git a/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletRequestAware.java b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletRequestAware.java index 8fee62a1a..8903d9f45 100644 --- a/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletRequestAware.java +++ b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletRequestAware.java @@ -20,8 +20,16 @@ package org.apache.struts2.portlet.interceptor; import javax.portlet.PortletRequest; +/** + * @deprecated please use {@link org.apache.struts2.portlet.action.PortletRequestAware} instead + */ +@Deprecated public interface PortletRequestAware { - void setPortletRequest(PortletRequest request); + /** + * @deprecated please use {@link org.apache.struts2.portlet.action.PortletRequestAware#withPortletRequest(PortletRequest)} instead + */ + @Deprecated + void setPortletRequest(PortletRequest request); } diff --git a/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletResponseAware.java b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletResponseAware.java index 48cbb445a..dc4f71b93 100644 --- a/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletResponseAware.java +++ b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletResponseAware.java @@ -20,8 +20,16 @@ package org.apache.struts2.portlet.interceptor; import javax.portlet.PortletResponse; +/** + * @deprecated please use {@link org.apache.struts2.portlet.action.PortletResponseAware} instead + */ +@Deprecated public interface PortletResponseAware { + /** + * @deprecated please use {@link org.apache.struts2.portlet.action.PortletResponseAware#withPortletResponse(PortletResponse)} instead + */ + @Deprecated void setPortletResponse(PortletResponse response); } diff --git a/plugins/portlet/src/test/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptorTest.java b/plugins/portlet/src/test/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptorTest.java index 907ceaf9d..b9664c9e5 100644 --- a/plugins/portlet/src/test/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptorTest.java +++ b/plugins/portlet/src/test/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptorTest.java @@ -25,38 +25,77 @@ import org.apache.struts2.portlet.PortletConstants; import org.easymock.EasyMock; import javax.portlet.PortletRequest; +import javax.portlet.PortletResponse; import java.util.HashMap; import java.util.Map; public class PortletAwareInterceptorTest extends TestCase { - private PortletAwareInterceptor interceptor; - - protected void setUp() throws Exception { - super.setUp(); - interceptor = new PortletAwareInterceptor(); - } - - protected void tearDown() throws Exception { - super.tearDown(); - } - - public void testPortletRequestIsSet() throws Exception { - PortletRequest request = EasyMock.createMock(PortletRequest.class); - Map ctx = new HashMap(); - ctx.put(PortletConstants.REQUEST, request); - PortletRequestAware action = EasyMock.createMock(PortletRequestAware.class); - action.setPortletRequest(request); - - ActionInvocation invocation = EasyMock.createNiceMock(ActionInvocation.class); - EasyMock.expect(invocation.getInvocationContext()).andReturn(new ActionContext(ctx)); - EasyMock.expect(invocation.getAction()).andReturn(action); - - EasyMock.replay(action); - EasyMock.replay(invocation); - - interceptor.intercept(invocation); - - EasyMock.verify(action); - } + private PortletAwareInterceptor interceptor; + + protected void setUp() throws Exception { + super.setUp(); + interceptor = new PortletAwareInterceptor(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testPortletRequestIsSet() throws Exception { + PortletRequest request = EasyMock.createMock(PortletRequest.class); + Map ctx = new HashMap(); + ctx.put(PortletConstants.REQUEST, request); + PortletRequestAware action = EasyMock.createMock(PortletRequestAware.class); + action.setPortletRequest(request); + + ActionInvocation invocation = EasyMock.createNiceMock(ActionInvocation.class); + EasyMock.expect(invocation.getInvocationContext()).andReturn(new ActionContext(ctx)); + EasyMock.expect(invocation.getAction()).andReturn(action); + + EasyMock.replay(action); + EasyMock.replay(invocation); + + interceptor.intercept(invocation); + + EasyMock.verify(action); + } + + public void testActionPortletRequestAware() throws Exception { + PortletRequest request = EasyMock.createMock(PortletRequest.class); + Map ctx = new HashMap<>(); + ctx.put(PortletConstants.REQUEST, request); + org.apache.struts2.portlet.action.PortletRequestAware action = EasyMock.createMock(org.apache.struts2.portlet.action.PortletRequestAware.class); + action.withPortletRequest(request); + + ActionInvocation invocation = EasyMock.createNiceMock(ActionInvocation.class); + EasyMock.expect(invocation.getInvocationContext()).andReturn(new ActionContext(ctx)); + EasyMock.expect(invocation.getAction()).andReturn(action); + + EasyMock.replay(action); + EasyMock.replay(invocation); + + interceptor.intercept(invocation); + + EasyMock.verify(action); + } + + public void testActionPortletResponseAware() throws Exception { + PortletResponse response = EasyMock.createMock(PortletResponse.class); + Map ctx = new HashMap<>(); + ctx.put(PortletConstants.RESPONSE, response); + org.apache.struts2.portlet.action.PortletResponseAware action = EasyMock.createMock(org.apache.struts2.portlet.action.PortletResponseAware.class); + action.withPortletResponse(response); + + ActionInvocation invocation = EasyMock.createNiceMock(ActionInvocation.class); + EasyMock.expect(invocation.getInvocationContext()).andReturn(new ActionContext(ctx)); + EasyMock.expect(invocation.getAction()).andReturn(action); + + EasyMock.replay(action); + EasyMock.replay(invocation); + + interceptor.intercept(invocation); + + EasyMock.verify(action); + } }