/** * Create a list of parameter models for a given Java method handling a resource * method, sub-resource method or a sub-resource locator. * * @param concreteClass concrete resource method handler implementation class. * @param declaringClass the class declaring the handling Java method. * @param javaMethod Java method handling a resource method, sub-resource * method or a sub-resource locator. * @param keepEncoded set to {@code true} to disable automatic decoding * of all the method parameters. (See {@link Encoded}. * @return a list of handling method parameter models. */ public static List<Parameter> create( Class concreteClass, Class declaringClass, Method javaMethod, boolean keepEncoded) { AnnotatedMethod method = new AnnotatedMethod(javaMethod); return create( concreteClass, declaringClass, ((null != method.getAnnotation(Encoded.class)) || keepEncoded), method.getParameterTypes(), method.getGenericParameterTypes(), method.getParameterAnnotations()); }
private void checkResourceClassSetters(final MethodList methodList, final boolean encodedFlag, Collection<Parameter> injectableParameters) { for (AnnotatedMethod method : methodList.withoutMetaAnnotation(HttpMethod.class) .withoutAnnotation(Path.class) .hasNumParams(1) .hasReturnType(void.class) .nameStartsWith("set")) { Parameter p = Parameter.create( handlerClass, method.getMethod().getDeclaringClass(), encodedFlag || method.isAnnotationPresent(Encoded.class), method.getParameterTypes()[0], method.getGenericParameterTypes()[0], method.getAnnotations()); if (null != p) { ResourceMethodValidator.validateParameter(p, method.getMethod(), method.getMethod().toGenericString(), "1", InvocableValidator.isSingleton(handlerClass)); // we do not inject entity parameters into class instance fields and properties. if (p.getSource() != Parameter.Source.ENTITY) { injectableParameters.add(p); } } } }
private static Annotation[][] mergeParameterAnnotations(final Method m, final Method am) { final Annotation[][] methodParamAnnotations = m.getParameterAnnotations(); final Annotation[][] annotatedMethodParamAnnotations = am.getParameterAnnotations(); final List<List<Annotation>> methodParamAnnotationsList = new ArrayList<>(); for (int i = 0; i < methodParamAnnotations.length; i++) { final List<Annotation> al = asList(methodParamAnnotations[i]); for (final Annotation a : annotatedMethodParamAnnotations[i]) { if (annotationNotInList(a.getClass(), al)) { al.add(a); } } methodParamAnnotationsList.add(al); } final Annotation[][] mergedAnnotations = new Annotation[methodParamAnnotations.length][]; for (int i = 0; i < methodParamAnnotations.length; i++) { final List<Annotation> paramAnnotations = methodParamAnnotationsList.get(i); mergedAnnotations[i] = paramAnnotations.toArray(new Annotation[paramAnnotations.size()]); } return mergedAnnotations; }
private static List<MediaType> resolveConsumedTypes(final AnnotatedMethod am, final List<MediaType> defaultConsumedTypes) { // Override default types if the annotation is present on the method if (am.isAnnotationPresent(Consumes.class)) { return extractMediaTypes(am.getAnnotation(Consumes.class)); } return defaultConsumedTypes; }
@Override public void configure(final ResourceInfo resourceInfo, final FeatureContext configuration) { final AnnotatedMethod am = new AnnotatedMethod(resourceInfo.getResourceMethod()); // check to see if it has cache control annotation final CacheControl cc = am.getAnnotation(CacheControl.class); if (cc != null) { configuration.register(new CacheControlledResponseFilter(cc)); } }
private void addSubResourceMethods( final Resource.Builder resourceBuilder, final MethodList methodList, final List<Parameter> resourceClassParameters, // parameters derived from fields and setters on the resource class final boolean encodedParameters, final List<MediaType> defaultConsumedTypes, final List<MediaType> defaultProducedTypes, final Collection<Class<? extends Annotation>> defaultNameBindings, final boolean extended ) { for (AnnotatedMethod am : methodList.withMetaAnnotation(HttpMethod.class).withAnnotation(Path.class)) { Resource.Builder childResourceBuilder = resourceBuilder.addChildResource(am.getAnnotation(Path.class).value()); ResourceMethod.Builder methodBuilder = childResourceBuilder.addMethod(am.getMetaMethodAnnotations(HttpMethod.class).get(0).value()) .consumes(resolveConsumedTypes(am, defaultConsumedTypes)) .produces(resolveProducedTypes(am, defaultProducedTypes)) .encodedParameters(encodedParameters || am.isAnnotationPresent(Encoded.class)) .nameBindings(defaultNameBindings) .nameBindings(am.getAnnotations()) .handledBy(handlerClass, am.getMethod()) .handlingMethod(am.getDeclaredMethod()) .handlerParameters(resourceClassParameters) .extended(extended || am.isAnnotationPresent(ExtendedResource.class)); introspectAsyncFeatures(am, methodBuilder); } }
@Override public void configure(final ResourceInfo resourceInfo, final FeatureContext configuration) { AnnotatedMethod am = new AnnotatedMethod(resourceInfo.getResourceMethod()); // RolesAllowed on the method takes precedence over PermitAll RolesAllowed ra = am.getAnnotation(RolesAllowed.class); if (ra != null) { configuration.register(AuthCheckFilter.INSTANCE); return; } // PermitAll takes precedence over RolesAllowed on the class if (am.isAnnotationPresent(PermitAll.class)) { // Do nothing. return; } // RolesAllowed on the class takes precedence over PermitAll ra = resourceInfo.getResourceClass().getAnnotation(RolesAllowed.class); if (ra != null) { configuration.register(AuthCheckFilter.INSTANCE); } }
private void addSubResourceLocators( final Resource.Builder resourceBuilder, final MethodList methodList, final List<Parameter> resourceClassParameters, // parameters derived from fields and setters on the resource class final boolean encodedParameters, final boolean extended) { for (AnnotatedMethod am : methodList.withoutMetaAnnotation(HttpMethod.class).withAnnotation(Path.class)) { final String path = am.getAnnotation(Path.class).value(); Resource.Builder builder = resourceBuilder; if (path != null && !path.isEmpty() && !"/".equals(path)) { builder = resourceBuilder.addChildResource(path); } builder.addMethod() .encodedParameters(encodedParameters || am.isAnnotationPresent(Encoded.class)) .handledBy(handlerClass, am.getMethod()) .handlingMethod(am.getDeclaredMethod()) .handlerParameters(resourceClassParameters) .extended(extended || am.isAnnotationPresent(ExtendedResource.class)); } } }
@Override public void configure(ResourceInfo resourceInfo, FeatureContext context) { final AnnotatedMethod am = new AnnotatedMethod(resourceInfo.getResourceMethod()); final Annotation[][] parameterAnnotations = am.getParameterAnnotations(); final Class<?>[] parameterTypes = am.getParameterTypes(); // First, check for any @Auth annotations on the method. for (int i = 0; i < parameterAnnotations.length; i++) { for (final Annotation annotation : parameterAnnotations[i]) { if (annotation instanceof Auth) { // Optional auth requires that a concrete AuthFilter be provided. if (parameterTypes[i].equals(Optional.class) && authFilter != null) { context.register(new WebApplicationExceptionCatchingFilter(authFilter)); return; } else { registerAuthFilter(context); return; } } } } // Second, check for any authorization annotations on the class or method. // Note that @DenyAll shouldn't be attached to classes. final boolean annotationOnClass = (resourceInfo.getResourceClass().getAnnotation(RolesAllowed.class) != null) || (resourceInfo.getResourceClass().getAnnotation(PermitAll.class) != null); final boolean annotationOnMethod = am.isAnnotationPresent(RolesAllowed.class) || am.isAnnotationPresent(DenyAll.class) || am.isAnnotationPresent(PermitAll.class); if (annotationOnClass || annotationOnMethod) { registerAuthFilter(context); } }
private static void introspectAsyncFeatures(AnnotatedMethod am, ResourceMethod.Builder resourceMethodBuilder) { if (am.isAnnotationPresent(ManagedAsync.class)) { resourceMethodBuilder.managedAsync(); } for (Annotation[] annotations : am.getParameterAnnotations()) { for (Annotation annotation : annotations) { if (annotation.annotationType() == Suspended.class) { resourceMethodBuilder.suspended(AsyncResponse.NO_TIMEOUT, TimeUnit.MILLISECONDS); } } } for (Class<?> paramType : am.getParameterTypes()) { if (SseEventSink.class.equals(paramType)) { resourceMethodBuilder.sse(); } } }
/** * Create new method list from the given collection of methods. * * The {@link Method#isBridge() bridge methods} and methods declared directly * on the {@link Object} class are filtered out. * * @param methods methods to be included in the method list. */ public MethodList(Collection<Method> methods) { List<AnnotatedMethod> l = new ArrayList<>(methods.size()); for (Method m : methods) { if (!m.isBridge() && m.getDeclaringClass() != Object.class) { l.add(new AnnotatedMethod(m)); } } this.methods = new AnnotatedMethod[l.size()]; this.methods = l.toArray(this.methods); }
@Override public void configure(ResourceInfo resourceInfo, FeatureContext context) { final AnnotatedMethod am = new AnnotatedMethod(resourceInfo.getResourceMethod()); final Annotation[][] parameterAnnotations = am.getParameterAnnotations(); if (am.isAnnotationPresent(RolesAllowed.class) || am.isAnnotationPresent(DenyAll.class) || am.isAnnotationPresent(PermitAll.class)) { context.register(authFilter); } else { for (Annotation[] annotations : parameterAnnotations) { for (Annotation annotation : annotations) { if (annotation instanceof Auth) { context.register(authFilter); return; } } } } } }
private static void introspectAsyncFeatures(AnnotatedMethod am, ResourceMethod.Builder resourceMethodBuilder) { if (am.isAnnotationPresent(ManagedAsync.class)) { resourceMethodBuilder.managedAsync(); } for (Annotation[] annotations : am.getParameterAnnotations()) { for (Annotation annotation : annotations) { if (annotation.annotationType() == Suspended.class) { resourceMethodBuilder.suspended(AsyncResponse.NO_TIMEOUT, TimeUnit.MILLISECONDS); return; } } } }
@Override public boolean keep(AnnotatedMethod m) { return m.getAnnotation(annotation) == null; } });
methods = methods.withMetaAnnotation(HttpMethod.class); for (AnnotatedMethod method : methods) { if (!Objects.equals(method.getMethod().getName(), link.method())) { continue; builder.append(template); Path methodPath = method.getAnnotation(Path.class); if (methodPath != null) { String methodTemplate = methodPath.value();
for (Annotation[] paramAnns : method.getParameterAnnotations()) { for (Annotation ann : paramAnns) { if (Objects.equals(ann.annotationType(), QueryParam.class)) { Class<?> beanParamType = method.getParameterTypes()[parameterIndex]; Field[] fields = beanParamType.getFields(); for (Field field : fields) {
@Override public boolean keep(AnnotatedMethod m) { return m.getMethod().getReturnType() == returnType; } });
@Override public boolean keep(AnnotatedMethod m) { return m.getParameterTypes().length == paramCount; } });
private static Annotation[] mergeMethodAnnotations(final Method m, final Method am) { final List<Annotation> al = asList(m.getAnnotations()); for (final Annotation a : am.getAnnotations()) { if (!m.isAnnotationPresent(a.getClass())) { al.add(a); } } return al.toArray(new Annotation[al.size()]); }
private void addSubResourceMethods( final Resource.Builder resourceBuilder, final MethodList methodList, final List<Parameter> resourceClassParameters, // parameters derived from fields and setters on the resource class final boolean encodedParameters, final List<MediaType> defaultConsumedTypes, final List<MediaType> defaultProducedTypes, final Collection<Class<? extends Annotation>> defaultNameBindings, final boolean extended ) { for (AnnotatedMethod am : methodList.withMetaAnnotation(HttpMethod.class).withAnnotation(Path.class)) { Resource.Builder childResourceBuilder = resourceBuilder.addChildResource(am.getAnnotation(Path.class).value()); ResourceMethod.Builder methodBuilder = childResourceBuilder.addMethod(am.getMetaMethodAnnotations(HttpMethod.class).get(0).value()) .consumes(resolveConsumedTypes(am, defaultConsumedTypes)) .produces(resolveProducedTypes(am, defaultProducedTypes)) .encodedParameters(encodedParameters || am.isAnnotationPresent(Encoded.class)) .nameBindings(defaultNameBindings) .nameBindings(am.getAnnotations()) .handledBy(handlerClass, am.getMethod()) .handlingMethod(am.getDeclaredMethod()) .handlerParameters(resourceClassParameters) .extended(extended || am.isAnnotationPresent(ExtendedResource.class)); introspectAsyncFeatures(am, methodBuilder); } }