Search in sources :

Example 1 with DynamicFeature

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

the class ServerProviderFactory method doApplyDynamicFeatures.

private void doApplyDynamicFeatures(ClassResourceInfo cri) {
    Set<OperationResourceInfo> oris = cri.getMethodDispatcher().getOperationResourceInfos();
    for (OperationResourceInfo ori : oris) {
        String nameBinding = DEFAULT_FILTER_NAME_BINDING + ori.getClassResourceInfo().getServiceClass().getName() + "." + ori.getMethodToInvoke().toString();
        for (DynamicFeature feature : dynamicFeatures) {
            FeatureContext featureContext = createServerFeatureContext();
            feature.configure(new ResourceInfoImpl(ori), featureContext);
            Configuration cfg = featureContext.getConfiguration();
            for (Object provider : cfg.getInstances()) {
                Map<Class<?>, Integer> contracts = cfg.getContracts(provider.getClass());
                if (contracts != null && !contracts.isEmpty()) {
                    Class<?> providerCls = ClassHelper.getRealClass(getBus(), provider);
                    registerUserProvider(new FilterProviderInfo<Object>(provider.getClass(), providerCls, provider, getBus(), Collections.singleton(nameBinding), true, contracts));
                    ori.addNameBindings(Collections.singletonList(nameBinding));
                }
            }
        }
    }
    Collection<ClassResourceInfo> subs = cri.getSubResources();
    for (ClassResourceInfo sub : subs) {
        if (sub != cri) {
            doApplyDynamicFeatures(sub);
        }
    }
}
Also used : Configuration(javax.ws.rs.core.Configuration) ResourceInfoImpl(org.apache.cxf.jaxrs.impl.ResourceInfoImpl) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) DynamicFeature(javax.ws.rs.container.DynamicFeature) FeatureContext(javax.ws.rs.core.FeatureContext) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo)

Example 2 with DynamicFeature

use of javax.ws.rs.container.DynamicFeature 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)

Aggregations

DynamicFeature (javax.ws.rs.container.DynamicFeature)2 Configuration (javax.ws.rs.core.Configuration)2 FeatureContext (javax.ws.rs.core.FeatureContext)2 LinkedList (java.util.LinkedList)1 ContainerRequestFilter (javax.ws.rs.container.ContainerRequestFilter)1 ContainerResponseFilter (javax.ws.rs.container.ContainerResponseFilter)1 Feature (javax.ws.rs.core.Feature)1 ResourceInfoImpl (org.apache.cxf.jaxrs.impl.ResourceInfoImpl)1 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)1 FilterProviderInfo (org.apache.cxf.jaxrs.model.FilterProviderInfo)1 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)1 ProviderInfo (org.apache.cxf.jaxrs.model.ProviderInfo)1