use of org.glassfish.jersey.server.model.AnnotatedMethod in project ratelimitj by mokies.
the class RateLimit429EnforcerFilter method filter.
@Override
public void filter(final ContainerRequestContext requestContext) {
try {
AnnotatedMethod method = new AnnotatedMethod(resource.getResourceMethod());
RateLimited rateLimited = method.getAnnotation(RateLimited.class);
RequestRateLimiter rateLimit = factory.getInstance(toLimitRules(rateLimited));
KeyProvider keyProvider = rateLimited.key();
KeyPart[] keyParts = rateLimited.keys();
if (keyProvider == Key.NO_VALUE && keyParts.length == 0) {
LOG.warn("No keys were provided by the key provide");
return;
}
Optional<CharSequence> legacyKey = keyProvider.create(request, resource, securityContext);
CharSequence key;
if (legacyKey.isPresent()) {
key = legacyKey.get();
} else {
Optional<CharSequence> keyResult = KeyPart.combineKeysParts(rateLimited.groupKeyPrefix(), Arrays.asList(keyParts), request, resource, securityContext);
if (keyResult.isPresent()) {
key = keyResult.get();
} else {
LOG.warn("No keys were provided by the key providers '{}'", Arrays.stream(keyParts).map(KeyPart::getClass).map(Object::toString).collect(Collectors.joining(", ")));
return;
}
}
// if (legacyKey.isPresent()) {
boolean overLimit = rateLimit.overLimitWhenIncremented(key.toString());
if (overLimit) {
if (!rateLimited.reportOnly()) {
LOG.info("rate-limit key '{}' over limit. HTTP Status 429 returned.", key);
requestContext.abortWith(Response.status(HTTP_STATUS_TOO_MANY_REQUESTS).build());
} else {
LOG.info("rate-limit key '{}' over limit. ReportOnly is true, no action taken.", key);
}
LOG.debug("rate-limit key '{}' under limit.", key);
}
// } else {
// //LOG.warn("No key was provided by the key provide '{}'", keyProvider.getClass());
// }
} catch (Exception e) {
LOG.error("Error occurred checking rate-limit. Assuming under limit", e);
}
}
use of org.glassfish.jersey.server.model.AnnotatedMethod in project drill by apache.
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);
}
}
use of org.glassfish.jersey.server.model.AnnotatedMethod in project drill by apache.
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);
}
}
Aggregations