cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1097817 - in /cxf/branches/2.3.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/multipart/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/
Date Fri, 29 Apr 2011 13:38:10 GMT
Author: sergeyb
Date: Fri Apr 29 13:38:09 2011
New Revision: 1097817

URL: http://svn.apache.org/viewvc?rev=1097817&view=rev
Log:
Merged revisions 1097816 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1097816 | sergeyb | 2011-04-29 14:34:04 +0100 (Fri, 29 Apr 2011) | 1 line
  
  [CXF-3479] Consolidating the code for handling multipart/form-data in MultipartProvider
........

Modified:
    cxf/branches/2.3.x-fixes/   (props changed)
    cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/multipart/Attachment.java
    cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java
    cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java
    cxf/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java

Propchange: cxf/branches/2.3.x-fixes/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Fri Apr 29 13:38:09 2011
@@ -1 +1 @@
-/cxf/trunk:1094926,1094992,1095349,1095666,1097071,1097175,1097178
+/cxf/trunk:1094926,1094992,1095349,1095666,1097071,1097175,1097178,1097816

Propchange: cxf/branches/2.3.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/multipart/Attachment.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/multipart/Attachment.java?rev=1097817&r1=1097816&r2=1097817&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/multipart/Attachment.java
(original)
+++ cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/multipart/Attachment.java
Fri Apr 29 13:38:09 2011
@@ -79,7 +79,9 @@ public class Attachment {
     
     public Attachment(String id, InputStream is, ContentDisposition cd) {
         handler = new DataHandler(new InputStreamDataSource(is, "application/octet-stream"));
-        headers.putSingle("Content-Disposition", cd.toString());
+        if (cd != null) {
+            headers.putSingle("Content-Disposition", cd.toString());
+        }
         headers.putSingle("Content-ID", id);
         headers.putSingle("Content-Type", "application/octet-stream");
     }

Modified: cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java?rev=1097817&r1=1097816&r2=1097817&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java
(original)
+++ cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java
Fri Apr 29 13:38:09 2011
@@ -19,8 +19,6 @@
 
 package org.apache.cxf.jaxrs.provider;
 
-import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -41,11 +39,9 @@ import javax.ws.rs.ext.MessageBodyReader
 import javax.ws.rs.ext.MessageBodyWriter;
 import javax.ws.rs.ext.Provider;
 
-import org.apache.cxf.attachment.AttachmentUtil;
 import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.ext.form.Form;
 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
-import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
 import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
 import org.apache.cxf.jaxrs.impl.MetadataMap;
 import org.apache.cxf.jaxrs.utils.AnnotationUtils;
@@ -166,9 +162,7 @@ public class FormEncodingProvider implem
 
     public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations,

                                MediaType mt) {
-        return isSupported(type, genericType, annotations, mt)
-            || mt.getType().equalsIgnoreCase("multipart")
-            && mt.isCompatible(MediaType.MULTIPART_FORM_DATA_TYPE) && File.class
== type;
+        return isSupported(type, genericType, annotations, mt);
     }
 
     private boolean isSupported(Class<?> type, Type genericType, Annotation[] annotations,

@@ -176,7 +170,7 @@ public class FormEncodingProvider implem
         return (MultivaluedMap.class.isAssignableFrom(type) || Form.class.isAssignableFrom(type))

             || (mt.getType().equalsIgnoreCase("multipart") 
             && mt.isCompatible(MediaType.MULTIPART_FORM_DATA_TYPE)
-            && (MultipartBody.class.isAssignableFrom(type) || Attachment.class.isAssignableFrom(type)));
+            && (MultivaluedMap.class.isAssignableFrom(type) || Form.class.isAssignableFrom(type)));
     }
     
     @SuppressWarnings("unchecked")
@@ -184,38 +178,27 @@ public class FormEncodingProvider implem
                         MediaType mt, MultivaluedMap<String, Object> headers, OutputStream
