|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| SecurityAnnotationAttributes.java | 50% | 100% | 100% | 93.3% |
|
||||||||||||||
| 1 | /* Copyright 2004, 2005 Acegi Technology Pty Limited | |
| 2 | * | |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 | * you may not use this file except in compliance with the License. | |
| 5 | * You may obtain a copy of the License at | |
| 6 | * | |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 | * | |
| 9 | * Unless required by applicable law or agreed to in writing, software | |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 | * See the License for the specific language governing permissions and | |
| 13 | * limitations under the License. | |
| 14 | */ | |
| 15 | package org.acegisecurity.annotation; | |
| 16 | ||
| 17 | import java.lang.annotation.Annotation; | |
| 18 | import java.lang.reflect.Field; | |
| 19 | import java.lang.reflect.Method; | |
| 20 | import java.util.Collection; | |
| 21 | import java.util.HashSet; | |
| 22 | import java.util.Set; | |
| 23 | ||
| 24 | import org.acegisecurity.SecurityConfig; | |
| 25 | ||
| 26 | import org.springframework.metadata.Attributes; | |
| 27 | ||
| 28 | /** | |
| 29 | * Java 5 Annotation <code>Attributes</code> metadata implementation used for | |
| 30 | * secure method interception. | |
| 31 | * | |
| 32 | * <p>This <code>Attributes</code> implementation will return security | |
| 33 | * configuration for classes described using the <code>Secured</code> Java 5 | |
| 34 | * annotation. | |
| 35 | * | |
| 36 | * <p>The <code>SecurityAnnotationAttributes</code> implementation can be used | |
| 37 | * to configure a <code>MethodDefinitionAttributes</code> and | |
| 38 | * <code>MethodSecurityInterceptor</code> bean definition (see below). | |
| 39 | * | |
| 40 | * <p>For example: | |
| 41 | * <pre> | |
| 42 | * <bean id="attributes" | |
| 43 | * class="org.acegisecurity.annotation.SecurityAnnotationAttributes"/> | |
| 44 | * | |
| 45 | * <bean id="objectDefinitionSource" | |
| 46 | * class="org.acegisecurity.intercept.method.MethodDefinitionAttributes"> | |
| 47 | * <property name="attributes"> | |
| 48 | * <ref local="attributes"/> | |
| 49 | * </property> | |
| 50 | * </bean> | |
| 51 | * | |
| 52 | * <bean id="securityInterceptor" | |
| 53 | * class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor"> | |
| 54 | * . . . | |
| 55 | * <property name="objectDefinitionSource"> | |
| 56 | * <ref local="objectDefinitionSource"/> | |
| 57 | * </property> | |
| 58 | * </bean> | |
| 59 | * </pre> | |
| 60 | * | |
| 61 | * <p>These security annotations are similiar to the Commons Attributes | |
| 62 | * approach, however they are using Java 5 language-level metadata support. | |
| 63 | * | |
| 64 | * @author Mark St.Godard | |
| 65 | * @version $Id: SecurityAnnotationAttributes.java,v 1.2 2005/11/17 00:55:48 benalex Exp $ | |
| 66 | * | |
| 67 | * @see org.acegisecurity.annotation.Secured | |
| 68 | */ | |
| 69 | public class SecurityAnnotationAttributes implements Attributes { | |
| 70 | ||
| 71 | /** | |
| 72 | * Get the <code>Secured</code> attributes for a given target class. | |
| 73 | * @param method The target method | |
| 74 | * @return Collection of <code>SecurityConfig</code> | |
| 75 | * @see Attributes#getAttributes | |
| 76 | */ | |
| 77 | 1 | public Collection getAttributes(Class target) { |
| 78 | ||
| 79 | 1 | Set<SecurityConfig> attributes = new HashSet<SecurityConfig>(); |
| 80 | ||
| 81 | 1 | for (Annotation annotation : target.getAnnotations()) { |
| 82 | // check for Secured annotations | |
| 83 | 1 | if (annotation instanceof Secured) { |
| 84 | 1 | Secured attr = (Secured) annotation; |
| 85 | 1 | for (String auth : attr.value()) { |
| 86 | 1 | attributes.add(new SecurityConfig(auth)); |
| 87 | } | |
| 88 | 1 | break; |
| 89 | } | |
| 90 | } | |
| 91 | 1 | return attributes; |
| 92 | } | |
| 93 | ||
| 94 | 1 | public Collection getAttributes(Class clazz, Class filter) { |
| 95 | 1 | throw new UnsupportedOperationException("Unsupported operation"); |
| 96 | } | |
| 97 | ||
| 98 | /** | |
| 99 | * Get the <code>Secured</code> attributes for a given target method. | |
| 100 | * @param method The target method | |
| 101 | * @return Collection of <code>SecurityConfig</code> | |
| 102 | * @see Attributes#getAttributes | |
| 103 | */ | |
| 104 | 1 | public Collection getAttributes(Method method) { |
| 105 | 1 | Set<SecurityConfig> attributes = new HashSet<SecurityConfig>(); |
| 106 | ||
| 107 | 1 | for (Annotation annotation : method.getAnnotations()) { |
| 108 | // check for Secured annotations | |
| 109 | 1 | if (annotation instanceof Secured) { |
| 110 | 1 | Secured attr = (Secured) annotation; |
| 111 | 1 | for (String auth : attr.value()) { |
| 112 | 2 | attributes.add(new SecurityConfig(auth)); |
| 113 | } | |
| 114 | 1 | break; |
| 115 | } | |
| 116 | } | |
| 117 | 1 | return attributes; |
| 118 | } | |
| 119 | ||
| 120 | 1 | public Collection getAttributes(Method method, Class clazz) { |
| 121 | 1 | throw new UnsupportedOperationException("Unsupported operation"); |
| 122 | } | |
| 123 | ||
| 124 | 1 | public Collection getAttributes(Field field) { |
| 125 | 1 | throw new UnsupportedOperationException("Unsupported operation"); |
| 126 | } | |
| 127 | ||
| 128 | 1 | public Collection getAttributes(Field field, Class clazz) { |
| 129 | 1 | throw new UnsupportedOperationException("Unsupported operation"); |
| 130 | } | |
| 131 | ||
| 132 | } |
|
||||||||||