Search in sources :

Example 6 with ThreadLocalProxy

use of org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy in project cxf by apache.

the class JAXRSUtilsTest method testSingletonHttpResourceFields.

@SuppressWarnings("unchecked")
@Test
public void testSingletonHttpResourceFields() throws Exception {
    ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
    Customer c = new Customer();
    cri.setResourceProvider(new SingletonResourceProvider(c));
    Message m = createMessage();
    ServletContext servletContextMock = EasyMock.createNiceMock(ServletContext.class);
    m.put(AbstractHTTPDestination.HTTP_CONTEXT, servletContextMock);
    HttpServletRequest httpRequest = EasyMock.createNiceMock(HttpServletRequest.class);
    m.put(AbstractHTTPDestination.HTTP_REQUEST, httpRequest);
    HttpServletResponse httpResponse = EasyMock.createMock(HttpServletResponse.class);
    m.put(AbstractHTTPDestination.HTTP_RESPONSE, httpResponse);
    InjectionUtils.injectContextProxies(cri, cri.getResourceProvider().getInstance(null));
    InjectionUtils.injectContextFields(c, cri, m);
    assertSame(servletContextMock, ((ThreadLocalProxy<ServletContext>) c.getServletContextResource()).get());
    HttpServletRequest currentReq = ((ThreadLocalProxy<HttpServletRequest>) c.getServletRequestResource()).get();
    assertSame(httpRequest, ((HttpServletRequestFilter) currentReq).getRequest());
    HttpServletResponseFilter filter = (HttpServletResponseFilter) ((ThreadLocalProxy<HttpServletResponse>) c.getServletResponseResource()).get();
    assertSame(httpResponse, filter.getResponse());
}
Also used : ThreadLocalHttpServletRequest(org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ThreadLocalProxy(org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy) Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) HttpServletResponseFilter(org.apache.cxf.jaxrs.impl.HttpServletResponseFilter) Test(org.junit.Test)

Example 7 with ThreadLocalProxy

use of org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy in project cxf by apache.

the class InjectionUtils method createThreadLocalProxy.

// TODO : investigate the possibility of using generic proxies only
@SuppressWarnings("unchecked")
public static <T> ThreadLocalProxy<T> createThreadLocalProxy(Class<T> type) {
    ThreadLocalProxy<?> proxy = null;
    if (UriInfo.class.isAssignableFrom(type)) {
        proxy = new ThreadLocalUriInfo();
    } else if (HttpHeaders.class.isAssignableFrom(type)) {
        proxy = new ThreadLocalHttpHeaders();
    } else if (ProtocolHeaders.class.isAssignableFrom(type)) {
        proxy = new ThreadLocalProtocolHeaders();
    } else if (SecurityContext.class.isAssignableFrom(type)) {
        proxy = new ThreadLocalSecurityContext();
    } else if (ContextResolver.class.isAssignableFrom(type)) {
        proxy = new ThreadLocalContextResolver<Object>();
    } else if (Request.class.isAssignableFrom(type)) {
        proxy = new ThreadLocalRequest();
    } else if (Providers.class.isAssignableFrom(type)) {
        proxy = new ThreadLocalProviders();
    } else if (MessageContext.class.isAssignableFrom(type)) {
        proxy = new ThreadLocalMessageContext();
    }
    if (proxy == null && isServletApiContext(type.getName())) {
        proxy = createThreadLocalServletApiContext(type.getName());
    }
    if (proxy == null) {
        ProxyClassLoader loader = new ProxyClassLoader(Proxy.class.getClassLoader());
        loader.addLoader(type.getClassLoader());
        loader.addLoader(ThreadLocalProxy.class.getClassLoader());
        return (ThreadLocalProxy<T>) Proxy.newProxyInstance(loader, new Class[] { type, ThreadLocalProxy.class }, new ThreadLocalInvocationHandler<T>());
    }
    return (ThreadLocalProxy<T>) proxy;
}
Also used : ThreadLocalHttpHeaders(org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpHeaders) ThreadLocalProxy(org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy) HttpHeaders(javax.ws.rs.core.HttpHeaders) ThreadLocalHttpHeaders(org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpHeaders) ThreadLocalUriInfo(org.apache.cxf.jaxrs.impl.tl.ThreadLocalUriInfo) ThreadLocalInvocationHandler(org.apache.cxf.jaxrs.impl.tl.ThreadLocalInvocationHandler) ThreadLocalRequest(org.apache.cxf.jaxrs.impl.tl.ThreadLocalRequest) Request(javax.ws.rs.core.Request) ThreadLocalRequest(org.apache.cxf.jaxrs.impl.tl.ThreadLocalRequest) Proxy(java.lang.reflect.Proxy) ThreadLocalProxy(org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy) ThreadLocalProtocolHeaders(org.apache.cxf.jaxrs.impl.tl.ThreadLocalProtocolHeaders) ProxyClassLoader(org.apache.cxf.common.util.ProxyClassLoader) ThreadLocalProviders(org.apache.cxf.jaxrs.impl.tl.ThreadLocalProviders) SecurityContext(javax.ws.rs.core.SecurityContext) ThreadLocalSecurityContext(org.apache.cxf.jaxrs.impl.tl.ThreadLocalSecurityContext) ThreadLocalMessageContext(org.apache.cxf.jaxrs.impl.tl.ThreadLocalMessageContext) ThreadLocalMessageContext(org.apache.cxf.jaxrs.impl.tl.ThreadLocalMessageContext) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) ThreadLocalSecurityContext(org.apache.cxf.jaxrs.impl.tl.ThreadLocalSecurityContext)

Example 8 with ThreadLocalProxy

use of org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy in project cxf 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]);
            }
        }
    }
    Object instance = null;
    try {
        instance = c.newInstance(cArgs);
    } catch (Throwable ex) {
        throw new RuntimeException("Resource or provider class " + c.getDeclaringClass().getName() + " can not be instantiated");
    }
    Map<Class<?>, ThreadLocalProxy<?>> proxies = new LinkedHashMap<Class<?>, ThreadLocalProxy<?>>();
    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);
}
Also used : ThreadLocalProxy(org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy) ApplicationInfo(org.apache.cxf.jaxrs.model.ApplicationInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) LinkedHashMap(java.util.LinkedHashMap) FilterProviderInfo(org.apache.cxf.jaxrs.model.FilterProviderInfo) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) TreeMap(java.util.TreeMap)

Aggregations

ThreadLocalProxy (org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy)8 Customer (org.apache.cxf.jaxrs.Customer)5 Test (org.junit.Test)5 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)4 ThreadLocalUriInfo (org.apache.cxf.jaxrs.impl.tl.ThreadLocalUriInfo)3 SingletonResourceProvider (org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)3 Message (org.apache.cxf.message.Message)3 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ServletContext (javax.servlet.ServletContext)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)2 UriInfo (javax.ws.rs.core.UriInfo)2 Server (org.apache.cxf.endpoint.Server)2 CustomerApplication (org.apache.cxf.jaxrs.CustomerApplication)2 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)2 HttpServletResponseFilter (org.apache.cxf.jaxrs.impl.HttpServletResponseFilter)2 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)2 ThreadLocalHttpServletRequest (org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpServletRequest)2