cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1475978 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/provider/ main/java/org/apache/cxf/jaxrs/utils/ main/java/org/apache/cxf/jaxrs/utils/multipart/ test/java/org/apache/cxf/jaxrs/provider/
Date Thu, 25 Apr 2013 21:44:00 GMT
Author: sergeyb
Date: Thu Apr 25 21:44:00 2013
New Revision: 1475978

URL: http://svn.apache.org/r1475978
Log:
[CXF-4988] Making sure MultipartProvider does not interfere when the multipart payload is
expected to be read directly, updating the way the providers are sorted - trunk only

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataSourceProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java
    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
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/multipart/AttachmentUtils.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/DataSourceProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataSourceProvider.java?rev=1475978&r1=1475977&r2=1475978&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataSourceProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/DataSourceProvider.java
Thu Apr 25 21:44:00 2013
@@ -65,8 +65,7 @@ public class DataSourceProvider<T> imple
     }
 
     private boolean isSupported(Class<?> type, MediaType mt) {
-        return  !mt.getType().equals("multipart")
-            && (DataSource.class.isAssignableFrom(type) || DataHandler.class.isAssignableFrom(type));
+        return  DataSource.class.isAssignableFrom(type) || DataHandler.class.isAssignableFrom(type);
     }
     
     public void writeTo(T src, Class<?> cls, Type genericType, Annotation[] annotations,


Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java?rev=1475978&r1=1475977&r2=1475978&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java
Thu Apr 25 21:44:00 2013
@@ -29,11 +29,13 @@ import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.ResourceBundle;
+import java.util.Set;
 import java.util.logging.Logger;
 
 import javax.activation.DataHandler;
@@ -45,7 +47,6 @@ import javax.ws.rs.InternalServerErrorEx
 import javax.ws.rs.Produces;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Context;
-import javax.ws.rs.core.Form;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.MessageBodyReader;
@@ -78,9 +79,23 @@ public class MultipartProvider extends A
     implements MessageBodyReader<Object>, MessageBodyWriter<Object> {
     
     private static final String ACTIVE_JAXRS_PROVIDER_KEY = "active.jaxrs.provider";
+    private static final String SUPPORT_TYPE_AS_MULTIPART = "support.type.as.multipart";
     private static final Logger LOG = LogUtils.getL7dLogger(MultipartProvider.class);
     private static final ResourceBundle BUNDLE = BundleUtils.getBundle(MultipartProvider.class);
-
+    private static final Set<Class<?>> WELL_KNOWN_MULTIPART_CLASSES;
+    private static final Set<String> MULTIPART_SUBTYPES;
+    static {
+        WELL_KNOWN_MULTIPART_CLASSES = new HashSet<Class<?>>();
+        WELL_KNOWN_MULTIPART_CLASSES.add(MultipartBody.class);
+        WELL_KNOWN_MULTIPART_CLASSES.add(Attachment.class);
+        
+        MULTIPART_SUBTYPES = new HashSet<String>();
+        MULTIPART_SUBTYPES.add("form-data");
+        MULTIPART_SUBTYPES.add("mixed");
+        MULTIPART_SUBTYPES.add("related");
+        MULTIPART_SUBTYPES.add("alternative");
+    }
+    
     @Context
     private MessageContext mc;
     private String attachmentDir;
@@ -105,33 +120,23 @@ public class MultipartProvider extends A
 
     public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations,

                               MediaType mt) {
-        return isSupported(type, genericType, annotations, mt);
+        return isSupported(type, genericType, annotations, mt); 
+            
     }
     
     private boolean isSupported(Class<?> type, Type genericType, Annotation[] anns,

                                 MediaType mt) {
-        if (DataHandler.class.isAssignableFrom(type) || DataSource.class.isAssignableFrom(type)
-            || Attachment.class.isAssignableFrom(type) || MultipartBody.class.isAssignableFrom(type)
-            || mediaTypeSupported(mt)
-            || isSupportedFormDataType(type, mt)) {
-            
-            if (type == InputStream.class 
-                && AnnotationUtils.getAnnotation(anns, Multipart.class) == null
-                && MessageUtils.isTrue(mc.getContextualProperty(JAXRSUtils.DEFAULT_PROVIDERS_FOR_SIMPLE_TYPES)))
{
-                return false;
-            }
-            
-            
+        if (mediaTypeSupported(mt) 
+            && (WELL_KNOWN_MULTIPART_CLASSES.contains(type)
+                || Collection.class.isAssignableFrom(type)
+                || Map.class.isAssignableFrom(type) && type != MultivaluedMap.class
+                || AnnotationUtils.getAnnotation(anns, Multipart.class) != null
+                || MessageUtils.isTrue(mc.getContextualProperty(SUPPORT_TYPE_AS_MULTIPART))))
{
             return true;
         }
         return false;
     }
 
-    private boolean isSupportedFormDataType(Class<?> type, MediaType mt) {
-        return mt.getType().equals("multipart") && mt.getSubtype().equals("form-data")

-            && !MultivaluedMap.class.isAssignableFrom(type) && !Form.class.isAssignableFrom(type);
-    }
-    
     protected void checkContentLength() {
         if (mc != null && isPayloadEmpty(mc.getHttpHeaders())) {
             String message = new org.apache.cxf.common.i18n.Message("EMPTY_BODY", BUNDLE).toString();
@@ -250,8 +255,7 @@ public class MultipartProvider extends A
     }
     
     private boolean mediaTypeSupported(MediaType mt) {
-        return mt.getType().equals("multipart") && (mt.getSubtype().equals("related")

-            || mt.getSubtype().equals("mixed") || mt.getSubtype().equals("alternative"));
+        return mt.getType().equals("multipart") && MULTIPART_SUBTYPES.contains(mt.getSubtype());
     }
 
     public long getSize(Object t, Class<?> type, Type genericType, Annotation[] annotations,


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=1475978&r1=1475977&r2=1475978&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
Thu Apr 25 21:44:00 2013
@@ -574,13 +574,15 @@ public abstract class ProviderFactory {
                     return (MessageBodyReader<T>) ep.getProvider();
                 }
                 handleMapper(candidates, ep, type, m, MessageBodyReader.class, false);
+                if (!candidates.isEmpty()) {
+                    break;
+                }
             }
         }     
         
-        if (candidates.size() == 0) {
+        if (candidates.isEmpty()) {
             return null;
         }
-        Collections.sort(candidates, new ClassComparator());
         return (MessageBodyReader<T>) candidates.get(0);
         
     }
@@ -636,12 +638,14 @@ public abstract class ProviderFactory {
                     return (MessageBodyWriter<T>) ep.getProvider();
                 }
                 handleMapper(candidates, ep, type, m, MessageBodyWriter.class, false);
+                if (!candidates.isEmpty()) {
+                    break;
+                }
             }
         }     
