Repository: deltaspike Updated Branches: refs/heads/master 49ea10eec -> 1596f17d1
DELTASPIKE-801 @Nonbinding support for @SecurityParameterBinding Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/1596f17d Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/1596f17d Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/1596f17d Branch: refs/heads/master Commit: 1596f17d14ae0a91be6c365b95ecd5e0b904c173 Parents: 49ea10e Author: gpetracek <[email protected]> Authored: Mon Dec 15 23:14:22 2014 +0100 Committer: gpetracek <[email protected]> Committed: Mon Dec 15 23:14:22 2014 +0100 ---------------------------------------------------------------------- .../deltaspike/core/util/AnnotationUtils.java | 98 ++++++++++++++++++++ .../SecurityParameterValueRedefiner.java | 19 ++++ 2 files changed, 117 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1596f17d/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/AnnotationUtils.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/AnnotationUtils.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/AnnotationUtils.java index 9d4c8e2..0e3fbe3 100644 --- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/AnnotationUtils.java +++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/AnnotationUtils.java @@ -21,8 +21,11 @@ package org.apache.deltaspike.core.util; import javax.enterprise.inject.Typed; import javax.enterprise.inject.spi.BeanManager; +import javax.enterprise.util.Nonbinding; import java.lang.annotation.Annotation; import java.lang.reflect.Method; +import java.lang.reflect.Type; +import java.util.Arrays; @Typed() public abstract class AnnotationUtils @@ -75,4 +78,99 @@ public abstract class AnnotationUtils } return null; } + + //based on org.apache.webbeans.container.BeanCacheKey#getQualifierHashCode + public static int getQualifierHashCode(Annotation annotation) + { + Class annotationClass = annotation.annotationType(); + + int hashCode = getTypeHashCode(annotationClass); + + for (Method member : annotationClass.getDeclaredMethods()) + { + if (member.isAnnotationPresent(Nonbinding.class)) + { + continue; + } + + final Object annotationMemberValue; + try + { + annotationMemberValue = ReflectionUtils.invokeMethod(annotation, member, Object.class, true); + } + catch (IllegalAccessException e) + { + throw ExceptionUtils.throwAsRuntimeException(e); + } + + final int arrayValue; + if (annotationMemberValue.getClass().isArray()) + { + Class<?> annotationMemberType = annotationMemberValue.getClass().getComponentType(); + if (annotationMemberType.isPrimitive()) + { + if (Long.TYPE == annotationMemberType) + { + arrayValue = Arrays.hashCode((long[]) annotationMemberValue); + } + else if (Integer.TYPE == annotationMemberType) + { + arrayValue = Arrays.hashCode((int[]) annotationMemberValue); + } + else if (Short.TYPE == annotationMemberType) + { + arrayValue = Arrays.hashCode((short[]) annotationMemberValue); + } + else if (Double.TYPE == annotationMemberType) + { + arrayValue = Arrays.hashCode((double[]) annotationMemberValue); + } + else if (Float.TYPE == annotationMemberType) + { + arrayValue = Arrays.hashCode((float[]) annotationMemberValue); + } + else if (Boolean.TYPE == annotationMemberType) + { + arrayValue = Arrays.hashCode((boolean[]) annotationMemberValue); + } + else if (Byte.TYPE == annotationMemberType) + { + arrayValue = Arrays.hashCode((byte[]) annotationMemberValue); + } + else if (Character.TYPE == annotationMemberType) + { + arrayValue = Arrays.hashCode((char[]) annotationMemberValue); + } + else + { + arrayValue = 0; + } + } + else + { + arrayValue = Arrays.hashCode((Object[]) annotationMemberValue); + } + } + else + { + arrayValue = annotationMemberValue.hashCode(); + } + + hashCode = 29 * hashCode + arrayValue; + hashCode = 29 * hashCode + member.getName().hashCode(); + } + + return hashCode; + } + + private static int getTypeHashCode(Type type) + { + int typeHash = type.hashCode(); + if (typeHash == 0 && type instanceof Class) + { + return ((Class)type).getName().hashCode(); + } + + return typeHash; + } } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/1596f17d/deltaspike/modules/security/impl/src/main/java/org/apache/deltaspike/security/impl/authorization/SecurityParameterValueRedefiner.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/security/impl/src/main/java/org/apache/deltaspike/security/impl/authorization/SecurityParameterValueRedefiner.java b/deltaspike/modules/security/impl/src/main/java/org/apache/deltaspike/security/impl/authorization/SecurityParameterValueRedefiner.java index 1f83179..9d8cc26 100644 --- a/deltaspike/modules/security/impl/src/main/java/org/apache/deltaspike/security/impl/authorization/SecurityParameterValueRedefiner.java +++ b/deltaspike/modules/security/impl/src/main/java/org/apache/deltaspike/security/impl/authorization/SecurityParameterValueRedefiner.java @@ -30,6 +30,7 @@ import javax.enterprise.inject.spi.Annotated; import javax.enterprise.inject.spi.InjectionPoint; import javax.interceptor.InvocationContext; +import org.apache.deltaspike.core.util.AnnotationUtils; import org.apache.deltaspike.core.util.metadata.builder.ParameterValueRedefiner; import org.apache.deltaspike.security.api.authorization.SecuredReturn; import org.apache.deltaspike.security.api.authorization.SecurityParameterBinding; @@ -96,6 +97,24 @@ public class SecurityParameterValueRedefiner implements ParameterValueRedefiner return invocation.getParameters()[i]; } } + + Set<Integer> hashCodesOfBusinessParameterAnnotations = + new HashSet<Integer>(businessParameterAnnotations.size()); + + for (Annotation annotation : businessParameterAnnotations) + { + hashCodesOfBusinessParameterAnnotations.add( + AnnotationUtils.getQualifierHashCode(annotation)); + } + //2nd try (detailed check) + for (Annotation annotation : requiredBindingAnnotations) + { + if (hashCodesOfBusinessParameterAnnotations.contains( + AnnotationUtils.getQualifierHashCode(annotation))) + { + return invocation.getParameters()[i]; + } + } } throw new IllegalStateException("Missing required security parameter binding "
