[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-commit] r1274 - in jetty/branches/jetty-8: jetty-annotations/src/main/java/org/eclipse/jetty/annotations jetty-annotations/src/test/java/org/eclipse/jetty/annotations jetty-servlet/src/main/java/org/eclipse/jetty/servlet

Author: jbartel
Date: 2010-02-17 22:25:47 -0500 (Wed, 17 Feb 2010)
New Revision: 1274

Added:
   jetty/branches/jetty-8/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/DeclareRolesAnnotationHandler.java
Modified:
   jetty/branches/jetty-8/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/WebAppDecoratorWrapper.java
   jetty/branches/jetty-8/jetty-annotations/src/test/java/org/eclipse/jetty/annotations/ServletC.java
   jetty/branches/jetty-8/jetty-annotations/src/test/java/org/eclipse/jetty/annotations/TestServletAnnotations.java
   jetty/branches/jetty-8/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java
Log:
Implement @DeclareRoles.


Added: jetty/branches/jetty-8/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/DeclareRolesAnnotationHandler.java
===================================================================
--- jetty/branches/jetty-8/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/DeclareRolesAnnotationHandler.java	                        (rev 0)
+++ jetty/branches/jetty-8/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/DeclareRolesAnnotationHandler.java	2010-02-18 03:25:47 UTC (rev 1274)
@@ -0,0 +1,72 @@
+// ========================================================================
+// Copyright (c) 2006-2010 Mort Bay Consulting Pty. Ltd.
+// ------------------------------------------------------------------------
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the Eclipse Public License v1.0
+// and Apache License v2.0 which accompanies this distribution.
+// The Eclipse Public License is available at 
+// http://www.eclipse.org/legal/epl-v10.html
+// The Apache License v2.0 is available at
+// http://www.opensource.org/licenses/apache2.0.php
+// You may elect to redistribute this code under either of these licenses. 
+// ========================================================================
+
+package org.eclipse.jetty.annotations;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.servlet.Servlet;
+
+import javax.annotation.security.DeclareRoles;
+import org.eclipse.jetty.annotations.AnnotationIntrospector.AbstractIntrospectableAnnotationHandler;
+import org.eclipse.jetty.security.ConstraintSecurityHandler;
+import org.eclipse.jetty.webapp.WebAppContext;
+
+/**
+ * DeclaresRolesAnnotationHandler
+ *
+ *
+ */
+public class DeclareRolesAnnotationHandler extends AbstractIntrospectableAnnotationHandler
+{
+
+    protected WebAppContext _wac;
+    
+    /**
+     * @param introspectAncestors
+     */
+    public DeclareRolesAnnotationHandler(WebAppContext context)
+    {
+        super(false);
+        _wac = context;
+    }
+ 
+
+    /** 
+     * @see org.eclipse.jetty.annotations.AnnotationIntrospector.AbstractIntrospectableAnnotationHandler#doHandle(java.lang.Class)
+     */
+    public void doHandle(Class clazz)
+    {
+        if (!Servlet.class.isAssignableFrom(clazz))
+            return; //only applicable on javax.servlet.Servlet derivatives
+        
+        DeclareRoles declareRoles = (DeclareRoles) clazz.getAnnotation(DeclareRoles.class);
+        if (declareRoles == null)
+            return;
+        
+        String[] roles = declareRoles.value();
+        
+        if (roles != null && roles.length > 0)
+        {
+            HashSet<String> union = new HashSet<String>();
+            Set<String> existing = ((ConstraintSecurityHandler)_wac.getSecurityHandler()).getRoles();
+            if (existing != null)
+                union.addAll(existing);
+            union.addAll(Arrays.asList(roles));
+            ((ConstraintSecurityHandler)_wac.getSecurityHandler()).setRoles(union);
+        }
+    }
+
+}


Property changes on: jetty/branches/jetty-8/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/DeclareRolesAnnotationHandler.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:keywords
   + Date Author Id Revision HeadURL
Added: svn:eol-style
   + native

Modified: jetty/branches/jetty-8/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/WebAppDecoratorWrapper.java
===================================================================
--- jetty/branches/jetty-8/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/WebAppDecoratorWrapper.java	2010-02-18 01:02:29 UTC (rev 1273)
+++ jetty/branches/jetty-8/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/WebAppDecoratorWrapper.java	2010-02-18 03:25:47 UTC (rev 1274)
@@ -47,6 +47,7 @@
         _introspector.registerHandler(new PreDestroyAnnotationHandler(context));
         _introspector.registerHandler(new ServletSecurityAnnotationHandler(context));
         _introspector.registerHandler(new MultiPartConfigAnnotationHandler(context));
+        _introspector.registerHandler(new DeclareRolesAnnotationHandler(context));
     }
 
     /* ------------------------------------------------------------ */

Modified: jetty/branches/jetty-8/jetty-annotations/src/test/java/org/eclipse/jetty/annotations/ServletC.java
===================================================================
--- jetty/branches/jetty-8/jetty-annotations/src/test/java/org/eclipse/jetty/annotations/ServletC.java	2010-02-18 01:02:29 UTC (rev 1273)
+++ jetty/branches/jetty-8/jetty-annotations/src/test/java/org/eclipse/jetty/annotations/ServletC.java	2010-02-18 03:25:47 UTC (rev 1274)
@@ -1,5 +1,5 @@
 //  ========================================================================