os) 
         throws IOException, WebApplicationException {
         
-        if (mt.isCompatible(MediaType.MULTIPART_FORM_DATA_TYPE)) {
-            Object body = obj;
-            if (obj.getClass() == File.class) {
-                File f = (File)obj;
-                ContentDisposition cd = new ContentDisposition("attachment;filename=" + f.getName());
-                body = new Attachment(AttachmentUtil.BODY_ATTACHMENT_ID, new FileInputStream(f),
cd);
-            }
-            MultipartProvider provider = new MultipartProvider();
-            provider.setMessageContext(mc);
-            provider.writeTo(body, body.getClass(), body.getClass(), anns, mt, headers, os);
-        } else {
-            MultivaluedMap<String, String> map = 
-                (MultivaluedMap<String, String>)(obj instanceof Form ? ((Form)obj).getData()
: obj);
-            boolean encoded = AnnotationUtils.getAnnotation(anns, Encoded.class) != null;
-            
-            String encoding = HttpUtils.getSetEncoding(mt, headers, "UTF-8");
-            
-            for (Iterator<Map.Entry<String, List<String>>> it = map.entrySet().iterator();
it.hasNext();) {
-                Map.Entry<String, List<String>> entry = it.next();
-                for (Iterator<String> entryIterator = entry.getValue().iterator();
entryIterator.hasNext();) {
-                    String value = entryIterator.next();
-                    os.write(entry.getKey().getBytes(encoding));
-                    os.write('=');
-                    String data = encoded ? value : HttpUtils.urlEncode(value);
-                    os.write(data.getBytes(encoding));
-                    if (entryIterator.hasNext() || it.hasNext()) {
-                        os.write('&');
-                    }
+        MultivaluedMap<String, String> map = 
+            (MultivaluedMap<String, String>)(obj instanceof Form ? ((Form)obj).getData()
: obj);
+        boolean encoded = AnnotationUtils.getAnnotation(anns, Encoded.class) != null;
+        
+        String encoding = HttpUtils.getSetEncoding(mt, headers, "UTF-8");
+        
+        for (Iterator<Map.Entry<String, List<String>>> it = map.entrySet().iterator();
it.hasNext();) {
+            Map.Entry<String, List<String>> entry = it.next();
+            for (Iterator<String> entryIterator = entry.getValue().iterator(); entryIterator.hasNext();)
{
+                String value = entryIterator.next();
+                os.write(entry.getKey().getBytes(encoding));
+                os.write('=');
+                String data = encoded ? value : HttpUtils.urlEncode(value);
+                os.write(data.getBytes(encoding));
+                if (entryIterator.hasNext() || it.hasNext()) {
+                    os.write('&');
                 }
-    
             }
+
         }
+        
     }
 
 }

Modified: cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java?rev=1097817&r1=1097816&r2=1097817&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java
(original)
+++ cxf/branches/2.3.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java
Fri Apr 29 13:38:09 2011
@@ -19,6 +19,9 @@
 
 package org.apache.cxf.jaxrs.provider;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -53,7 +56,9 @@ import org.apache.cxf.attachment.ByteDat
 import org.apache.cxf.common.i18n.BundleUtils;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.jaxrs.ext.MessageContext;
+import org.apache.cxf.jaxrs.ext.form.Form;
 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
+import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
 import org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource;
 import org.apache.cxf.jaxrs.ext.multipart.Multipart;
 import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
@@ -66,7 +71,7 @@ import org.apache.cxf.message.MessageUti
 
 @Provider
 @Consumes({"multipart/related", "multipart/mixed", "multipart/alternative", "multipart/form-data"
})
-@Produces({"multipart/related", "multipart/mixed", "multipart/alternative" })
+@Produces({"multipart/related", "multipart/mixed", "multipart/alternative", "multipart/form-data"
})
 public class MultipartProvider extends AbstractConfigurableProvider
     implements MessageBodyReader<Object>, MessageBodyWriter<Object> {
     
@@ -92,21 +97,25 @@ public class MultipartProvider extends A
     
     public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations,

                               MediaType mt) {
-        return isSupported(type, genericType, annotations, mt)
-             || (mt.getType().equals("multipart") && mt.getSubtype().equals("form-data")

-                && !MultivaluedMap.class.isAssignableFrom(type));
+        return isSupported(type, genericType, annotations, mt);
     }
     
     private boolean isSupported(Class<?> type, Type genericType, Annotation[] annotations,

                                 MediaType mt) {
         if (DataHandler.class.isAssignableFrom(type) || DataSource.class.isAssignableFrom(type)
             || Attachment.class.isAssignableFrom(type) || MultipartBody.class.isAssignableFrom(type)
-            || mediaTypeSupported(mt)) {
+            || mediaTypeSupported(mt)
+            || isSupportedFormDataType(type, mt)) {
             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) {
             List<String> values = mc.getHttpHeaders().getRequestHeader(HttpHeaders.CONTENT_LENGTH);
@@ -247,6 +256,7 @@ public class MultipartProvider extends A
                 Attachment handler = createDataHandler(value, value.getClass(), value.getClass(),

                                                        new Annotation[]{},
                                                        entry.getKey().toString(),
+                                                       mt.toString(),
                                                        i++);
                 handlers.add(handler);
             }
@@ -263,7 +273,7 @@ public class MultipartProvider extends A
                 }
                 Attachment handler = createDataHandler(obj,
                                                        type, genericType, anns,
-                                                       rootMediaType, 1);
+                                                       rootMediaType, mt.toString(), 1);
                 return Collections.singletonList(handler);
             }
         }
