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();
}
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;
}
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();
}
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();
}
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();
}
Aggregations