use of javax.ws.rs.container.ContainerRequestFilter 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)) {
if (type == Optional.class) {
final ContainerRequestFilter filter = authFilterMap.get(paramType);
context.register(new WebApplicationExceptionCatchingFilter(filter));
return;
} else {
context.register(authFilterMap.get(type));
return;
}
}
}
}
}
use of javax.ws.rs.container.ContainerRequestFilter in project jersey by jersey.
the class ProviderBinderTest method getRequestFilter.
private ContainerRequestFilter getRequestFilter(InjectionManager injectionManager) {
ContainerRequestFilter requestFilter = injectionManager.getInstance(ContainerRequestFilter.class, CustomAnnotationLiteral.INSTANCE);
assertEquals(Child.class, requestFilter.getClass());
return requestFilter;
}
use of javax.ws.rs.container.ContainerRequestFilter in project jersey by jersey.
the class ApplicationHandler method filterNameBound.
/**
* Takes collection of all filters/interceptors (either request/reader or response/writer)
* and separates out all name-bound filters/interceptors, returns them as a separate MultivaluedMap,
* mapping the name-bound annotation to the list of name-bound filters/interceptors. The same key values
* are also added into the inverse map passed in {@code inverseNameBoundMap}.
* <p/>
* Note, the name-bound filters/interceptors are removed from the original filters/interceptors collection.
* If non-null collection is passed in the postMatching parameter (applicable for filters only),
* this method also removes all the global
* postMatching filters from the original collection and adds them to the collection passed in the postMatching
* parameter.
*
* @param all Collection of all filters to be processed.
* @param preMatchingFilters Collection into which pre-matching filters should be added.
* @param componentBag Component bag
* @param applicationNameBindings Collection of name binding annotations attached to the JAX-RS application.
* @param inverseNameBoundMap Inverse name bound map into which the name bound providers should be inserted. The keys
* are providers (filters, interceptor)
* @return {@link MultivaluedMap} of all name-bound filters.
*/
private static <T> MultivaluedMap<Class<? extends Annotation>, RankedProvider<T>> filterNameBound(final Iterable<RankedProvider<T>> all, final Collection<RankedProvider<ContainerRequestFilter>> preMatchingFilters, final ComponentBag componentBag, final Collection<Class<? extends Annotation>> applicationNameBindings, final MultivaluedMap<RankedProvider<T>, Class<? extends Annotation>> inverseNameBoundMap) {
final MultivaluedMap<Class<? extends Annotation>, RankedProvider<T>> result = new MultivaluedHashMap<>();
for (final Iterator<RankedProvider<T>> it = all.iterator(); it.hasNext(); ) {
final RankedProvider<T> provider = it.next();
Class<?> providerClass = provider.getProvider().getClass();
final Set<Type> contractTypes = provider.getContractTypes();
if (contractTypes != null && !contractTypes.contains(providerClass)) {
providerClass = ReflectionHelper.theMostSpecificTypeOf(contractTypes);
}
ContractProvider model = componentBag.getModel(providerClass);
if (model == null) {
// the provider was (most likely) bound in HK2 externally
model = ComponentBag.modelFor(providerClass);
}
final boolean preMatching = providerClass.getAnnotation(PreMatching.class) != null;
if (preMatching && preMatchingFilters != null) {
it.remove();
preMatchingFilters.add(new RankedProvider<>((ContainerRequestFilter) provider.getProvider(), model.getPriority(ContainerRequestFilter.class)));
}
boolean nameBound = model.isNameBound();
if (nameBound && !applicationNameBindings.isEmpty() && applicationNameBindings.containsAll(model.getNameBindings())) {
// override the name-bound flag
nameBound = false;
}
if (nameBound) {
// not application-bound
if (!preMatching) {
it.remove();
for (final Class<? extends Annotation> binding : model.getNameBindings()) {
result.add(binding, provider);
inverseNameBoundMap.add(provider, binding);
}
} else {
LOGGER.warning(LocalizationMessages.PREMATCHING_ALSO_NAME_BOUND(providerClass));
}
}
}
return result;
}
use of javax.ws.rs.container.ContainerRequestFilter in project jersey by jersey.
the class ApplicationFilterTest method testFilterExceptionHandling.
@Test
public void testFilterExceptionHandling() throws Exception {
final List<ContainerRequestFilter> requestFilterList = new ArrayList<>();
requestFilterList.add(new ExceptionFilter());
final ResourceConfig resourceConfig = new ResourceConfig().register(new ProviderInstanceBindingBinder<>(requestFilterList, ContainerRequestFilter.class));
final Resource.Builder rb = Resource.builder("test");
rb.addMethod("GET").handledBy(new Inflector<ContainerRequestContext, Response>() {
@Override
public Response apply(final ContainerRequestContext request) {
return Response.ok().build();
}
});
resourceConfig.registerResources(rb.build());
final ApplicationHandler application = new ApplicationHandler(resourceConfig);
try {
application.apply(RequestContextBuilder.from("/test", "GET").build()).get().getStatus();
Assert.fail("should throw an exception");
} catch (final Exception e) {
// ok
}
}
use of javax.ws.rs.container.ContainerRequestFilter in project jersey by jersey.
the class ApplicationFilterTest method testMultipleFiltersWithBindingPriority.
@Test
public void testMultipleFiltersWithBindingPriority() throws Exception {
final Filter1 filter1 = new Filter1();
final Filter10 filter10 = new Filter10();
final Filter100 filter100 = new Filter100();
filter1.setFilters(filter10, filter100);
filter10.setFilters(filter1, filter100);
filter100.setFilters(filter1, filter10);
final List<ContainerRequestFilter> requestFilterList = new ArrayList<>();
requestFilterList.add(filter100);
requestFilterList.add(filter1);
requestFilterList.add(filter10);
final ResourceConfig resourceConfig = new ResourceConfig().register(new ProviderInstanceBindingBinder<>(requestFilterList, ContainerRequestFilter.class));
final Resource.Builder rb = Resource.builder("test");
rb.addMethod("GET").handledBy(new Inflector<ContainerRequestContext, Response>() {
@Override
public Response apply(final ContainerRequestContext request) {
return Response.ok().build();
}
});
resourceConfig.registerResources(rb.build());
final ApplicationHandler application = new ApplicationHandler(resourceConfig);
assertEquals(200, application.apply(RequestContextBuilder.from("/test", "GET").build()).get().getStatus());
}
Aggregations