use of org.apache.cxf.jaxrs.lifecycle.ResourceProvider in project cxf by apache.
the class ResourceContextImpl method getResource.
@Override
public <T> T getResource(Class<T> cls) {
final ResourceProvider rp;
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);
}
use of org.apache.cxf.jaxrs.lifecycle.ResourceProvider in project cxf by apache.
the class JAXRSInInterceptor method setExchangeProperties.
private void setExchangeProperties(Message message, Exchange exchange, OperationResourceInfo ori, MultivaluedMap<String, String> values, int numberOfResources) {
final ClassResourceInfo cri = ori.getClassResourceInfo();
exchange.put(OperationResourceInfo.class, ori);
exchange.put(JAXRSUtils.ROOT_RESOURCE_CLASS, cri);
message.put(RESOURCE_METHOD, ori.getMethodToInvoke());
message.put(URITemplate.TEMPLATE_PARAMETERS, values);
message.put(URITemplate.URI_TEMPLATE, JAXRSUtils.getUriTemplate(message, cri, ori));
String plainOperationName = ori.getMethodToInvoke().getName();
if (numberOfResources > 1) {
plainOperationName = cri.getServiceClass().getSimpleName() + "#" + plainOperationName;
}
exchange.put(RESOURCE_OPERATION_NAME, plainOperationName);
if (ori.isOneway() || PropertyUtils.isTrue(HttpUtils.getProtocolHeader(message, Message.ONE_WAY_REQUEST, null))) {
exchange.setOneWay(true);
}
ResourceProvider rp = cri.getResourceProvider();
if (rp instanceof SingletonResourceProvider) {
// cri.isSingleton is not guaranteed to indicate we have a 'pure' singleton
exchange.put(Message.SERVICE_OBJECT, rp.getInstance(message));
}
}
use of org.apache.cxf.jaxrs.lifecycle.ResourceProvider in project cxf by apache.
the class CXFNonSpringJaxrsServlet method init.
@Override
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
if (getApplication() != null) {
createServerFromApplication(servletConfig);
return;
}
String applicationClass = servletConfig.getInitParameter(JAXRS_APPLICATION_PARAM);
if (applicationClass != null) {
createServerFromApplication(applicationClass, servletConfig);
return;
}
String splitChar = getParameterSplitChar(servletConfig);
JAXRSServerFactoryBean bean = new JAXRSServerFactoryBean();
bean.setBus(getBus());
String address = servletConfig.getInitParameter(SERVICE_ADDRESS_PARAM);
if (address == null) {
address = "/";
}
bean.setAddress(address);
bean.setStaticSubresourceResolution(getStaticSubResolutionValue(servletConfig));
String modelRef = servletConfig.getInitParameter(USER_MODEL_PARAM);
if (modelRef != null) {
bean.setModelRef(modelRef.trim());
}
setDocLocation(bean, servletConfig);
setSchemasLocations(bean, servletConfig);
setAllInterceptors(bean, servletConfig, splitChar);
setInvoker(bean, servletConfig);
Map<Class<?>, Map<String, List<String>>> resourceClasses = getServiceClasses(servletConfig, modelRef != null, splitChar);
Map<Class<?>, ResourceProvider> resourceProviders = getResourceProviders(servletConfig, resourceClasses);
List<?> providers = getProviders(servletConfig, splitChar);
bean.setResourceClasses(new ArrayList<Class<?>>(resourceClasses.keySet()));
bean.setProviders(providers);
for (Map.Entry<Class<?>, ResourceProvider> entry : resourceProviders.entrySet()) {
bean.setResourceProvider(entry.getKey(), entry.getValue());
}
setExtensions(bean, servletConfig);
List<? extends Feature> features = getFeatures(servletConfig, splitChar);
bean.getFeatures().addAll(features);
bean.create();
}
use of org.apache.cxf.jaxrs.lifecycle.ResourceProvider in project cxf by apache.
the class CdiResourceProviderTest method assertNotSingleton.
private void assertNotSingleton() {
final ResourceProvider provider = new PerRequestResourceProvider(() -> new Lifecycle(beanManager, bean), Object.class);
assertFalse(new JAXRSCdiResourceExtension().isCxfSingleton(beanManager, bean));
assertFalse(provider.isSingleton());
final MessageImpl message1 = new MessageImpl();
final MessageImpl message2 = new MessageImpl();
final Object instance = provider.getInstance(message1);
assertNotNull(instance);
assertNotEquals(provider.getInstance(message1), provider.getInstance(message2));
// ensure we can close the lifecycle
final Lifecycle lifecycle1 = message1.get(Lifecycle.class);
assertNotNull(lifecycle1);
assertNotNull(message2.get(Lifecycle.class));
}
use of org.apache.cxf.jaxrs.lifecycle.ResourceProvider in project carbon-apimgt by wso2.
the class ValidationInInterceptor method handleMessage.
@MethodStats
public void handleMessage(Message message) {
final OperationResourceInfo operationResource = message.getExchange().get(OperationResourceInfo.class);
if (operationResource == null) {
log.info("OperationResourceInfo is not available, skipping validation");
return;
}
final ClassResourceInfo classResource = operationResource.getClassResourceInfo();
if (classResource == null) {
log.info("ClassResourceInfo is not available, skipping validation");
return;
}
final ResourceProvider resourceProvider = classResource.getResourceProvider();
if (resourceProvider == null) {
log.info("ResourceProvider is not available, skipping validation");
return;
}
final List<Object> arguments = MessageContentsList.getContentsList(message);
final Method method = operationResource.getAnnotatedMethod();
final Object instance = resourceProvider.getInstance(message);
if (method != null && arguments != null) {
// validate the parameters(arguments) over the invoked method
validate(method, arguments.toArray(), instance);
// validate the fields of each argument
for (Object arg : arguments) {
if (arg != null)
validate(arg);
}
}
}
Aggregations