From b608549933bd2a6fa954ec35beae11519a6632a2 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Tue, 25 Sep 2018 10:02:58 +0200 Subject: [PATCH] WW-4963 Implements new ServletContextAware interface and fixes actions that uses withServletContext instead of setServletContext --- .../showcase/source/ViewSourceAction.java | 4 +-- .../osgi/admin/actions/BundlesAction.java | 4 +-- .../struts2/action/ServletContextAware.java | 31 +++++++++++++++++++ .../interceptor/ServletConfigInterceptor.java | 6 ++++ .../struts2/util/ServletContextAware.java | 7 +++++ .../ServletConfigInterceptorTest.java | 16 ++++++++++ 6 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 core/src/main/java/org/apache/struts2/action/ServletContextAware.java 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/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/interceptor/ServletConfigInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java index 0312da9ff..970789623 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java +++ b/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java @@ -196,6 +196,12 @@ public class ServletConfigInterceptor extends AbstractInterceptor implements Str 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/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/ServletConfigInterceptorTest.java b/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java index 4bb8684d9..20057e235 100644 --- a/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java +++ b/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java @@ -326,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");