use of org.apache.cxf.jaxrs.model.ProviderInfo in project cxf by apache.
the class MicroProfileClientProxyImpl method mergeHeaders.
private void mergeHeaders(Class<ClientHeadersFactory> factoryCls, MultivaluedMap<String, String> existingHeaders) {
try {
ClientHeadersFactory factory;
Message m = JAXRS_UTILS_GET_CURRENT_MESSAGE_METHOD == null ? null : (Message) JAXRS_UTILS_GET_CURRENT_MESSAGE_METHOD.invoke(null);
if (m != null) {
factory = CDIFacade.getInstanceFromCDI(factoryCls, m.getExchange().getBus()).map(this::mapInstance).orElse(factoryCls.newInstance());
ProviderInfo<ClientHeadersFactory> pi = clientHeaderFactories.computeIfAbsent(factoryCls, k -> {
return new ProviderInfo<ClientHeadersFactory>(factory, m.getExchange().getBus(), true);
});
InjectionUtils.injectContexts(factory, pi, m);
} else {
factory = CDIFacade.getInstanceFromCDI(factoryCls).map(this::mapInstance).orElse(factoryCls.newInstance());
}
MultivaluedMap<String, String> updatedHeaders = factory.update(getJaxrsHeaders(m), existingHeaders);
existingHeaders.putAll(updatedHeaders);
} catch (Throwable t) {
// expected if not running in a JAX-RS server environment.
if (LOG.isLoggable(Level.FINEST)) {
LOG.log(Level.FINEST, "Caught exception getting JAX-RS incoming headers", t);
}
}
}
use of org.apache.cxf.jaxrs.model.ProviderInfo in project tomee by apache.
the class ProviderFactory method createMessageBodyWriterInterceptor.
public <T> List<WriterInterceptor> createMessageBodyWriterInterceptor(Class<T> bodyType, Type parameterType, Annotation[] parameterAnnotations, MediaType mediaType, Message m, Set<String> names) {
MessageBodyWriter<T> mw = createMessageBodyWriter(bodyType, parameterType, parameterAnnotations, mediaType, m);
int size = writerInterceptors.size();
if (mw != null || size > 0) {
@SuppressWarnings({ "unchecked", "rawtypes" }) WriterInterceptor mbwWriter = new WriterInterceptorMBW((MessageBodyWriter) mw, m);
final List<WriterInterceptor> interceptors;
if (size > 0) {
interceptors = new ArrayList<>(size + 1);
List<ProviderInfo<WriterInterceptor>> writers = getBoundFilters(writerInterceptors, names);
for (ProviderInfo<WriterInterceptor> p : writers) {
injectContextValues(p, m);
interceptors.add(p.getProvider());
}
interceptors.add(mbwWriter);
} else {
interceptors = Collections.singletonList(mbwWriter);
}
return interceptors;
}
return null;
}
use of org.apache.cxf.jaxrs.model.ProviderInfo in project tomee by apache.
the class ProviderFactory method createProviderFromConstructor.
public static ProviderInfo<? extends Object> createProviderFromConstructor(Constructor<?> c, Map<Class<?>, Object> values, Bus theBus, boolean checkContexts, boolean custom) {
Map<Class<?>, Map<Class<?>, ThreadLocalProxy<?>>> proxiesMap = CastUtils.cast((Map<?, ?>) theBus.getProperty(AbstractResourceInfo.CONSTRUCTOR_PROXY_MAP));
Map<Class<?>, ThreadLocalProxy<?>> existingProxies = null;
if (proxiesMap != null) {
existingProxies = proxiesMap.get(c.getDeclaringClass());
}
Class<?>[] paramTypes = c.getParameterTypes();
Object[] cArgs = ResourceUtils.createConstructorArguments(c, null, false, values);
if (existingProxies != null && existingProxies.size() <= paramTypes.length) {
for (int i = 0; i < paramTypes.length; i++) {
if (cArgs[i] instanceof ThreadLocalProxy) {
cArgs[i] = existingProxies.get(paramTypes[i]);
}
}
}
final Object instance;
try {
instance = c.newInstance(cArgs);
} catch (Throwable ex) {
throw new RuntimeException("Resource or provider class " + c.getDeclaringClass().getName() + " can not be instantiated", ex);
}
Map<Class<?>, ThreadLocalProxy<?>> proxies = new LinkedHashMap<>();
for (int i = 0; i < paramTypes.length; i++) {
if (cArgs[i] instanceof ThreadLocalProxy) {
@SuppressWarnings("unchecked") ThreadLocalProxy<Object> proxy = (ThreadLocalProxy<Object>) cArgs[i];
proxies.put(paramTypes[i], proxy);
}
}
boolean isApplication = Application.class.isAssignableFrom(c.getDeclaringClass());
if (isApplication) {
return new ApplicationInfo((Application) instance, proxies, theBus);
}
return new ProviderInfo<Object>(instance, proxies, theBus, checkContexts, custom);
}
use of org.apache.cxf.jaxrs.model.ProviderInfo 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.model.ProviderInfo in project cxf by apache.
the class ProviderFactoryTest method testCustomProviderSortingWithBus2.
@Test
public void testCustomProviderSortingWithBus2() {
WildcardReader wc1 = new WildcardReader();
WildcardReader2 wc2 = new WildcardReader2();
Bus bus = BusFactory.newInstance().createBus();
bus.setProperty(MessageBodyReader.class.getName(), wc2);
ProviderFactory pf = ServerProviderFactory.createInstance(bus);
pf.registerUserProvider(wc1);
List<ProviderInfo<MessageBodyReader<?>>> readers = pf.getMessageReaders();
assertEquals(11, readers.size());
assertSame(wc1, readers.get(7).getProvider());
assertSame(wc2, readers.get(8).getProvider());
}
Aggregations