1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.file; |
12 | |
|
13 | |
import org.mule.api.MuleContext; |
14 | |
import org.mule.api.MuleMessage; |
15 | |
import org.mule.util.IOUtils; |
16 | |
|
17 | |
import java.io.File; |
18 | |
import java.io.FileInputStream; |
19 | |
import java.io.InputStream; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
public class FileContentsMuleMessageFactory extends FileMuleMessageFactory |
27 | |
{ |
28 | |
public FileContentsMuleMessageFactory(MuleContext context) |
29 | |
{ |
30 | 0 | super(context); |
31 | 0 | } |
32 | |
|
33 | |
@Override |
34 | |
protected Class<?>[] getSupportedTransportMessageTypes() |
35 | |
{ |
36 | 0 | return new Class[]{InputStream.class, File.class}; |
37 | |
} |
38 | |
|
39 | |
@Override |
40 | |
protected Object extractPayload(Object transportMessage, String encoding) throws Exception |
41 | |
{ |
42 | 0 | InputStream inputStream = convertToInputStream(transportMessage); |
43 | 0 | byte[] payload = IOUtils.toByteArray(inputStream); |
44 | 0 | inputStream.close(); |
45 | 0 | return payload; |
46 | |
} |
47 | |
|
48 | |
private InputStream convertToInputStream(Object transportMessage) throws Exception |
49 | |
{ |
50 | 0 | InputStream stream = null; |
51 | |
|
52 | 0 | if (transportMessage instanceof InputStream) |
53 | |
{ |
54 | 0 | stream = (InputStream) transportMessage; |
55 | |
} |
56 | 0 | else if (transportMessage instanceof File) |
57 | |
{ |
58 | 0 | stream = new FileInputStream((File) transportMessage); |
59 | |
} |
60 | |
|
61 | 0 | return stream; |
62 | |
} |
63 | |
} |