Search in sources :

Example 6 with ContainerResponseFilter

use of javax.ws.rs.container.ContainerResponseFilter in project jersey by jersey.

the class ProviderBinderTest method testCustomRegistration.

@Test
public void testCustomRegistration() {
    InjectionManager injectionManager = Injections.createInjectionManager();
    ProviderBinder providerBinder = new ProviderBinder(injectionManager);
    providerBinder.bindClasses(Child.class);
    providerBinder.bindClasses(NotFilterChild.class);
    ContainerRequestFilter requestFilter = getRequestFilter(injectionManager);
    ContainerRequestFilter requestFilter2 = getRequestFilter(injectionManager);
    assertEquals(requestFilter, requestFilter2);
    ContainerResponseFilter responseFilter = getResponseFilter(injectionManager);
    ContainerResponseFilter responseFilter2 = getResponseFilter(injectionManager);
    assertTrue(responseFilter == responseFilter2);
    assertTrue(responseFilter == requestFilter);
    // only one filter should be registered
    Collection<ContainerResponseFilter> filters = Providers.getCustomProviders(injectionManager, ContainerResponseFilter.class);
    assertEquals(1, filters.size());
    Child child = injectionManager.getInstance(Child.class);
    Child child2 = injectionManager.getInstance(Child.class);
    assertTrue(child != responseFilter);
    assertTrue(child == child2);
}
Also used : ProviderBinder(org.glassfish.jersey.internal.inject.ProviderBinder) ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 7 with ContainerResponseFilter

use of javax.ws.rs.container.ContainerResponseFilter in project jersey by jersey.

the class EncodingFilterTest method testNoInterceptor.

@Test
public void testNoInterceptor() {
    ResourceConfig rc = new ResourceConfig(EncodingFilter.class);
    ContainerResponseFilter filter = new ApplicationHandler(rc).getInjectionManager().getInstance(ContainerResponseFilter.class);
    assertNotNull(filter);
    assertTrue(filter instanceof EncodingFilter);
    assertEquals(1, ((EncodingFilter) filter).getSupportedEncodings().size());
}
Also used : ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) ApplicationHandler(org.glassfish.jersey.server.ApplicationHandler) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Test(org.junit.Test)

Example 8 with ContainerResponseFilter

use of javax.ws.rs.container.ContainerResponseFilter in project minijax by minijax.

the class MinijaxApplication method runResponseFilters.

private void runResponseFilters(final MinijaxRequestContext context, final Response response) throws IOException {
    final ContainerResponseContext responseContext = (ContainerResponseContext) response;
    for (final Class<? extends ContainerResponseFilter> filterClass : responseFilters) {
        final ContainerResponseFilter filter = getResource(filterClass);
        filter.filter(context, responseContext);
    }
}
Also used : ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext)

Example 9 with ContainerResponseFilter

use of javax.ws.rs.container.ContainerResponseFilter in project cxf by apache.

the class ServerProviderFactory method setProviders.

@SuppressWarnings("unchecked")
@Override
protected void setProviders(boolean custom, boolean busGlobal, Object... providers) {
    List<Object> allProviders = new LinkedList<>();
    for (Object p : providers) {
        if (p instanceof Feature) {
            FeatureContext featureContext = createServerFeatureContext();
            Feature feature = (Feature) p;
            injectApplicationIntoFeature(feature);
            feature.configure(featureContext);
            Configuration cfg = featureContext.getConfiguration();
            for (Object featureProvider : cfg.getInstances()) {
                Map<Class<?>, Integer> contracts = cfg.getContracts(featureProvider.getClass());
                if (contracts != null && !contracts.isEmpty()) {
                    Class<?> providerCls = ClassHelper.getRealClass(getBus(), featureProvider);
                    allProviders.add(new FilterProviderInfo<Object>(featureProvider.getClass(), providerCls, featureProvider, getBus(), getFilterNameBindings(getBus(), featureProvider), false, contracts));
                } else {
                    allProviders.add(featureProvider);
                }
            }
        } else {
            allProviders.add(p);
        }
    }
    List<ProviderInfo<ContainerRequestFilter>> postMatchRequestFilters = new LinkedList<>();
    List<ProviderInfo<ContainerResponseFilter>> postMatchResponseFilters = new LinkedList<>();
    List<ProviderInfo<? extends Object>> theProviders = prepareProviders(custom, busGlobal, allProviders.toArray(), application);
    super.setCommonProviders(theProviders, RuntimeType.SERVER);
    for (ProviderInfo<? extends Object> provider : theProviders) {
        Class<?> providerCls = ClassHelper.getRealClass(getBus(), provider.getProvider());
        // Check if provider is constrained to server
        if (!constrainedTo(providerCls, RuntimeType.SERVER)) {
            continue;
        }
        if (filterContractSupported(provider, providerCls, ContainerRequestFilter.class)) {
            addContainerRequestFilter(postMatchRequestFilters, (ProviderInfo<ContainerRequestFilter>) provider);
        }
        if (filterContractSupported(provider, providerCls, ContainerResponseFilter.class)) {
            postMatchResponseFilters.add((ProviderInfo<ContainerResponseFilter>) provider);
        }
        if (DynamicFeature.class.isAssignableFrom(providerCls)) {
            // TODO: review the possibility of DynamicFeatures needing to have Contexts injected
            Object feature = provider.getProvider();
            dynamicFeatures.add((DynamicFeature) feature);
        }
        if (filterContractSupported(provider, providerCls, ExceptionMapper.class)) {
            addProviderToList(exceptionMappers, provider);
        }
    }
    Collections.sort(preMatchContainerRequestFilters, new BindingPriorityComparator(ContainerRequestFilter.class, true));
    mapInterceptorFilters(postMatchContainerRequestFilters, postMatchRequestFilters, ContainerRequestFilter.class, true);
    mapInterceptorFilters(containerResponseFilters, postMatchResponseFilters, ContainerResponseFilter.class, false);
    injectContextProxies(exceptionMappers, postMatchContainerRequestFilters.values(), preMatchContainerRequestFilters, containerResponseFilters.values());
}
Also used : Configuration(javax.ws.rs.core.Configuration) DynamicFeature(javax.ws.rs.container.DynamicFeature) Feature(javax.ws.rs.core.Feature) LinkedList(java.util.LinkedList) FeatureContext(javax.ws.rs.core.FeatureContext) ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) FilterProviderInfo(org.apache.cxf.jaxrs.model.FilterProviderInfo) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter)

Example 10 with ContainerResponseFilter

use of javax.ws.rs.container.ContainerResponseFilter in project cxf 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)

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