From 64e78003fb49c2a3637254757b79ba7614968dd9 Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Wed, 9 Jun 2010 08:22:37 +0000 Subject: [PATCH] 316254 Implement @DeclareRoles git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@1953 7e9141cc-0065-0410-87d8-b60c137991c4 --- VERSION.txt | 1 + .../annotations/AnnotationConfiguration.java | 1 + .../DeclareRolesAnnotationHandler.java | 89 +++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 jetty-annotations/src/main/java/org/eclipse/jetty/annotations/DeclareRolesAnnotationHandler.java diff --git a/VERSION.txt b/VERSION.txt index e4ff2dc581f..db29a577685 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -22,6 +22,7 @@ jetty-7.1.4-SNAPSHOT + 315925 Improved context xml configuration handling + 315995 Incorrect package name in system classes list + 316119 Fixed maxIdleTime for SocketEndPoint + + 316254 Implement @DeclareRoles + JETTY-547 Delay close after shutdown until request read + JETTY-1231 Support context request log handler diff --git a/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationConfiguration.java b/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationConfiguration.java index 4edb6f5e082..a26915b5c66 100644 --- a/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationConfiguration.java +++ b/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/AnnotationConfiguration.java @@ -56,6 +56,7 @@ public class AnnotationConfiguration extends AbstractConfiguration parser.registerAnnotationHandler("javax.annotation.PostConstruct", new PostConstructAnnotationHandler(context)); parser.registerAnnotationHandler("javax.annotation.PreDestroy", new PreDestroyAnnotationHandler(context)); parser.registerAnnotationHandler("javax.annotation.security.RunAs", new RunAsAnnotationHandler(context)); + parser.registerAnnotationHandler("javax.annotation.security.DeclareRoles", new DeclareRolesAnnotationHandler(context)); ClassInheritanceHandler classHandler = new ClassInheritanceHandler(); parser.registerClassHandler(classHandler); diff --git a/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/DeclareRolesAnnotationHandler.java b/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/DeclareRolesAnnotationHandler.java new file mode 100644 index 00000000000..50c5bdcd2f8 --- /dev/null +++ b/jetty-annotations/src/main/java/org/eclipse/jetty/annotations/DeclareRolesAnnotationHandler.java @@ -0,0 +1,89 @@ +// ======================================================================== +// Copyright (c) 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.HashSet; +import java.util.List; +import java.util.Set; + + +import org.eclipse.jetty.annotations.AnnotationParser.AnnotationHandler; +import org.eclipse.jetty.annotations.AnnotationParser.ListValue; +import org.eclipse.jetty.annotations.AnnotationParser.SimpleValue; +import org.eclipse.jetty.annotations.AnnotationParser.Value; +import org.eclipse.jetty.security.ConstraintSecurityHandler; +import org.eclipse.jetty.util.log.Log; +import org.eclipse.jetty.webapp.WebAppContext; + +public class DeclareRolesAnnotationHandler implements AnnotationHandler +{ + protected WebAppContext _wac; + + public DeclareRolesAnnotationHandler (WebAppContext wac) + { + _wac = wac; + } + + /** + * A DeclareRoles annotation is equivalent to a in web.xml. + * + * @see org.eclipse.jetty.annotations.AnnotationParser.AnnotationHandler#handleClass(java.lang.String, int, int, java.lang.String, java.lang.String, java.lang.String[], java.lang.String, java.util.List) + */ + public void handleClass(String className, int version, int access, String signature, String superName, String[] interfaces, String annotation, + List values) + { + //Add the role names to the list of roles for the webapp + Set roles = new HashSet(); + + try + { + Set existing = ((ConstraintSecurityHandler)_wac.getSecurityHandler()).getRoles(); + roles.addAll(existing); + + if (values != null && values.size() == 1) + { + Value v = values.get(0); + if (v instanceof SimpleValue) + { + roles.add((String)((SimpleValue)v).getValue()); + } + else if (v instanceof ListValue) + { + for (Value vv:((ListValue)v).getList()) + { + roles.add((String)((SimpleValue)vv).getValue()); + } + } + } + } + catch (Exception e) + { + Log.warn(e); + } + } + + public void handleField(String className, String fieldName, int access, String fieldType, String signature, Object value, String annotation, + List values) + { + Log.warn ("@DeclareRoles annotation not applicable for field: "+className+"."+fieldName); + } + + public void handleMethod(String className, String methodName, int access, String desc, String signature, String[] exceptions, String annotation, + List values) + { + Log.warn ("@DeclareRoles annotation not applicable for method: "+className+"."+methodName); + } + +}