use of org.apache.cxf.jaxrs.ext.ContextProvider in project cxf by apache.
the class ProviderFactory method createContextProvider.
@SuppressWarnings("unchecked")
public <T> ContextProvider<T> createContextProvider(Type contextType, Message m) {
Class<?> contextCls = InjectionUtils.getActualType(contextType);
if (contextCls == null) {
return null;
}
for (ProviderInfo<ContextProvider<?>> cr : contextProviders) {
Type[] types = cr.getProvider().getClass().getGenericInterfaces();
for (Type t : types) {
if (t instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) t;
Type[] args = pt.getActualTypeArguments();
if (args.length > 0) {
Class<?> argCls = InjectionUtils.getActualType(args[0]);
if (argCls != null && argCls.isAssignableFrom(contextCls)) {
return (ContextProvider<T>) cr.getProvider();
}
}
}
}
}
return null;
}
use of org.apache.cxf.jaxrs.ext.ContextProvider in project cxf by apache.
the class InjectionUtils method injectContextProxiesAndApplication.
public static void injectContextProxiesAndApplication(AbstractResourceInfo cri, Object instance, Application app, ProviderFactory factory) {
if (!cri.contextsAvailable() || !cri.isSingleton()) {
return;
}
synchronized (instance) {
for (Map.Entry<Class<?>, Method> entry : cri.getContextMethods().entrySet()) {
Method method = entry.getValue();
Object value = null;
Class<?> cls = method.getParameterTypes()[0];
if (cls == Application.class) {
value = app;
} else if (VALUE_CONTEXTS.contains(cls.getName()) && factory != null) {
ContextProvider<?> p = factory.createContextProvider(cls, null);
if (p != null) {
value = p.createContext(null);
}
} else {
value = cri.getContextSetterProxy(method);
}
try {
if (value == InjectionUtils.extractFromMethod(instance, getGetterFromSetter(method), false)) {
continue;
}
} catch (Throwable t) {
// continue
}
InjectionUtils.injectThroughMethod(instance, method, value);
}
for (Field f : cri.getContextFields()) {
Object value = null;
Class<?> cls = f.getType();
if (cls == Application.class) {
value = app;
} else if (VALUE_CONTEXTS.contains(cls.getName()) && factory != null) {
ContextProvider<?> p = factory.createContextProvider(cls, null);
if (p != null) {
value = p.createContext(null);
}
} else {
value = cri.getContextFieldProxy(f);
}
try {
if (value == InjectionUtils.extractFieldValue(f, instance)) {
continue;
}
} catch (Throwable t) {
// continue
}
InjectionUtils.injectFieldValue(f, instance, value);
}
}
}
use of org.apache.cxf.jaxrs.ext.ContextProvider in project cxf by apache.
the class JAXRSUtils method createContextValue.
public static <T> T createContextValue(Message m, Type genericType, Class<T> clazz) {
Message contextMessage = getContextMessage(m);
Object o = null;
if (UriInfo.class.isAssignableFrom(clazz)) {
o = createUriInfo(contextMessage);
} else if (HttpHeaders.class.isAssignableFrom(clazz) || ProtocolHeaders.class.isAssignableFrom(clazz)) {
o = createHttpHeaders(contextMessage, clazz);
} else if (SecurityContext.class.isAssignableFrom(clazz)) {
SecurityContext customContext = contextMessage.get(SecurityContext.class);
o = customContext == null ? new SecurityContextImpl(contextMessage) : customContext;
} else if (MessageContext.class.isAssignableFrom(clazz)) {
o = new MessageContextImpl(m);
} else if (ResourceInfo.class.isAssignableFrom(clazz)) {
o = new ResourceInfoImpl(contextMessage);
} else if (ResourceContext.class.isAssignableFrom(clazz)) {
o = new ResourceContextImpl(contextMessage, contextMessage.getExchange().get(OperationResourceInfo.class));
} else if (Request.class.isAssignableFrom(clazz)) {
o = new RequestImpl(contextMessage);
} else if (Providers.class.isAssignableFrom(clazz)) {
o = new ProvidersImpl(contextMessage);
} else if (ContextResolver.class.isAssignableFrom(clazz)) {
o = createContextResolver(genericType, contextMessage);
} else if (Configuration.class.isAssignableFrom(clazz)) {
o = ProviderFactory.getInstance(contextMessage).getConfiguration(contextMessage);
} else if (Application.class.isAssignableFrom(clazz)) {
ProviderInfo<?> providerInfo = (ProviderInfo<?>) contextMessage.getExchange().getEndpoint().get(Application.class.getName());
o = providerInfo == null ? null : providerInfo.getProvider();
} else if (contextMessage != null) {
ContextProvider<?> provider = ProviderFactory.getInstance(contextMessage).createContextProvider(clazz, contextMessage);
if (provider != null) {
o = provider.createContext(contextMessage);
}
}
if (o == null && contextMessage != null && !MessageUtils.isRequestor(contextMessage)) {
o = HttpUtils.createServletResourceValue(contextMessage, clazz);
}
return clazz.cast(o);
}
Aggregations