From 384d418039998db6957b49cec806a7a0b2fd03a7 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Tue, 25 Sep 2018 08:10:36 +0200 Subject: [PATCH] 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);