Refactored PrincipalProxy to interface and provided a servlet and a portlet based implementation

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@502196 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
René Gielen
2007-02-01 11:28:57 +00:00
parent 41786a83eb
commit f31faacc4a
4 changed files with 166 additions and 31 deletions
@@ -25,21 +25,10 @@ import java.security.Principal;
import javax.servlet.http.HttpServletRequest;
/**
* Proxy class used together with PrincipalAware interface. It allows to get indirect access to
* HttpServletRequest Principal related methods.
*
* Proxy interface used together with PrincipalAware interface. It allows to get indirect access to
* HttpServletRequest or PortletRequest Principal related methods.
*/
public class PrincipalProxy {
private HttpServletRequest request;
/**
* Constructs a proxy
*
* @param request The underlying request
*/
public PrincipalProxy(HttpServletRequest request) {
this.request = request;
}
public interface PrincipalProxy {
/**
* True if the user is in the given role
@@ -47,43 +36,35 @@ public class PrincipalProxy {
* @param role The role
* @return True if the user is in that role
*/
public boolean isUserInRole(String role) {
return request.isUserInRole(role);
}
boolean isUserInRole(String role);
/**
* Gets the user principal
*
* @return The principal
*/
public Principal getUserPrincipal() {
return request.getUserPrincipal();
}
Principal getUserPrincipal();
/**
* Gets the user id
*
* @return The user id
*/
public String getRemoteUser() {
return request.getRemoteUser();
}
String getRemoteUser();
/**
* Is the request using https?
*
* @return True if using https
*/
public boolean isRequestSecure() {
return request.isSecure();
}
boolean isRequestSecure();
/**
* Gets the request
* Gets the request.
*
* @return The request
* @deprecated To obtain the HttpServletRequest in your action, use
* {@link org.apache.struts2.servlet.ServletRequestAware}, since this method will be dropped in future.
*/
public HttpServletRequest getRequest() {
return request;
}
HttpServletRequest getRequest();
}
@@ -25,8 +25,12 @@ import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.portlet.PortletRequest;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.servlet.interceptor.ServletPrincipalProxy;
import org.apache.struts2.portlet.PortletActionConstants;
import org.apache.struts2.portlet.interceptor.PortletPrincipalProxy;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionContext;
@@ -150,7 +154,14 @@ public class ServletConfigInterceptor extends AbstractInterceptor implements Str
if (action instanceof PrincipalAware) {
HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
((PrincipalAware) action).setPrincipalProxy(new PrincipalProxy(request));
Object portletRequest = context.get(PortletActionConstants.REQUEST);
if (portletRequest != null) {
// We are in portlet environment, so principal information resides in PortletRequest
((PrincipalAware) action).setPrincipalProxy(new PortletPrincipalProxy((PortletRequest) portletRequest));
} else {
// We are in servtlet environment, so principal information resides in HttpServletRequest
((PrincipalAware) action).setPrincipalProxy(new ServletPrincipalProxy(request));
}
}
if (action instanceof ServletContextAware) {
ServletContext servletContext = (ServletContext) context.get(SERVLET_CONTEXT);
@@ -0,0 +1,73 @@
package org.apache.struts2.portlet.interceptor;
import org.apache.struts2.interceptor.PrincipalProxy;
import javax.portlet.PortletRequest;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
/**
* PrincipalProxy implementation for using PortletRequest Principal related methods.
*/
public class PortletPrincipalProxy implements PrincipalProxy {
private PortletRequest request;
/**
* Constructs a proxy
*
* @param request The underlying request
*/
public PortletPrincipalProxy(PortletRequest request) {
this.request = request;
}
/**
* True if the user is in the given role
*
* @param role The role
* @return True if the user is in that role
*/
public boolean isUserInRole(String role) {
return request.isUserInRole(role);
}
/**
* Gets the user principal
*
* @return The principal
*/
public Principal getUserPrincipal() {
return request.getUserPrincipal();
}
/**
* Gets the user id
*
* @return The user id
*/
public String getRemoteUser() {
return request.getRemoteUser();
}
/**
* Is the request using https?
*
* @return True if using https
*/
public boolean isRequestSecure() {
return request.isSecure();
}
/**
* Gets the request.
*
* @return The request
* @throws UnsupportedOperationException not supported in this implementation.
* @deprecated To obtain the HttpServletRequest in your action, use
* {@link org.apache.struts2.servlet.ServletRequestAware}, since this method will be dropped in future.
*/
public HttpServletRequest getRequest() {
throw new UnsupportedOperationException("Usage of getRequest() method is deprecadet and not supported for this implementation");
}
}
@@ -0,0 +1,70 @@
package org.apache.struts2.servlet.interceptor;
import org.apache.struts2.interceptor.PrincipalProxy;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
/**
* PrincipalProxy implementation for using HttpServletRequest Principal related methods.
*/
public class ServletPrincipalProxy implements PrincipalProxy {
private HttpServletRequest request;
/**
* Constructs a proxy
*
* @param request The underlying request
*/
public ServletPrincipalProxy(HttpServletRequest request) {
this.request = request;
}
/**
* True if the user is in the given role
*
* @param role The role
* @return True if the user is in that role
*/
public boolean isUserInRole(String role) {
return request.isUserInRole(role);
}
/**
* Gets the user principal
*
* @return The principal
*/
public Principal getUserPrincipal() {
return request.getUserPrincipal();
}
/**
* Gets the user id
*
* @return The user id
*/
public String getRemoteUser() {
return request.getRemoteUser();
}
/**
* Is the request using https?
*
* @return True if using https
*/
public boolean isRequestSecure() {
return request.isSecure();
}
/**
* Gets the request.
*
* @return The request
* @deprecated To obtain the HttpServletRequest in your action, use
* {@link org.apache.struts2.servlet.ServletRequestAware}, since this method will be dropped in future.
*/
public HttpServletRequest getRequest() {
return request;
}
}