cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1393029 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
Date Tue, 02 Oct 2012 16:45:11 GMT
Author: sergeyb
Date: Tue Oct  2 16:45:10 2012
New Revision: 1393029

URL: http://svn.apache.org/viewvc?rev=1393029&view=rev
Log:
[CXF-4455] ParamConverter support, for ParamHandler be deprecated

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/InjectionUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.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=1393029&r1=1393028&r2=1393029&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
Tue Oct  2 16:45:10 2012
@@ -55,6 +55,8 @@ import javax.ws.rs.ext.ContextResolver;
 import javax.ws.rs.ext.ExceptionMapper;
 import javax.ws.rs.ext.MessageBodyReader;
 import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.ParamConverter;
+import javax.ws.rs.ext.ParamConverterProvider;
 import javax.ws.rs.ext.ReaderInterceptor;
 import javax.ws.rs.ext.WriterInterceptor;
 
@@ -121,10 +123,8 @@ public final class ProviderFactory {
     private List<ProviderInfo<ContextProvider<?>>> contextProviders = 
         new ArrayList<ProviderInfo<ContextProvider<?>>>(1);
     
-    // ParamConverter and ParamConverterProvider is introduced in JAX-RS 2.0
-    // ParameterHandler will have to be deprecated
-    private List<ProviderInfo<ParameterHandler<?>>> paramHandlers = 
-        new ArrayList<ProviderInfo<ParameterHandler<?>>>(1);
+    private ParamConverterProvider newParamConverter;
+    private LegacyParamConverterProvider legacyParamConverter; 
     
     private List<ProviderInfo<MessageBodyReader<?>>> jaxbReaders = 
         new ArrayList<ProviderInfo<MessageBodyReader<?>>>();
@@ -158,7 +158,13 @@ public final class ProviderFactory {
     private ProviderInfo<Application> application;
     private List<DynamicFeature> dynamicFeatures = new LinkedList<DynamicFeature>();
     
-    // Client-only providers, consider introducing ClientProviderFactory
+    // List of injected providers
+    private Collection<ProviderInfo<?>> injectedProviders = 
+        new LinkedList<ProviderInfo<?>>();
+    
+    
+    //TODO: Client-only providers, consider introducing ClientProviderFactory,
+    //      will make it easier to split the client API into a separate module
     private List<ProviderInfo<ClientRequestFilter>> clientRequestFilters = 
         new ArrayList<ProviderInfo<ClientRequestFilter>>(1);
     private List<ProviderInfo<ClientResponseFilter>> clientResponseFilters =

@@ -166,10 +172,6 @@ public final class ProviderFactory {
     private List<ProviderInfo<ResponseExceptionMapper<?>>> responseExceptionMappers
= 
         new ArrayList<ProviderInfo<ResponseExceptionMapper<?>>>(1);
    
-    // List of injected providers
-    private Collection<ProviderInfo<?>> injectedProviders = 
-        new LinkedList<ProviderInfo<?>>();
-    
     private Bus bus;
     
     
@@ -370,19 +372,16 @@ public final class ProviderFactory {
         return (ExceptionMapper<T>) candidates.get(0);
     }
     
-    @SuppressWarnings("unchecked")
-    public <T> ParameterHandler<T> createParameterHandler(Class<?> paramType)
{
+    public <T> ParamConverter<T> createParameterHandler(Class<T> paramType)
{
         
-        List<ParameterHandler<?>> candidates = new LinkedList<ParameterHandler<?>>();
-        
-        for (ProviderInfo<ParameterHandler<?>> em : paramHandlers) {
-            handleMapper(candidates, em, paramType, null, ParameterHandler.class, true);
-        }
-        if (candidates.size() == 0) {
+        if (newParamConverter != null) {
+            return newParamConverter.getConverter(paramType, null, null);
+        } else if (legacyParamConverter != null) {
+            return legacyParamConverter.getConverter(paramType, null, null);
+        } else {
             return null;
         }
-        Collections.sort(candidates, new ClassComparator());
-        return (ParameterHandler<T>) candidates.get(0);
+        
     }
     
     @SuppressWarnings("unchecked")
@@ -751,9 +750,18 @@ public final class ProviderFactory {
                 responseExceptionMappers.add(new ProviderInfo<ResponseExceptionMapper<?>>((ResponseExceptionMapper<?>)o,
bus)); 
             }
             
+            if (ParamConverterProvider.class.isAssignableFrom(oClass)) {
+                newParamConverter = (ParamConverterProvider)o;
+            }
+            
             if (ParameterHandler.class.isAssignableFrom(oClass)) {
-                paramHandlers.add(new ProviderInfo<ParameterHandler<?>>((ParameterHandler<?>)o,
bus)); 
+                if (legacyParamConverter == null) {
+                    legacyParamConverter = new LegacyParamConverterProvider();
+                }
+                legacyParamConverter.add(o, bus);
             }
+            
+            
         }
         sortReaders();
         sortWriters();
@@ -1104,7 +1112,9 @@ public final class ProviderFactory {
         postMatchContainerRequestFilters.clear();
         postMatchContainerResponseFilters.clear();
         preMatchContainerRequestFilters.clear();
-        paramHandlers.clear();
+        if (legacyParamConverter != null) {
+            legacyParamConverter.clear();
+        }
         responseExceptionMappers.clear();
         clientRequestFilters.clear();
         clientResponseFilters.clear();
@@ -1558,4 +1568,60 @@ public final class ProviderFactory {
             }
         }
     }
+    
+    private static class LegacyParamConverterProvider implements ParamConverterProvider {
+
+        // ParamConverter and ParamConverterProvider is introduced in JAX-RS 2.0
+        // ParameterHandler will have to be deprecated
+        private List<ProviderInfo<ParameterHandler<?>>> paramHandlers =

+            new ArrayList<ProviderInfo<ParameterHandler<?>>>(1);
+        
+        @SuppressWarnings({
+            "unchecked", "rawtypes"
+        })
+        @Override
+        public <T> ParamConverter<T> getConverter(Class<T> rawType, Type
genericType, Annotation[] annotations) {
+            List<ParameterHandler<?>> candidates = new LinkedList<ParameterHandler<?>>();
+            
+            for (ProviderInfo<ParameterHandler<?>> em : paramHandlers) {
+                handleMapper(candidates, em, rawType, null, ParameterHandler.class, true);
+            }
+            if (candidates.size() == 0) {
+                return null;
+            }
+            Collections.sort(candidates, new ClassComparator());
+            return new LegacyParamConverter((ParameterHandler<T>) candidates.get(0));
+        }
+        
+        public void clear() {
+            paramHandlers.clear();
+        }
+        
+        public void add(Object o, Bus bus) {
+            paramHandlers.add(new ProviderInfo<ParameterHandler<?>>((ParameterHandler<?>)o,
bus));
+        }
+    }
+    
+    static class LegacyParamConverter<T> implements ParamConverter<T> {
+
+        private ParameterHandler<T> handler;
+        public LegacyParamConverter(ParameterHandler<T> handler) {
+            this.handler = handler;
+        }
+        
+        @Override
+        public T fromString(String value) throws IllegalArgumentException {
+            return handler.fromString(value);
+        }
+
+        @Override
+        public String toString(Object value) throws IllegalArgumentException {
+            // TODO Auto-generated method stub
+            return null;
+        }
+        
+        ParameterHandler<T> getHandler() {
+            return handler;
+        }
+    }
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java?rev=1393029&r1=1393028&r2=1393029&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
Tue Oct  2 16:45:10 2012
@@ -61,6 +61,7 @@ import javax.ws.rs.core.Response;
 import javax.ws.rs.core.SecurityContext;
 import javax.ws.rs.core.UriInfo;
 import javax.ws.rs.ext.ContextResolver;
+import javax.ws.rs.ext.ParamConverter;
 import javax.ws.rs.ext.Providers;
 
 import org.apache.cxf.common.classloader.ClassLoaderUtils;
@@ -69,7 +70,6 @@ import org.apache.cxf.common.logging.Log
 import org.apache.cxf.common.util.PrimitiveUtils;
 import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.jaxrs.ext.MessageContext;
-import org.apache.cxf.jaxrs.ext.ParameterHandler;
 import org.apache.cxf.jaxrs.ext.ProtocolHeaders;
 import org.apache.cxf.jaxrs.impl.MetadataMap;
 import org.apache.cxf.jaxrs.impl.PathSegmentImpl;
@@ -401,7 +401,7 @@ public final class InjectionUtils {
                                                     Message message) {
         T result = null;
         if (message != null) {
-            ParameterHandler<T> pm = ProviderFactory.getInstance(message)
+            ParamConverter<T> pm = ProviderFactory.getInstance(message)
                 .createParameterHandler(pClass);
             if (pm != null) {
                 result = pm.fromString(value);

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java?rev=1393029&r1=1393028&r2=1393029&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
Tue Oct  2 16:45:10 2012
@@ -51,6 +51,7 @@ import javax.ws.rs.ext.ContextResolver;
 import javax.ws.rs.ext.ExceptionMapper;
 import javax.ws.rs.ext.MessageBodyReader;
 import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.ParamConverter;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.validation.Schema;
@@ -70,6 +71,7 @@ import org.apache.cxf.jaxrs.model.Abstra
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
 import org.apache.cxf.jaxrs.model.ProviderInfo;
 import org.apache.cxf.jaxrs.model.wadl.WadlGenerator;
+import org.apache.cxf.jaxrs.provider.ProviderFactory.LegacyParamConverter;
 import org.apache.cxf.jaxrs.resources.Book;
 import org.apache.cxf.jaxrs.resources.SuperBook;
 import org.apache.cxf.jaxrs.utils.JAXRSUtils;
@@ -482,8 +484,9 @@ public class ProviderFactoryTest extends
         ProviderFactory pf = ProviderFactory.getInstance();
         ParameterHandler<Customer> h = new CustomerParameterHandler();
         pf.registerUserProvider(h);
-        ParameterHandler<Customer> h2 = pf.createParameterHandler(Customer.class);
-        assertSame(h2, h);
+        ParamConverter<Customer> h2 = pf.createParameterHandler(Customer.class);
+        
+        assertSame(((LegacyParamConverter<Customer>)h2).getHandler(), h);
     }
     
     @Test



Mime
View raw message