use of javax.ws.rs.core.Feature in project cxf by apache.
the class JAXRSClientFactoryBean method applyFeatures.
protected void applyFeatures(AbstractClient client) {
if (getFeatures() != null) {
getFeatures().forEach(feature -> {
feature.initialize(client.getConfiguration(), getBus());
});
}
// Process JAX-RS features which are passed through as providers
final Set<Object> providers = new HashSet<>();
for (final Object provider : getProviders()) {
if (provider instanceof Feature) {
final Feature feature = (Feature) provider;
final FeatureContextImpl context = new FeatureContextImpl();
final Configurable<?> configurable = getConfigurableFor(context);
final Configuration configuration = configurable.getConfiguration();
final Set<Object> registered = configuration.getInstances();
if (!configuration.isRegistered(feature)) {
configurable.register(feature);
// Disregarding if the feature is enabled or disabled, register only newly added providers,
// excluding pre-existing ones and the feature instance itself.
final Set<Object> added = new HashSet<Object>(configuration.getInstances());
added.remove(feature);
added.removeAll(registered);
providers.addAll(added);
}
}
}
if (!providers.isEmpty()) {
setProviders(Arrays.asList(providers));
}
}
use of javax.ws.rs.core.Feature 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());
}
use of javax.ws.rs.core.Feature in project cxf by apache.
the class ConfigurationImplTest method testFeatureDisabledInstance.
@Test
public void testFeatureDisabledInstance() {
FeatureContextImpl featureContext = new FeatureContextImpl();
Configurable<FeatureContext> configurable = new ConfigurableImpl<>(featureContext, RuntimeType.SERVER);
featureContext.setConfigurable(configurable);
Feature feature = new DisablableFeature();
featureContext.register(feature);
Configuration config = configurable.getConfiguration();
assertFalse(config.isEnabled(feature));
}
use of javax.ws.rs.core.Feature in project cxf by apache.
the class BookServer20 method createServer.
@Override
protected Server createServer(Bus bus) throws Exception {
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
sf.setResourceClasses(BookStore.class);
List<Object> providers = new ArrayList<>();
providers.add(new PreMatchContainerRequestFilter2());
providers.add(new PreMatchContainerRequestFilter());
providers.add(new PostMatchContainerResponseFilter());
providers.add((Feature) context -> {
context.register(new PostMatchContainerResponseFilter3());
return true;
});
providers.add(new PostMatchContainerResponseFilter2());
providers.add(new CustomReaderBoundInterceptor());
providers.add(new CustomReaderInterceptor());
providers.add(new CustomWriterInterceptor());
providers.add(new CustomDynamicFeature());
providers.add(new PostMatchContainerRequestFilter());
providers.add(new FaultyContainerRequestFilter());
providers.add(new PreMatchReplaceStreamOrAddress());
providers.add(new ServerTestFeature());
providers.add(new JacksonJaxbJsonProvider());
providers.add(new IOExceptionMapper());
providers.add(new GregorianCalendarMessageBodyWriter());
sf.setApplication(new Application());
sf.setProviders(providers);
sf.setResourceProvider(BookStore.class, new SingletonResourceProvider(new BookStore(), true));
sf.setAddress("http://localhost:" + PORT + "/");
return sf.create();
}
Aggregations