View Javadoc

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