use of io.helidon.webserver.cors.CrossOriginConfig 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));
}
Aggregations