cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1389470 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/ main/java/org/apache/cxf/jaxrs/model/ test/java/org/apache/cxf/jaxrs/model/
Date Mon, 24 Sep 2012 16:15:59 GMT
Author: sergeyb
Date: Mon Sep 24 16:15:58 2012
New Revision: 1389470

URL: http://svn.apache.org/viewvc?rev=1389470&view=rev
Log:
[CXF-4455] Minor update to get Application name-bindings applied to root resources, as well
as an update to get the inheritance between sub and root resources working better

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ClassResourceInfo.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfo.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/ClassResourceInfoTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java?rev=1389470&r1=1389469&r2=1389470&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.java
Mon Sep 24 16:15:58 2012
@@ -44,6 +44,7 @@ import org.apache.cxf.jaxrs.lifecycle.Re
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
 import org.apache.cxf.jaxrs.model.ProviderInfo;
 import org.apache.cxf.jaxrs.provider.ProviderFactory;
+import org.apache.cxf.jaxrs.utils.AnnotationUtils;
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
 import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 import org.apache.cxf.service.factory.FactoryBeanListener;
@@ -93,7 +94,10 @@ public class JAXRSServerFactoryBean exte
      * @param app
      */
     public void setApplication(Application app) {
-        appProvider = new ProviderInfo<Application>(app, getBus());    
+        appProvider = new ProviderInfo<Application>(app, getBus());
+        for (ClassResourceInfo cri : getServiceFactory().getClassResourceInfo()) {
+            cri.setNameBindings(AnnotationUtils.getNameBindings(app.getClass().getAnnotations()));
+        }
     }
     
     /**

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ClassResourceInfo.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ClassResourceInfo.java?rev=1389470&r1=1389469&r2=1389470&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ClassResourceInfo.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/ClassResourceInfo.java
Mon Sep 24 16:15:58 2012
@@ -57,6 +57,8 @@ public class ClassResourceInfo extends A
     private boolean createdFromModel; 
     private String consumesTypes;
     private String producesTypes;
+    private List<String> nameBindings = Collections.emptyList();
+    private ClassResourceInfo parent;
     
     public ClassResourceInfo(ClassResourceInfo cri) {
         super(cri.getBus());       
@@ -69,6 +71,8 @@ public class ClassResourceInfo extends A
             this.paramFields = cri.paramFields;
             this.paramMethods = cri.paramMethods;
             this.enableStatic = true;
+            this.nameBindings = cri.nameBindings;
+            this.parent = cri.parent;
         } else {
             throw new IllegalArgumentException();
         }
@@ -82,6 +86,7 @@ public class ClassResourceInfo extends A
         if (root && resourceClass != null) {
             setParamField(serviceClass);
             setParamMethods(serviceClass);
+            nameBindings = AnnotationUtils.getNameBindings(serviceClass.getAnnotations());
         }
     }
     
@@ -102,7 +107,7 @@ public class ClassResourceInfo extends A
     
     // The following constructors are used by tests only
     public ClassResourceInfo(Class<?> theResourceClass) {
-        this(theResourceClass, false);
+        this(theResourceClass, true);
     }
     
     public ClassResourceInfo(Class<?> theResourceClass, boolean theRoot) {
@@ -136,16 +141,40 @@ public class ClassResourceInfo extends A
                 ClassResourceInfo tmpCri = subResources.putIfAbsent(key, cri);
                 if (tmpCri != null) {
                     cri = tmpCri;
+                    if (cri != this) {
+                        cri.setParent(this);
+                    }
                 }
             }
         }
         return cri;
     }
     
+    public void addSubClassResourceInfo(ClassResourceInfo cri) {
+        subResources.putIfAbsent(new SubresourceKey(cri.getResourceClass(), 
+                                            cri.getServiceClass()),
+                                 cri);
+        if (cri != this) {
+            cri.setParent(this);
+        }
+    }
+    
     public Collection<ClassResourceInfo> getSubResources() {
         return Collections.unmodifiableCollection(subResources.values());
     }
     
+    public List<String> getNameBindings() {
+        if (root || parent == null) {
+            return nameBindings;
+        } else {
+            return parent.nameBindings;
+        }
+    }
+    
+    public void setNameBindings(List<String> names) {
+        nameBindings = names;
+    }
+    
     public Set<String> getAllowedMethods() {
         Set<String> methods = new HashSet<String>();
         for (OperationResourceInfo o : methodDispatcher.getOperationResourceInfos()) {
@@ -214,11 +243,6 @@ public class ClassResourceInfo extends A
         return !subResources.isEmpty();
     }
     
-    public void addSubClassResourceInfo(ClassResourceInfo cri) {
-        subResources.putIfAbsent(new SubresourceKey(cri.getResourceClass(), 
-                                            cri.getServiceClass()),
-                                 cri);
-    }
     
     public boolean isCreatedFromModel() {
         return createdFromModel;
@@ -233,19 +257,27 @@ public class ClassResourceInfo extends A
     }
     
     public List<MediaType> getProduceMime() {
-        if (producesTypes != null) {
-            return JAXRSUtils.parseMediaTypes(producesTypes);
+        if (root || parent == null) {
+            if (producesTypes != null) {
+                return JAXRSUtils.parseMediaTypes(producesTypes);
+            }
+            return JAXRSUtils.getProduceTypes(
+                 AnnotationUtils.getClassAnnotation(getServiceClass(), Produces.class));
+        } else {
+            return parent.getProduceMime();
         }
-        return JAXRSUtils.getProduceTypes(
-             AnnotationUtils.getClassAnnotation(getServiceClass(), Produces.class));
     }
     
     public List<MediaType> getConsumeMime() {
-        if (consumesTypes != null) {
-            return JAXRSUtils.parseMediaTypes(consumesTypes);
+        if (root || parent == null) {
+            if (consumesTypes != null) {
+                return JAXRSUtils.parseMediaTypes(consumesTypes);
+            }
+            return JAXRSUtils.getConsumeTypes(
+                 AnnotationUtils.getClassAnnotation(getServiceClass(), Consumes.class));
+        } else {
+            return parent.getConsumeMime();
         }
-        return JAXRSUtils.getConsumeTypes(
-             AnnotationUtils.getClassAnnotation(getServiceClass(), Consumes.class));
     }
     
     public Path getPath() {
@@ -281,4 +313,8 @@ public class ClassResourceInfo extends A
     public boolean isSingleton() {
         return resourceProvider != null && resourceProvider.isSingleton();
     }
+
+    void setParent(ClassResourceInfo parent) {
+        this.parent = parent;
+    }
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfo.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfo.java?rev=1389470&r1=1389469&r2=1389470&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfo.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfo.java
Mon Sep 24 16:15:58 2012
@@ -20,6 +20,7 @@
 package org.apache.cxf.jaxrs.model;
 
 import java.lang.reflect.Method;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
@@ -102,7 +103,15 @@ public class OperationResourceInfo {
     }
     
     public List<String> getNameBindings() {
-        return nameBindings;
+        List<String> criNames = classResourceInfo.getNameBindings();
+        if (criNames.isEmpty()) {
+            return nameBindings;
+        } else {
+            List<String> all = new ArrayList<String>(criNames.size() + nameBindings.size());
+            all.addAll(criNames);
+            all.addAll(nameBindings);
+            return all;
+        }
     }
     
     private void checkOneway() {

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/ClassResourceInfoTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/ClassResourceInfoTest.java?rev=1389470&r1=1389469&r2=1389470&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/ClassResourceInfoTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/ClassResourceInfoTest.java
Mon Sep 24 16:15:58 2012
@@ -19,7 +19,12 @@
 
 package org.apache.cxf.jaxrs.model;
 
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
 import java.lang.reflect.Field;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -31,12 +36,15 @@ import javax.servlet.http.HttpServletRes
 import javax.ws.rs.Consumes;
 import javax.ws.rs.GET;
 import javax.ws.rs.HEAD;
+import javax.ws.rs.NameBinding;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
+import javax.ws.rs.core.Application;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.UriInfo;
 
+import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
 import org.apache.cxf.jaxrs.utils.ResourceUtils;
 
 import org.junit.After;
@@ -48,7 +56,7 @@ public class ClassResourceInfoTest exten
     @Path("/bar")
     @Produces("test/bar")
     @Consumes("test/foo")
-    static class TestClass {
+    public static class TestClass {
         @Context UriInfo u;
         @Context HttpHeaders h;
         @Resource HttpServletRequest req;
@@ -74,6 +82,16 @@ public class ClassResourceInfoTest exten
         public void getIt() { 
             
         }
+      
+        @Path("/same")
+        public TestClass2 getThis() { 
+            return this;
+        }
+        
+        @Path("sub")
+        public TestClass3 getTestClass3() { 
+            return new TestClass3();
+        }
     }
     
     static class TestClass3 {
@@ -99,7 +117,7 @@ public class ClassResourceInfoTest exten
     
     @Test
     public void testGetHttpContexts() {
-        ClassResourceInfo c = new ClassResourceInfo(TestClass.class);
+        ClassResourceInfo c = new ClassResourceInfo(TestClass.class, false);
         List<Field> fields = c.getContextFields();
         assertEquals("Only root classes should check these fields", 0, fields.size());
         
@@ -191,4 +209,43 @@ public class ClassResourceInfoTest exten
         assertEquals(2, methods.size());
         assertTrue(methods.contains("HEAD") && methods.contains("GET"));
     }
+    
+    @Test
+    public void testSubresourceInheritProduces() {
+        ClassResourceInfo c = ResourceUtils.createClassResourceInfo(
+                                  TestClass2.class, TestClass2.class, true, true);
+        assertEquals("test/bar", c.getProduceMime().get(0).toString());
+        ClassResourceInfo sub = c.getSubResource(TestClass2.class, TestClass3.class);
+        assertNotNull(sub);
+        assertEquals("test/bar", sub.getProduceMime().get(0).toString());
+        sub = c.getSubResource(TestClass2.class, TestClass2.class);
+        assertNotNull(sub);
+        assertEquals("test/bar", sub.getProduceMime().get(0).toString());
+    }
+    
+    @Test
+    public void testNameBindings() {
+        Application app = new TestApplication();
+        JAXRSServerFactoryBean bean = ResourceUtils.createApplication(app, true, true);
+        ClassResourceInfo cri = bean.getServiceFactory().getClassResourceInfo().get(0);
+        List<String> names = cri.getNameBindings();
+        assertEquals(Collections.singletonList(CustomNameBinding.class.getName()), names);
+    }
+    
+    @Target({ ElementType.TYPE, ElementType.METHOD })
+    @Retention(value = RetentionPolicy.RUNTIME)
+    @NameBinding
+    public @interface CustomNameBinding { 
+        
+    }
+    
+    @CustomNameBinding
+    public class TestApplication extends Application {
+        @Override
+        public Set<Class<?>> getClasses() {
+            Set<Class<?>> classes = new HashSet<Class<?>>();
+            classes.add(TestClass.class);
+            return classes;
+        }
+    }
 }



Mime
View raw message