-        if (candidates.size() == 0) {
+        if (candidates.isEmpty()) {
             return null;
         }
-        Collections.sort(candidates, new ClassComparator());
         return (MessageBodyWriter<T>) candidates.get(0);
     }
     
@@ -704,6 +708,12 @@ public abstract class ProviderFactory {
                            ProviderInfo<MessageBodyReader<?>> p2) {
             MessageBodyReader<?> e1 = p1.getProvider();
             MessageBodyReader<?> e2 = p2.getProvider();
+            
+            int result = compareClasses(e1, e2);
+            if (result != 0) {
+                return result;
+            }
+            
             List<MediaType> types1 = JAXRSUtils.getProviderConsumeTypes(e1);
             types1 = JAXRSUtils.sortMediaTypes(types1, null);
             List<MediaType> types2 = JAXRSUtils.getProviderConsumeTypes(e2);
@@ -721,6 +731,10 @@ public abstract class ProviderFactory {
             MessageBodyWriter<?> e1 = p1.getProvider();
             MessageBodyWriter<?> e2 = p2.getProvider();
             
+            int result = compareClasses(e1, e2);
+            if (result != 0) {
+                return result;
+            }
             List<MediaType> types1 =
                 JAXRSUtils.sortMediaTypes(JAXRSUtils.getProviderProduceTypes(e1), JAXRSUtils.MEDIA_TYPE_QS_PARAM);
             List<MediaType> types2 =

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=1475978&r1=1475977&r2=1475978&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 Thu
Apr 25 21:44:00 2013
@@ -161,7 +161,6 @@ public final class JAXRSUtils {
     public static final String PARTIAL_HIERARCHICAL_MEDIA_SUBTYPE_CHECK = 
         "media.subtype.partial.check"; 
     public static final String DOC_LOCATION = "wadl.location";
-    public static final String DEFAULT_PROVIDERS_FOR_SIMPLE_TYPES = "defaultProviders.for.simpleTypes";
     public static final String MEDIA_TYPE_Q_PARAM = "q";
     public static final String MEDIA_TYPE_QS_PARAM = "qs";
     private static final String MEDIA_TYPE_DISTANCE_PARAM = "d";

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/multipart/AttachmentUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/multipart/AttachmentUtils.java?rev=1475978&r1=1475977&r2=1475978&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/multipart/AttachmentUtils.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/multipart/AttachmentUtils.java
Thu Apr 25 21:44:00 2013
@@ -142,6 +142,9 @@ public final class AttachmentUtils {
     }
     
     private static boolean matchAttachmentId(Attachment at, Multipart mid, MediaType multipartType)
{
+        if (mid.value().isEmpty()) {
+            return true;
+        }
         if (at.getContentId().equals(mid.value())) {
             return true;
         }

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=1475978&r1=1475977&r2=1475978&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
Thu Apr 25 21:44:00 2013
@@ -175,17 +175,6 @@ public class ProviderFactoryTest extends
     }
     
     @Test
-    public void testNoDataSourceWriter() {
-        ProviderFactory pf = ServerProviderFactory.getInstance();
-        pf.registerUserProvider(new DataSourceProvider<Object>());
-        MessageBodyWriter<DataSource> writer = pf.createMessageBodyWriter(
-              DataSource.class, null, null, 
-              MediaType.valueOf("multipart/form-data"), new MessageImpl());
-        assertFalse(writer instanceof DataSourceProvider);
-    }
-    
-    
-    @Test
     public void testSchemaLocations() {
         ProviderFactory pf = ServerProviderFactory.getInstance();
         MessageBodyReader<Book> jaxbReader = pf.createMessageBodyReader(Book.class,
null, null, 



Mime
View raw message