Search in sources :

Example 1 with RankedProvider

use of org.glassfish.jersey.model.internal.RankedProvider in project jersey by jersey.

the class Providers method getAllRankedProviders.

/**
     * Get the iterable of all {@link RankedProvider providers} (custom and default) registered for the given service provider
     * contract in the underlying {@link InjectionManager injection manager} container.
     *
     * @param <T>             service provider contract Java type.
     * @param injectionManager underlying injection manager.
     * @param contract        service provider contract.
     * @return iterable of all available ranked service providers for the contract. Return value is never null.
     */
public static <T> Iterable<RankedProvider<T>> getAllRankedProviders(InjectionManager injectionManager, Class<T> contract) {
    List<ServiceHolder<T>> providers = getServiceHolders(injectionManager, contract, CustomAnnotationLiteral.INSTANCE);
    providers.addAll(getServiceHolders(injectionManager, contract));
    LinkedHashMap<ServiceHolder<T>, RankedProvider<T>> providerMap = new LinkedHashMap<>();
    for (ServiceHolder<T> provider : providers) {
        if (!providerMap.containsKey(provider)) {
            Set<Type> contractTypes = provider.getContractTypes();
            Class<?> implementationClass = provider.getImplementationClass();
            boolean proxyGenerated = true;
            for (Type ct : contractTypes) {
                if (((Class<?>) ct).isAssignableFrom(implementationClass)) {
                    proxyGenerated = false;
                    break;
                }
            }
            Set<Type> contracts = proxyGenerated ? contractTypes : null;
            providerMap.put(provider, new RankedProvider<>(provider.getInstance(), provider.getRank(), contracts));
        }
    }
    return providerMap.values();
}
Also used : LinkedHashMap(java.util.LinkedHashMap) RuntimeType(javax.ws.rs.RuntimeType) Type(java.lang.reflect.Type) RankedProvider(org.glassfish.jersey.model.internal.RankedProvider)

Example 2 with RankedProvider

use of org.glassfish.jersey.model.internal.RankedProvider in project jersey by jersey.

the class ApplicationHandler method filterNameBound.

/**
     * Takes collection of all filters/interceptors (either request/reader or response/writer)
     * and separates out all name-bound filters/interceptors, returns them as a separate MultivaluedMap,
     * mapping the name-bound annotation to the list of name-bound filters/interceptors. The same key values
     * are also added into the inverse map passed in {@code inverseNameBoundMap}.
     * <p/>
     * Note, the name-bound filters/interceptors are removed from the original filters/interceptors collection.
     * If non-null collection is passed in the postMatching parameter (applicable for filters only),
     * this method also removes all the global
     * postMatching filters from the original collection and adds them to the collection passed in the postMatching
     * parameter.
     *
     * @param all                     Collection of all filters to be processed.
     * @param preMatchingFilters      Collection into which pre-matching filters should be added.
     * @param componentBag            Component bag
     * @param applicationNameBindings Collection of name binding annotations attached to the JAX-RS application.
     * @param inverseNameBoundMap     Inverse name bound map into which the name bound providers should be inserted. The keys
     *                                are providers (filters, interceptor)
     * @return {@link MultivaluedMap} of all name-bound filters.
     */
private static <T> MultivaluedMap<Class<? extends Annotation>, RankedProvider<T>> filterNameBound(final Iterable<RankedProvider<T>> all, final Collection<RankedProvider<ContainerRequestFilter>> preMatchingFilters, final ComponentBag componentBag, final Collection<Class<? extends Annotation>> applicationNameBindings, final MultivaluedMap<RankedProvider<T>, Class<? extends Annotation>> inverseNameBoundMap) {
    final MultivaluedMap<Class<? extends Annotation>, RankedProvider<T>> result = new MultivaluedHashMap<>();
    for (final Iterator<RankedProvider<T>> it = all.iterator(); it.hasNext(); ) {
        final RankedProvider<T> provider = it.next();
        Class<?> providerClass = provider.getProvider().getClass();
        final Set<Type> contractTypes = provider.getContractTypes();
        if (contractTypes != null && !contractTypes.contains(providerClass)) {
            providerClass = ReflectionHelper.theMostSpecificTypeOf(contractTypes);
        }
        ContractProvider model = componentBag.getModel(providerClass);
        if (model == null) {
            // the provider was (most likely) bound in HK2 externally
            model = ComponentBag.modelFor(providerClass);
        }
        final boolean preMatching = providerClass.getAnnotation(PreMatching.class) != null;
        if (preMatching && preMatchingFilters != null) {
            it.remove();
            preMatchingFilters.add(new RankedProvider<>((ContainerRequestFilter) provider.getProvider(), model.getPriority(ContainerRequestFilter.class)));
        }
        boolean nameBound = model.isNameBound();
        if (nameBound && !applicationNameBindings.isEmpty() && applicationNameBindings.containsAll(model.getNameBindings())) {
            // override the name-bound flag
            nameBound = false;
        }
        if (nameBound) {
            // not application-bound
            if (!preMatching) {
                it.remove();
                for (final Class<? extends Annotation> binding : model.getNameBindings()) {
                    result.add(binding, provider);
                    inverseNameBoundMap.add(provider, binding);
                }
            } else {
                LOGGER.warning(LocalizationMessages.PREMATCHING_ALSO_NAME_BOUND(providerClass));
            }
        }
    }
    return result;
}
Also used : ContractProvider(org.glassfish.jersey.model.ContractProvider) Annotation(java.lang.annotation.Annotation) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) RuntimeType(javax.ws.rs.RuntimeType) GenericType(javax.ws.rs.core.GenericType) Type(java.lang.reflect.Type) RankedProvider(org.glassfish.jersey.model.internal.RankedProvider) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) PreMatching(javax.ws.rs.container.PreMatching)

