Search in sources :

Example 26 with SingletonResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider in project cxf by apache.

the class ClientResponseFilterTest method setUp.

@Before
public void setUp() throws Exception {
    final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(TestEndpoint.class);
    sf.setResourceProvider(TestEndpoint.class, new SingletonResourceProvider(new TestEndpoint(), false));
    sf.setTransportId(LocalTransportFactory.TRANSPORT_ID);
    sf.setAddress(ADDRESS);
    server = sf.create();
}
Also used : JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) Before(org.junit.Before)

Example 27 with SingletonResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider in project cxf by apache.

the class ResourceUtils method createApplication.

@SuppressWarnings("unchecked")
public static JAXRSServerFactoryBean createApplication(Application app, boolean ignoreAppPath, boolean staticSubresourceResolution, boolean useSingletonResourceProvider, Bus bus) {
    Set<Object> singletons = app.getSingletons();
    verifySingletons(singletons);
    List<Class<?>> resourceClasses = new ArrayList<>();
    List<Object> providers = new ArrayList<>();
    List<Feature> features = new ArrayList<>();
    Map<Class<?>, ResourceProvider> map = new HashMap<>();
    // or singleton provider classes
    for (Class<?> cls : app.getClasses()) {
        if (isValidApplicationClass(cls, singletons)) {
            if (isValidProvider(cls)) {
                providers.add(createProviderInstance(cls));
            } else if (Feature.class.isAssignableFrom(cls)) {
                features.add(createFeatureInstance((Class<? extends Feature>) cls));
            } else {
                resourceClasses.add(cls);
                if (useSingletonResourceProvider) {
                    map.put(cls, new SingletonResourceProvider(createProviderInstance(cls)));
                } else {
                    map.put(cls, new PerRequestResourceProvider(cls));
                }
            }
        }
    }
    // we can get either a provider or resource class here
    for (Object o : singletons) {
        if (isValidProvider(o.getClass())) {
            providers.add(o);
        } else if (o instanceof Feature) {
            features.add((Feature) o);
        } else {
            resourceClasses.add(o.getClass());
            map.put(o.getClass(), new SingletonResourceProvider(o));
        }
    }
    JAXRSServerFactoryBean bean = new JAXRSServerFactoryBean();
    if (bus != null) {
        bean.setBus(bus);
    }
    String address = "/";
    if (!ignoreAppPath) {
        ApplicationPath appPath = locateApplicationPath(app.getClass());
        if (appPath != null) {
            address = appPath.value();
        }
    }
    if (!address.startsWith("/")) {
        address = "/" + address;
    }
    bean.setAddress(address);
    bean.setStaticSubresourceResolution(staticSubresourceResolution);
    bean.setResourceClasses(resourceClasses);
    bean.setProviders(providers);
    bean.getFeatures().addAll(features);
    for (Map.Entry<Class<?>, ResourceProvider> entry : map.entrySet()) {
        bean.setResourceProvider(entry.getKey(), entry.getValue());
    }
    Map<String, Object> appProps = app.getProperties();
    if (appProps != null) {
        bean.getProperties(true).putAll(appProps);
    }
    bean.setApplication(app);
    return bean;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) ApplicationPath(javax.ws.rs.ApplicationPath) Feature(org.apache.cxf.feature.Feature) PerRequestResourceProvider(org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider) ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) ElementClass(org.apache.cxf.jaxrs.ext.xml.ElementClass) PerRequestResourceProvider(org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider) Map(java.util.Map) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 28 with SingletonResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider in project cxf by apache.

the class BookServer method createServer.

