Search in sources :

Example 1 with Feature

use of org.apache.cxf.feature.Feature in project tomee by apache.

the class CxfRsHttpListener method configureFactory.

private void configureFactory(final Collection<Object> givenAdditionalProviders, final ServiceConfiguration serviceConfiguration, final JAXRSServerFactoryBean factory, final WebBeansContext ctx) {
    if (!"true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.cxf.rs.skip-provider-sorting", "false"))) {
        final Comparator<?> providerComparator = findProviderComparator(serviceConfiguration, ctx);
        if (providerComparator != null) {
            factory.setProviderComparator(providerComparator);
        }
    }
    CxfUtil.configureEndpoint(factory, serviceConfiguration, CXF_JAXRS_PREFIX);
    boolean enforceCxfBvalMapper = false;
    if (ctx == null || !ctx.getBeanManagerImpl().isInUse()) {
        // activate bval
        boolean bvalActive = Boolean.parseBoolean(SystemInstance.get().getProperty("openejb.cxf.rs.bval.active", serviceConfiguration.getProperties().getProperty(CXF_JAXRS_PREFIX + "bval.active", "true")));
        if (factory.getFeatures() == null && bvalActive) {
            factory.setFeatures(new ArrayList<Feature>());
        } else if (bvalActive) {
            // check we should activate it and user didn't configure it
            for (final Feature f : factory.getFeatures()) {
                if (BeanValidationFeature.class.isInstance(f)) {
                    bvalActive = false;
                    break;
                }
            }
            for (final Interceptor<?> i : factory.getInInterceptors()) {
                if (BeanValidationInInterceptor.class.isInstance(i)) {
                    bvalActive = false;
                    break;
                }
            }
            for (final Interceptor<?> i : factory.getOutInterceptors()) {
                if (BeanValidationOutInterceptor.class.isInstance(i)) {
                    bvalActive = false;
                    break;
                }
            }
        }
        if (bvalActive) {
            // bval doesn't need the actual instance so faking it to avoid to lookup the bean
            final BeanValidationProvider provider = new BeanValidationProvider();
            final BeanValidationInInterceptor in = new JAXRSBeanValidationInInterceptor() {

                @Override
                protected Object getServiceObject(final Message message) {
                    return CxfRsHttpListener.this.getServiceObject(message);
                }
            };
            in.setProvider(provider);
            in.setServiceObject(FAKE_SERVICE_OBJECT);
            factory.getInInterceptors().add(in);
            final BeanValidationOutInterceptor out = new JAXRSBeanValidationOutInterceptor() {

                @Override
                protected Object getServiceObject(final Message message) {
                    return CxfRsHttpListener.this.getServiceObject(message);
                }
            };
            out.setProvider(provider);
            out.setServiceObject(FAKE_SERVICE_OBJECT);
            factory.getOutInterceptors().add(out);
            // and add a mapper to get back a 400 like for bval
            enforceCxfBvalMapper = true;
        }
    }
    final Collection<ServiceInfo> services = serviceConfiguration.getAvailableServices();
    final String staticSubresourceResolution = serviceConfiguration.getProperties().getProperty(CXF_JAXRS_PREFIX + STATIC_SUB_RESOURCE_RESOLUTION_KEY);
    if (staticSubresourceResolution != null) {
        factory.setStaticSubresourceResolution("true".equalsIgnoreCase(staticSubresourceResolution));
    }
    // resource comparator
    final String resourceComparator = serviceConfiguration.getProperties().getProperty(RESOURCE_COMPARATOR_KEY);
    if (resourceComparator != null) {
        try {
            ResourceComparator instance = (ResourceComparator) ServiceInfos.resolve(services, resourceComparator);
            if (instance == null) {
                instance = (ResourceComparator) Thread.currentThread().getContextClassLoader().loadClass(resourceComparator).newInstance();
            }
            factory.setResourceComparator(instance);
        } catch (final Exception e) {
            LOGGER.error("Can't create the resource comparator " + resourceComparator, e);
        }
    }
    // static resources
    final String staticResources = serviceConfiguration.getProperties().getProperty(STATIC_RESOURCE_KEY);
    if (staticResources != null) {
        final String[] resources = staticResources.split(",");
        for (final String r : resources) {
            final String trimmed = r.trim();
            if (!trimmed.isEmpty()) {
                staticResourcesList.add(Pattern.compile(trimmed));
            }
        }
    }
    // providers
    Set<String> providersConfig = null;
    {
        final String provider = serviceConfiguration.getProperties().getProperty(PROVIDERS_KEY);
        if (provider != null) {
            providersConfig = new HashSet<>();
            for (final String p : Arrays.asList(provider.split(","))) {
                providersConfig.add(p.trim());
            }
        }
        {
            if (GLOBAL_PROVIDERS != null) {
                if (providersConfig == null) {
                    providersConfig = new HashSet<>();
                }
                providersConfig.addAll(Arrays.asList(GLOBAL_PROVIDERS.split(",")));
            }
        }
    }
    // another property to configure the scanning of providers but this one is consistent with current cxf config
    // the other one is more generic but need another file
    final String key = CXF_JAXRS_PREFIX + "skip-provider-scanning";
    final boolean ignoreAutoProviders = "true".equalsIgnoreCase(SystemInstance.get().getProperty(key, serviceConfiguration.getProperties().getProperty(key, "false")));
    final Collection<Object> additionalProviders = ignoreAutoProviders ? Collections.emptyList() : givenAdditionalProviders;
    List<Object> providers = null;
    if (providersConfig != null) {
        providers = ServiceInfos.resolve(services, providersConfig.toArray(new String[providersConfig.size()]), OpenEJBProviderFactory.INSTANCE);
        if (providers != null && additionalProviders != null && !additionalProviders.isEmpty()) {
            providers.addAll(providers(serviceConfiguration.getAvailableServices(), additionalProviders, ctx));
        }
    }
    if (providers == null) {
        providers = new ArrayList<>(4);
        if (additionalProviders != null && !additionalProviders.isEmpty()) {
            providers.addAll(providers(serviceConfiguration.getAvailableServices(), additionalProviders, ctx));
        }
    }
    if (!ignoreAutoProviders) {
        addMandatoryProviders(providers, serviceConfiguration);
        if (enforceCxfBvalMapper) {
            if (!shouldSkipProvider(CxfResponseValidationExceptionMapper.class.getName())) {
                providers.add(new CxfResponseValidationExceptionMapper());
            }
        }
    }
    SystemInstance.get().fireEvent(new ExtensionProviderRegistration(AppFinder.findAppContextOrWeb(Thread.currentThread().getContextClassLoader(), AppFinder.AppContextTransformer.INSTANCE), providers));
    if (!providers.isEmpty()) {
        factory.setProviders(providers);
    }
}
Also used : Message(org.apache.cxf.message.Message) JAXRSBeanValidationOutInterceptor(org.apache.cxf.jaxrs.validation.JAXRSBeanValidationOutInterceptor) BeanValidationFeature(org.apache.cxf.validation.BeanValidationFeature) Feature(org.apache.cxf.feature.Feature) EJBRestServiceInfo(org.apache.openejb.server.rest.EJBRestServiceInfo) ServiceInfo(org.apache.openejb.assembler.classic.ServiceInfo) JAXRSBeanValidationOutInterceptor(org.apache.cxf.jaxrs.validation.JAXRSBeanValidationOutInterceptor) Interceptor(org.apache.cxf.interceptor.Interceptor) BeanValidationInInterceptor(org.apache.cxf.validation.BeanValidationInInterceptor) JAXRSBeanValidationInInterceptor(org.apache.cxf.jaxrs.validation.JAXRSBeanValidationInInterceptor) BeanValidationOutInterceptor(org.apache.cxf.validation.BeanValidationOutInterceptor) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) BeanValidationFeature(org.apache.cxf.validation.BeanValidationFeature) BeanValidationInInterceptor(org.apache.cxf.validation.BeanValidationInInterceptor) JAXRSBeanValidationInInterceptor(org.apache.cxf.jaxrs.validation.JAXRSBeanValidationInInterceptor) ResourceComparator(org.apache.cxf.jaxrs.ext.ResourceComparator) JAXRSBeanValidationInInterceptor(org.apache.cxf.jaxrs.validation.JAXRSBeanValidationInInterceptor) ResponseConstraintViolationException(org.apache.cxf.validation.ResponseConstraintViolationException) IOException(java.io.IOException) EndpointException(org.apache.cxf.endpoint.EndpointException) ServletException(javax.servlet.ServletException) BusException(org.apache.cxf.BusException) BeanValidationProvider(org.apache.cxf.validation.BeanValidationProvider) JAXRSBeanValidationOutInterceptor(org.apache.cxf.jaxrs.validation.JAXRSBeanValidationOutInterceptor) BeanValidationOutInterceptor(org.apache.cxf.validation.BeanValidationOutInterceptor) ExtensionProviderRegistration(org.apache.openejb.server.cxf.rs.event.ExtensionProviderRegistration)

