Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.x] Fix CORS annotation handling error in certain cases #5102

Merged
merged 1 commit into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Initialized;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.inject.spi.ProcessManagedBean;
import javax.enterprise.inject.spi.WithAnnotations;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.Path;

Expand Down Expand Up @@ -63,27 +60,20 @@ public class CorsCdiExtension implements Extension {

private CorsSupportMp corsSupportMp;

private final Set<AnnotatedType<?>> annotatedTypes = new HashSet<>();
private final Set<Method> methodsWithCrossOriginIncorrectlyUsed = new HashSet<>();
private final Map<Method, CrossOriginConfig> corsConfigs = new HashMap<>();

void recordCrossOrigin(@Observes @WithAnnotations(CrossOrigin.class) ProcessAnnotatedType<?> pat) {
annotatedTypes.add(pat.getAnnotatedType());
}

void processManagedBean(@Observes ProcessManagedBean<?> pmb) {
if (annotatedTypes.contains(pmb.getAnnotatedBeanClass())) {
pmb.getAnnotatedBeanClass().getMethods().forEach(am -> {
Method method = am.getJavaMember();
if (am.isAnnotationPresent(CrossOrigin.class) && !am.isAnnotationPresent(OPTIONS.class)) {
methodsWithCrossOriginIncorrectlyUsed.add(method);
} else {
crossOriginConfigFromAnnotationOnAssociatedMethod(method)
.ifPresent(crossOriginConfig -> corsConfigs.put(method,
crossOriginConfig));
}
});
}
pmb.getAnnotatedBeanClass().getMethods().forEach(am -> {
Method method = am.getJavaMember();
if (am.isAnnotationPresent(CrossOrigin.class) && !am.isAnnotationPresent(OPTIONS.class)) {
methodsWithCrossOriginIncorrectlyUsed.add(method);
} else {
crossOriginConfigFromAnnotationOnAssociatedMethod(method)
.ifPresent(crossOriginConfig -> corsConfigs.put(method,
crossOriginConfig));
}
});
}

void recordSupplierOfCrossOriginConfigFromAnnotation(Supplier<Optional<CrossOriginConfig>> supplier) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.microprofile.cors;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.util.AnnotationLiteral;
import javax.inject.Inject;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

import io.helidon.microprofile.tests.junit5.AddBean;
import io.helidon.microprofile.tests.junit5.AddExtension;

import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

@AddBean(CrossOriginTest.CorsResource0.class)
@AddExtension(TestWithAdjustedCorsType.AugmentingExtension.class)
class TestWithAdjustedCorsType extends BaseCrossOriginTest {

@Inject
private WebTarget webTarget;

@Test
void checkAdjustedResource() {
// The extension below will have augmented the AnnotatedType (adding a synthetic annotation).
// Weld delivers a different concrete subtype of AnnotatedType to the CORS extension as a result.
// Make sure the CORS request still works correctly.

Response response = webTarget.path("/cors0")
.request()
.header("Origin", "http://foo.com")
.header("Host", "here.com")
.buildGet()
.invoke();

assertThat("Status from simple CORS request", response.getStatus(), is(200));
}

public static class AugmentingExtension implements Extension {

void processAnnotatedType(@Observes ProcessAnnotatedType<?> pat) {
// Single out the CORS-controlled resource.
if (pat.getAnnotatedType().getJavaClass().getName().equals(CrossOriginTest.CorsResource0.class.getName())) {
pat.configureAnnotatedType()
.add(AugmentingAnnotation.Literal.getInstance());
}
}
}

@Target(ElementType.TYPE)
static @interface AugmentingAnnotation {
class Literal extends AnnotationLiteral<AugmentingAnnotation>implements AugmentingAnnotation {

private static final long serialVersionUID = 1L;

private static final Literal INSTANCE=new Literal();

static Literal getInstance(){
return INSTANCE;
}

private Literal(){
}
}
}
}