From d38efae5f09776e45dd0e0faaa8bf1cf2ab04dad Mon Sep 17 00:00:00 2001
From: Lukasz Lenart
Date: Wed, 19 Sep 2018 09:20:42 +0200
Subject: [PATCH 01/14] WW-4963 Implements new SessionAware interface that uses
withSession instead of setSession
---
.../apache/struts2/action/SessionAware.java | 40 +++++++++++++++++++
.../interceptor/ServletConfigInterceptor.java | 4 ++
.../struts2/interceptor/SessionAware.java | 4 ++
.../ServletConfigInterceptorTest.java | 16 ++++++++
4 files changed, 64 insertions(+)
create mode 100644 core/src/main/java/org/apache/struts2/action/SessionAware.java
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/ServletConfigInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java
index 813a87d91..bd017d07b 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java
@@ -149,6 +149,10 @@ public class ServletConfigInterceptor extends AbstractInterceptor implements Str
((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"));
}
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/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java b/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java
index c394d2b90..34353c87f 100644
--- a/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java
+++ b/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java
@@ -128,6 +128,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);
From 3820eae31f9db2016ac8d9576ed2346cda41fc9b Mon Sep 17 00:00:00 2001
From: Lukasz Lenart
Date: Wed, 19 Sep 2018 16:26:24 +0200
Subject: [PATCH 02/14] WW-4963 Implements new ServletRequestAware interface
that uses withRequest instead of setRequest
---
.../struts2/action/ServletRequestAware.java | 39 +++++++++++++++++++
.../interceptor/ServletConfigInterceptor.java | 5 +++
.../interceptor/ServletRequestAware.java | 4 ++
.../ServletConfigInterceptorTest.java | 16 ++++++++
4 files changed, 64 insertions(+)
create mode 100644 core/src/main/java/org/apache/struts2/action/ServletRequestAware.java
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/interceptor/ServletConfigInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java
index bd017d07b..e5b9aa432 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java
@@ -128,6 +128,11 @@ 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);
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/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java b/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java
index 34353c87f..dfecbfe15 100644
--- a/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java
+++ b/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java
@@ -64,6 +64,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);
From e0fb8031ce99dcb893dbc0b223845235eca7721a Mon Sep 17 00:00:00 2001
From: Lukasz Lenart
Date: Wed, 19 Sep 2018 16:38:37 +0200
Subject: [PATCH 03/14] WW-4963 Implements new ServletResponseAware interface
that uses withResponseServlet instead of setResponseServlet
---
.../struts2/action/ServletResponseAware.java | 39 +++++++++++++++++++
.../interceptor/ServletConfigInterceptor.java | 5 +++
.../interceptor/ServletResponseAware.java | 4 ++
.../ServletConfigInterceptorTest.java | 16 ++++++++
4 files changed, 64 insertions(+)
create mode 100644 core/src/main/java/org/apache/struts2/action/ServletResponseAware.java
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/interceptor/ServletConfigInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java
index e5b9aa432..e6b8a11cc 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java
@@ -138,6 +138,11 @@ public class ServletConfigInterceptor extends AbstractInterceptor implements Str
((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);
}
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/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java b/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java
index dfecbfe15..4fc43ebd3 100644
--- a/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java
+++ b/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java
@@ -96,6 +96,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);
From dc7138ee2ea42194ee51cc886b1598021d536f0f Mon Sep 17 00:00:00 2001
From: Lukasz Lenart
Date: Wed, 19 Sep 2018 16:46:27 +0200
Subject: [PATCH 04/14] WW-4963 Implements new ApplicationAware interface that
uses withApplication instead of setApplication
---
.../struts2/action/ApplicationAware.java | 38 ++++++++++++++
.../struts2/interceptor/ApplicationAware.java | 4 ++
.../interceptor/ServletConfigInterceptor.java | 4 ++
.../ServletConfigInterceptorTest.java | 51 ++++++++++++-------
4 files changed, 80 insertions(+), 17 deletions(-)
create mode 100644 core/src/main/java/org/apache/struts2/action/ApplicationAware.java
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/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/ServletConfigInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java
index e6b8a11cc..24a3d81b6 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java
@@ -155,6 +155,10 @@ public class ServletConfigInterceptor extends AbstractInterceptor implements Str
((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());
}
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 4fc43ebd3..7187a1d95 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,32 @@
*/
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.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 {
@@ -192,6 +193,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);
@@ -200,7 +217,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);
@@ -228,8 +245,8 @@ 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());
}
From 384d418039998db6957b49cec806a7a0b2fd03a7 Mon Sep 17 00:00:00 2001
From: Lukasz Lenart
Date: Tue, 25 Sep 2018 08:10:36 +0200
Subject: [PATCH 05/14] WW-4963 Implements new PrincipalAware interface that
uses withPrincipalProxy instead of setPrincipalProxy
---
.../apache/struts2/action/PrincipalAware.java | 34 +++++++++++
.../struts2/interceptor/PrincipalAware.java | 7 +++
.../interceptor/ServletConfigInterceptor.java | 9 +++
.../ServletConfigInterceptorTest.java | 59 +++++++++++++++++++
.../interceptor/PortletAwareInterceptor.java | 6 ++
5 files changed, 115 insertions(+)
create mode 100644 core/src/main/java/org/apache/struts2/action/PrincipalAware.java
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/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/ServletConfigInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java
index 24a3d81b6..302fcba66 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java
@@ -178,6 +178,15 @@ 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);
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 7187a1d95..4306aaa8b 100644
--- a/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java
+++ b/core/src/test/java/org/apache/struts2/interceptor/ServletConfigInterceptorTest.java
@@ -229,6 +229,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();
@@ -251,6 +271,28 @@ public class ServletConfigInterceptorTest extends StrutsInternalTestCase {
}
+ 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());
+
+ }
+
public void testServletContextAware() throws Exception {
ServletContextAware mock = (ServletContextAware) createMock(ServletContextAware.class);
@@ -305,4 +347,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/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..53b2e95fe 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
@@ -61,6 +61,12 @@ public class PortletAwareInterceptor extends AbstractInterceptor implements Stru
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);
From 01016b458e7af42b8f645fa6d35c6e929e3fa4c0 Mon Sep 17 00:00:00 2001
From: Lukasz Lenart
Date: Tue, 25 Sep 2018 08:23:45 +0200
Subject: [PATCH 06/14] WW-4963 Implements new BundleContextAware interface
that uses withBundleContext instead of setBundleContext
---
.../main/java/actions/osgi/BundlesAction.java | 4 +-
.../osgi/action/BundleContextAware.java | 31 ++++++++++++++
.../osgi/interceptor/BundleContextAware.java | 7 ++++
.../osgi/interceptor/OsgiInterceptor.java | 4 ++
.../osgi/interceptor/OsgiInterceptorTest.java | 41 +++++++++++++++++++
5 files changed, 85 insertions(+), 2 deletions(-)
create mode 100644 plugins/osgi/src/main/java/org/apache/struts2/osgi/action/BundleContextAware.java
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/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/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..11dd1f9a0 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
@@ -51,6 +51,10 @@ public class OsgiInterceptor extends AbstractInterceptor {
if (action instanceof BundleContextAware)
((BundleContextAware)action).setBundleContext(bundleContext);
+ if (action instanceof org.apache.struts2.osgi.action.BundleContextAware) {
+ ((org.apache.struts2.osgi.action.BundleContextAware) action).withBundleContext(bundleContext);
+ }
+
//inject service implementations
if (action instanceof ServiceAware) {
Type[] types = action.getClass().getGenericInterfaces();
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..96f287569 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
@@ -53,6 +53,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 +94,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);
From 23e9c88135116e420348b2f9867a81f0de2c63e6 Mon Sep 17 00:00:00 2001
From: Lukasz Lenart
Date: Tue, 25 Sep 2018 08:32:54 +0200
Subject: [PATCH 07/14] WW-4963 Implements new CookiesAware interface that uses
withCookies instead of setCookiesMap
---
.../apache/struts2/action/CookiesAware.java | 40 +++++++++++
.../interceptor/CookieInterceptor.java | 8 ++-
.../struts2/interceptor/CookiesAware.java | 4 ++
.../interceptor/CookieInterceptorTest.java | 68 +++++++++++++++++++
4 files changed, 118 insertions(+), 2 deletions(-)
create mode 100644 core/src/main/java/org/apache/struts2/action/CookiesAware.java
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/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/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; }
+ }
+
}
From 42245be546c8f8db57ca1b129022ed5a5c05ae89 Mon Sep 17 00:00:00 2001
From: Lukasz Lenart
Date: Tue, 25 Sep 2018 08:36:32 +0200
Subject: [PATCH 08/14] WW-4963 Implements new PortletContextAware interface
that uses withPortletContext instead of setPortletContext
---
.../portlet/action/PortletContextAware.java | 30 +++++++++++++++++++
.../interceptor/PortletAwareInterceptor.java | 6 ++++
.../interceptor/PortletContextAware.java | 9 +++++-
3 files changed, 44 insertions(+), 1 deletion(-)
create mode 100644 plugins/portlet/src/main/java/org/apache/struts2/portlet/action/PortletContextAware.java
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/interceptor/PortletAwareInterceptor.java b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptor.java
index 53b2e95fe..8fedf5d51 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
@@ -71,6 +71,12 @@ public class PortletAwareInterceptor extends AbstractInterceptor implements Stru
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);
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);
}
From f59860026c890f0e2dcc3edea8a41f0e989027f9 Mon Sep 17 00:00:00 2001
From: Lukasz Lenart
Date: Tue, 25 Sep 2018 08:40:27 +0200
Subject: [PATCH 09/14] WW-4963 Implements new PortletPreferencesAware
interface that uses withPortletPreferences instead of setPortletPreferences
---
.../action/PortletPreferencesAware.java | 39 +++++++++++++++++++
.../interceptor/PortletAwareInterceptor.java | 31 ++++++++++-----
.../interceptor/PortletPreferencesAware.java | 7 +++-
3 files changed, 67 insertions(+), 10 deletions(-)
create mode 100644 plugins/portlet/src/main/java/org/apache/struts2/portlet/action/PortletPreferencesAware.java
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/interceptor/PortletAwareInterceptor.java b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptor.java
index 8fedf5d51..a2be6a7c5 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.
*
@@ -78,16 +78,29 @@ public class PortletAwareInterceptor extends AbstractInterceptor implements Stru
}
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/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);
}
From ec4a44567d6470ed1a9f457d857e8ce4ac4c3e09 Mon Sep 17 00:00:00 2001
From: Lukasz Lenart
Date: Tue, 25 Sep 2018 08:51:16 +0200
Subject: [PATCH 10/14] WW-4963 Implements new PortletRequestAware interface
that uses withPortletRequest instead of setPortletRequest
---
.../portlet/action/PortletRequestAware.java | 30 ++++++++
.../interceptor/PortletAwareInterceptor.java | 5 ++
.../interceptor/PortletRequestAware.java | 10 ++-
.../PortletAwareInterceptorTest.java | 77 ++++++++++++-------
4 files changed, 92 insertions(+), 30 deletions(-)
create mode 100644 plugins/portlet/src/main/java/org/apache/struts2/portlet/action/PortletRequestAware.java
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/interceptor/PortletAwareInterceptor.java b/plugins/portlet/src/main/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptor.java
index a2be6a7c5..8c7f5857a 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
@@ -53,6 +53,11 @@ 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);
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/test/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptorTest.java b/plugins/portlet/src/test/java/org/apache/struts2/portlet/interceptor/PortletAwareInterceptorTest.java
index 907ceaf9d..4443411f4 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
@@ -30,33 +30,52 @@ 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);
+ }
}
From 5428252d5789c701174949d130e3e44cc7f99013 Mon Sep 17 00:00:00 2001
From: Lukasz Lenart
Date: Tue, 25 Sep 2018 09:03:29 +0200
Subject: [PATCH 11/14] WW-4963 Implements new PortletResponseAware interface
with a test that uses withPortletResponse instead of setPortletReponse
---
.../portlet/action/PortletResponseAware.java | 30 +++++++++++++++++++
.../interceptor/PortletAwareInterceptor.java | 6 ++++
.../interceptor/PortletResponseAware.java | 8 +++++
.../PortletAwareInterceptorTest.java | 20 +++++++++++++
4 files changed, 64 insertions(+)
create mode 100644 plugins/portlet/src/main/java/org/apache/struts2/portlet/action/PortletResponseAware.java
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 8c7f5857a..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
@@ -62,6 +62,12 @@ public class PortletAwareInterceptor extends AbstractInterceptor implements Stru
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));
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 4443411f4..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,6 +25,7 @@ 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;
@@ -78,4 +79,23 @@ public class PortletAwareInterceptorTest extends TestCase {
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);
+ }
}
From 56fc731670167878e6b42fdd3a113e62dc871a1d Mon Sep 17 00:00:00 2001
From: Lukasz Lenart
Date: Tue, 25 Sep 2018 09:44:51 +0200
Subject: [PATCH 12/14] WW-4963 Implements new ParametersAware interface that
uses withParameters instead of setHttpParameters and cleans up logic a bit
---
.../struts2/action/ParametersAware.java | 41 +++++++++++++++++++
.../interceptor/HttpParametersAware.java | 6 +++
.../struts2/interceptor/ParameterAware.java | 2 +-
.../struts2/interceptor/RequestAware.java | 6 +++
.../interceptor/ServletConfigInterceptor.java | 7 +++-
.../ServletConfigInterceptorTest.java | 17 ++++++++
6 files changed, 77 insertions(+), 2 deletions(-)
create mode 100644 core/src/main/java/org/apache/struts2/action/ParametersAware.java
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);
From 8cf787b39c1077f8db47c7f1bd7d91bfcc78c222 Mon Sep 17 00:00:00 2001
From: Lukasz Lenart
Date: Tue, 25 Sep 2018 09:53:50 +0200
Subject: [PATCH 13/14] WW-4963 Implements new ServiceAware interface that uses
withServices instead of setServices
---
.../struts2/osgi/action/ServiceAware.java | 34 ++++++
.../osgi/interceptor/OsgiInterceptor.java | 109 ++++++++++++------
.../osgi/interceptor/ServiceAware.java | 6 +
.../struts2/osgi/action/ServiceAction.java | 34 ++++++
.../osgi/interceptor/OsgiInterceptorTest.java | 30 +++++
5 files changed, 180 insertions(+), 33 deletions(-)
create mode 100644 plugins/osgi/src/main/java/org/apache/struts2/osgi/action/ServiceAware.java
create mode 100644 plugins/osgi/src/test/java/org/apache/struts2/osgi/action/ServiceAction.java
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/OsgiInterceptor.java b/plugins/osgi/src/main/java/org/apache/struts2/osgi/interceptor/OsgiInterceptor.java
index 11dd1f9a0..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,39 +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();
+ }
- if (action instanceof org.apache.struts2.osgi.action.BundleContextAware) {
- ((org.apache.struts2.osgi.action.BundleContextAware) action).withBundleContext(bundleContext);
+ private void injectBundleContext(Object action) {
+ if (action instanceof BundleContextAware)
+ ((BundleContextAware) action).setBundleContext(bundleContext);
+
+ 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);
+ }
+ }
+ }
+ }
+ }
}
+ }
+ }
- //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 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