View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.http;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleContext;
11  import org.mule.transport.http.multipart.MultiPartInputStream;
12  import org.mule.transport.http.multipart.Part;
13  import org.mule.transport.http.multipart.PartDataSource;
14  
15  import java.io.IOException;
16  import java.util.Collection;
17  import java.util.Map;
18  
19  import javax.activation.DataHandler;
20  
21  public class HttpMultipartMuleMessageFactory extends HttpMuleMessageFactory
22  {
23  
24      private Collection<Part> parts;
25  
26      public HttpMultipartMuleMessageFactory(MuleContext context)
27      {
28          super(context);
29      }
30  
31      @Override
32      protected Object extractPayloadFromHttpRequest(HttpRequest httpRequest) throws IOException
33      {
34          Object body = null;
35  
36          if (httpRequest.getContentType().contains("multipart/form-data"))
37          {
38              MultiPartInputStream in = new MultiPartInputStream(httpRequest.getBody(), httpRequest.getContentType(), null);
39  
40              // We need to store this so that the headers for the part can be read
41              parts = in.getParts();
42              for (Part part : parts)
43              {
44                  if (part.getName().equals("payload"))
45                  {
46                      body = part.getInputStream();
47                      break;
48                  }
49              }
50              if (body == null)
51              {
52                  throw new IllegalArgumentException("no part named \"payload\" found");
53              }
54          }
55          else
56          {
57              body = super.extractPayloadFromHttpRequest(httpRequest);
58          }
59  
60          return body;
61      }
62  
63      @Override
64      protected void addAttachments(DefaultMuleMessage message, Object transportMessage) throws Exception
65      {
66          if (parts != null)
67          {
68              try
69              {
70                  for (Part part : parts)
71                  {
72                      if (!part.getName().equals("payload"))
73                      {
74                          message.addInboundAttachment(part.getName(), new DataHandler(new PartDataSource(part)));
75                      }
76                  }
77              }
78              finally
79              {
80                  // Attachments are the last thing to get processed
81                  parts.clear();
82                  parts = null;
83              }
84          }
85      }
86  
87      @Override
88      protected void convertMultiPartHeaders(Map<String, Object> headers)
89      {
90          if (parts != null)
91          {
92              for (Part part : parts)
93              {
94                  if (part.getName().equals("payload"))
95                  {
96                      for (String name : part.getHeaderNames())
97                      {
98                          headers.put(name, part.getHeader(name));
99                      }
100                     break;
101                 }
102             }
103 
104         }
105 
106     }
107 
108 }
109 
110