Search in sources :

Example 1 with MediaTypeMap

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));
    }
}
Also used : MediaTypeMap(org.jboss.resteasy.core.MediaTypeMap) ResteasyJaxrsProviderBuildItem(io.quarkus.resteasy.common.spi.ResteasyJaxrsProviderBuildItem) Collection(java.util.Collection) DotName(org.jboss.jandex.DotName) AnnotationInstance(org.jboss.jandex.AnnotationInstance) ResponseEntityFeature(org.jboss.resteasy.spring.web.ResponseEntityFeature) HashSet(java.util.HashSet) ResponseStatusFeature(org.jboss.resteasy.spring.web.ResponseStatusFeature) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 2 with MediaTypeMap

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;
}
Also used : DotNames(io.quarkus.arc.processor.DotNames) ConfigGroup(io.quarkus.runtime.annotations.ConfigGroup) Arrays(java.util.Arrays) Produces(javax.ws.rs.Produces) AcceptEncodingGZIPFilter(org.jboss.resteasy.plugins.interceptors.AcceptEncodingGZIPFilter) UnremovableBeanBuildItem(io.quarkus.arc.deployment.UnremovableBeanBuildItem) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) ResteasyConfigSourceProvider(io.quarkus.resteasy.common.runtime.config.ResteasyConfigSourceProvider) FieldInfo(org.jboss.jandex.FieldInfo) CombinedIndexBuildItem(io.quarkus.deployment.builditem.CombinedIndexBuildItem) BuildProducer(io.quarkus.deployment.annotations.BuildProducer) Capabilities(io.quarkus.deployment.Capabilities) ContextResolver(javax.ws.rs.ext.ContextResolver) MediaType(javax.ws.rs.core.MediaType) MethodInfo(org.jboss.jandex.MethodInfo) ServerFormUrlEncodedProvider(io.quarkus.resteasy.common.runtime.providers.ServerFormUrlEncodedProvider) AdditionalBeanBuildItem(io.quarkus.arc.deployment.AdditionalBeanBuildItem) Consumes(javax.ws.rs.Consumes) ResteasyConfigBuildItem(io.quarkus.resteasy.common.spi.ResteasyConfigBuildItem) AnnotationTarget(org.jboss.jandex.AnnotationTarget) ParameterizedType(org.jboss.jandex.ParameterizedType) AnnotationValue(org.jboss.jandex.AnnotationValue) Consume(io.quarkus.deployment.annotations.Consume) Providers(javax.ws.rs.ext.Providers) STATIC_INIT(io.quarkus.deployment.annotations.ExecutionTime.STATIC_INIT) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) ServletContextConfigSourceImpl(org.jboss.resteasy.microprofile.config.ServletContextConfigSourceImpl) StringTextStar(org.jboss.resteasy.plugins.providers.StringTextStar) List(java.util.List) MediaTypeMap(org.jboss.resteasy.core.MediaTypeMap) ConfigItem(io.quarkus.runtime.annotations.ConfigItem) AnnotationInstance(org.jboss.jandex.AnnotationInstance) BeanContainerBuildItem(io.quarkus.arc.deployment.BeanContainerBuildItem) ServiceProviderBuildItem(io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem) SseConstants(org.jboss.resteasy.plugins.providers.sse.SseConstants) ServletConfigSourceImpl(org.jboss.resteasy.microprofile.config.ServletConfigSourceImpl) StaticInitConfigSourceProviderBuildItem(io.quarkus.deployment.builditem.StaticInitConfigSourceProviderBuildItem) GZIPDecodingInterceptor(org.jboss.resteasy.plugins.interceptors.GZIPDecodingInterceptor) ReflectiveClassBuildItem(io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem) Record(io.quarkus.deployment.annotations.Record) MemorySize(io.quarkus.runtime.configuration.MemorySize) Logger(org.jboss.logging.Logger) DotName(org.jboss.jandex.DotName) Type(org.jboss.jandex.Type) FilterConfigSourceImpl(org.jboss.resteasy.microprofile.config.FilterConfigSourceImpl) Function(java.util.function.Function) ResteasyInjectorFactoryRecorder(io.quarkus.resteasy.common.runtime.ResteasyInjectorFactoryRecorder) ArrayList(java.util.ArrayList) ConfigRoot(io.quarkus.runtime.annotations.ConfigRoot) HashSet(java.util.HashSet) BuildStep(io.quarkus.deployment.annotations.BuildStep) GZIPEncodingInterceptor(org.jboss.resteasy.plugins.interceptors.GZIPEncodingInterceptor) ResteasyDotNames(io.quarkus.resteasy.common.spi.ResteasyDotNames) RuntimeValue(io.quarkus.runtime.RuntimeValue) BeanArchiveIndexBuildItem(io.quarkus.arc.deployment.BeanArchiveIndexBuildItem) IndexView(org.jboss.jandex.IndexView) Capability(io.quarkus.deployment.Capability) InjectorFactory(org.jboss.resteasy.spi.InjectorFactory) Kind(org.jboss.jandex.AnnotationValue.Kind) ServiceUtil(io.quarkus.deployment.util.ServiceUtil) ResteasyJaxrsProviderBuildItem(io.quarkus.resteasy.common.spi.ResteasyJaxrsProviderBuildItem) Collections(java.util.Collections) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) MediaTypeMap(org.jboss.resteasy.core.MediaTypeMap) IndexView(org.jboss.jandex.IndexView) ResteasyJaxrsProviderBuildItem(io.quarkus.resteasy.common.spi.ResteasyJaxrsProviderBuildItem) UnremovableBeanBuildItem(io.quarkus.arc.deployment.UnremovableBeanBuildItem) Providers(javax.ws.rs.ext.Providers) AnnotationInstance(org.jboss.jandex.AnnotationInstance) ServerFormUrlEncodedProvider(io.quarkus.resteasy.common.runtime.providers.ServerFormUrlEncodedProvider) ReflectiveClassBuildItem(io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem) HashSet(java.util.HashSet) StringTextStar(org.jboss.resteasy.plugins.providers.StringTextStar) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 3 with MediaTypeMap

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;
}
Also used : Decorator(org.jboss.resteasy.annotations.Decorator) MediaTypeMap(org.jboss.resteasy.core.MediaTypeMap) HashMap(java.util.HashMap) Decorators(org.jboss.resteasy.annotations.Decorators) Annotation(java.lang.annotation.Annotation)

