Search in sources :

Example 1 with ConstrainedTo

use of javax.ws.rs.ConstrainedTo in project jersey by jersey.

the class CommonConfig method configureAutoDiscoverableProviders.

/**
     * Configure {@link AutoDiscoverable auto-discoverables} in the injection manager.
     *
     * @param injectionManager locator in which the auto-discoverables should be configured.
     * @param forcedOnly defines whether all or only forced auto-discoverables should be configured.
     */
public void configureAutoDiscoverableProviders(final InjectionManager injectionManager, final boolean forcedOnly) {
    // Check whether meta providers have been initialized for a config this config has been loaded from.
    if (!disableMetaProviderConfiguration) {
        final Set<AutoDiscoverable> providers = new TreeSet<>((o1, o2) -> {
            final int p1 = o1.getClass().isAnnotationPresent(Priority.class) ? o1.getClass().getAnnotation(Priority.class).value() : Priorities.USER;
            final int p2 = o2.getClass().isAnnotationPresent(Priority.class) ? o2.getClass().getAnnotation(Priority.class).value() : Priorities.USER;
            return (p1 < p2 || p1 == p2) ? -1 : 1;
        });
        // Forced (always invoked).
        final List<ForcedAutoDiscoverable> forcedAutoDiscroverables = new LinkedList<>();
        for (Class<ForcedAutoDiscoverable> forcedADType : ServiceFinder.find(ForcedAutoDiscoverable.class, true).toClassArray()) {
            forcedAutoDiscroverables.add(injectionManager.createAndInitialize(forcedADType));
        }
        providers.addAll(forcedAutoDiscroverables);
        // Regular.
        if (!forcedOnly) {
            providers.addAll(Providers.getProviders(injectionManager, AutoDiscoverable.class));
        }
        for (final AutoDiscoverable autoDiscoverable : providers) {
            final ConstrainedTo constrainedTo = autoDiscoverable.getClass().getAnnotation(ConstrainedTo.class);
            if (constrainedTo == null || type.equals(constrainedTo.value())) {
                try {
                    autoDiscoverable.configure(this);
                } catch (final Exception e) {
                    LOGGER.log(Level.FINE, LocalizationMessages.AUTODISCOVERABLE_CONFIGURATION_FAILED(autoDiscoverable.getClass()), e);
                }
            }
        }
    }
}
Also used : ConstrainedTo(javax.ws.rs.ConstrainedTo) AutoDiscoverable(org.glassfish.jersey.internal.spi.AutoDiscoverable) ForcedAutoDiscoverable(org.glassfish.jersey.internal.spi.ForcedAutoDiscoverable) TreeSet(java.util.TreeSet) Priority(javax.annotation.Priority) ForcedAutoDiscoverable(org.glassfish.jersey.internal.spi.ForcedAutoDiscoverable) LinkedList(java.util.LinkedList)

Example 2 with ConstrainedTo

use of javax.ws.rs.ConstrainedTo in project jersey by jersey.

the class Providers method getContractConstraint.

private static RuntimeType getContractConstraint(final Class<?> clazz, final RuntimeType defaultConstraint) {
    final ProviderRuntime jaxRsProvider = EXTERNAL_PROVIDER_INTERFACE_WHITELIST.get(clazz);
    RuntimeType result = null;
    if (jaxRsProvider != null) {
        result = jaxRsProvider.getRuntime();
    } else if (clazz.getAnnotation(Contract.class) != null) {
        final ConstrainedTo constrainedToAnnotation = clazz.getAnnotation(ConstrainedTo.class);
        if (constrainedToAnnotation != null) {
            result = constrainedToAnnotation.value();
        }
    }
    return (result == null) ? defaultConstraint : result;
}
Also used : ConstrainedTo(javax.ws.rs.ConstrainedTo) RuntimeType(javax.ws.rs.RuntimeType)

Example 3 with ConstrainedTo

use of javax.ws.rs.ConstrainedTo in project jersey by jersey.

the class Providers method checkProviderRuntime.

/**
     * Check the {@code component} whether it is appropriate correctly configured for client or server
     * {@link RuntimeType runtime}.
     * <p>
     * If a problem occurs a warning is logged and if the component is not usable at all in the current runtime
     * {@code false} is returned. For classes found during component scanning (scanned=true) certain warnings are
     * completely ignored (e.g. components {@link ConstrainedTo constrained to} the client runtime and found by
     * server-side class path scanning will be silently ignored and no warning will be logged).
     *
     * @param component         the class of the component being checked.
     * @param model             model of the component.
     * @param runtimeConstraint current runtime (client or server).
     * @param scanned           {@code false} if the component type has been registered explicitly;
     *                          {@code true} if the class has been discovered during any form of component scanning.
     * @param isResource        {@code true} if the component is also a resource class.
     * @return {@code true} if component is acceptable for use in the given runtime type, {@code false} otherwise.
     */
