use of javax.ws.rs.container.ContainerResponseFilter in project jersey by jersey.
the class ApplicationFilterTest method testSingleResponseFilter.
@Test
public void testSingleResponseFilter() throws Exception {
final AtomicInteger called = new AtomicInteger(0);
final List<ContainerResponseFilter> responseFilterList = new ArrayList<>();
responseFilterList.add(new ContainerResponseFilter() {
@Override
public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext) throws IOException {
called.incrementAndGet();
}
});
final ResourceConfig resourceConfig = new ResourceConfig().register(new ProviderInstanceBindingBinder<>(responseFilterList, ContainerResponseFilter.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());
assertEquals(1, called.intValue());
}
use of javax.ws.rs.container.ContainerResponseFilter in project jersey by jersey.
the class ProviderBinderTest method getResponseFilter.
private ContainerResponseFilter getResponseFilter(InjectionManager injectionManager) {
ContainerResponseFilter responseFilter = injectionManager.getInstance(ContainerResponseFilter.class, CustomAnnotationLiteral.INSTANCE);
assertEquals(Child.class, responseFilter.getClass());
return responseFilter;
}
use of javax.ws.rs.container.ContainerResponseFilter in project tomee by apache.
the class JAXRSUtils method runContainerResponseFilters.
public static void runContainerResponseFilters(ServerProviderFactory pf, ResponseImpl r, Message m, OperationResourceInfo ori, Method invoked) throws IOException, Throwable {
List<ProviderInfo<ContainerResponseFilter>> containerFilters = pf.getContainerResponseFilters(ori == null ? null : ori.getNameBindings());
if (!containerFilters.isEmpty()) {
ContainerRequestContext requestContext = new ContainerRequestContextImpl(m.getExchange().getInMessage(), false, true);
ContainerResponseContext responseContext = new ContainerResponseContextImpl(r, m, ori == null ? null : ori.getClassResourceInfo().getServiceClass(), invoked);
for (ProviderInfo<ContainerResponseFilter> filter : containerFilters) {
InjectionUtils.injectContexts(filter.getProvider(), filter, m);
filter.getProvider().filter(requestContext, responseContext);
}
}
}
use of javax.ws.rs.container.ContainerResponseFilter in project graylog2-server by Graylog2.
the class ShiroSecurityBinding method configure.
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
final Class<?> resourceClass = resourceInfo.getResourceClass();
final Method resourceMethod = resourceInfo.getResourceMethod();
if (resourceMethod.isAnnotationPresent(RequiresAuthentication.class) || resourceClass.isAnnotationPresent(RequiresAuthentication.class)) {
if (resourceMethod.isAnnotationPresent(RequiresGuest.class)) {
LOG.debug("Resource method {}#{} is marked as unauthenticated, skipping setting filter.");
} else {
LOG.debug("Resource method {}#{} requires an authenticated user.", resourceClass.getCanonicalName(), resourceMethod.getName());
context.register(new ShiroAuthenticationFilter());
}
}
if (resourceMethod.isAnnotationPresent(RequiresPermissions.class) || resourceClass.isAnnotationPresent(RequiresPermissions.class)) {
RequiresPermissions requiresPermissions = resourceClass.getAnnotation(RequiresPermissions.class);
if (requiresPermissions == null) {
requiresPermissions = resourceMethod.getAnnotation(RequiresPermissions.class);
}
LOG.debug("Resource method {}#{} requires an authorization checks.", resourceClass.getCanonicalName(), resourceMethod.getName());
context.register(new ShiroAuthorizationFilter(requiresPermissions));
}
// TODO this is the wrong approach, we should have an Environment and proper request wrapping
context.register((ContainerResponseFilter) (requestContext, responseContext) -> ThreadContext.unbindSubject());
}
use of javax.ws.rs.container.ContainerResponseFilter in project cxf by apache.
the class ConfigurationImplTest method testInvalidContract.
@Test
public void testInvalidContract() {
TestHandler handler = new TestHandler();
LogUtils.getL7dLogger(ConfigurationImpl.class).addHandler(handler);
ConfigurationImpl c = new ConfigurationImpl(RuntimeType.SERVER);
ContainerResponseFilter filter = new ContainerResponseFilterImpl();
assertFalse(c.register(filter, Collections.<Class<?>, Integer>singletonMap(MessageBodyReader.class, 1000)));
for (String message : handler.messages) {
if (message.startsWith("WARN") && message.contains("does not implement specified contract")) {
// success
return;
}
}
fail("did not log expected message");
}
Aggregations