Repository: cxf Updated Branches: refs/heads/master b9ebd26d1 -> 3261d7ddf
[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/3261d7dd Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/3261d7dd Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/3261d7dd Branch: refs/heads/master Commit: 3261d7ddf666781716e84b10b959c28018d19223 Parents: b9ebd26 Author: Sergey Beryozkin <sberyoz...@gmail.com> Authored: Fri Aug 28 17:53:35 2015 +0100 Committer: Sergey Beryozkin <sberyoz...@gmail.com> Committed: Fri Aug 28 17:53:35 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/3261d7dd/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 f904ecb..bbcfa06 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/3261d7dd/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 7f6b209..f6a5869 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();