@Override
protected Server createServer(Bus bus) throws Exception {
    bus.setProperty(ExceptionMapper.class.getName(), new BusMapperExceptionMapper());
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(BookStore.class, SimpleBookStore.class, BookStorePerRequest.class, BookStoreRegex.class);
    // default lifecycle is per-request, change it to singleton
    final BinaryDataProvider<Object> p = new BinaryDataProvider<>();
    p.setProduceMediaTypes(Collections.singletonList("application/bar"));
    p.setEnableBuffering(true);
    p.setReportByteArraySize(true);
    final JAXBElementProvider<?> jaxbProvider = new JAXBElementProvider<>();
    jaxbProvider.setJaxbElementClassMap(Collections.singletonMap(BookNoXmlRootElement.class.getName(), "BookNoXmlRootElement"));
    sf.setProviders(Arrays.asList(p, new BookStore.PrimitiveIntArrayReaderWriter(), new BookStore.PrimitiveDoubleArrayReaderWriter(), new BookStore.StringArrayBodyReaderWriter(), new BookStore.StringListBodyReaderWriter(), new StreamingResponseProvider<Object>(), new ContentTypeModifyingMBW(), jaxbProvider, new FormatResponseHandler(), new GenericHandlerWriter(), new FaultyRequestHandler(), new SearchContextProvider(), new QueryContextProvider(), new BlockingRequestFilter(), new FaultyResponseFilter(), new BlockedExceptionMapper(), new ParamConverterImpl()));
    sf.setInInterceptors(Arrays.asList(new CustomInFaultyInterceptor(), new LoggingInInterceptor()));
    sf.setOutInterceptors(Arrays.asList(new CustomOutInterceptor(), new LoggingOutInterceptor()));
    sf.setOutFaultInterceptors(Arrays.asList(new CustomOutFaultInterceptor()));
    sf.setResourceProvider(BookStore.class, new SingletonResourceProvider(new BookStore(), true));
    sf.setAddress("http://localhost:" + PORT + "/");
    sf.getProperties(true).put("org.apache.cxf.jaxrs.mediaTypeCheck.strict", true);
    sf.getProperties().put("search.visitor", new SQLPrinterVisitor<SearchBean>("books"));
    sf.getProperties().put("org.apache.cxf.http.header.split", true);
    sf.getProperties().put("default.content.type", "*/*");
    sf.getProperties().putAll(properties);
    return sf.create();
}
Also used : SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) ResponseExceptionMapper(org.apache.cxf.jaxrs.client.ResponseExceptionMapper) LoggingInInterceptor(org.apache.cxf.ext.logging.LoggingInInterceptor) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) SearchBean(org.apache.cxf.jaxrs.ext.search.SearchBean) BinaryDataProvider(org.apache.cxf.jaxrs.provider.BinaryDataProvider) SearchContextProvider(org.apache.cxf.jaxrs.ext.search.SearchContextProvider) JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) LoggingOutInterceptor(org.apache.cxf.ext.logging.LoggingOutInterceptor) StreamingResponseProvider(org.apache.cxf.jaxrs.provider.StreamingResponseProvider) QueryContextProvider(org.apache.cxf.jaxrs.ext.search.QueryContextProvider)

Example 29 with SingletonResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider in project cxf by apache.

the class BookServerThrottled method createServer.

@Override
protected Server createServer(Bus bus) throws Exception {
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(BookStore.class);
    sf.setFeatures(Collections.singletonList(new ThrottlingFeature(new ThrottlingManagerImpl())));
    sf.setResourceProvider(BookStore.class, new SingletonResourceProvider(new BookStore(), true));
    sf.setAddress("http://localhost:" + PORT + "/");
    return sf.create();
}
Also used : JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) ThrottlingFeature(org.apache.cxf.throttling.ThrottlingFeature) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)

Example 30 with SingletonResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider in project cxf by apache.

the class UserLDAPServer method run.

protected void run() {
    // First server
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(UserService.class);
    sf.setResourceProvider(UserService.class, new SingletonResourceProvider(new UserServiceImpl()));
    sf.setProviders(Collections.singletonList(new org.apache.cxf.jaxrs.ext.search.SearchContextProvider()));
    sf.setAddress("http://localhost:" + PORT + "/");
    sf.create();
    // Second server - don't encode query values
    sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(UserService.class);
    UserServiceImpl userService = new UserServiceImpl();
    userService.setEncodeQueryValues(false);
    sf.setResourceProvider(UserService.class, new SingletonResourceProvider(userService));
    sf.setProviders(Collections.singletonList(new org.apache.cxf.jaxrs.ext.search.SearchContextProvider()));
    sf.setAddress("http://localhost:" + PORT2 + "/");
    sf.create();
}
Also used : JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)

Aggregations

SingletonResourceProvider (org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)84 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)63 Bus (org.apache.cxf.Bus)17 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)15 ArrayList (java.util.ArrayList)14 LoggingOutInterceptor (org.apache.cxf.ext.logging.LoggingOutInterceptor)14 ResourceProvider (org.apache.cxf.jaxrs.lifecycle.ResourceProvider)9 HashMap (java.util.HashMap)7 Before (org.junit.Before)7 SpringBusFactory (org.apache.cxf.bus.spring.SpringBusFactory)5 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)5 Map (java.util.Map)4 ReactiveIOCustomizer (org.apache.cxf.jaxrs.rx3.server.ReactiveIOCustomizer)4 DetectorResource (org.apache.tika.server.resource.DetectorResource)4 MetadataResource (org.apache.tika.server.resource.MetadataResource)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 Feature (org.apache.cxf.feature.Feature)3 ReactiveIOCustomizer (org.apache.cxf.jaxrs.rx2.server.ReactiveIOCustomizer)3 Message (org.apache.cxf.message.Message)3