Search in sources :

Example 6 with AnnotatedMethod

use of org.glassfish.jersey.server.model.AnnotatedMethod in project dropwizard by dropwizard.

the class PolymorphicAuthDynamicFeature method configure.

@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();
    final Type[] parameterGenericTypes = am.getGenericParameterTypes();
    for (int i = 0; i < parameterAnnotations.length; i++) {
        final Class<?> type = parameterTypes[i];
        // If the parameter type is an Optional, extract its type
        // parameter. Otherwise, use the parameter type itself.
        final Type paramType = type == Optional.class ? ((ParameterizedType) parameterGenericTypes[i]).getActualTypeArguments()[0] : type;
        for (final Annotation annotation : parameterAnnotations[i]) {
            if (annotation instanceof Auth && authFilterMap.containsKey(paramType)) {
                final ContainerRequestFilter filter = authFilterMap.get(paramType);
                context.register(type == Optional.class ? new WebApplicationExceptionCatchingFilter(filter) : filter);
                return;
            }
        }
    }
}
Also used : AnnotatedMethod(org.glassfish.jersey.server.model.AnnotatedMethod) Optional(java.util.Optional) Annotation(java.lang.annotation.Annotation) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter)

Example 7 with AnnotatedMethod

use of org.glassfish.jersey.server.model.AnnotatedMethod in project drill by axbaretto.

the class AuthDynamicFeature method configure.

@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;
    }
    // path's doesn't go through AuthCheckFilter.
    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);
    }
}
Also used : RolesAllowed(javax.annotation.security.RolesAllowed) AnnotatedMethod(org.glassfish.jersey.server.model.AnnotatedMethod)

Example 8 with AnnotatedMethod

use of org.glassfish.jersey.server.model.AnnotatedMethod in project drill by axbaretto.

the class AuthDynamicFeature method configure.

@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);
    }
}
Also used : RolesAllowed(javax.annotation.security.RolesAllowed) AnnotatedMethod(org.glassfish.jersey.server.model.AnnotatedMethod)

Example 9 with AnnotatedMethod

use of org.glassfish.jersey.server.model.AnnotatedMethod in project dropwizard by dropwizard.

the class AuthDynamicFeature method configure.

@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++) {
        if (containsAuthAnnotation(parameterAnnotations[i])) {
            // Optional auth requires that a concrete AuthFilter be provided.
            if (parameterTypes[i].equals(Optional.class) && authFilter != null) {
                context.register(new WebApplicationExceptionCatchingFilter(authFilter));
            } 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);
    }
}
Also used : RolesAllowed(javax.annotation.security.RolesAllowed) AnnotatedMethod(org.glassfish.jersey.server.model.AnnotatedMethod) Optional(java.util.Optional) PermitAll(javax.annotation.security.PermitAll)

Example 10 with AnnotatedMethod

use of org.glassfish.jersey.server.model.AnnotatedMethod in project dropwizard by dropwizard.

the class CacheControlledResponseFeature method configure.

@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));
    }
}
Also used : AnnotatedMethod(org.glassfish.jersey.server.model.AnnotatedMethod)

Aggregations

AnnotatedMethod (org.glassfish.jersey.server.model.AnnotatedMethod)13 RolesAllowed (javax.annotation.security.RolesAllowed)7 RateLimited (es.moki.ratelimij.dropwizard.annotation.RateLimited)2 Annotation (java.lang.annotation.Annotation)2 Optional (java.util.Optional)2 HttpMethod (javax.ws.rs.HttpMethod)2 RequestRateLimiter (es.moki.ratelimitj.core.limiter.request.RequestRateLimiter)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 PermitAll (javax.annotation.security.PermitAll)1 Path (javax.ws.rs.Path)1 QueryParam (javax.ws.rs.QueryParam)1 ContainerRequestFilter (javax.ws.rs.container.ContainerRequestFilter)1 ResourceMappingContext (org.glassfish.jersey.linking.mapping.ResourceMappingContext)1 MethodList (org.glassfish.jersey.server.model.MethodList)1