Example 2 with Feature

use of org.apache.cxf.feature.Feature in project cxf by apache.

the class BookServerThrottled method run.

protected void run() {
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(BookStore.class);
    List<Feature> features = new ArrayList<>();
    ThrottlingFeature tf = new ThrottlingFeature(new ThrottlingManagerImpl());
    features.add(tf);
    sf.setFeatures(features);
    sf.setResourceProvider(BookStore.class, new SingletonResourceProvider(new BookStore(), true));
    sf.setAddress("http://localhost:" + PORT + "/");
    server = sf.create();
}
Also used : JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) ArrayList(java.util.ArrayList) ThrottlingFeature(org.apache.cxf.throttling.ThrottlingFeature) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) Feature(org.apache.cxf.feature.Feature) ThrottlingFeature(org.apache.cxf.throttling.ThrottlingFeature)

Example 3 with Feature

use of org.apache.cxf.feature.Feature in project fabric8 by jboss-fuse.

the class Java2SwaggerJsonMojo method execute.

public void execute() throws MojoExecutionException {
    List<Class<?>> resourceClasses = loadResourceClasses();
    List<Object> resourceObjects = new ArrayList<Object>();
    for (Class<?> resourceClass : resourceClasses) {
        try {
            resourceObjects.add(resourceClass.newInstance());
        } catch (InstantiationException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        } catch (IllegalAccessException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }
    Thread.currentThread().setContextClassLoader(getClassLoader());
    List<Feature> features = new ArrayList<Feature>();
    features.add(new SwaggerFeature());
    JAXRSServerFactoryBean serverFacBean = new JAXRSServerFactoryBean();
    serverFacBean.setAddress(address);
    serverFacBean.setServiceBeans(resourceObjects);
    serverFacBean.setFeatures(features);
    Server server = serverFacBean.create();
    InputStream in = null;
    try {
        String res = "";
        for (Class<?> resourceClass : resourceClasses) {
            com.wordnik.swagger.annotations.Api api = resourceClass.getAnnotation(com.wordnik.swagger.annotations.Api.class);
            if (api != null) {
                String apiPath = api.value();
                String serverAddress = server.getEndpoint().getEndpointInfo().getAddress();
                String apiDocs = serverAddress + "/api-docs";
                URL url = new URL(apiDocs + apiPath);
                in = url.openStream();
                res = res + getStringFromInputStream(in);
            }
        }
        generateJson(resourceClasses, res);
    } catch (Exception e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } finally {
        server.stop();
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Server(org.apache.cxf.endpoint.Server) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) SwaggerFeature(io.fabric8.cxf.endpoint.SwaggerFeature) Feature(org.apache.cxf.feature.Feature) URL(java.net.URL) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) SwaggerFeature(io.fabric8.cxf.endpoint.SwaggerFeature)

Example 4 with Feature

use of org.apache.cxf.feature.Feature in project fabric8 by jboss-fuse.

the class ManagedApiFeature method initializeProvider.

@Override
protected void initializeProvider(InterceptorProvider provider, final Bus bus) {
    if (provider instanceof Endpoint) {
        EndpointImpl endpointImpl = (EndpointImpl) provider;
        List<Feature> features = endpointImpl.getActiveFeatures();
        if (features == null) {
            features = new ArrayList<Feature>();
            features.add(this);
            endpointImpl.initializeActiveFeatures(features);
        } else {
            features.add(this);
        }
    } else if (provider instanceof Bus) {
        FactoryBeanListenerManager factoryBeanListenerManager = bus.getExtension(FactoryBeanListenerManager.class);
        if (factoryBeanListenerManager == null) {
            factoryBeanListenerManager = new FactoryBeanListenerManager(bus);
        }
        factoryBeanListenerManager.addListener(new FactoryBeanListener() {

            @Override
            public void handleEvent(Event arg0, AbstractServiceFactoryBean arg1, Object... arg2) {
                if (arg0.equals(Event.SERVER_CREATED) && (arg2[0] instanceof Server)) {
                    Server server = (Server) arg2[0];
                    initialize(server, bus);
                }
            }
        });
    } else {
        List<Feature> features = (List<Feature>) bus.getFeatures();
        if (features == null) {
            features = new ArrayList<Feature>();
            features.add(this);
        } else {
            features.add(this);
        }
    }
}
Also used : AbstractServiceFactoryBean(org.apache.cxf.service.factory.AbstractServiceFactoryBean) Bus(org.apache.cxf.Bus) MBeanServer(javax.management.MBeanServer) Server(org.apache.cxf.endpoint.Server) EndpointImpl(org.apache.cxf.endpoint.EndpointImpl) ArrayList(java.util.ArrayList) Feature(org.apache.cxf.feature.Feature) AbstractFeature(org.apache.cxf.feature.AbstractFeature) FactoryBeanListenerManager(org.apache.cxf.service.factory.FactoryBeanListenerManager) Endpoint(org.apache.cxf.endpoint.Endpoint) ArrayList(java.util.ArrayList) List(java.util.List) FactoryBeanListener(org.apache.cxf.service.factory.FactoryBeanListener)

Example 5 with Feature

use of org.apache.cxf.feature.Feature in project tesb-rt-se by Talend.

the class JmsConfiguratorTest method testCreateAndConfigureEndpoint.

@Test
public void testCreateAndConfigureEndpoint() {
    HelloWorldImpl2 implementor = new HelloWorldImpl2();
    String address = "local://JmsUriConfiguratorTest";
    ep = Endpoint.publish(address, implementor);
    JmsConfigurator jmsConfigurator = JmsConfigurator.create(ep);
    Assert.assertNotNull(jmsConfigurator);
    Assert.assertEquals("HelloWorldImpl2Port", jmsConfigurator.getConfigurationPrefix());
    Assert.assertEquals(SERVICE_NAME, jmsConfigurator.getServiceName());
    JMSConfiguration jmsConf = new JMSConfiguration();
    jmsConfigurator.setJmsConfiguration(jmsConf);
    Endpoint ep2 = jmsConfigurator.configureEndpoint(ep);
    Assert.assertNotNull(ep2);
    Configuration cnf = jmsConfigurator.getConfiguration();
    Assert.assertNotNull(cnf);
    Assert.assertEquals("org.apache.activemq.jndi.ActiveMQInitialContextFactory", cnf.get("jndiInitialContextFactory"));
    Assert.assertEquals("jndi", cnf.get("variant"));
    Assert.assertEquals("ConnectionFactory", cnf.get("jndiConnectionFactoryName"));
    Assert.assertEquals("tcp://localhost:61616", cnf.get("jndiURL"));
    Assert.assertEquals("dynamicQueues/libraryprovider.queue", cnf.get("destinationName"));
    List<Feature> features = ((EndpointImpl) ep2).getFeatures();
    boolean jmsConfigFeaturePresent = false;
    for (Feature f : features) {
        if (f instanceof org.apache.cxf.transport.jms.JMSConfigFeature) {
            jmsConfigFeaturePresent = true;
            break;
        }
    }
    Assert.assertTrue(jmsConfigFeaturePresent);
}
Also used : JMSConfiguration(org.apache.cxf.transport.jms.JMSConfiguration) Configuration(org.talend.esb.mep.requestcallback.feature.Configuration) JMSConfiguration(org.apache.cxf.transport.jms.JMSConfiguration) EndpointImpl(org.apache.cxf.jaxws.EndpointImpl) Feature(org.apache.cxf.feature.Feature) HelloWorldImpl2(org.talend.esb.mep.requestcallback.test.internal.HelloWorldImpl2) JmsConfigurator(org.talend.esb.mep.requestcallback.beans.JmsConfigurator) Endpoint(javax.xml.ws.Endpoint) Test(org.junit.Test)

Aggregations

Feature (org.apache.cxf.feature.Feature)37 ArrayList (java.util.ArrayList)16 Bus (org.apache.cxf.Bus)8 Endpoint (org.apache.cxf.endpoint.Endpoint)7 EndpointImpl (org.apache.cxf.endpoint.EndpointImpl)7 List (java.util.List)6 Server (org.apache.cxf.endpoint.Server)6 AbstractFeature (org.apache.cxf.feature.AbstractFeature)6 LoggingFeature (org.apache.cxf.feature.LoggingFeature)6 IOException (java.io.IOException)5 QName (javax.xml.namespace.QName)5 Interceptor (org.apache.cxf.interceptor.Interceptor)5 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)4 Message (org.apache.cxf.message.Message)4 AbstractServiceFactoryBean (org.apache.cxf.service.factory.AbstractServiceFactoryBean)4 SingletonResourceProvider (org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)3 EndpointImpl (org.apache.cxf.jaxws.EndpointImpl)3 FactoryBeanListener (org.apache.cxf.service.factory.FactoryBeanListener)3