use of org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider in project cxf by apache.
the class ResourceUtils method createApplication.
@SuppressWarnings("unchecked")
public static JAXRSServerFactoryBean createApplication(Application app, boolean ignoreAppPath, boolean staticSubresourceResolution, boolean useSingletonResourceProvider, Bus bus) {
Set<Object> singletons = app.getSingletons();
verifySingletons(singletons);
List<Class<?>> resourceClasses = new ArrayList<Class<?>>();
List<Object> providers = new ArrayList<>();
List<Feature> features = new ArrayList<>();
Map<Class<?>, ResourceProvider> map = new HashMap<>();
// or singleton provider classes
for (Class<?> cls : app.getClasses()) {
if (isValidApplicationClass(cls, singletons)) {
if (isValidProvider(cls)) {
providers.add(createProviderInstance(cls));
} else if (Feature.class.isAssignableFrom(cls)) {
features.add(createFeatureInstance((Class<? extends Feature>) cls));
} else {
resourceClasses.add(cls);
if (useSingletonResourceProvider) {
map.put(cls, new SingletonResourceProvider(createProviderInstance(cls)));
} else {
map.put(cls, new PerRequestResourceProvider(cls));
}
}
}
}
// we can get either a provider or resource class here
for (Object o : singletons) {
if (isValidProvider(o.getClass())) {
providers.add(o);
} else if (o instanceof Feature) {
features.add((Feature) o);
} else {
resourceClasses.add(o.getClass());
map.put(o.getClass(), new SingletonResourceProvider(o));
}
}
JAXRSServerFactoryBean bean = new JAXRSServerFactoryBean();
if (bus != null) {
bean.setBus(bus);
}
String address = "/";
if (!ignoreAppPath) {
ApplicationPath appPath = locateApplicationPath(app.getClass());
if (appPath != null) {
address = appPath.value();
}
}
if (!address.startsWith("/")) {
address = "/" + address;
}
bean.setAddress(address);
bean.setStaticSubresourceResolution(staticSubresourceResolution);
bean.setResourceClasses(resourceClasses);
bean.setProviders(providers);
bean.setFeatures(features);
for (Map.Entry<Class<?>, ResourceProvider> entry : map.entrySet()) {
bean.setResourceProvider(entry.getKey(), entry.getValue());
}
Map<String, Object> appProps = app.getProperties();
if (appProps != null) {
bean.getProperties(true).putAll(appProps);
}
bean.setApplication(app);
return bean;
}
use of org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider in project cxf by apache.
the class JAXRSUtilsTest method testPerRequestContextFields.
@Test
public void testPerRequestContextFields() throws Exception {
ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
cri.setResourceProvider(new PerRequestResourceProvider(Customer.class));
OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethod("postConstruct", new Class[] {}), cri);
Customer c = new Customer();
Message m = createMessage();
m.put(Message.PROTOCOL_HEADERS, new HashMap<String, List<String>>());
HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
m.put(AbstractHTTPDestination.HTTP_RESPONSE, response);
InjectionUtils.injectContextFields(c, ori.getClassResourceInfo(), m);
assertSame(UriInfoImpl.class, c.getUriInfo2().getClass());
assertSame(HttpHeadersImpl.class, c.getHeaders().getClass());
assertSame(RequestImpl.class, c.getRequest().getClass());
assertSame(SecurityContextImpl.class, c.getSecurityContext().getClass());
assertSame(ProvidersImpl.class, c.getBodyWorkers().getClass());
}
use of org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider in project cxf by apache.
the class JAXRSUtilsTest method testContextResolverFields.
@Test
public void testContextResolverFields() throws Exception {
ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
cri.setResourceProvider(new PerRequestResourceProvider(Customer.class));
OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethod("postConstruct", new Class[] {}), cri);
Message m = createMessage();
HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
m.put(AbstractHTTPDestination.HTTP_RESPONSE, response);
Customer c = new Customer();
ContextResolver<JAXBContext> cr = new JAXBContextProvider();
ProviderFactory.getInstance(m).registerUserProvider(cr);
m.put(Message.BASE_PATH, "/");
InjectionUtils.injectContextFields(c, ori.getClassResourceInfo(), m);
assertSame(cr.getClass(), c.getContextResolver().getClass());
}
use of org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider in project cxf by apache.
the class JAXRSUtilsTest method testServletResourceFields.
@Test
public void testServletResourceFields() throws Exception {
ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
cri.setResourceProvider(new PerRequestResourceProvider(Customer.class));
OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethod("postConstruct", new Class[] {}), cri);
Customer c = new Customer();
// Creating mocks for the servlet request, response and context
HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
ServletContext context = EasyMock.createMock(ServletContext.class);
EasyMock.replay(request);
EasyMock.replay(response);
EasyMock.replay(context);
Message m = createMessage();
m.put(AbstractHTTPDestination.HTTP_REQUEST, request);
m.put(AbstractHTTPDestination.HTTP_RESPONSE, response);
m.put(AbstractHTTPDestination.HTTP_CONTEXT, context);
InjectionUtils.injectContextFields(c, ori.getClassResourceInfo(), m);
assertSame(request.getClass(), ((HttpServletRequestFilter) c.getServletRequestResource()).getRequest().getClass());
HttpServletResponseFilter filter = (HttpServletResponseFilter) c.getServletResponseResource();
assertSame(response.getClass(), filter.getResponse().getClass());
assertSame(context.getClass(), c.getServletContextResource().getClass());
assertNotNull(c.getServletRequest());
assertNotNull(c.getServletResponse());
assertNotNull(c.getServletContext());
assertNotNull(c.getServletRequestResource());
assertNotNull(c.getServletResponseResource());
assertNotNull(c.getServletContextResource());
assertSame(request.getClass(), ((HttpServletRequestFilter) c.getServletRequestResource()).getRequest().getClass());
filter = (HttpServletResponseFilter) c.getServletResponse();
assertSame(response.getClass(), filter.getResponse().getClass());
assertSame(context.getClass(), c.getServletContext().getClass());
}
use of org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider in project cxf by apache.
the class ResourceContextImpl method getResource.
@Override
public <T> T getResource(Class<T> cls) {
ResourceProvider rp = null;
Object propValue = m.getContextualProperty(CONTEXT_PROVIDER_PROP);
if (propValue instanceof ResourceContextProvider) {
rp = ((ResourceContextProvider) propValue).getResourceProvider(cls);
} else {
rp = new PerRequestResourceProvider(cls);
}
T resource = cls.cast(rp.getInstance(m));
return doInitResource(cls, resource);
}
Aggregations