Example 3 with RankedProvider

use of org.glassfish.jersey.model.internal.RankedProvider in project jersey by jersey.

the class RuntimeLocatorModelBuilder method enhance.

private ResourceModel enhance(ResourceModel subResourceModel) {
    final Iterable<RankedProvider<ModelProcessor>> allRankedProviders = Providers.getAllRankedProviders(injectionManager, ModelProcessor.class);
    final Iterable<ModelProcessor> modelProcessors = Providers.sortRankedProviders(new RankedComparator<ModelProcessor>(), allRankedProviders);
    for (final ModelProcessor modelProcessor : modelProcessors) {
        subResourceModel = modelProcessor.processSubResource(subResourceModel, config);
        validateSubResource(subResourceModel);
    }
    return subResourceModel;
}
Also used : RankedProvider(org.glassfish.jersey.model.internal.RankedProvider) ModelProcessor(org.glassfish.jersey.server.model.ModelProcessor)

Example 4 with RankedProvider

use of org.glassfish.jersey.model.internal.RankedProvider in project jersey by jersey.

the class ApplicationHandler method getRankedComponentProviders.

private static Iterable<RankedProvider<ComponentProvider>> getRankedComponentProviders() throws ServiceConfigurationError {
    final List<RankedProvider<ComponentProvider>> result = new LinkedList<>();
    for (final ComponentProvider provider : ServiceFinder.find(ComponentProvider.class)) {
        result.add(new RankedProvider<>(provider));
    }
    Collections.sort(result, new RankedComparator<ComponentProvider>(Order.DESCENDING));
    return result;
}
Also used : RankedProvider(org.glassfish.jersey.model.internal.RankedProvider) ComponentProvider(org.glassfish.jersey.server.spi.ComponentProvider) LinkedList(java.util.LinkedList)

Example 5 with RankedProvider

use of org.glassfish.jersey.model.internal.RankedProvider in project jersey by jersey.

the class ApplicationHandler method processResourceModel.

private ResourceModel processResourceModel(ResourceModel resourceModel) {
    final Iterable<RankedProvider<ModelProcessor>> allRankedProviders = Providers.getAllRankedProviders(injectionManager, ModelProcessor.class);
    final Iterable<ModelProcessor> modelProcessors = Providers.sortRankedProviders(new RankedComparator<ModelProcessor>(), allRankedProviders);
    for (final ModelProcessor modelProcessor : modelProcessors) {
        resourceModel = modelProcessor.processResourceModel(resourceModel, getConfiguration());
    }
    return resourceModel;
}
Also used : RankedProvider(org.glassfish.jersey.model.internal.RankedProvider) ModelProcessor(org.glassfish.jersey.server.model.ModelProcessor)

Aggregations

RankedProvider (org.glassfish.jersey.model.internal.RankedProvider)5 Type (java.lang.reflect.Type)2 RuntimeType (javax.ws.rs.RuntimeType)2 ModelProcessor (org.glassfish.jersey.server.model.ModelProcessor)2 Annotation (java.lang.annotation.Annotation)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 ContainerRequestFilter (javax.ws.rs.container.ContainerRequestFilter)1 PreMatching (javax.ws.rs.container.PreMatching)1 GenericType (javax.ws.rs.core.GenericType)1 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)1 ContractProvider (org.glassfish.jersey.model.ContractProvider)1 ComponentProvider (org.glassfish.jersey.server.spi.ComponentProvider)1