Author: rgielen
Date: Thu Feb  1 03:28:57 2007
New Revision: 502196

URL: http://svn.apache.org/viewvc?view=rev&rev=502196
Log:
WW-1696:
Refactored PrincipalProxy to interface and provided a servlet and a portlet 
based implementation

Added:
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/portlet/interceptor/PortletPrincipalProxy.java
    struts/struts2/trunk/core/src/main/java/org/apache/struts2/servlet/
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/servlet/interceptor/
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/servlet/interceptor/ServletPrincipalProxy.java
Modified:
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/PrincipalProxy.java
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java

Modified: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/PrincipalProxy.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/PrincipalProxy.java?view=diff&rev=502196&r1=502195&r2=502196
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/PrincipalProxy.java
 (original)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/PrincipalProxy.java
 Thu Feb  1 03:28:57 2007
@@ -25,21 +25,10 @@
 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 @@
      * @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
+     *             [EMAIL PROTECTED] 
org.apache.struts2.servlet.ServletRequestAware}, since this method will be 
dropped in future.
      */
-    public HttpServletRequest getRequest() {
-        return request;
-    }
+    HttpServletRequest getRequest();
 }

Modified: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java?view=diff&rev=502196&r1=502195&r2=502196
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java
 (original)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/ServletConfigInterceptor.java
 Thu Feb  1 03:28:57 2007
@@ -25,8 +25,12 @@
 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 @@
 
         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);

Added: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/portlet/interceptor/PortletPrincipalProxy.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/portlet/interceptor/PortletPrincipalProxy.java?view=auto&rev=502196
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/portlet/interceptor/PortletPrincipalProxy.java
 (added)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/portlet/interceptor/PortletPrincipalProxy.java
 Thu Feb  1 03:28:57 2007
@@ -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
+     *             [EMAIL PROTECTED] 
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");
+    }
+}

Added: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/servlet/interceptor/ServletPrincipalProxy.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/servlet/interceptor/ServletPrincipalProxy.java?view=auto&rev=502196
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/servlet/interceptor/ServletPrincipalProxy.java
 (added)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/servlet/interceptor/ServletPrincipalProxy.java
 Thu Feb  1 03:28:57 2007
@@ -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
+     *             [EMAIL PROTECTED] 
org.apache.struts2.servlet.ServletRequestAware}, since this method will be 
dropped in future.
+     */
+    public HttpServletRequest getRequest() {
+        return request;
+    }
+}


Reply via email to