Search in sources :

Example 1 with OPTIONS

use of jakarta.ws.rs.OPTIONS in project helidon by oracle.

the class CorsCdiExtension method crossOriginConfigFromAnnotationOnAssociatedMethod.

private Optional<CrossOriginConfig> crossOriginConfigFromAnnotationOnAssociatedMethod(Method resourceMethod) {
    /*
         * Only @OPTIONS methods should bear the @CrossOrigin annotation, but the annotation on such methods applies to
         * all methods sharing the same path.
         */
    Class<?> resourceClass = resourceMethod.getDeclaringClass();
    CrossOrigin corsAnnot;
    OPTIONS optsAnnot = resourceMethod.getAnnotation(OPTIONS.class);
    Path pathAnnot = resourceMethod.getAnnotation(Path.class);
    if (optsAnnot != null) {
        corsAnnot = resourceMethod.getAnnotation(CrossOrigin.class);
    } else {
        // Find the @OPTIONS method with the same path as the resource method, if any. That one might have a
        // @CrossOrigin annotation which applies to the resource method.
        Optional<Method> optionsMethod = Arrays.stream(resourceClass.getDeclaredMethods()).filter(m -> {
            OPTIONS optsAnnot2 = m.getAnnotation(OPTIONS.class);
            Path pathAnnot2 = m.getAnnotation(Path.class);
            if (optsAnnot2 != null) {
                if (pathAnnot != null) {
                    return pathAnnot2 != null && pathAnnot.value().equals(pathAnnot2.value());
                }
                return pathAnnot2 == null;
            }
            return false;
        }).findFirst();
        corsAnnot = optionsMethod.map(m -> m.getAnnotation(CrossOrigin.class)).orElse(null);
    }
    return Optional.ofNullable(corsAnnot == null ? null : annotationToConfig(corsAnnot));
}
Also used : Path(jakarta.ws.rs.Path) Arrays(java.util.Arrays) Observes(jakarta.enterprise.event.Observes) Config(io.helidon.config.Config) MpConfig(io.helidon.config.mp.MpConfig) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) Set(java.util.Set) HashMap(java.util.HashMap) LIBRARY_BEFORE(jakarta.interceptor.Interceptor.Priority.LIBRARY_BEFORE) CrossOriginConfig(io.helidon.webserver.cors.CrossOriginConfig) ProcessManagedBean(jakarta.enterprise.inject.spi.ProcessManagedBean) Supplier(java.util.function.Supplier) OPTIONS(jakarta.ws.rs.OPTIONS) Path(jakarta.ws.rs.Path) HashSet(java.util.HashSet) Extension(jakarta.enterprise.inject.spi.Extension) ProcessAnnotatedType(jakarta.enterprise.inject.spi.ProcessAnnotatedType) WithAnnotations(jakarta.enterprise.inject.spi.WithAnnotations) ConfigProvider(org.eclipse.microprofile.config.ConfigProvider) Map(java.util.Map) Optional(java.util.Optional) Priority(jakarta.annotation.Priority) AnnotatedType(jakarta.enterprise.inject.spi.AnnotatedType) Method(java.lang.reflect.Method) Initialized(jakarta.enterprise.context.Initialized) Method(java.lang.reflect.Method) OPTIONS(jakarta.ws.rs.OPTIONS)

Aggregations

Config (io.helidon.config.Config)1 MpConfig (io.helidon.config.mp.MpConfig)1 CrossOriginConfig (io.helidon.webserver.cors.CrossOriginConfig)1 Priority (jakarta.annotation.Priority)1 ApplicationScoped (jakarta.enterprise.context.ApplicationScoped)1 Initialized (jakarta.enterprise.context.Initialized)1 Observes (jakarta.enterprise.event.Observes)1 AnnotatedType (jakarta.enterprise.inject.spi.AnnotatedType)1 Extension (jakarta.enterprise.inject.spi.Extension)1 ProcessAnnotatedType (jakarta.enterprise.inject.spi.ProcessAnnotatedType)1 ProcessManagedBean (jakarta.enterprise.inject.spi.ProcessManagedBean)1 WithAnnotations (jakarta.enterprise.inject.spi.WithAnnotations)1 LIBRARY_BEFORE (jakarta.interceptor.Interceptor.Priority.LIBRARY_BEFORE)1 OPTIONS (jakarta.ws.rs.OPTIONS)1 Path (jakarta.ws.rs.Path)1 Method (java.lang.reflect.Method)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1