Search in sources :

Example 16 with Feature

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));
    }
}
Also used : FeatureContextImpl(org.apache.cxf.jaxrs.impl.FeatureContextImpl) Configuration(javax.ws.rs.core.Configuration) Feature(javax.ws.rs.core.Feature) HashSet(java.util.HashSet)

Example 17 with Feature

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());
}
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 18 with Feature

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));
}
Also used : Configuration(javax.ws.rs.core.Configuration) Feature(javax.ws.rs.core.Feature) FeatureContext(javax.ws.rs.core.FeatureContext) Test(org.junit.Test)

Example 19 with 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();
}
Also used : Arrays(java.util.Arrays) Provider(javax.ws.rs.ext.Provider) StringUtils(org.apache.cxf.common.util.StringUtils) ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) Application(javax.ws.rs.core.Application) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) MediaType(javax.ws.rs.core.MediaType) ByteArrayInputStream(java.io.ByteArrayInputStream) ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) Map(java.util.Map) URI(java.net.URI) Method(java.lang.reflect.Method) Server(org.apache.cxf.endpoint.Server) Context(javax.ws.rs.core.Context) GregorianCalendar(java.util.GregorianCalendar) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) Collectors(java.util.stream.Collectors) JacksonJaxbJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider) PreMatching(javax.ws.rs.container.PreMatching) Priority(javax.annotation.Priority) DynamicFeature(javax.ws.rs.container.DynamicFeature) List(java.util.List) HttpHeaders(javax.ws.rs.core.HttpHeaders) WriterInterceptorContext(javax.ws.rs.ext.WriterInterceptorContext) Response(javax.ws.rs.core.Response) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) WebApplicationException(javax.ws.rs.WebApplicationException) UriInfo(javax.ws.rs.core.UriInfo) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext) Bus(org.apache.cxf.Bus) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) Feature(javax.ws.rs.core.Feature) SimpleDateFormat(java.text.SimpleDateFormat) ReaderInterceptorContext(javax.ws.rs.ext.ReaderInterceptorContext) HashMap(java.util.HashMap) Retention(java.lang.annotation.Retention) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) FeatureContext(javax.ws.rs.core.FeatureContext) ResourceInfo(javax.ws.rs.container.ResourceInfo) WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) AbstractServerTestServerBase(org.apache.cxf.testutil.common.AbstractServerTestServerBase) OutputStream(java.io.OutputStream) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOUtils(org.apache.cxf.helpers.IOUtils) IOException(java.io.IOException) Target(java.lang.annotation.Target) ElementType(java.lang.annotation.ElementType) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) NameBinding(javax.ws.rs.NameBinding) InjectionUtils(org.apache.cxf.jaxrs.utils.InjectionUtils) InputStream(java.io.InputStream) RetentionPolicy(java.lang.annotation.RetentionPolicy) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) ArrayList(java.util.ArrayList) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) JacksonJaxbJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider) Application(javax.ws.rs.core.Application)

Aggregations

Feature (javax.ws.rs.core.Feature)19 Response (javax.ws.rs.core.Response)9 Test (org.junit.Test)9 IOException (java.io.IOException)7 List (java.util.List)7 InputStream (java.io.InputStream)6 Annotation (java.lang.annotation.Annotation)6 Type (java.lang.reflect.Type)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 Priority (javax.annotation.Priority)6 WebApplicationException (javax.ws.rs.WebApplicationException)6 Client (javax.ws.rs.client.Client)6 MediaType (javax.ws.rs.core.MediaType)6 ExceptionMapper (javax.ws.rs.ext.ExceptionMapper)6 OutputStream (java.io.OutputStream)5 URI (java.net.URI)5 Arrays (java.util.Arrays)5 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)5 MessageBodyWriter (javax.ws.rs.ext.MessageBodyWriter)5