Search in sources :

Example 1 with ThreadLocalProxy

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

the class InjectionUtils method injectContextMethods.

@SuppressWarnings("unchecked")
public static void injectContextMethods(Object requestObject, AbstractResourceInfo cri, Message message) {
    for (Map.Entry<Class<?>, Method> entry : cri.getContextMethods().entrySet()) {
        Method method = entry.getValue();
        if (VALUE_CONTEXTS.contains(method.getParameterTypes()[0].getName()) && cri.isSingleton()) {
            continue;
        }
        Object o = JAXRSUtils.createContextValue(message, method.getGenericParameterTypes()[0], entry.getKey());
        if (o != null) {
            if (!cri.isSingleton()) {
                InjectionUtils.injectThroughMethod(requestObject, method, o, message);
            } else {
                ThreadLocalProxy<Object> proxy = (ThreadLocalProxy<Object>) cri.getContextSetterProxy(method);
                if (proxy != null) {
                    proxy.set(o);
                }
            }
        }
    }
}
Also used : ThreadLocalProxy(org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy) Method(java.lang.reflect.Method) 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 2 with ThreadLocalProxy

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

the class JAXRSUtilsTest method testInjectApplicationInPerRequestResource.

@Test
public void testInjectApplicationInPerRequestResource() throws Exception {
    CustomerApplication app = new CustomerApplication();
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setServiceClass(Customer.class);
    sf.setApplication(app);
    sf.setStart(false);
    Server server = sf.create();
    @SuppressWarnings("unchecked") ThreadLocalProxy<UriInfo> proxy = (ThreadLocalProxy<UriInfo>) app.getUriInfo();
    assertNotNull(proxy);
    ClassResourceInfo cri = sf.getServiceFactory().getClassResourceInfo().get(0);
    Customer customer = (Customer) cri.getResourceProvider().getInstance(createMessage());
    assertNull(customer.getApplication1());
    assertNull(customer.getApplication2());
    invokeCustomerMethod(cri, customer, server);
    assertSame(app, customer.getApplication1());
    assertSame(app, customer.getApplication2());
    assertTrue(proxy.get() instanceof UriInfo);
}
Also used : ThreadLocalProxy(org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy) Server(org.apache.cxf.endpoint.Server) CustomerApplication(org.apache.cxf.jaxrs.CustomerApplication) Customer(org.apache.cxf.jaxrs.Customer) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) UriInfo(javax.ws.rs.core.UriInfo) ThreadLocalUriInfo(org.apache.cxf.jaxrs.impl.tl.ThreadLocalUriInfo) Test(org.junit.Test)

Example 3 with ThreadLocalProxy

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

the class JAXRSUtilsTest method testSingletonContextFields.

@SuppressWarnings("unchecked")
@Test
public void testSingletonContextFields() throws Exception {
    ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
    Customer c = new Customer();
    cri.setResourceProvider(new SingletonResourceProvider(c));
    Message m = createMessage();
    m.put(Message.PROTOCOL_HEADERS, new HashMap<String, List<String>>());
    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);
    InjectionUtils.injectContextMethods(c, cri, m);
    assertSame(ThreadLocalUriInfo.class, c.getUriInfo2().getClass());
    assertSame(UriInfoImpl.class, ((ThreadLocalProxy<UriInfo>) c.getUriInfo2()).get().getClass());
    assertSame(HttpHeadersImpl.class, ((ThreadLocalProxy<HttpHeaders>) c.getHeaders()).get().getClass());
    assertSame(RequestImpl.class, ((ThreadLocalProxy<Request>) c.getRequest()).get().getClass());
    assertSame(ResourceInfoImpl.class, ((ThreadLocalProxy<ResourceInfo>) c.getResourceInfo()).get().getClass());
    assertSame(SecurityContextImpl.class, ((ThreadLocalProxy<SecurityContext>) c.getSecurityContext()).get().getClass());
    assertSame(ProvidersImpl.class, ((ThreadLocalProxy<Providers>) c.getBodyWorkers()).get().getClass());
    assertSame(servletContextMock, ((ThreadLocalProxy<ServletContext>) c.getThreadLocalServletContext()).get());
    assertSame(servletContextMock, ((ThreadLocalProxy<ServletContext>) c.getServletContext()).get());
    assertSame(servletContextMock, ((ThreadLocalProxy<ServletContext>) c.getSuperServletContext()).get());
    HttpServletRequest currentReq = ((ThreadLocalProxy<HttpServletRequest>) c.getServletRequest()).get();
    assertSame(httpRequest, ((HttpServletRequestFilter) currentReq).getRequest());
    HttpServletResponseFilter filter = (HttpServletResponseFilter) ((ThreadLocalProxy<HttpServletResponse>) c.getServletResponse()).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) List(java.util.List) ArrayList(java.util.ArrayList) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) HttpServletResponseFilter(org.apache.cxf.jaxrs.impl.HttpServletResponseFilter) Test(org.junit.Test)

Example 4 with ThreadLocalProxy

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

the class JAXRSUtilsTest method testContextAnnotationOnMethod.

@SuppressWarnings("unchecked")
@Test
public void testContextAnnotationOnMethod() throws Exception {
    ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
    Customer c = new Customer();
    cri.setResourceProvider(new SingletonResourceProvider(c));
    InjectionUtils.injectContextProxies(cri, cri.getResourceProvider().getInstance(null));
    OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethods()[0], cri);
    Message message = createMessage();
    InjectionUtils.injectContextMethods(c, ori.getClassResourceInfo(), message);
    assertNotNull(c.getUriInfo());
    assertSame(ThreadLocalUriInfo.class, c.getUriInfo().getClass());
    assertSame(UriInfoImpl.class, ((ThreadLocalProxy<UriInfo>) c.getUriInfo()).get().getClass());
    assertSame(ThreadLocalServletConfig.class, c.getSuperServletConfig().getClass());
    assertSame(ThreadLocalHttpServletRequest.class, c.getHttpServletRequest().getClass());
}
Also used : 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) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) Test(org.junit.Test)

Example 5 with ThreadLocalProxy

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

the class JAXRSUtilsTest method testInjectApplicationInSingleton.

@Test
public void testInjectApplicationInSingleton() throws Exception {
    CustomerApplication app = new CustomerApplication();
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    Customer customer = new Customer();
    sf.setServiceBeanObjects(customer);
    sf.setApplication(app);
    sf.setStart(false);
    Server server = sf.create();
    assertSame(app, customer.getApplication1());
    assertSame(app, customer.getApplication2());
    @SuppressWarnings("unchecked") ThreadLocalProxy<UriInfo> proxy = (ThreadLocalProxy<UriInfo>) app.getUriInfo();
    assertNotNull(proxy);
    invokeCustomerMethod(sf.getServiceFactory().getClassResourceInfo().get(0), customer, server);
    assertSame(app, customer.getApplication2());
    assertTrue(proxy.get() instanceof UriInfo);
}
Also used : ThreadLocalProxy(org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy) Server(org.apache.cxf.endpoint.Server) CustomerApplication(org.apache.cxf.jaxrs.CustomerApplication) Customer(org.apache.cxf.jaxrs.Customer) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) UriInfo(javax.ws.rs.core.UriInfo) ThreadLocalUriInfo(org.apache.cxf.jaxrs.impl.tl.ThreadLocalUriInfo) Test(org.junit.Test)

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