1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
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 | 0 | super(context); |
29 | 0 | } |
30 | |
|
31 | |
@Override |
32 | |
protected Object extractPayloadFromHttpRequest(HttpRequest httpRequest) throws IOException |
33 | |
{ |
34 | 0 | Object body = null; |
35 | |
|
36 | 0 | if (httpRequest.getContentType().contains("multipart/form-data")) |
37 | |
{ |
38 | 0 | MultiPartInputStream in = new MultiPartInputStream(httpRequest.getBody(), httpRequest.getContentType(), null); |
39 | |
|
40 | |
|
41 | 0 | parts = in.getParts(); |
42 | 0 | for (Part part : parts) |
43 | |
{ |
44 | 0 | if (part.getName().equals("payload")) |
45 | |
{ |
46 | 0 | body = part.getInputStream(); |
47 | 0 | break; |
48 | |
} |
49 | |
} |
50 | 0 | if (body == null) |
51 | |
{ |
52 | 0 | throw new IllegalArgumentException("no part named \"payload\" found"); |
53 | |
} |
54 | 0 | } |
55 | |
else |
56 | |
{ |
57 | 0 | body = super.extractPayloadFromHttpRequest(httpRequest); |
58 | |
} |
59 | |
|
60 | 0 | return body; |
61 | |
} |
62 | |
|
63 | |
@Override |
64 | |
protected void addAttachments(DefaultMuleMessage message, Object transportMessage) throws Exception |
65 | |
{ |
66 | 0 | if (parts != null) |
67 | |
{ |
68 | |
try |
69 | |
{ |
70 | 0 | for (Part part : parts) |
71 | |
{ |
72 | 0 | if (!part.getName().equals("payload")) |
73 | |
{ |
74 | 0 | message.addInboundAttachment(part.getName(), new DataHandler(new PartDataSource(part))); |
75 | |
} |
76 | |
} |
77 | |
} |
78 | |
finally |
79 | |
{ |
80 | |
|
81 | 0 | parts.clear(); |
82 | 0 | parts = null; |
83 | 0 | } |
84 | |
} |
85 | 0 | } |
86 | |
|
87 | |
@Override |
88 | |
protected void convertMultiPartHeaders(Map<String, Object> headers) |
89 | |
{ |
90 | 0 | if (parts != null) |
91 | |
{ |
92 | 0 | for (Part part : parts) |
93 | |
{ |
94 | 0 | if (part.getName().equals("payload")) |
95 | |
{ |
96 | 0 | for (String name : part.getHeaderNames()) |
97 | |
{ |
98 | 0 | headers.put(name, part.getHeader(name)); |
99 | |
} |
100 | 0 | break; |
101 | |
} |
102 | |
} |
103 | |
|
104 | |
} |
105 | |
|
106 | 0 | } |
107 | |
|
108 | |
} |
109 | |
|
110 | |
|