Search in sources :

Example 1 with ContainerResponseFilter

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());
}
Also used : ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) ArrayList(java.util.ArrayList) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext) Resource(org.glassfish.jersey.server.model.Resource) IOException(java.io.IOException) ContainerResponse(org.glassfish.jersey.server.ContainerResponse) Response(javax.ws.rs.core.Response) ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Example 2 with ContainerResponseFilter

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;
}
Also used : ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter)

Example 3 with ContainerResponseFilter

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);
        }
    }
}
Also used : ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) ContainerRequestContextImpl(org.apache.cxf.jaxrs.impl.ContainerRequestContextImpl) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext) ContainerResponseContextImpl(org.apache.cxf.jaxrs.impl.ContainerResponseContextImpl)

Example 4 with ContainerResponseFilter

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());
}
Also used : DynamicFeature(javax.ws.rs.container.DynamicFeature) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) FeatureContext(javax.ws.rs.core.FeatureContext) Logger(org.slf4j.Logger) ResourceInfo(javax.ws.rs.container.ResourceInfo) ThreadContext(org.apache.shiro.util.ThreadContext) LoggerFactory(org.slf4j.LoggerFactory) RequiresGuest(org.apache.shiro.authz.annotation.RequiresGuest) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) Method(java.lang.reflect.Method) ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) Method(java.lang.reflect.Method)

Example 5 with ContainerResponseFilter

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");
}
Also used : ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) Test(org.junit.Test)

Aggregations

ContainerResponseFilter (javax.ws.rs.container.ContainerResponseFilter)10 ContainerResponseContext (javax.ws.rs.container.ContainerResponseContext)4 Test (org.junit.Test)4 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)3 ProviderInfo (org.apache.cxf.jaxrs.model.ProviderInfo)3 ContainerRequestFilter (javax.ws.rs.container.ContainerRequestFilter)2 DynamicFeature (javax.ws.rs.container.DynamicFeature)2 FeatureContext (javax.ws.rs.core.FeatureContext)2 ContainerRequestContextImpl (org.apache.cxf.jaxrs.impl.ContainerRequestContextImpl)2 ContainerResponseContextImpl (org.apache.cxf.jaxrs.impl.ContainerResponseContextImpl)2 ApplicationHandler (org.glassfish.jersey.server.ApplicationHandler)2 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)2 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ResourceInfo (javax.ws.rs.container.ResourceInfo)1 Configuration (javax.ws.rs.core.Configuration)1 Feature (javax.ws.rs.core.Feature)1