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);
}
}
}
}
}
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);
}
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());
}
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());
}
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);
}
Aggregations