public static boolean checkProviderRuntime(final Class<?> component, final ContractProvider model, final RuntimeType runtimeConstraint, final boolean scanned, final boolean isResource) {
    final Set<Class<?>> contracts = model.getContracts();
    final ConstrainedTo constrainedTo = component.getAnnotation(ConstrainedTo.class);
    final RuntimeType componentConstraint = constrainedTo == null ? null : constrainedTo.value();
    if (Feature.class.isAssignableFrom(component)) {
        return true;
    }
    final StringBuilder warnings = new StringBuilder();
    try {
        /*
             * Indicates that the provider implements at least one contract compatible
             * with it's implementation class constraint.
             */
        boolean foundComponentCompatible = componentConstraint == null;
        boolean foundRuntimeCompatibleContract = isResource && runtimeConstraint == RuntimeType.SERVER;
        for (final Class<?> contract : contracts) {
            // if the contract is common/not constrained, default to provider constraint
            final RuntimeType contractConstraint = getContractConstraint(contract, componentConstraint);
            foundRuntimeCompatibleContract |= contractConstraint == null || contractConstraint == runtimeConstraint;
            if (componentConstraint != null) {
                if (contractConstraint != componentConstraint) {
                    //noinspection ConstantConditions
                    warnings.append(LocalizationMessages.WARNING_PROVIDER_CONSTRAINED_TO_WRONG_PACKAGE(component.getName(), componentConstraint.name(), contract.getName(), // is never null
                    contractConstraint.name())).append(" ");
                } else {
                    foundComponentCompatible = true;
                }
            }
        }
        if (!foundComponentCompatible) {
            //noinspection ConstantConditions
            warnings.append(LocalizationMessages.ERROR_PROVIDER_CONSTRAINED_TO_WRONG_PACKAGE(component.getName(), // is never null
            componentConstraint.name())).append(" ");
            logProviderSkipped(warnings, component, isResource);
            return false;
        }
        final boolean isProviderRuntimeCompatible;
        // runtimeConstraint vs. providerConstraint
        isProviderRuntimeCompatible = componentConstraint == null || componentConstraint == runtimeConstraint;
        if (!isProviderRuntimeCompatible && !scanned) {
            // log failure for manually registered providers
            warnings.append(LocalizationMessages.ERROR_PROVIDER_CONSTRAINED_TO_WRONG_RUNTIME(component.getName(), componentConstraint.name(), runtimeConstraint.name())).append(" ");
            logProviderSkipped(warnings, component, isResource);
        }
        // runtimeConstraint vs contractConstraint
        if (!foundRuntimeCompatibleContract && !scanned) {
            warnings.append(LocalizationMessages.ERROR_PROVIDER_REGISTERED_WRONG_RUNTIME(component.getName(), runtimeConstraint.name())).append(" ");
            logProviderSkipped(warnings, component, isResource);
            return false;
        }
        return isProviderRuntimeCompatible && foundRuntimeCompatibleContract;
    } finally {
        if (warnings.length() > 0) {
            LOGGER.log(Level.WARNING, warnings.toString());
        }
    }
}
Also used : ConstrainedTo(javax.ws.rs.ConstrainedTo) RuntimeType(javax.ws.rs.RuntimeType)

Example 4 with ConstrainedTo

use of javax.ws.rs.ConstrainedTo in project jersey by jersey.

the class AbstractTypeTester method configureClient.

/**
     * Looks for all providers declared as inner classes of the subclass of this class
     * and adds them to the client configuration (unless constrained to server side).
     */
@Override
protected void configureClient(ClientConfig config) {
    config.register(RequestEntityInterceptor.class);
    config.register(ResponseFilter.class);
    for (Class<?> cls : getClass().getDeclaredClasses()) {
        if (cls.getAnnotation(Provider.class) != null) {
            final ConstrainedTo constrainedTo = cls.getAnnotation(ConstrainedTo.class);
            if (constrainedTo == null || constrainedTo.value() == RuntimeType.CLIENT) {
                config.register(cls);
            }
        }
    }
}
Also used : ConstrainedTo(javax.ws.rs.ConstrainedTo) Provider(javax.ws.rs.ext.Provider)

Aggregations

ConstrainedTo (javax.ws.rs.ConstrainedTo)4 RuntimeType (javax.ws.rs.RuntimeType)2 LinkedList (java.util.LinkedList)1 TreeSet (java.util.TreeSet)1 Priority (javax.annotation.Priority)1 Provider (javax.ws.rs.ext.Provider)1 AutoDiscoverable (org.glassfish.jersey.internal.spi.AutoDiscoverable)1 ForcedAutoDiscoverable (org.glassfish.jersey.internal.spi.ForcedAutoDiscoverable)1