You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2013/07/11 19:15:13 UTC

svn commit: r1502289 - in /cxf/branches/2.7.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/multipart/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/

Author: sergeyb
Date: Thu Jul 11 17:15:13 2013
New Revision: 1502289

URL: http://svn.apache.org/r1502289
Log:
Merged revisions 1502275,1502284 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1502275 | sergeyb | 2013-07-11 17:32:41 +0100 (Thu, 11 Jul 2013) | 1 line
  
  Some updates to AttachmentUtils to make it simpler to finf a matching part if Content-Disposition is available
........
  r1502284 | sergeyb | 2013-07-11 17:44:08 +0100 (Thu, 11 Jul 2013) | 1 line
  
  Adding a simple test where multiparts are looked up explicitly
........

Modified:
    cxf/branches/2.7.x-fixes/   (props changed)
    cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java
    cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/multipart/AttachmentUtils.java
    cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java
    cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/MultipartStore.java

Propchange: cxf/branches/2.7.x-fixes/
------------------------------------------------------------------------------
  Merged /cxf/trunk:r1502275-1502284

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

Modified: cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java?rev=1502289&r1=1502288&r2=1502289&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java (original)
+++ cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java Thu Jul 11 17:15:13 2013
@@ -168,7 +168,7 @@ public class MultipartProvider extends A
         Attachment multipart = AttachmentUtils.getMultipart(id, mt, infos);
         if (multipart != null) {
             if (collectionExpected && !mediaTypeSupported(multipart.getContentType())) {
-                List<Attachment> allMultiparts = AttachmentUtils.getAllMultiparts(id, mt, infos);
+                List<Attachment> allMultiparts = AttachmentUtils.getMatchingAttachments(id, infos);
                 return getAttachmentCollection(t, allMultiparts, anns);
             } else {
                 return fromAttachment(multipart, c, t, anns);

Modified: cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/multipart/AttachmentUtils.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/multipart/AttachmentUtils.java?rev=1502289&r1=1502288&r2=1502289&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/multipart/AttachmentUtils.java (original)
+++ cxf/branches/2.7.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/multipart/AttachmentUtils.java Thu Jul 11 17:15:13 2013
@@ -56,20 +56,44 @@ public final class AttachmentUtils {
         return (MultipartBody)mc.get(MultipartBody.INBOUND_MESSAGE_ATTACHMENTS);
     }
     
+    public static Map<String, Attachment> getChildAttachmentsMap(MessageContext mc,
+                                                                 boolean preferContentDisposition) {
+        return fromListToMap(getChildAttachments(mc), preferContentDisposition);
+    }
+    
     public static Map<String, Attachment> getChildAttachmentsMap(MessageContext mc) {
-        return fromListToMap(getChildAttachments(mc));
+        return fromListToMap(getChildAttachments(mc), false);
     }
     
     public static List<Attachment> getChildAttachments(MessageContext mc) {
-        return ((MultipartBody)mc.get(MultipartBody.INBOUND_MESSAGE_ATTACHMENTS)).getChildAttachments();
+        return getMultipartBody(mc).getChildAttachments();
+    }
+    
+    public static Map<String, Attachment> getAttachmentsMap(MessageContext mc, 
+                                                            boolean preferContentDisposition) {
+        return fromListToMap(getAttachments(mc), preferContentDisposition);
     }
     
     public static Map<String, Attachment> getAttachmentsMap(MessageContext mc) {
-        return fromListToMap(getAttachments(mc));
+        return fromListToMap(getAttachments(mc), false);
     }
     
     public static List<Attachment> getAttachments(MessageContext mc) {
-        return ((MultipartBody)mc.get(MultipartBody.INBOUND_MESSAGE_ATTACHMENTS)).getAllAttachments();
+        return getMultipartBody(mc).getAllAttachments();
+    }
+    
+    public static Attachment getFirstMatchingPart(MessageContext mc, Multipart id) {
+        return getFirstMatchingPart(mc, id.value());
+    }
+    
+    public static Attachment getFirstMatchingPart(MessageContext mc, String id) {
+        return getFirstMatchingPart(mc, id, null);
+    }
+    
+    public static Attachment getFirstMatchingPart(MessageContext mc, String id, String mediaType) {
+        List<Attachment> all = getAttachments(mc);
+        List<Attachment> matching = getMatchingAttachments(id, mediaType, all);
+        return matching.isEmpty() ? null : matching.get(0);
     }
     
     public static MultipartBody getMultipartBody(MessageContext mc,
@@ -105,7 +129,7 @@ public final class AttachmentUtils {
         
         if (id != null) {
             for (Attachment a : infos) {
-                if (matchAttachmentId(a, id, mt)) {
+                if (matchAttachmentId(a, id)) {
                     checkMediaTypes(a.getContentType(), id.type());
                     return a;    
                 }
@@ -127,26 +151,45 @@ public final class AttachmentUtils {
         return infos.size() > 0 ? infos.get(0) : null; 
     }
     
-    public static List<Attachment> getAllMultiparts(Multipart id, 
-                                              MediaType mt, 
-                                              List<Attachment> infos) throws IOException {
+    public static List<Attachment> getMatchingAttachments(Multipart id,
+                                                    List<Attachment> infos) {
+        return getMatchingAttachments(id.value(), id.type(), infos);
+    }
+    
+    public static List<Attachment> getMatchingAttachments(String id,
+                                                    List<Attachment> infos) {
+        return getMatchingAttachments(id, null, infos);
+    }
+    
+    public static List<Attachment> getMatchingAttachments(String id,
+                                                         String mediaType,
+                                                         List<Attachment> infos) {
     
         List<Attachment> all = new LinkedList<Attachment>();
         for (Attachment a : infos) {
-            if (matchAttachmentId(a, id, mt)) {
-                checkMediaTypes(a.getContentType(), id.type());
+            if (matchAttachmentId(a, id)) {
+                if (mediaType != null) {
+                    checkMediaTypes(a.getContentType(), mediaType);
+                }
                 all.add(a);    
             }
         }
         return all;
     }
     
-    private static boolean matchAttachmentId(Attachment at, Multipart mid, MediaType multipartType) {
-        if (at.getContentId().equals(mid.value())) {
+    public static boolean matchAttachmentId(Attachment at, Multipart mid) {
+        return matchAttachmentId(at, mid.value());
+    }
+    
+    public static boolean matchAttachmentId(Attachment at, String value) {
+        if (value.isEmpty()) {
+            return true;
+        }
+        if (at.getContentId().equals(value)) {
             return true;
         }
         ContentDisposition cd = at.getContentDisposition();
-        if (cd != null && mid.value().equals(cd.getParameter("name"))) {
+        if (cd != null && value.equals(cd.getParameter("name"))) {
             return true;
         }
         return false;
@@ -166,10 +209,21 @@ public final class AttachmentUtils {
         return populateFormMap(mc, true);
     }
     
-    private static Map<String, Attachment> fromListToMap(List<Attachment> atts) {
+    private static Map<String, Attachment> fromListToMap(List<Attachment> atts,
+                                                         boolean preferContentDisposition) {
         Map<String, Attachment> map = new LinkedHashMap<String, Attachment>();
         for (Attachment a : atts) {
-            map.put(a.getContentId(), a);    
+            String contentId = null;
+            if (preferContentDisposition) {
+                ContentDisposition cd = a.getContentDisposition();
+                if (cd != null) {
+                    contentId = cd.getParameter("name");
+                }
+            } 
+            if (contentId == null) {
+                contentId = a.getContentId();
+            }
+            map.put(contentId, a);    
         }
         return map;
     }

Modified: cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java?rev=1502289&r1=1502288&r2=1502289&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java (original)
+++ cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartTest.java Thu Jul 11 17:15:13 2013
@@ -169,6 +169,12 @@ public class JAXRSMultipartTest extends 
     }
     
     @Test
+    public void testBookJSONFormTwoFiles2() throws Exception {
+        String address = "http://localhost:" + PORT + "/bookstore/books/filesform2";
+        doAddFormBook(address, "attachmentFormJsonFiles", 200);               
+    }
+    
+    @Test
     public void testBookJSONFormTwoFilesNotRecursive() throws Exception {
         String address = "http://localhost:" + PORT + "/bookstore/books/filesform";
         doAddFormBook(address, "attachmentFormJsonFiles2", 200);               

Modified: cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/MultipartStore.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/MultipartStore.java?rev=1502289&r1=1502288&r2=1502289&view=diff
==============================================================================
--- cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/MultipartStore.java (original)
+++ cxf/branches/2.7.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/MultipartStore.java Thu Jul 11 17:15:13 2013
@@ -478,6 +478,16 @@ public class MultipartStore {
     }
     
     @POST
+    @Path("/books/filesform2")
+    @Produces("text/xml")
+    @Consumes("multipart/form-data")
+    public Response addBookFilesFormNoOwnerParam(@Multipart("files") List<Book> books) 
+        throws Exception {
+        Attachment attOwner = AttachmentUtils.getFirstMatchingPart(context, "owner");
+        return addBookFilesForm(attOwner.getObject(String.class), books);
+    }
+    
+    @POST
     @Path("/books/filesform/singlefile")
     @Produces("text/xml")
     @Consumes("multipart/form-data")