1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.file;
12
13 import org.mule.DefaultMuleMessage;
14 import org.mule.api.MuleContext;
15 import org.mule.api.MuleMessage;
16 import org.mule.transport.AbstractMuleMessageFactory;
17
18 import java.io.File;
19
20
21
22
23
24
25
26
27 public class FileMuleMessageFactory extends AbstractMuleMessageFactory
28 {
29 public FileMuleMessageFactory(MuleContext context)
30 {
31 super(context);
32 }
33
34 @Override
35 protected Class<?>[] getSupportedTransportMessageTypes()
36 {
37 return new Class[]{File.class, ReceiverFileInputStream.class};
38 }
39
40 @Override
41 protected Object extractPayload(Object transportMessage, String encoding) throws Exception
42 {
43 return transportMessage;
44 }
45
46 @Override
47 protected void addProperties(DefaultMuleMessage message, Object transportMessage) throws Exception
48 {
49 super.addProperties(message, transportMessage);
50 File file = convertToFile(transportMessage);
51 setPropertiesFromFile(message, file);
52 }
53
54 protected File convertToFile(Object transportMessage)
55 {
56 File file = null;
57
58 if (transportMessage instanceof File)
59 {
60 file = (File) transportMessage;
61 }
62 else if (transportMessage instanceof ReceiverFileInputStream)
63 {
64 file = ((ReceiverFileInputStream) transportMessage).getCurrentFile();
65 }
66
67 return file;
68 }
69
70 protected void setPropertiesFromFile(MuleMessage message, File file)
71 {
72 message.setOutboundProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, file.getName());
73 message.setOutboundProperty(FileConnector.PROPERTY_DIRECTORY, file.getParent());
74 }
75 }