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);
}
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));
}
});
}
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;
}
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());
}
}
}
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;
}
Aggregations