use of org.apache.cxf.jaxrs.model.ProviderInfo in project cxf by apache.
the class ClientResponseFilterInterceptor method handleMessage.
public void handleMessage(Message inMessage) throws Fault {
ClientProviderFactory pf = ClientProviderFactory.getInstance(inMessage);
if (pf == null) {
return;
}
List<ProviderInfo<ClientResponseFilter>> filters = pf.getClientResponseFilters();
if (!filters.isEmpty()) {
final ClientRequestContext reqContext = new ClientRequestContextImpl(inMessage.getExchange().getOutMessage(), true);
final ResponseImpl response = (ResponseImpl) getResponse(inMessage);
final ClientResponseContext respContext = new ClientResponseContextImpl(response, inMessage);
for (ProviderInfo<ClientResponseFilter> filter : filters) {
InjectionUtils.injectContexts(filter.getProvider(), filter, inMessage);
try {
filter.getProvider().filter(reqContext, respContext);
} catch (RuntimeException | IOException ex) {
// would be stuck waiting for the IN chain completion.
if (!inMessage.getExchange().isOneWay()) {
synchronized (inMessage.getExchange()) {
inMessage.getExchange().put("IN_CHAIN_COMPLETE", Boolean.TRUE);
}
}
// a response (4.5.2 Client Runtime).
throw new ResponseProcessingException(response, ex);
}
}
}
}
use of org.apache.cxf.jaxrs.model.ProviderInfo in project cxf by apache.
the class ServerProviderFactory method setProviders.
@SuppressWarnings("unchecked")
@Override
protected void setProviders(boolean custom, boolean busGlobal, Object... providers) {
List<Object> allProviders = new LinkedList<>();
for (Object p : providers) {
if (p instanceof Feature) {
FeatureContext featureContext = createServerFeatureContext();
Feature feature = (Feature) p;
injectApplicationIntoFeature(feature);
feature.configure(featureContext);
Configuration cfg = featureContext.getConfiguration();
for (Object featureProvider : cfg.getInstances()) {
Map<Class<?>, Integer> contracts = cfg.getContracts(featureProvider.getClass());
if (contracts != null && !contracts.isEmpty()) {
Class<?> providerCls = ClassHelper.getRealClass(getBus(), featureProvider);
allProviders.add(new FilterProviderInfo<Object>(featureProvider.getClass(), providerCls, featureProvider, getBus(), getFilterNameBindings(getBus(), featureProvider), false, contracts));
} else {
allProviders.add(featureProvider);
}
}
} else {
allProviders.add(p);
}
}
List<ProviderInfo<ContainerRequestFilter>> postMatchRequestFilters = new LinkedList<>();
List<ProviderInfo<ContainerResponseFilter>> postMatchResponseFilters = new LinkedList<>();
List<ProviderInfo<? extends Object>> theProviders = prepareProviders(custom, busGlobal, allProviders.toArray(), application);
super.setCommonProviders(theProviders, RuntimeType.SERVER);
for (ProviderInfo<? extends Object> provider : theProviders) {
Class<?> providerCls = ClassHelper.getRealClass(getBus(), provider.getProvider());
// Check if provider is constrained to server
if (!constrainedTo(providerCls, RuntimeType.SERVER)) {
continue;
}
if (filterContractSupported(provider, providerCls, ContainerRequestFilter.class)) {
addContainerRequestFilter(postMatchRequestFilters, (ProviderInfo<ContainerRequestFilter>) provider);
}
if (filterContractSupported(provider, providerCls, ContainerResponseFilter.class)) {
postMatchResponseFilters.add((ProviderInfo<ContainerResponseFilter>) provider);
}
if (DynamicFeature.class.isAssignableFrom(providerCls)) {
// TODO: review the possibility of DynamicFeatures needing to have Contexts injected
Object feature = provider.getProvider();
dynamicFeatures.add((DynamicFeature) feature);
}
if (filterContractSupported(provider, providerCls, ExceptionMapper.class)) {
addProviderToList(exceptionMappers, provider);
}
}
Collections.sort(preMatchContainerRequestFilters, new BindingPriorityComparator(ContainerRequestFilter.class, true));
mapInterceptorFilters(postMatchContainerRequestFilters, postMatchRequestFilters, ContainerRequestFilter.class, true);
mapInterceptorFilters(containerResponseFilters, postMatchResponseFilters, ContainerResponseFilter.class, false);
injectContextProxies(exceptionMappers, postMatchContainerRequestFilters.values(), preMatchContainerRequestFilters, containerResponseFilters.values());
}
use of org.apache.cxf.jaxrs.model.ProviderInfo in project cxf by apache.
the class PerRequestResourceProvider method createInstance.
protected Object createInstance(Message m) {
ProviderInfo<?> application = (ProviderInfo<?>) m.getExchange().getEndpoint().get(Application.class.getName());
Map<Class<?>, Object> mapValues = CastUtils.cast(application == null ? null : Collections.singletonMap(Application.class, application.getProvider()));
Object[] values = ResourceUtils.createConstructorArguments(c, m, true, mapValues, params, anns, genericTypes);
try {
Object instance = values.length > 0 ? c.newInstance(values) : c.newInstance(new Object[] {});
InjectionUtils.invokeLifeCycleMethod(instance, postConstructMethod);
return instance;
} catch (InstantiationException ex) {
String msg = "Resource class " + c.getDeclaringClass().getName() + " can not be instantiated";
throw ExceptionUtils.toInternalServerErrorException(null, serverError(msg));
} catch (IllegalAccessException ex) {
String msg = "Resource class " + c.getDeclaringClass().getName() + " can not be instantiated" + " due to IllegalAccessException";
throw ExceptionUtils.toInternalServerErrorException(null, serverError(msg));
} catch (InvocationTargetException ex) {
Response r = JAXRSUtils.convertFaultToResponse(ex.getCause(), m);
if (r != null) {
m.getExchange().put(Response.class, r);
throw new WebApplicationException();
}
String msg = "Resource class " + c.getDeclaringClass().getName() + " can not be instantiated" + " due to InvocationTargetException";
throw ExceptionUtils.toInternalServerErrorException(null, serverError(msg));
}
}
use of org.apache.cxf.jaxrs.model.ProviderInfo in project cxf by apache.
the class JAXRSUtils method runContainerResponseFilters.
public static void runContainerResponseFilters(ServerProviderFactory pf, ResponseImpl r, Message m, OperationResourceInfo ori, Method invoked) throws IOException, Throwable {
List<ProviderInfo<ContainerResponseFilter>> containerFilters = pf.getContainerResponseFilters(ori == null ? null : ori.getNameBindings());
if (!containerFilters.isEmpty()) {
ContainerRequestContext requestContext = new ContainerRequestContextImpl(m.getExchange().getInMessage(), false, true);
ContainerResponseContext responseContext = new ContainerResponseContextImpl(r, m, ori == null ? null : ori.getClassResourceInfo().getServiceClass(), invoked);
for (ProviderInfo<ContainerResponseFilter> filter : containerFilters) {
InjectionUtils.injectContexts(filter.getProvider(), filter, m);
filter.getProvider().filter(requestContext, responseContext);
}
}
}
use of org.apache.cxf.jaxrs.model.ProviderInfo 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]);
}
}
}
final Object instance;
try {
instance = c.newInstance(cArgs);
} catch (Throwable ex) {
throw new RuntimeException("Resource or provider class " + c.getDeclaringClass().getName() + " can not be instantiated", ex);
}
Map<Class<?>, ThreadLocalProxy<?>> proxies = new LinkedHashMap<>();
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);
}
Aggregations