Commits

Anonymous committed c9248f9

Added slightly modified classes from WW-626.

git-svn-id: http://svn.opensymphony.com/svn/webwork/trunk@577573baa09-0c28-0410-bef9-dab3c582ae83

Comments (0)

Files changed (3)

src/java/com/opensymphony/webwork/interceptor/PrincipalAware.java

+package com.opensymphony.webwork.interceptor;
+
+/**
+ * Actions that want access to the Principal information from HttpServletRequest object
+ * should implement this interface.
+ *
+ * <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>
+ *
+ * @see PrincipalInterceptor
+ *
+ * @author Remigijus Bauzys
+ * @version $Revision$
+ */
+public interface PrincipalAware {
+    void setPrincipalProxy(PrincipalProxy principalProxy);
+}

src/java/com/opensymphony/webwork/interceptor/PrincipalInterceptor.java

+package com.opensymphony.webwork.interceptor;
+
+import com.opensymphony.webwork.ServletActionContext;
+import com.opensymphony.xwork.Action;
+import com.opensymphony.xwork.ActionInvocation;
+import com.opensymphony.xwork.interceptor.AroundInterceptor;
+
+/**
+ * PrincipalInterceptor gives access to principal and roles information from action, without
+ * making action class tied to HttpServlerRequest.
+ *
+ * @author Remigijus Bauzys
+ * @version $Revision$
+ */
+public class PrincipalInterceptor extends AroundInterceptor {
+    protected void before(ActionInvocation invocation) throws Exception {
+        Action action = invocation.getAction();
+
+        if (action instanceof PrincipalAware) {
+            ((PrincipalAware) action).setPrincipalProxy(new PrincipalProxy(ServletActionContext.getRequest()));
+        }
+    }
+
+    protected void after(ActionInvocation dispatcher, String result) throws Exception {
+    }
+}

src/java/com/opensymphony/webwork/interceptor/PrincipalProxy.java

+package com.opensymphony.webwork.interceptor;
+
+import javax.servlet.http.HttpServletRequest;
+import java.security.Principal;
+
+/**
+ * Proxy class used together with PrincipalAware interface. It allows to get indirect access to
+ * HttpServletRequest Principal related methods.
+ *
+ * @author Remigijus Bauzys
+ * @version $Revision$
+ */
+public class PrincipalProxy {
+    private HttpServletRequest request;
+
+    public PrincipalProxy(HttpServletRequest request) {
+        this.request = request;
+    }
+
+    public boolean isUserInRole(String role) {
+        return request.isUserInRole(role);
+    }
+
+    public Principal getUserPrincipal() {
+        return request.getUserPrincipal();
+    }
+
+    public String getRemoteUser() {
+        return request.getRemoteUser();
+    }
+
+    public boolean isRequestSecure() {
+        return request.isSecure();
+    }
+
+    public HttpServletRequest getRequest() {
+        return request;
+    }
+}