-// Copyright (c) 2008-2009 Mort Bay Consulting Pty. Ltd.
+// Copyright (c) 2008-2010 Mort Bay Consulting Pty. Ltd.
 // ------------------------------------------------------------------------
 // All rights reserved. This program and the accompanying materials
 // are made available under the terms of the Eclipse Public License v1.0
@@ -17,6 +17,7 @@
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 import javax.annotation.Resource;
+import javax.annotation.security.DeclareRoles;
 import javax.annotation.security.RunAs;
 import javax.servlet.ServletException;
 import javax.servlet.annotation.HttpConstraint;
@@ -31,7 +32,7 @@
 
 
 
-
+@DeclareRoles({"alice"})
 @WebServlet(urlPatterns = { "/foo/*", "/bah/*" }, name="CServlet", initParams={@WebInitParam(name="x", value="y")}, loadOnStartup=2, asyncSupported=false)
 @MultipartConfig(fileSizeThreshold=1000, maxFileSize=2000, maxRequestSize=3000)
 @RunAs("admin")

Modified: jetty/branches/jetty-8/jetty-annotations/src/test/java/org/eclipse/jetty/annotations/TestServletAnnotations.java
===================================================================
--- jetty/branches/jetty-8/jetty-annotations/src/test/java/org/eclipse/jetty/annotations/TestServletAnnotations.java	2010-02-18 01:02:29 UTC (rev 1273)
+++ jetty/branches/jetty-8/jetty-annotations/src/test/java/org/eclipse/jetty/annotations/TestServletAnnotations.java	2010-02-18 03:25:47 UTC (rev 1274)
@@ -15,6 +15,8 @@
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Arrays;
+import java.util.HashSet;
 
 import org.eclipse.jetty.annotations.AnnotationParser.DiscoverableAnnotationHandler;
 import org.eclipse.jetty.annotations.AnnotationParser.Value;
@@ -22,6 +24,7 @@
 import org.eclipse.jetty.annotations.AnnotationParser.SimpleValue;
 import org.eclipse.jetty.plus.annotation.LifeCycleCallbackCollection;
 import org.eclipse.jetty.plus.annotation.RunAsCollection;
+import org.eclipse.jetty.security.ConstraintSecurityHandler;
 import org.eclipse.jetty.servlet.ServletHolder;
 import org.eclipse.jetty.servlet.ServletMapping;
 import org.eclipse.jetty.webapp.WebAppContext;
@@ -80,4 +83,18 @@
         assertEquals(2,holders[0].getInitOrder());
         assertFalse(holders[0].isAsyncSupported());
     }
+    
+    public void testDeclareRoles ()
+    throws Exception
+    { 
+        WebAppContext wac = new WebAppContext();
+        ConstraintSecurityHandler sh = new ConstraintSecurityHandler();
+        wac.setSecurityHandler(sh);
+        sh.setRoles(new HashSet<String>(Arrays.asList(new String[]{"humpty", "dumpty"})));
+        DeclareRolesAnnotationHandler handler = new DeclareRolesAnnotationHandler(wac);
+        handler.doHandle(ServletC.class);
+        assertTrue(sh.getRoles().contains("alice"));
+        assertTrue(sh.getRoles().contains("humpty"));
+        assertTrue(sh.getRoles().contains("dumpty"));
+    }
 }

Modified: jetty/branches/jetty-8/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java
===================================================================
--- jetty/branches/jetty-8/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java	2010-02-18 01:02:29 UTC (rev 1273)
+++ jetty/branches/jetty-8/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletContextHandler.java	2010-02-18 03:25:47 UTC (rev 1274)
@@ -39,6 +39,7 @@
 import javax.servlet.FilterRegistration.Dynamic;
 import javax.servlet.descriptor.JspConfigDescriptor;
 
+import org.eclipse.jetty.security.ConstraintAware;
 import org.eclipse.jetty.security.ConstraintSecurityHandler;
 import org.eclipse.jetty.security.SecurityHandler;
 import org.eclipse.jetty.server.Dispatcher;
@@ -815,8 +816,15 @@
                 throw new UnsupportedOperationException();
            
             //Get a reference to the SecurityHandler, which must be ConstraintAware
-            if (_securityHandler != null && _securityHandler instanceof ConstraintSecurityHandler)
-                ((ConstraintSecurityHandler)_securityHandler).setRoles(new HashSet(Arrays.asList(roleNames)));          
+            if (_securityHandler != null && _securityHandler instanceof ConstraintAware)
+            {
+                HashSet<String> union = new HashSet<String>();
+                Set<String> existing = ((ConstraintAware)_securityHandler).getRoles();
+                if (existing != null)
+                    union.addAll(existing);
+                union.addAll(Arrays.asList(roleNames));
+                ((ConstraintSecurityHandler)_securityHandler).setRoles(union);
+            }
         }
     }