use of org.glassfish.jersey.internal.inject.Providers in project jersey by jersey.
the class ParameterValueHelper method createValueProviders.
/**
* Create list of parameter value providers for the given {@link Parameterized
* parameterized} resource model component.
*
* @param injectionManager injection manager.
* @param parameterized parameterized resource model component.
* @return list of parameter value providers for the parameterized component.
*/
public static List<ParamValueFactoryWithSource<?>> createValueProviders(InjectionManager injectionManager, Parameterized parameterized) {
if ((null == parameterized.getParameters()) || (0 == parameterized.getParameters().size())) {
return Collections.emptyList();
}
List<ValueSupplierProvider> valueSupplierProviders = Providers.getProviders(injectionManager, ValueSupplierProvider.class).stream().sorted((o1, o2) -> o2.getPriority().getWeight() - o1.getPriority().getWeight()).collect(Collectors.toList());
boolean entityParamFound = false;
final List<ParamValueFactoryWithSource<?>> providers = new ArrayList<>(parameterized.getParameters().size());
for (final Parameter parameter : parameterized.getParameters()) {
final Parameter.Source parameterSource = parameter.getSource();
entityParamFound = entityParamFound || Parameter.Source.ENTITY == parameterSource;
final Supplier<?> valueSupplier = getParamValueSupplier(valueSupplierProviders, parameter);
if (valueSupplier != null) {
providers.add(wrapParamValueSupplier(valueSupplier, parameterSource));
} else {
providers.add(null);
}
}
if (!entityParamFound && Collections.frequency(providers, null) == 1) {
// Try to find entity if there is one unresolved parameter and the annotations are unknown
final int entityParamIndex = providers.lastIndexOf(null);
final Parameter parameter = parameterized.getParameters().get(entityParamIndex);
if (Parameter.Source.UNKNOWN == parameter.getSource() && !parameter.isQualified()) {
final Parameter overriddenParameter = Parameter.overrideSource(parameter, Parameter.Source.ENTITY);
final Supplier<?> valueSupplier = getParamValueSupplier(valueSupplierProviders, overriddenParameter);
if (valueSupplier != null) {
providers.set(entityParamIndex, wrapParamValueSupplier(valueSupplier, overriddenParameter.getSource()));
} else {
providers.set(entityParamIndex, null);
}
}
}
return providers;
}
use of org.glassfish.jersey.internal.inject.Providers in project jersey by jersey.
the class ParameterInjectionBinder method configure.
@Override
public void configure() {
// Param converter providers
// TODO: Replace by non-di version
bind(new ParamConverters.AggregatedProvider()).to(ParamConverterProvider.class);
Provider<ContainerRequest> requestProvider = Injections.getProvider(injectionManager, ContainerRequest.class);
Provider<AsyncContext> asyncContextProvider = Injections.getProvider(injectionManager, AsyncContext.class);
Function<Class<? extends Configuration>, Configuration> clientConfigProvider = clientConfigClass -> Injections.getOrCreate(injectionManager, clientConfigClass);
// Param Converters must be initialized Lazy and created at the time of the call on extractor
LazyValue<ParamConverterFactory> lazyParamConverterFactory = Values.lazy((Value<ParamConverterFactory>) () -> new ParamConverterFactory(Providers.getProviders(injectionManager, ParamConverterProvider.class), Providers.getCustomProviders(injectionManager, ParamConverterProvider.class)));
LazyValue<Configuration> lazyConfiguration = Values.lazy((Value<Configuration>) () -> injectionManager.getInstance(Configuration.class));
MultivaluedParameterExtractorFactory paramExtractor = new MultivaluedParameterExtractorFactory(lazyParamConverterFactory);
bind(paramExtractor).to(MultivaluedParameterExtractorProvider.class);
// Parameter injection value providers
AsyncResponseValueSupplierProvider asyncSupplier = new AsyncResponseValueSupplierProvider(asyncContextProvider);
bindValueSupplier(asyncSupplier);
CookieParamValueSupplierProvider cookieSupplier = new CookieParamValueSupplierProvider(paramExtractor, requestProvider);
bindValueSupplier(cookieSupplier);
EntityParamValueSupplierProvider entitySupplier = new EntityParamValueSupplierProvider(paramExtractor, requestProvider);
bindValueSupplier(entitySupplier);
FormParamValueSupplierProvider formSupplier = new FormParamValueSupplierProvider(paramExtractor, requestProvider);
bindValueSupplier(formSupplier);
HeaderParamValueSupplierProvider headerSupplier = new HeaderParamValueSupplierProvider(paramExtractor, requestProvider);
bindValueSupplier(headerSupplier);
MatrixParamValueSupplierProvider matrixSupplier = new MatrixParamValueSupplierProvider(paramExtractor, requestProvider);
bindValueSupplier(matrixSupplier);
PathParamValueSupplierProvider pathSupplier = new PathParamValueSupplierProvider(paramExtractor, requestProvider);
bindValueSupplier(pathSupplier);
QueryParamValueSupplierProvider querySupplier = new QueryParamValueSupplierProvider(paramExtractor, requestProvider);
bindValueSupplier(querySupplier);
WebTargetValueSupplierProvider webTargetSupplier = new WebTargetValueSupplierProvider(requestProvider, lazyConfiguration, clientConfigProvider);
bindValueSupplier(webTargetSupplier);
BeanParamValueSupplierProvider beanSupplier = new BeanParamValueSupplierProvider(paramExtractor, requestProvider, injectionManager);
bindValueSupplier(beanSupplier);
// Register InjectionResolvers with param providers
// TODO: RENAME INJECTION RESOLVER
bind(new ParamInjectionResolver<>(cookieSupplier, CookieParam.class));
bind(new ParamInjectionResolver<>(formSupplier, FormParam.class));
bind(new ParamInjectionResolver<>(headerSupplier, HeaderParam.class));
bind(new ParamInjectionResolver<>(matrixSupplier, MatrixParam.class));
bind(new ParamInjectionResolver<>(querySupplier, QueryParam.class));
bind(new ParamInjectionResolver<>(pathSupplier, PathParam.class));
bind(new ParamInjectionResolver<>(asyncSupplier, Suspended.class));
bind(new ParamInjectionResolver<>(webTargetSupplier, Uri.class));
bind(new ParamInjectionResolver<>(beanSupplier, BeanParam.class));
// Delegated value supplier for Context InjectionResolver which is implemented directly in DI provider
ContextInjectionResolver contextInjectionResolver = injectionManager.getInstance(ContextInjectionResolver.class);
bind(new DelegatedInjectionValueSupplierProvider(contextInjectionResolver, injectionManager::createForeignDescriptor)).to(ValueSupplierProvider.class);
}
Aggregations