Search in sources :

Example 1 with Providers

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;
}
Also used : Iterator(java.util.Iterator) Collection(java.util.Collection) MappableException(org.glassfish.jersey.server.internal.process.MappableException) Providers(org.glassfish.jersey.internal.inject.Providers) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) ArrayList(java.util.ArrayList) List(java.util.List) MessageBodyProviderNotFoundException(org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException) Parameterized(org.glassfish.jersey.server.model.Parameterized) ProcessingException(javax.ws.rs.ProcessingException) WebApplicationException(javax.ws.rs.WebApplicationException) Parameter(org.glassfish.jersey.server.model.Parameter) Collections(java.util.Collections) NotSupportedException(javax.ws.rs.NotSupportedException) ArrayList(java.util.ArrayList) Parameter(org.glassfish.jersey.server.model.Parameter)

Example 2 with 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);
}
Also used : FormParam(javax.ws.rs.FormParam) ContextInjectionResolver(org.glassfish.jersey.internal.inject.ContextInjectionResolver) PathParam(javax.ws.rs.PathParam) Provider(javax.inject.Provider) Configuration(javax.ws.rs.core.Configuration) AsyncContext(org.glassfish.jersey.server.internal.process.AsyncContext) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) Providers(org.glassfish.jersey.internal.inject.Providers) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) Function(java.util.function.Function) Suspended(javax.ws.rs.container.Suspended) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Uri(org.glassfish.jersey.server.Uri) BeanParam(javax.ws.rs.BeanParam) QueryParam(javax.ws.rs.QueryParam) Value(org.glassfish.jersey.internal.util.collection.Value) MatrixParam(javax.ws.rs.MatrixParam) LazyValue(org.glassfish.jersey.internal.util.collection.LazyValue) ValueSupplierProvider(org.glassfish.jersey.server.spi.internal.ValueSupplierProvider) HeaderParam(javax.ws.rs.HeaderParam) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider) CookieParam(javax.ws.rs.CookieParam) Injections(org.glassfish.jersey.internal.inject.Injections) Values(org.glassfish.jersey.internal.util.collection.Values) HeaderParam(javax.ws.rs.HeaderParam) MatrixParam(javax.ws.rs.MatrixParam) Configuration(javax.ws.rs.core.Configuration) AsyncContext(org.glassfish.jersey.server.internal.process.AsyncContext) Uri(org.glassfish.jersey.server.Uri) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) PathParam(javax.ws.rs.PathParam) Suspended(javax.ws.rs.container.Suspended) ContextInjectionResolver(org.glassfish.jersey.internal.inject.ContextInjectionResolver) BeanParam(javax.ws.rs.BeanParam) CookieParam(javax.ws.rs.CookieParam) QueryParam(javax.ws.rs.QueryParam) FormParam(javax.ws.rs.FormParam)

Aggregations

InjectionManager (org.glassfish.jersey.internal.inject.InjectionManager)2 Providers (org.glassfish.jersey.internal.inject.Providers)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Function (java.util.function.Function)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1 Provider (javax.inject.Provider)1 BeanParam (javax.ws.rs.BeanParam)1 CookieParam (javax.ws.rs.CookieParam)1 FormParam (javax.ws.rs.FormParam)1 HeaderParam (javax.ws.rs.HeaderParam)1 MatrixParam (javax.ws.rs.MatrixParam)1 NotSupportedException (javax.ws.rs.NotSupportedException)1 PathParam (javax.ws.rs.PathParam)1 ProcessingException (javax.ws.rs.ProcessingException)1 QueryParam (javax.ws.rs.QueryParam)1