Search in sources :

Example 1 with ContextProvider

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;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) ContextProvider(org.apache.cxf.jaxrs.ext.ContextProvider)

Example 2 with ContextProvider

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);
        }
    }
}
Also used : Field(java.lang.reflect.Field) Method(java.lang.reflect.Method) ContextProvider(org.apache.cxf.jaxrs.ext.ContextProvider) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 3 with ContextProvider

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);
}
Also used : SecurityContextImpl(org.apache.cxf.jaxrs.impl.SecurityContextImpl) ResourceContext(javax.ws.rs.container.ResourceContext) Message(org.apache.cxf.message.Message) Configuration(javax.ws.rs.core.Configuration) ResourceInfoImpl(org.apache.cxf.jaxrs.impl.ResourceInfoImpl) ContextProvider(org.apache.cxf.jaxrs.ext.ContextProvider) Providers(javax.ws.rs.ext.Providers) ProvidersImpl(org.apache.cxf.jaxrs.impl.ProvidersImpl) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) SecurityContext(javax.ws.rs.core.SecurityContext) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) ResourceContextImpl(org.apache.cxf.jaxrs.impl.ResourceContextImpl) RequestImpl(org.apache.cxf.jaxrs.impl.RequestImpl) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl)

Aggregations

ContextProvider (org.apache.cxf.jaxrs.ext.ContextProvider)3 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 WildcardType (java.lang.reflect.WildcardType)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ResourceContext (javax.ws.rs.container.ResourceContext)1 Configuration (javax.ws.rs.core.Configuration)1 MediaType (javax.ws.rs.core.MediaType)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 SecurityContext (javax.ws.rs.core.SecurityContext)1 Providers (javax.ws.rs.ext.Providers)1 MessageContext (org.apache.cxf.jaxrs.ext.MessageContext)1 MessageContextImpl (org.apache.cxf.jaxrs.ext.MessageContextImpl)1 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)1 ProvidersImpl (org.apache.cxf.jaxrs.impl.ProvidersImpl)1 RequestImpl (org.apache.cxf.jaxrs.impl.RequestImpl)1