Search in sources :

Example 6 with RuntimeType

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

the class CommittingOutputStreamTest method testPropertiesWithMessageContext.

@Test
public void testPropertiesWithMessageContext() throws IOException {
    final int size = 20;
    Map<String, Object> properties = new HashMap<>();
    properties.put(CommonProperties.OUTBOUND_CONTENT_LENGTH_BUFFER, size);
    final RuntimeType runtime = RuntimeType.CLIENT;
    checkBufferSize(size, properties, runtime);
}
Also used : HashMap(java.util.HashMap) RuntimeType(javax.ws.rs.RuntimeType) Test(org.junit.Test)

Example 7 with RuntimeType

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

the class MetaInfServicesAutoDiscoverable method configure.

@Override
public void configure(final FeatureContext context) {
    final Map<String, Object> properties = context.getConfiguration().getProperties();
    final RuntimeType runtimeType = context.getConfiguration().getRuntimeType();
    context.register(new AbstractBinder() {

        @Override
        protected void configure() {
            // Message Body providers.
            install(new ServiceFinderBinder<MessageBodyReader>(MessageBodyReader.class, properties, runtimeType));
            install(new ServiceFinderBinder<MessageBodyWriter>(MessageBodyWriter.class, properties, runtimeType));
            // Exception Mappers.
            install(new ServiceFinderBinder<ExceptionMapper>(ExceptionMapper.class, properties, runtimeType));
        }
    });
}
Also used : ServiceFinderBinder(org.glassfish.jersey.internal.ServiceFinderBinder) RuntimeType(javax.ws.rs.RuntimeType) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder)

Example 8 with RuntimeType

use of javax.ws.rs.RuntimeType 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 9 with RuntimeType

use of javax.ws.rs.RuntimeType 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 10 with RuntimeType

use of javax.ws.rs.RuntimeType in project cxf by apache.

the class ConfigurableImpl method checkConstraints.

private boolean checkConstraints(Object provider) {
    Class<?> providerClass = provider.getClass();
    ConstrainedTo providerConstraint = providerClass.getAnnotation(ConstrainedTo.class);
    if (providerConstraint != null) {
        RuntimeType currentRuntime = config.getRuntimeType();
        RuntimeType providerRuntime = providerConstraint.value();
        // and (2) does the provider implement an invalid interface based on the constrained runtime type
        if (!providerRuntime.equals(currentRuntime)) {
            LOG.warning("Provider " + provider + " cannot be registered in this " + currentRuntime + " runtime because it is constrained to " + providerRuntime + " runtimes.");
            return false;
        }
        Class<?>[] restrictedInterfaces = RuntimeType.CLIENT.equals(providerRuntime) ? RESTRICTED_CLASSES_IN_CLIENT : RESTRICTED_CLASSES_IN_SERVER;
        for (Class<?> restrictedContract : restrictedInterfaces) {
            if (restrictedContract.isAssignableFrom(providerClass)) {
                RuntimeType opposite = RuntimeType.CLIENT.equals(providerRuntime) ? RuntimeType.SERVER : RuntimeType.CLIENT;
                LOG.warning("Provider " + providerClass.getName() + " is invalid - it is constrained to " + providerRuntime + " runtimes but implements a " + opposite + " interface ");
                return false;
            }
        }
    }
    return true;
}
Also used : ConstrainedTo(javax.ws.rs.ConstrainedTo) RuntimeType(javax.ws.rs.RuntimeType)

Aggregations

RuntimeType (javax.ws.rs.RuntimeType)10 HashMap (java.util.HashMap)3 ConstrainedTo (javax.ws.rs.ConstrainedTo)3 Annotation (java.lang.annotation.Annotation)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 List (java.util.List)2 Set (java.util.Set)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 Singleton (javax.inject.Singleton)2 AbstractBinder (org.glassfish.jersey.internal.inject.AbstractBinder)2 Test (org.junit.Test)2 OutputStream (java.io.OutputStream)1 Type (java.lang.reflect.Type)1 Principal (java.security.Principal)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 IdentityHashMap (java.util.IdentityHashMap)1