Search in sources :

Example 46 with JAXRSServerFactoryBean

use of org.apache.cxf.jaxrs.JAXRSServerFactoryBean in project cxf by apache.

the class JAXRSUtilsTest method testInjectCustomContext.

@Test
public void testInjectCustomContext() throws Exception {
    final CustomerContext contextImpl = new CustomerContext() {

        public String get() {
            return "customerContext";
        }
    };
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    Customer customer = new Customer();
    sf.setServiceBeanObjects(customer);
    sf.setProvider(new ContextProvider<CustomerContext>() {

        public CustomerContext createContext(Message message) {
            return contextImpl;
        }
    });
    sf.setStart(false);
    Server s = sf.create();
    assertTrue(customer.getCustomerContext() instanceof ThreadLocalProxy<?>);
    invokeCustomerMethod(sf.getServiceFactory().getClassResourceInfo().get(0), customer, s);
    CustomerContext context = customer.getCustomerContext();
    assertEquals("customerContext", context.get());
}
Also used : Message(org.apache.cxf.message.Message) Server(org.apache.cxf.endpoint.Server) Customer(org.apache.cxf.jaxrs.Customer) CustomerContext(org.apache.cxf.jaxrs.Customer.CustomerContext) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) Test(org.junit.Test)

Example 47 with JAXRSServerFactoryBean

use of org.apache.cxf.jaxrs.JAXRSServerFactoryBean in project cxf by apache.

the class ResourceUtilsTest method shouldCreateApplicationWhichInheritsApplicationPath.

@Test
public void shouldCreateApplicationWhichInheritsApplicationPath() throws Exception {
    JAXRSServerFactoryBean application = ResourceUtils.createApplication(new SuperApplication(), false, false, false, null);
    assertEquals("/base", application.getAddress());
}
Also used : JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) Test(org.junit.Test)

Example 48 with JAXRSServerFactoryBean

use of org.apache.cxf.jaxrs.JAXRSServerFactoryBean in project cxf by apache.

the class AbstractSpringComponentScanServer method createJaxRsServer.

@Override
protected Server createJaxRsServer() {
    JAXRSServerFactoryBean factoryBean = null;
    String[] beanNames = applicationContext.getBeanNamesForAnnotation(ApplicationPath.class);
    if (beanNames.length > 0) {
        Set<String> componentScanPackagesSet = parseSetProperty(componentScanPackages);
        Set<String> componentScanBeansSet = parseSetProperty(componentScanBeans);
        for (String beanName : beanNames) {
            if (isComponentMatched(beanName, componentScanPackagesSet, componentScanBeansSet)) {
                Application app = applicationContext.getBean(beanName, Application.class);
                factoryBean = createFactoryBeanFromApplication(app);
                for (String cxfBeanName : applicationContext.getBeanNamesForAnnotation(org.apache.cxf.annotations.Provider.class)) {
                    if (isComponentMatched(cxfBeanName, componentScanPackagesSet, componentScanBeansSet)) {
                        addCxfProvider(getProviderBean(cxfBeanName));
                    }
                }
                break;
            }
        }
    }
    if (!StringUtils.isEmpty(classesScanPackages)) {
        try {
            final Map<Class<? extends Annotation>, Collection<Class<?>>> appClasses = ClasspathScanner.findClasses(classesScanPackages, ApplicationPath.class);
            List<Application> apps = CastUtils.cast(JAXRSServerFactoryBeanDefinitionParser.createBeansFromDiscoveredClasses(super.applicationContext, appClasses.get(ApplicationPath.class), null));
            if (apps.size() > 0) {
                factoryBean = createFactoryBeanFromApplication(apps.get(0));
                final Map<Class<? extends Annotation>, Collection<Class<?>>> cxfClasses = ClasspathScanner.findClasses(classesScanPackages, org.apache.cxf.annotations.Provider.class);
                addCxfProvidersFromClasses(cxfClasses.get(org.apache.cxf.annotations.Provider.class));
            }
        } catch (Exception ex) {
            throw new ServiceConstructionException(ex);
        }
    }
    if (factoryBean != null) {
        setFactoryCxfProviders(factoryBean);
        return factoryBean.create();
    }
    return super.createJaxRsServer();
}
Also used : JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) Annotation(java.lang.annotation.Annotation) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) Provider(javax.ws.rs.ext.Provider) Collection(java.util.Collection) Application(javax.ws.rs.core.Application)

Example 49 with JAXRSServerFactoryBean

use of org.apache.cxf.jaxrs.JAXRSServerFactoryBean in project cxf by apache.

the class MultipartServer method run.

protected void run() {
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(MultipartStore.class);
    Map<String, Object> props = new HashMap<>();
    props.put(AttachmentDeserializer.ATTACHMENT_MAX_SIZE, String.valueOf(1024 * 10));
    props.put(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD, String.valueOf(1024 * 5));
    props.put(AttachmentDeserializer.ATTACHMENT_MAX_HEADER_SIZE, String.valueOf(400));
    sf.setProperties(props);
    // default lifecycle is per-request, change it to singleton
    sf.setResourceProvider(MultipartStore.class, new SingletonResourceProvider(new MultipartStore()));
    sf.setAddress("http://localhost:" + PORT + "/");
    server = sf.create();
}
Also used : HashMap(java.util.HashMap) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)

Example 50 with JAXRSServerFactoryBean

use of org.apache.cxf.jaxrs.JAXRSServerFactoryBean in project cxf by apache.

the class BookServerWebSocket method run.

protected void run() {
    Bus bus = BusFactory.getDefaultBus();
    setBus(bus);
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setBus(bus);
    sf.setResourceClasses(BookStoreWebSocket.class, BookStorePerRequest.class);
    sf.setProvider(new StreamingResponseProvider<Book>());
    sf.setResourceProvider(BookStoreWebSocket.class, new SingletonResourceProvider(new BookStoreWebSocket(), true));
    sf.setAddress("ws://localhost:" + port + "/websocket");
    server = sf.create();
    BusFactory.setDefaultBus(null);
    BusFactory.setThreadDefaultBus(null);
}
Also used : Bus(org.apache.cxf.Bus) Book(org.apache.cxf.systest.jaxrs.Book) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)

Aggregations

JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)63 SingletonResourceProvider (org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)30 Bus (org.apache.cxf.Bus)11 Test (org.junit.Test)10 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)7 ArrayList (java.util.ArrayList)7 Application (javax.ws.rs.core.Application)6 HashMap (java.util.HashMap)5 LoggingOutInterceptor (org.apache.cxf.ext.logging.LoggingOutInterceptor)5 ResourceProvider (org.apache.cxf.jaxrs.lifecycle.ResourceProvider)4 Before (org.junit.Before)4 Map (java.util.Map)3 UriInfo (javax.ws.rs.core.UriInfo)3 BindingFactoryManager (org.apache.cxf.binding.BindingFactoryManager)3 Server (org.apache.cxf.endpoint.Server)3 JAXRSBindingFactory (org.apache.cxf.jaxrs.JAXRSBindingFactory)3 Bean (org.springframework.context.annotation.Bean)3 Annotation (java.lang.annotation.Annotation)2 Endpoint (org.apache.cxf.endpoint.Endpoint)2 ManagedEndpoint (org.apache.cxf.endpoint.ManagedEndpoint)2