use of org.apache.cxf.jaxrs.provider.ServerProviderFactory in project cxf by apache.
the class InjectionUtils method createFromParameterHandler.
public static <T> T createFromParameterHandler(String value, Class<T> pClass, Type genericType, Annotation[] anns, Message message) {
T result = null;
if (message != null) {
ServerProviderFactory pf = ServerProviderFactory.getInstance(message);
ParamConverter<T> pm = pf.createParameterHandler(pClass, genericType, anns, message);
if (pm != null) {
result = pm.fromString(value);
}
}
return result;
}
use of org.apache.cxf.jaxrs.provider.ServerProviderFactory in project tomee by apache.
the class JAXRSServerFactoryBean method create.
/**
* Creates the JAX-RS Server instance
* @return the server
*/
public Server create() {
ClassLoaderHolder origLoader = null;
try {
Bus bus = getBus();
ClassLoader loader = bus.getExtension(ClassLoader.class);
if (loader != null) {
origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
}
serviceFactory.setBus(bus);
checkResources(true);
if (serviceFactory.getService() == null) {
serviceFactory.create();
}
Endpoint ep = createEndpoint();
getServiceFactory().sendEvent(FactoryBeanListener.Event.PRE_SERVER_CREATE, server);
server = new ServerImpl(getBus(), ep, getDestinationFactory(), getBindingFactory());
Invoker invoker = serviceFactory.getInvoker();
if (invoker == null) {
ep.getService().setInvoker(createInvoker());
} else {
ep.getService().setInvoker(invoker);
}
ServerProviderFactory factory = setupFactory(ep);
ep.put(Application.class.getName(), appProvider);
factory.setRequestPreprocessor(new RequestPreprocessor(languageMappings, extensionMappings));
ep.put(Bus.class.getName(), getBus());
if (documentLocation != null) {
ep.put(JAXRSUtils.DOC_LOCATION, documentLocation);
}
if (rc != null) {
ep.put("org.apache.cxf.jaxrs.comparator", rc);
}
checkPrivateEndpoint(ep);
applyBusFeatures(getBus());
applyFeatures();
updateClassResourceProviders(ep);
injectContexts(factory, (ApplicationInfo) ep.get(Application.class.getName()));
factory.applyDynamicFeatures(getServiceFactory().getClassResourceInfo());
getServiceFactory().sendEvent(FactoryBeanListener.Event.SERVER_CREATED, server, null, null);
if (start) {
try {
server.start();
} catch (RuntimeException re) {
if (!(re instanceof ServiceConstructionException && re.getMessage().startsWith("There is an endpoint already running on"))) {
// avoid destroying another server on the same endpoint url
// prevent resource leak if server really started by itself
server.destroy();
}
throw re;
}
}
} catch (Exception e) {
throw new ServiceConstructionException(e);
} finally {
if (origLoader != null) {
origLoader.reset();
}
}
return server;
}
use of org.apache.cxf.jaxrs.provider.ServerProviderFactory in project tomee by apache.
the class CxfRsHttpListener method fixProviders.
/**
* Fix providers set in ProviderFactory
* - changes default Jonhzon by the TomEE specific one
* - remove deactivated providers
* @param serviceConfiguration
*/
private void fixProviders(final ServiceConfiguration serviceConfiguration) {
final ServerProviderFactory spf = ServerProviderFactory.class.cast(server.getEndpoint().get(ServerProviderFactory.class.getName()));
for (final String field : asList("messageWriters", "messageReaders")) {
final List<ProviderInfo<?>> values = List.class.cast(Reflections.get(spf, field));
boolean customJsonProvider = false;
for (final ProviderInfo<?> o : values) {
// using getName to not suppose any classloader setup
final String name = o.getResourceClass().getName();
if ("org.apache.johnzon.jaxrs.ConfigurableJohnzonProvider".equals(name) || "org.apache.johnzon.jaxrs.jsonb.jaxrs.JsonbJaxrsProvider".equals(name) || // contains in case of proxying
name.contains("com.fasterxml.jackson.jaxrs.json")) {
customJsonProvider = true;
// cause we only handle json for now
break;
}
}
final Iterator<ProviderInfo<?>> it = values.iterator();
while (it.hasNext()) {
final String name = it.next().getResourceClass().getName();
// remove JohnzonProvider default versions
if (("org.apache.johnzon.jaxrs.JohnzonProvider".equals(name) || "org.apache.openejb.server.cxf.rs.CxfRSService$TomEEJohnzonProvider".equals(name)) && customJsonProvider) {
it.remove();
}
// remove deactivated providers
if (!isActive(name, serviceConfiguration)) {
it.remove();
}
}
}
}
use of org.apache.cxf.jaxrs.provider.ServerProviderFactory in project cxf by apache.
the class JAXRSServerFactoryBeanTest method testRegisterProviders.
@Test
public void testRegisterProviders() {
JAXRSServerFactoryBean bean = new JAXRSServerFactoryBean();
bean.setAddress("http://localhost:8080/rest");
bean.setStart(false);
bean.setResourceClasses(BookStore.class);
List<Object> providers = new ArrayList<>();
Object provider1 = new CustomExceptionMapper();
providers.add(provider1);
Object provider2 = new RuntimeExceptionMapper();
providers.add(provider2);
bean.setProviders(providers);
Server s = bean.create();
ServerProviderFactory factory = (ServerProviderFactory) s.getEndpoint().get(ServerProviderFactory.class.getName());
ExceptionMapper<Exception> mapper1 = factory.createExceptionMapper(Exception.class, new MessageImpl());
assertNotNull(mapper1);
ExceptionMapper<RuntimeException> mapper2 = factory.createExceptionMapper(RuntimeException.class, new MessageImpl());
assertNotNull(mapper2);
assertNotSame(mapper1, mapper2);
assertSame(provider1, mapper1);
assertSame(provider2, mapper2);
}
use of org.apache.cxf.jaxrs.provider.ServerProviderFactory in project cxf by apache.
the class JAXRSServerFactoryBean method create.
/**
* Creates the JAX-RS Server instance
* @return the server
*/
public Server create() {
ClassLoaderHolder origLoader = null;
try {
Bus bus = getBus();
ClassLoader loader = bus.getExtension(ClassLoader.class);
if (loader != null) {
origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
}
serviceFactory.setBus(bus);
checkResources(true);
if (serviceFactory.getService() == null) {
serviceFactory.create();
}
Endpoint ep = createEndpoint();
getServiceFactory().sendEvent(FactoryBeanListener.Event.PRE_SERVER_CREATE, server);
server = new ServerImpl(getBus(), ep, getDestinationFactory(), getBindingFactory());
Invoker invoker = serviceFactory.getInvoker();
if (invoker == null) {
ep.getService().setInvoker(createInvoker());
} else {
ep.getService().setInvoker(invoker);
}
ServerProviderFactory factory = setupFactory(ep);
ep.put(Application.class.getName(), appProvider);
factory.setRequestPreprocessor(new RequestPreprocessor(languageMappings, extensionMappings));
ep.put(Bus.class.getName(), getBus());
if (documentLocation != null) {
ep.put(JAXRSUtils.DOC_LOCATION, documentLocation);
}
if (rc != null) {
ep.put("org.apache.cxf.jaxrs.comparator", rc);
}
checkPrivateEndpoint(ep);
applyBusFeatures(getBus());
applyFeatures();
updateClassResourceProviders(ep);
injectContexts(factory, (ApplicationInfo) ep.get(Application.class.getName()));
factory.applyDynamicFeatures(getServiceFactory().getClassResourceInfo());
getServiceFactory().sendEvent(FactoryBeanListener.Event.SERVER_CREATED, server, null, null);
if (start) {
try {
server.start();
} catch (RuntimeException re) {
if (!(re instanceof ServiceConstructionException && re.getMessage().startsWith("There is an endpoint already running on"))) {
// avoid destroying another server on the same endpoint url
// prevent resource leak if server really started by itself
server.destroy();
}
throw re;
}
}
} catch (Exception e) {
throw new ServiceConstructionException(e);
} finally {
if (origLoader != null) {
origLoader.reset();
}
}
return server;
}
Aggregations