mirror of
https://github.com/apache/struts.git
synced 2026-08-07 15:46:57 +00:00
WW-4963 Implements new PrincipalAware interface
that uses withPrincipalProxy instead of setPrincipalProxy
This commit is contained in:
@@ -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);
|
||||
|
||||
}
|
||||
@@ -25,7 +25,14 @@ package org.apache.struts2.interceptor;
|
||||
* <p>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.</p>
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+6
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user