Search in sources :

Example 1 with Parameterized

use of org.glassfish.jersey.server.model.Parameterized 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)

Aggregations

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 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1 NotSupportedException (javax.ws.rs.NotSupportedException)1 ProcessingException (javax.ws.rs.ProcessingException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 InjectionManager (org.glassfish.jersey.internal.inject.InjectionManager)1 Providers (org.glassfish.jersey.internal.inject.Providers)1 MessageBodyProviderNotFoundException (org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException)1 MappableException (org.glassfish.jersey.server.internal.process.MappableException)1 Parameter (org.glassfish.jersey.server.model.Parameter)1 Parameterized (org.glassfish.jersey.server.model.Parameterized)1