Search in sources :

Example 1 with AnnotatedMethod

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

the class InjectLinkFieldDescriptor method getLinkTemplate.

/**
     * TODO javadoc.
     */
public static String getLinkTemplate(ResourceMappingContext rmc, InjectLink link) {
    String template = null;
    if (!link.resource().equals(Class.class)) {
        ResourceMappingContext.Mapping map = rmc.getMapping(link.resource());
        if (map != null) {
            template = map.getTemplate().getTemplate().toString();
        } else {
            // extract template from specified class' @Path annotation
            Path path = link.resource().getAnnotation(Path.class);
            template = path == null ? "" : path.value();
        }
        // extract template from specified class' @Path annotation
        if (link.method().length() > 0) {
            // append value of method's @Path annotation
            MethodList methods = new MethodList(link.resource());
            methods = methods.withMetaAnnotation(HttpMethod.class);
            Iterator<AnnotatedMethod> iterator = methods.iterator();
            while (iterator.hasNext()) {
                AnnotatedMethod method = iterator.next();
                if (!method.getMethod().getName().equals(link.method())) {
                    continue;
                }
                StringBuilder builder = new StringBuilder();
                builder.append(template);
                Path methodPath = method.getAnnotation(Path.class);
                if (methodPath != null) {
                    String methodTemplate = methodPath.value();
                    if (!(template.endsWith("/") || methodTemplate.startsWith("/"))) {
                        builder.append("/");
                    }
                    builder.append(methodTemplate);
                }
                CharSequence querySubString = extractQueryParams(method);
                if (querySubString.length() > 0) {
                    builder.append("{?");
                    builder.append(querySubString);
                    builder.append("}");
                }
                template = builder.toString();
                break;
            }
        }
    } else {
        template = link.value();
    }
    return template;
}
Also used : Path(javax.ws.rs.Path) ResourceMappingContext(org.glassfish.jersey.linking.mapping.ResourceMappingContext) AnnotatedMethod(org.glassfish.jersey.server.model.AnnotatedMethod) MethodList(org.glassfish.jersey.server.model.MethodList) HttpMethod(javax.ws.rs.HttpMethod)

Example 2 with AnnotatedMethod

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

the class InjectLinkFieldDescriptor method extractQueryParams.

private static CharSequence extractQueryParams(AnnotatedMethod method) throws SecurityException {
    // append query parameters
    StringBuilder querySubString = new StringBuilder();
    int parameterIndex = 0;
    for (Annotation[] paramAnns : method.getParameterAnnotations()) {
        for (Annotation ann : paramAnns) {
            if (ann.annotationType() == QueryParam.class) {
                querySubString.append(((QueryParam) ann).value());
                querySubString.append(',');
            }
            if (ann.annotationType() == BeanParam.class) {
                Class<?> beanParamType = method.getParameterTypes()[parameterIndex];
                Field[] fields = beanParamType.getFields();
                for (Field field : fields) {
                    QueryParam queryParam = field.getAnnotation(QueryParam.class);
                    if (queryParam != null) {
                        querySubString.append(queryParam.value());
                        querySubString.append(',');
                    }
                }
                Method[] beanMethods = beanParamType.getMethods();
                for (Method beanMethod : beanMethods) {
                    QueryParam queryParam = beanMethod.getAnnotation(QueryParam.class);
                    if (queryParam != null) {
                        querySubString.append(queryParam.value());
                        querySubString.append(',');
                    }
                }
            }
        }
        parameterIndex++;
    }
    CharSequence result = "";
    if (querySubString.length() > 0) {
        result = querySubString.subSequence(0, querySubString.length() - 1);
    }
    return result;
}
Also used : Field(java.lang.reflect.Field) QueryParam(javax.ws.rs.QueryParam) AnnotatedMethod(org.glassfish.jersey.server.model.AnnotatedMethod) HttpMethod(javax.ws.rs.HttpMethod) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation)

Example 3 with AnnotatedMethod

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

the class RolesAllowedDynamicFeature method configure.

@Override
public void configure(final ResourceInfo resourceInfo, final FeatureContext configuration) {
    final AnnotatedMethod am = new AnnotatedMethod(resourceInfo.getResourceMethod());
    // DenyAll on the method take precedence over RolesAllowed and PermitAll
    if (am.isAnnotationPresent(DenyAll.class)) {
        configuration.register(new RolesAllowedRequestFilter());
        return;
    }
    // RolesAllowed on the method takes precedence over PermitAll
    RolesAllowed ra = am.getAnnotation(RolesAllowed.class);
    if (ra != null) {
        configuration.register(new RolesAllowedRequestFilter(ra.value()));
        return;
    }
    // PermitAll takes precedence over RolesAllowed on the class
    if (am.isAnnotationPresent(PermitAll.class)) {
        // Do nothing.
        return;
    }
    // DenyAll can't be attached to classes
    // RolesAllowed on the class takes precedence over PermitAll
    ra = resourceInfo.getResourceClass().getAnnotation(RolesAllowed.class);
    if (ra != null) {
        configuration.register(new RolesAllowedRequestFilter(ra.value()));
    }
}
Also used : RolesAllowed(javax.annotation.security.RolesAllowed) AnnotatedMethod(org.glassfish.jersey.server.model.AnnotatedMethod)

Example 4 with AnnotatedMethod

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

the class RateLimited429EnforcerFeature method configure.

@Override
public void configure(final ResourceInfo resourceInfo, final FeatureContext context) {
    final AnnotatedMethod method = new AnnotatedMethod(resourceInfo.getResourceMethod());
    final RateLimited rateLimited = method.getAnnotation(RateLimited.class);
    if (null != rateLimited) {
        context.register(RateLimit429EnforcerFilter.class);
    }
}
Also used : AnnotatedMethod(org.glassfish.jersey.server.model.AnnotatedMethod) RateLimited(es.moki.ratelimij.dropwizard.annotation.RateLimited)

Example 5 with AnnotatedMethod

use of org.glassfish.jersey.server.model.AnnotatedMethod in project coastal-hazards by USGS-CIDA.

the class DynamicRolesLoginRedirectFeature method configure.

@Override
public void configure(final ResourceInfo resourceInfo, final FeatureContext configuration) {
    AnnotatedMethod am = new AnnotatedMethod(resourceInfo.getResourceMethod());
    // DenyAll on the method take precedence over RolesAllowed and PermitAll
    if (am.isAnnotationPresent(DenyAll.class)) {
        configuration.register(new RolesAllowedPastLoginFilter());
        return;
    }
    // RolesAllowed on the method takes precedence over PermitAll
    RolesAllowed ra = am.getAnnotation(RolesAllowed.class);
    if (ra != null) {
        configuration.register(new RolesAllowedPastLoginFilter(ra.value()));
        return;
    }
    // PermitAll takes precedence over RolesAllowed on the class
    if (am.isAnnotationPresent(PermitAll.class)) {
        // Do nothing.
        return;
    }
    // DenyAll can't be attached to classes
    // RolesAllowed on the class takes precedence over PermitAll
    ra = resourceInfo.getResourceClass().getAnnotation(RolesAllowed.class);
    if (ra != null) {
        configuration.register(new RolesAllowedPastLoginFilter(ra.value()));
    }
}
Also used : RolesAllowed(javax.annotation.security.RolesAllowed) 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