cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-6568] Optionally making default WAE least specific
Date Fri, 28 Aug 2015 16:55:36 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.0.x-fixes 7ae54f4cc -> 088ede7b4


[CXF-6568] Optionally making default WAE least specific


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/088ede7b
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/088ede7b
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/088ede7b

Branch: refs/heads/3.0.x-fixes
Commit: 088ede7b409faceb7bfd9e007c2b13839a22345f
Parents: 7ae54f4
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Fri Aug 28 17:53:35 2015 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Fri Aug 28 17:54:59 2015 +0100

----------------------------------------------------------------------
 .../jaxrs/provider/ServerProviderFactory.java   | 26 +++++++++++++-
 .../cxf/jaxrs/provider/ProviderFactoryTest.java | 36 ++++++++++++++++++++
 2 files changed, 61 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/088ede7b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ServerProviderFactory.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ServerProviderFactory.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ServerProviderFactory.java
index 685b608..b8a7789 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ServerProviderFactory.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ServerProviderFactory.java
@@ -63,6 +63,7 @@ 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.message.Message;
+import org.apache.cxf.message.MessageUtils;
 
 public final class ServerProviderFactory extends ProviderFactory {
     private static final Set<Class<?>> SERVER_FILTER_INTERCEPTOR_CLASSES = 
@@ -72,6 +73,7 @@ public final class ServerProviderFactory extends ProviderFactory {
                                                       WriterInterceptor.class));
     
     private static final String WADL_PROVIDER_NAME = "org.apache.cxf.jaxrs.model.wadl.WadlGenerator";
+    private static final String MAKE_DEFAULT_WAE_LEAST_SPECIFIC = "make.default.wae.least.specific";
     private List<ProviderInfo<ExceptionMapper<?>>> exceptionMappers = 
         new ArrayList<ProviderInfo<ExceptionMapper<?>>>(1);
     
@@ -176,7 +178,10 @@ public final class ServerProviderFactory extends ProviderFactory {
         if (candidates.size() == 0) {
             return null;
         }
-        Collections.sort(candidates, new ProviderInfoClassComparator(exceptionType));
+        boolean makeDefaultWaeLeastSpecific = 
+            MessageUtils.getContextualBoolean(m, MAKE_DEFAULT_WAE_LEAST_SPECIFIC, false);
+        Collections.sort(candidates, new ExceptionProviderInfoComparator(exceptionType,
+                                                                         makeDefaultWaeLeastSpecific));
         return (ExceptionMapper<T>) candidates.get(0).getProvider();
     }
     
@@ -605,5 +610,24 @@ public final class ServerProviderFactory extends ProviderFactory {
             return Priorities.USER;
         }
     }
+    public static class ExceptionProviderInfoComparator extends ProviderInfoClassComparator
{
+        private boolean makeDefaultWaeLeastSpecific;
+        public ExceptionProviderInfoComparator(Class<?> expectedCls, boolean makeDefaultWaeLeastSpecific)
{
+            super(expectedCls);
+            this.makeDefaultWaeLeastSpecific = makeDefaultWaeLeastSpecific;
+        }
+        public int compare(ProviderInfo<?> p1, ProviderInfo<?> p2) {
+            if (makeDefaultWaeLeastSpecific) {
+                if (p1.getProvider() instanceof WebApplicationExceptionMapper
+                    && !p1.isCustom()) {
+                    return 1;
+                } else if (p2.getProvider() instanceof WebApplicationExceptionMapper
+                    && !p2.isCustom()) {
+                    return -1;
+                } 
+            }
+            return super.compare(p1, p2);
+        }
+    }
     
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/088ede7b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
index 35d5891..b9160eb 100644
--- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
@@ -373,6 +373,42 @@ public class ProviderFactoryTest extends Assert {
     }
     
     @Test
+    public void testExceptionMappersHierarchy3() throws Exception {
+        Message m = new MessageImpl();
+        m.put("make.default.wae.least.specific", true);
+        ServerProviderFactory pf = ServerProviderFactory.getInstance();
+        
+        TestRuntimeExceptionMapper rm = new TestRuntimeExceptionMapper(); 
+        pf.registerUserProvider(rm);
+        ExceptionMapper<WebApplicationException> em = 
+            pf.createExceptionMapper(WebApplicationException.class, m);
+        assertSame(rm, em);
+        assertSame(rm, pf.createExceptionMapper(RuntimeException.class, m));
+        
+        WebApplicationExceptionMapper wm = new WebApplicationExceptionMapper(); 
+        pf.registerUserProvider(wm);
+        assertSame(wm, pf.createExceptionMapper(WebApplicationException.class, m));
+        assertSame(rm, pf.createExceptionMapper(RuntimeException.class, m));
+    }
+    @Test
+    public void testExceptionMappersHierarchy4() throws Exception {
+        Message m = new MessageImpl();
+        m.put("make.default.wae.least.specific", true);
+        ServerProviderFactory pf = ServerProviderFactory.getInstance();
+        ExceptionMapper<WebApplicationException> em = 
+            pf.createExceptionMapper(WebApplicationException.class, m);
+        assertTrue(em instanceof WebApplicationExceptionMapper);
+    }
+    @Test
+    public void testExceptionMappersHierarchy5() throws Exception {
+        Message m = new MessageImpl();
+        ServerProviderFactory pf = ServerProviderFactory.getInstance();
+        ExceptionMapper<WebApplicationException> em = 
+            pf.createExceptionMapper(WebApplicationException.class, m);
+        assertTrue(em instanceof WebApplicationExceptionMapper);
+    }
+    
+    @Test
     public void testExceptionMappersHierarchyWithGenerics() throws Exception {
         ServerProviderFactory pf = ServerProviderFactory.getInstance();
         RuntimeExceptionMapper1 exMapper1 = new RuntimeExceptionMapper1(); 


Mime
View raw message