Example 4 with MediaTypeMap

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;
}
Also used : MediaTypeMap(org.jboss.resteasy.core.MediaTypeMap) ResourceBuilder(org.jboss.resteasy.spi.metadata.ResourceBuilder) SnapshotSet(org.jboss.resteasy.util.snapshot.SnapshotSet) SnapshotMap(org.jboss.resteasy.util.snapshot.SnapshotMap) TreeSet(java.util.TreeSet) ContextResolver(jakarta.ws.rs.ext.ContextResolver)

Aggregations

MediaTypeMap (org.jboss.resteasy.core.MediaTypeMap)4 BuildStep (io.quarkus.deployment.annotations.BuildStep)2 ResteasyJaxrsProviderBuildItem (io.quarkus.resteasy.common.spi.ResteasyJaxrsProviderBuildItem)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 AdditionalBeanBuildItem (io.quarkus.arc.deployment.AdditionalBeanBuildItem)1 BeanArchiveIndexBuildItem (io.quarkus.arc.deployment.BeanArchiveIndexBuildItem)1 BeanContainerBuildItem (io.quarkus.arc.deployment.BeanContainerBuildItem)1 UnremovableBeanBuildItem (io.quarkus.arc.deployment.UnremovableBeanBuildItem)1 DotNames (io.quarkus.arc.processor.DotNames)1 Capabilities (io.quarkus.deployment.Capabilities)1 Capability (io.quarkus.deployment.Capability)1 BuildProducer (io.quarkus.deployment.annotations.BuildProducer)1 Consume (io.quarkus.deployment.annotations.Consume)1 STATIC_INIT (io.quarkus.deployment.annotations.ExecutionTime.STATIC_INIT)1 Record (io.quarkus.deployment.annotations.Record)1 CombinedIndexBuildItem (io.quarkus.deployment.builditem.CombinedIndexBuildItem)1 StaticInitConfigSourceProviderBuildItem (io.quarkus.deployment.builditem.StaticInitConfigSourceProviderBuildItem)1 ReflectiveClassBuildItem (io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem)1 ServiceProviderBuildItem (io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem)1