cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject [cxf] branch 3.1.x-fixes updated: [CXF-7561] Checking the Cors annotation on interfaces as well
Date Mon, 20 Nov 2017 17:01:26 GMT
This is an automated email from the ASF dual-hosted git repository.

sergeyb pushed a commit to branch 3.1.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/3.1.x-fixes by this push:
     new fe9ed5b  [CXF-7561] Checking the Cors annotation on interfaces as well
fe9ed5b is described below

commit fe9ed5bd5787916aff36d6987ab7786c16325cbd
Author: Sergey Beryozkin <sberyozkin@gmail.com>
AuthorDate: Mon Nov 20 17:01:12 2017 +0000

    [CXF-7561] Checking the Cors annotation on interfaces as well
---
 .../org/apache/cxf/common/util/ReflectionUtil.java | 23 +++++++++++++++++++++-
 .../systest/jaxrs/cors/AnnotatedCorsServer.java    |  3 ---
 .../systest/jaxrs/cors/CrossOriginSimpleTest.java  | 18 +++++++++++++++++
 .../test/resources/jaxrs_cors/WEB-INF/beans.xml    |  9 +++++++++
 4 files changed, 49 insertions(+), 4 deletions(-)

diff --git a/core/src/main/java/org/apache/cxf/common/util/ReflectionUtil.java b/core/src/main/java/org/apache/cxf/common/util/ReflectionUtil.java
index 3684d6b..9d46bb6 100644
--- a/core/src/main/java/org/apache/cxf/common/util/ReflectionUtil.java
+++ b/core/src/main/java/org/apache/cxf/common/util/ReflectionUtil.java
@@ -278,6 +278,27 @@ public final class ReflectionUtil {
         if (annotation != null) {
             return annotation;
         }
-        return m.getDeclaringClass().getAnnotation(annotationType);
+        annotation = m.getDeclaringClass().getAnnotation(annotationType);
+        if (annotation != null) {
+            return annotation;
+        }
+        for (Class<?> intf : m.getDeclaringClass().getInterfaces()) {
+            annotation = getAnnotationForInterface(intf, annotationType);
+            if (annotation != null) {
+                return annotation;
+            }
+        }
+        return null;
+    }
+    
+    private static <T extends Annotation> T getAnnotationForInterface(Class<?>
intf, Class<T> annotationType) {
+        T annotation = intf.getAnnotation(annotationType);
+        if (annotation != null) {
+            return annotation;
+        }
+        for (Class<?> intf2 : intf.getInterfaces()) {
+            return getAnnotationForInterface(intf2, annotationType);
+        }
+        return null;
     }
 }
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/cors/AnnotatedCorsServer.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/cors/AnnotatedCorsServer.java
index cf9383c..cbf78f0 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/cors/AnnotatedCorsServer.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/cors/AnnotatedCorsServer.java
@@ -36,9 +36,6 @@ import org.apache.cxf.rs.security.cors.CorsHeaderConstants;
 import org.apache.cxf.rs.security.cors.CrossOriginResourceSharing;
 import org.apache.cxf.rs.security.cors.LocalPreflight;
 
-/**
- * Service bean with no class-level annotation for cross-script control.
- */
 @CrossOriginResourceSharing(allowOrigins = {
         "http://area51.mil:31415"
         }, allowCredentials = true, maxAge = 1, allowHeaders = {
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/cors/CrossOriginSimpleTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/cors/CrossOriginSimpleTest.java
index 281f997..acbc5da 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/cors/CrossOriginSimpleTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/cors/CrossOriginSimpleTest.java
@@ -484,6 +484,24 @@ public class CrossOriginSimpleTest extends AbstractBusClientServerTestBase
{
     }
     
     @Test
+    public void testAnnotatedClassCorrectOrigin2() throws Exception {
+        HttpClient httpclient = HttpClientBuilder.create().build();
+        HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/antest2/simpleGet/HelloThere");
+        httpget.addHeader("Origin", "http://area51.mil:31415");
+
+        HttpResponse response = httpclient.execute(httpget);
+        assertEquals(200, response.getStatusLine().getStatusCode());
+        HttpEntity entity = response.getEntity();
+        String e = IOUtils.toString(entity.getContent(), "utf-8");
+
+        assertEquals("HelloThere", e); // ensure that we didn't bust the operation itself.
+        assertOriginResponse(false, new String[] {"http://area51.mil:31415" }, true, response);
+        if (httpclient instanceof Closeable) {
+            ((Closeable)httpclient).close();
+        }
+    }
+
+    @Test
     public void testAnnotatedClassWrongOrigin() throws Exception {
         HttpClient httpclient = HttpClientBuilder.create().build();
         HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/antest/simpleGet/HelloThere");
diff --git a/systests/jaxrs/src/test/resources/jaxrs_cors/WEB-INF/beans.xml b/systests/jaxrs/src/test/resources/jaxrs_cors/WEB-INF/beans.xml
index 17962f2..e850859 100644
--- a/systests/jaxrs/src/test/resources/jaxrs_cors/WEB-INF/beans.xml
+++ b/systests/jaxrs/src/test/resources/jaxrs_cors/WEB-INF/beans.xml
@@ -33,6 +33,14 @@
             <ref bean="cors-filter"/>
         </jaxrs:providers>
     </jaxrs:server>
+    <jaxrs:server id="ann-cors-service2" address="/antest2">
+        <jaxrs:serviceBeans>
+            <ref bean="ann-cors-server2"/>
+        </jaxrs:serviceBeans>
+        <jaxrs:providers>
+            <ref bean="cors-filter"/>
+        </jaxrs:providers>
+    </jaxrs:server>
     <jaxrs:server id="config-service" address="/config">
         <jaxrs:serviceBeans>
             <ref bean="config-server"/>
@@ -46,4 +54,5 @@
     </bean>
     <bean id="unann-cors-server" scope="prototype" class="org.apache.cxf.systest.jaxrs.cors.UnannotatedCorsServer"/>
     <bean id="ann-cors-server" scope="prototype" class="org.apache.cxf.systest.jaxrs.cors.AnnotatedCorsServer"/>
+    <bean id="ann-cors-server2" scope="prototype" class="org.apache.cxf.systest.jaxrs.cors.AnnotatedCorsServer2"/>
 </beans>

-- 
To stop receiving notification emails like this one, please contact
['"commits@cxf.apache.org" <commits@cxf.apache.org>'].

Mime
View raw message