use of org.jboss.resteasy.core.MediaTypeMap in project quarkus by quarkusio.
the class SpringWebResteasyClassicProcessor method registerProviders.
@BuildStep
public void registerProviders(BeanArchiveIndexBuildItem beanArchiveIndexBuildItem, BuildProducer<ResteasyJaxrsProviderBuildItem> providersProducer) throws IOException {
// TODO only read this information once since it is exactly the same in ResteasyCommonProcessor#setupProviders
final Set<String> availableProviders = ServiceUtil.classNamesNamedIn(getClass().getClassLoader(), "META-INF/services/" + Providers.class.getName());
final MediaTypeMap<String> categorizedReaders = new MediaTypeMap<>();
final MediaTypeMap<String> categorizedWriters = new MediaTypeMap<>();
final MediaTypeMap<String> categorizedContextResolvers = new MediaTypeMap<>();
final Set<String> otherProviders = new HashSet<>();
ResteasyCommonProcessor.categorizeProviders(availableProviders, categorizedReaders, categorizedWriters, categorizedContextResolvers, otherProviders);
boolean useAllAvailable = false;
Set<String> providersToRegister = new HashSet<>();
OUTER: for (DotName mappingClass : MAPPING_ANNOTATIONS) {
final Collection<AnnotationInstance> instances = beanArchiveIndexBuildItem.getIndex().getAnnotations(mappingClass);
for (AnnotationInstance instance : instances) {
if (collectProviders(providersToRegister, categorizedWriters, instance, "produces")) {
useAllAvailable = true;
break OUTER;
}
if (collectProviders(providersToRegister, categorizedContextResolvers, instance, "produces")) {
useAllAvailable = true;
break OUTER;
}
if (collectProviders(providersToRegister, categorizedReaders, instance, "consumes")) {
useAllAvailable = true;
break OUTER;
}
}
}
if (useAllAvailable) {
providersToRegister = availableProviders;
} else {
// for Spring Web we register all the json providers by default because using "produces" in @RequestMapping
// and friends is optional
providersToRegister.addAll(categorizedWriters.getPossible(MediaType.APPLICATION_JSON_TYPE));
// we also need to register the custom Spring related providers
providersToRegister.add(ResponseEntityFeature.class.getName());
providersToRegister.add(ResponseStatusFeature.class.getName());
}
for (String provider : providersToRegister) {
providersProducer.produce(new ResteasyJaxrsProviderBuildItem(provider));
}
}
use of org.jboss.resteasy.core.MediaTypeMap in project quarkus by quarkusio.
the class ResteasyCommonProcessor method setupProviders.
@BuildStep
JaxrsProvidersToRegisterBuildItem setupProviders(BuildProducer<ReflectiveClassBuildItem> reflectiveClass, CombinedIndexBuildItem indexBuildItem, BeanArchiveIndexBuildItem beanArchiveIndexBuildItem, BuildProducer<UnremovableBeanBuildItem> unremovableBeans, List<ResteasyJaxrsProviderBuildItem> contributedProviderBuildItems, List<RestClientBuildItem> restClients, ResteasyConfigBuildItem resteasyConfig, Capabilities capabilities) throws Exception {
Set<String> contributedProviders = new HashSet<>();
for (ResteasyJaxrsProviderBuildItem contributedProviderBuildItem : contributedProviderBuildItems) {
contributedProviders.add(contributedProviderBuildItem.getName());
}
Set<String> annotatedProviders = new HashSet<>();
for (AnnotationInstance i : indexBuildItem.getIndex().getAnnotations(ResteasyDotNames.PROVIDER)) {
if (i.target().kind() == AnnotationTarget.Kind.CLASS) {
annotatedProviders.add(i.target().asClass().name().toString());
}
checkProperConfigAccessInProvider(i);
}
contributedProviders.addAll(annotatedProviders);
Set<String> availableProviders = new HashSet<>(ServiceUtil.classNamesNamedIn(getClass().getClassLoader(), "META-INF/services/" + Providers.class.getName()));
// this one is added manually in RESTEasy's ResteasyDeploymentImpl
availableProviders.add(ServerFormUrlEncodedProvider.class.getName());
MediaTypeMap<String> categorizedReaders = new MediaTypeMap<>();
MediaTypeMap<String> categorizedWriters = new MediaTypeMap<>();
MediaTypeMap<String> categorizedContextResolvers = new MediaTypeMap<>();
Set<String> otherProviders = new HashSet<>();
categorizeProviders(availableProviders, categorizedReaders, categorizedWriters, categorizedContextResolvers, otherProviders);
// add the other providers detected
Set<String> providersToRegister = new HashSet<>(otherProviders);
if (!capabilities.isPresent(Capability.VERTX) && !capabilities.isCapabilityWithPrefixPresent(Capability.RESTEASY_JSON)) {
boolean needJsonSupport = restJsonSupportNeeded(indexBuildItem, ResteasyDotNames.CONSUMES) || restJsonSupportNeeded(indexBuildItem, ResteasyDotNames.PRODUCES) || restJsonSupportNeeded(indexBuildItem, ResteasyDotNames.RESTEASY_SSE_ELEMENT_TYPE) || restJsonSupportNeeded(indexBuildItem, ResteasyDotNames.RESTEASY_PART_TYPE);
if (needJsonSupport) {
LOGGER.warn("Quarkus detected the need of REST JSON support but you have not provided the necessary JSON " + "extension for this. You can visit https://quarkus.io/guides/rest-json for more " + "information on how to set one.");
}
}
if (!capabilities.isPresent(Capability.RESTEASY_MUTINY)) {
String needsMutinyClasses = mutinySupportNeeded(indexBuildItem);
if (needsMutinyClasses != null) {
LOGGER.warn("Quarkus detected the need for Mutiny reactive programming support, however the quarkus-resteasy-mutiny extension " + "was not present. Reactive REST endpoints in your application that return Uni or Multi " + "will not function as you expect until you add this extension. Endpoints that need Mutiny are: " + needsMutinyClasses);
}
}
// we add a couple of default providers
providersToRegister.add(StringTextStar.class.getName());
providersToRegister.addAll(categorizedWriters.getPossible(MediaType.APPLICATION_JSON_TYPE));
IndexView index = indexBuildItem.getIndex();
IndexView beansIndex = beanArchiveIndexBuildItem.getIndex();
// find the providers declared in our services
boolean useBuiltinProviders = collectDeclaredProviders(restClients, resteasyConfig, providersToRegister, categorizedReaders, categorizedWriters, categorizedContextResolvers, index, beansIndex);
if (useBuiltinProviders) {
providersToRegister = new HashSet<>(contributedProviders);
providersToRegister.addAll(availableProviders);
} else {
providersToRegister.addAll(contributedProviders);
}
if (providersToRegister.contains("org.jboss.resteasy.plugins.providers.jsonb.JsonBindingProvider")) {
// This abstract one is also accessed directly via reflection
reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, "org.jboss.resteasy.plugins.providers.jsonb.AbstractJsonBindingProvider"));
}
JaxrsProvidersToRegisterBuildItem result = new JaxrsProvidersToRegisterBuildItem(providersToRegister, contributedProviders, annotatedProviders, useBuiltinProviders);
// Providers that are also beans are unremovable
unremovableBeans.produce(new UnremovableBeanBuildItem(b -> result.getProviders().contains(b.getBeanClass().toString())));
return result;
}
use of org.jboss.resteasy.core.MediaTypeMap in project resteasy by resteasy.
the class DecoratorMatcher method decorate.
/**
* @param targetClass i.e. Marshaller
* @param target target object
* @param type i.e. a JAXB annotated class
* @param annotations i.e. method or parameter annotations
* @param mediaType media type
* @param <T> type
* @return decorated target object
*/
@SuppressWarnings(value = "rawtypes")
public <T> T decorate(Class<T> targetClass, T target, Class type, Annotation[] annotations, MediaType mediaType) {
HashMap<Class<?>, Annotation> meta = new HashMap<Class<?>, Annotation>();
if (type != null) {
registerDecorators(targetClass, meta, type.getAnnotations());
}
// override any class level ones
if (annotations != null) {
registerDecorators(targetClass, meta, annotations);
}
if (meta.size() == 0)
return target;
MediaTypeMap<Class<?>> typeMap = new MediaTypeMap<Class<?>>();
for (Class<?> decoratorAnnotation : meta.keySet()) {
Decorators decorators = decoratorAnnotation.getAnnotation(Decorators.class);
if (decorators != null) {
for (Decorator decorator : decorators.values()) {
injectDecorator(typeMap, decoratorAnnotation, decorator);
}
} else {
Decorator decorator = decoratorAnnotation.getAnnotation(Decorator.class);
injectDecorator(typeMap, decoratorAnnotation, decorator);
}
}
List<Class<?>> list = typeMap.getPossible(mediaType);
for (Class<?> decoratorAnnotation : list) {
Annotation annotation = meta.get(decoratorAnnotation);
Decorators decorators = decoratorAnnotation.getAnnotation(Decorators.class);
if (decorators != null) {
for (Decorator decorator : decorators.values()) {
if (decorator.target().isAssignableFrom(target.getClass())) {
target = doDecoration(target, type, annotations, mediaType, annotation, decorator);
}
}
} else {
Decorator decorator = decoratorAnnotation.getAnnotation(Decorator.class);
target = doDecoration(target, type, annotations, mediaType, annotation, decorator);
}
}
return target;
}
use of org.jboss.resteasy.core.MediaTypeMap in project resteasy by resteasy.
the class ResteasyProviderFactoryImpl method initializeCommon.
protected void initializeCommon(ResteasyProviderFactoryImpl parent, boolean lockSnapshots, boolean snapFirst) {
properties = parent == null ? new SnapshotMap<>(lockSnapshots) : new SnapshotMap<>(parent.properties, true, lockSnapshots, snapFirst);
providerClasses = parent == null ? new SnapshotSet<>(lockSnapshots) : new SnapshotSet<>(parent.providerClasses, true, lockSnapshots, snapFirst);
providerInstances = parent == null ? new SnapshotSet<>(lockSnapshots) : new SnapshotSet<>(parent.providerInstances, true, lockSnapshots, snapFirst);
classContracts = parent == null ? new SnapshotMap<>(lockSnapshots) : new SnapshotMap<>(parent.classContracts, true, lockSnapshots, snapFirst);
enabledFeatures = parent == null ? new SnapshotSet<>(lockSnapshots) : new SnapshotSet<>(parent.enabledFeatures, true, lockSnapshots, snapFirst);
if (parent != null) {
if (snapFirst) {
// resourcemethod invoker factory
// we don't want to copy these
attachedParamConverterProviders = true;
sortedParamConverterProviders = parent.sortedParamConverterProviders;
attachedContextResolvers = true;
contextResolvers = parent.contextResolvers;
} else {
contextResolvers = new ConcurrentHashMap<>();
for (Entry<Class<?>, MediaTypeMap<SortedKey<ContextResolver>>> entry : parent.contextResolvers.entrySet()) {
contextResolvers.put(entry.getKey(), new MediaTypeMap<>(entry.getValue()));
}
sortedParamConverterProviders = Collections.synchronizedSortedSet(new TreeSet<>(parent.sortedParamConverterProviders));
}
} else {
contextResolvers = new ConcurrentHashMap<>();
sortedParamConverterProviders = Collections.synchronizedSortedSet(new TreeSet<>());
}
resourceBuilder = new ResourceBuilder();
headerDelegates = parent == null ? new SnapshotMap<>(lockSnapshots) : new SnapshotMap<>(parent.getHeaderDelegates(), true, lockSnapshots, snapFirst);
if (parent == null) {
// parent should always have these delegates
addHeaderDelegateIfAbsent(MediaType.class, MediaTypeHeaderDelegate.INSTANCE);
addHeaderDelegateIfAbsent(NewCookie.class, NewCookieHeaderDelegate.INSTANCE);
addHeaderDelegateIfAbsent(Cookie.class, CookieHeaderDelegate.INSTANCE);
addHeaderDelegateIfAbsent(URI.class, UriHeaderDelegate.INSTANCE);
addHeaderDelegateIfAbsent(EntityTag.class, EntityTagDelegate.INSTANCE);
addHeaderDelegateIfAbsent(CacheControl.class, CacheControlDelegate.INSTANCE);
addHeaderDelegateIfAbsent(Locale.class, LocaleDelegate.INSTANCE);
addHeaderDelegateIfAbsent(LinkHeader.class, LinkHeaderDelegate.INSTANCE);
addHeaderDelegateIfAbsent(jakarta.ws.rs.core.Link.class, LinkDelegate.INSTANCE);
addHeaderDelegateIfAbsent(Date.class, DateDelegate.INSTANCE);
}
builtinsRegistered = false;
registerBuiltins = true;
stringParameterUnmarshallers = parent == null ? new SnapshotMap<>(lockSnapshots) : new SnapshotMap<>(parent.stringParameterUnmarshallers, true, lockSnapshots, snapFirst);
contextInjectors = parent == null ? new SnapshotMap<>(lockSnapshots) : new SnapshotMap<>(parent.contextInjectors, true, lockSnapshots, snapFirst);
asyncContextInjectors = parent == null ? new SnapshotMap<>(lockSnapshots) : new SnapshotMap<>(parent.asyncContextInjectors, true, lockSnapshots, snapFirst);
injectorFactory = parent == null ? InjectorFactoryImpl.INSTANCE : parent.getInjectorFactory();
initialized = true;
}
Aggregations