@@ -275,7 +285,7 @@ public class MultipartProvider extends A
             Object value = objects.get(i);
             Attachment handler = createDataHandler(value,
                                            value.getClass(), value.getClass(), new Annotation[]{},
-                                           rootMediaType, i);
+                                           rootMediaType, rootMediaType, i);
             handlers.add(handler);
         }
         return handlers;
@@ -284,7 +294,9 @@ public class MultipartProvider extends A
     private Attachment createDataHandler(Object obj, 
                                          Class<?> cls, Type genericType,
                                          Annotation[] anns,
-                                         String mimeType, int id) {
+                                         String mimeType,
+                                         String mainMediaType,
+                                         int id) {
         DataHandler dh = null;
         if (InputStream.class.isAssignableFrom(obj.getClass())) {
             dh = createInputStreamDH((InputStream)obj, mimeType);
@@ -292,6 +304,15 @@ public class MultipartProvider extends A
             dh = (DataHandler)obj;
         } else if (DataSource.class.isAssignableFrom(obj.getClass())) {
             dh = new DataHandler((DataSource)obj);
+        } else if (File.class.isAssignableFrom(obj.getClass())) {
+            File f = (File)obj;
+            ContentDisposition cd = mainMediaType.startsWith(MediaType.MULTIPART_FORM_DATA)

+                ? new ContentDisposition("form-data;name=file;filename=" + f.getName()) :
 null;
+            try {
+                return new Attachment(AttachmentUtil.BODY_ATTACHMENT_ID, new FileInputStream(f),
cd);
+            } catch (FileNotFoundException ex) {
+                throw new WebApplicationException(ex);
+            }
         } else if (Attachment.class.isAssignableFrom(obj.getClass())) {
             Attachment att = (Attachment)obj;
             if (att.getObject() == null) {

Modified: cxf/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java?rev=1097817&r1=1097816&r2=1097817&view=diff
==============================================================================
--- cxf/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java
(original)
+++ cxf/branches/2.3.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java
Fri Apr 29 13:38:09 2011
@@ -506,7 +506,7 @@ public class JAXRSMultipartTest extends 
         byte[] image2 = IOUtils.readBytesFromStream(is2);
         assertTrue(Arrays.equals(image1, image2));
         ContentDisposition cd2 = body2.getRootAttachment().getContentDisposition();
-        assertEquals("attachment;filename=java.jpg", cd2.toString());
+        assertEquals("form-data;name=file;filename=java.jpg", cd2.toString());
         assertEquals("java.jpg", cd2.getParameter("filename"));
     }
     



Mime
View raw message