cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1374433 - in /cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs: provider/ProviderFactory.java utils/JAXRSUtils.java
Date Fri, 17 Aug 2012 20:52:53 GMT
Author: sergeyb
Date: Fri Aug 17 20:52:53 2012
New Revision: 1374433

URL: http://svn.apache.org/viewvc?rev=1374433&view=rev
Log:
[CXF-4455] Prototyping some code for supporting the mapping of new WebApplicationException
extensions

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java?rev=1374433&r1=1374432&r2=1374433&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
Fri Aug 17 20:52:53 2012
@@ -34,6 +34,7 @@ import java.util.Set;
 import java.util.logging.Logger;
 
 import javax.ws.rs.Produces;
+import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Application;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
@@ -82,6 +83,7 @@ public final class ProviderFactory {
                                     new MultipartProvider(),
                                     new WebApplicationExceptionMapper(),
                                     new WadlGenerator());
+        
     }
     
     private List<ProviderInfo<MessageBodyReader<?>>> messageReaders = 
@@ -265,23 +267,38 @@ public final class ProviderFactory {
         return null;
     }
     
+    public <T extends Throwable> ExceptionMapper<T> createExceptionMapper(Class<?>
exceptionType,
+                                                                          Message m) {
+        return createExceptionMapper(null, exceptionType, m);
+    }
+    
+    public <T extends Throwable> ExceptionMapper<T> createExceptionMapper(T ex,

+                                                                          Message m) {
+        return createExceptionMapper(ex, ex.getClass(), m);
+    }
     
-    public <T extends Throwable> ExceptionMapper<T> createExceptionMapper(Class<?>
exceptionType, Message m) {
+    private <T extends Throwable> ExceptionMapper<T> createExceptionMapper(T
ex, 
+                                                                           Class<?>
exceptionType,
+                                                                           Message m) {
         
-        ExceptionMapper<T> mapper = doCreateExceptionMapper(exceptionType, m);
+        ExceptionMapper<T> mapper = doCreateExceptionMapper(ex, exceptionType, m);
         if (mapper != null || this == SHARED_FACTORY) {
             return mapper;
         }
         
-        return SHARED_FACTORY.createExceptionMapper(exceptionType, m);
+        return SHARED_FACTORY.createExceptionMapper(ex, exceptionType, m);
     }
     
     @SuppressWarnings("unchecked")
     private <T extends Throwable> ExceptionMapper<T> doCreateExceptionMapper(
-        Class<?> exceptionType, Message m) {
+        T exception, Class<?> exceptionType, Message m) {
         
         List<ExceptionMapper<?>> candidates = new LinkedList<ExceptionMapper<?>>();
-        
+        if (WebApplicationException.class == exceptionType 
+            && exception instanceof WebApplicationException) {
+            exceptionType = 
+                JAXRSUtils.getWebApplicationExceptionClass((WebApplicationException)exception,
exceptionType);
+        }
         for (ProviderInfo<ExceptionMapper<?>> em : exceptionMappers) {
             handleMapper(candidates, em, exceptionType, m, ExceptionMapper.class, true);
         }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java?rev=1374433&r1=1374432&r2=1374433&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java Fri
Aug 17 20:52:53 2012
@@ -22,6 +22,7 @@ package org.apache.cxf.jaxrs.utils;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.annotation.Annotation;
+import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.lang.reflect.ParameterizedType;
@@ -31,6 +32,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
@@ -86,6 +88,7 @@ import org.apache.cxf.jaxrs.impl.Provide
 import org.apache.cxf.jaxrs.impl.RequestImpl;
 import org.apache.cxf.jaxrs.impl.SecurityContextImpl;
 import org.apache.cxf.jaxrs.impl.UriInfoImpl;
+import org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper;
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
 import org.apache.cxf.jaxrs.model.ClassResourceInfoComparator;
 import org.apache.cxf.jaxrs.model.OperationResourceInfo;
@@ -115,6 +118,12 @@ public final class JAXRSUtils {
     private static final String PROPAGATE_EXCEPTION = "org.apache.cxf.propagate.exception";
     private static final String REPORT_FAULT_MESSAGE_PROPERTY = "org.apache.cxf.jaxrs.report-fault-message";
     
+    private static final Map<Integer, Class<?>> EXCEPTIONS_MAP;
+    static {
+        //TODO: Populate it with the upgrade to the more up to date API
+        EXCEPTIONS_MAP = new HashMap<Integer, Class<?>>();
+    }
+    
     private JAXRSUtils() {        
     }
     
@@ -1214,11 +1223,39 @@ public final class JAXRSUtils {
         return types;
     }
     
+    public static Class<?> getWebApplicationExceptionClass(WebApplicationException
ex,
+                                                           Class<?> exceptionType)
{
+        int status = ex.getResponse().getStatus();
+        Class<?> cls = EXCEPTIONS_MAP.get(status);
+        //TODO: enable this code after the upgrade to the more up-to-date API
+        //if (cls == null && status / 100 == 5) {
+        //    cls = ServerErrorException.class;
+        //}
+        return cls == null ? exceptionType : cls;
+    }
+    
+    @SuppressWarnings("unchecked")
     public static <T extends Throwable> Response convertFaultToResponse(T ex, Message
inMessage) {
         
         ExceptionMapper<T> mapper = 
-            ProviderFactory.getInstance(inMessage).createExceptionMapper(ex.getClass(), inMessage);
+            ProviderFactory.getInstance(inMessage).createExceptionMapper(ex, inMessage);
         if (mapper != null) {
+            if (ex.getClass() == WebApplicationException.class 
+                && mapper.getClass() != WebApplicationExceptionMapper.class) {
+                WebApplicationException webEx = (WebApplicationException)ex;
+                Class<?> exceptionClass = getWebApplicationExceptionClass(webEx, 
+                                                                          WebApplicationException.class);
+                if (exceptionClass != WebApplicationException.class) {
+                    //TODO: consider using switch statements
+                    try {
+                        Constructor<?> ctr = exceptionClass.getConstructor(Response.class);
+                        ex = (T)ctr.newInstance(webEx.getResponse());
+                    } catch (Exception ex2) {
+                        ex2.printStackTrace();
+                        return Response.serverError().build();
+                    }
+                }
+            }
             try {
                 return mapper.toResponse(ex);
             } catch (Exception mapperEx) {



Mime
View raw message