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