1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.file; |
8 | |
|
9 | |
import org.mule.DefaultMuleMessage; |
10 | |
import org.mule.api.DefaultMuleException; |
11 | |
import org.mule.api.MuleEvent; |
12 | |
import org.mule.api.MuleException; |
13 | |
import org.mule.api.MuleMessage; |
14 | |
import org.mule.api.endpoint.OutboundEndpoint; |
15 | |
import org.mule.api.transport.OutputHandler; |
16 | |
import org.mule.transformer.types.DataTypeFactory; |
17 | |
import org.mule.transport.AbstractMessageDispatcher; |
18 | |
import org.mule.transport.NullPayload; |
19 | |
import org.mule.transport.file.i18n.FileMessages; |
20 | |
import org.mule.util.FileUtils; |
21 | |
import org.mule.util.IOUtils; |
22 | |
import org.mule.util.StringUtils; |
23 | |
|
24 | |
import java.io.File; |
25 | |
import java.io.FileFilter; |
26 | |
import java.io.FileOutputStream; |
27 | |
import java.io.FilenameFilter; |
28 | |
import java.io.InputStream; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class FileMessageDispatcher extends AbstractMessageDispatcher |
34 | |
{ |
35 | |
private final FileConnector connector; |
36 | |
|
37 | |
public FileMessageDispatcher(OutboundEndpoint endpoint) |
38 | |
{ |
39 | 0 | super(endpoint); |
40 | 0 | this.connector = (FileConnector) endpoint.getConnector(); |
41 | |
|
42 | 0 | if (endpoint.getProperty("outputAppend") != null) |
43 | |
{ |
44 | 0 | throw new IllegalArgumentException("Configuring 'outputAppend' on a file endpoint is no longer supported. You may configure it on a file connector instead."); |
45 | |
} |
46 | 0 | } |
47 | |
|
48 | |
@Override |
49 | |
protected void doDispatch(MuleEvent event) throws Exception |
50 | |
{ |
51 | 0 | Object data = event.getMessage().getPayload(); |
52 | |
|
53 | 0 | MuleMessage message = new DefaultMuleMessage(data, event.getMessage(), event.getMuleContext()); |
54 | |
|
55 | 0 | FileOutputStream fos = (FileOutputStream) connector.getOutputStream((OutboundEndpoint) endpoint, event); |
56 | |
try |
57 | |
{ |
58 | 0 | if (event.getMessage().getOutboundProperty(FileConnector.PROPERTY_FILENAME) == null) |
59 | |
{ |
60 | 0 | event.getMessage().setOutboundProperty(FileConnector.PROPERTY_FILENAME, |
61 | |
message.getOutboundProperty(FileConnector.PROPERTY_FILENAME, |
62 | |
StringUtils.EMPTY)); |
63 | |
} |
64 | |
|
65 | 0 | if (data instanceof byte[]) |
66 | |
{ |
67 | 0 | fos.write((byte[]) data); |
68 | |
} |
69 | 0 | else if (data instanceof String) |
70 | |
{ |
71 | 0 | fos.write(data.toString().getBytes(event.getEncoding())); |
72 | |
} |
73 | 0 | else if (data instanceof OutputHandler) |
74 | |
{ |
75 | 0 | ((OutputHandler) data).write(event, fos); |
76 | |
} |
77 | |
else |
78 | |
{ |
79 | 0 | InputStream is = (InputStream) event.transformMessage(DataTypeFactory.create(InputStream.class)); |
80 | 0 | IOUtils.copyLarge(is, fos); |
81 | 0 | is.close(); |
82 | |
} |
83 | |
} |
84 | |
finally |
85 | |
{ |
86 | 0 | logger.debug("Closing file"); |
87 | 0 | fos.close(); |
88 | 0 | } |
89 | 0 | } |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public Object getDelegateSession() throws MuleException |
97 | |
{ |
98 | 0 | return null; |
99 | |
} |
100 | |
|
101 | |
protected static File getNextFile(String dir, Object filter) throws MuleException |
102 | |
{ |
103 | |
File[] files; |
104 | 0 | File file = FileUtils.newFile(dir); |
105 | 0 | File result = null; |
106 | |
try |
107 | |
{ |
108 | 0 | if (file.exists()) |
109 | |
{ |
110 | 0 | if (file.isFile()) |
111 | |
{ |
112 | 0 | result = file; |
113 | |
} |
114 | 0 | else if (file.isDirectory()) |
115 | |
{ |
116 | 0 | if (filter != null) |
117 | |
{ |
118 | 0 | if (filter instanceof FileFilter) |
119 | |
{ |
120 | 0 | files = file.listFiles((FileFilter) filter); |
121 | |
} |
122 | 0 | else if (filter instanceof FilenameFilter) |
123 | |
{ |
124 | 0 | files = file.listFiles((FilenameFilter) filter); |
125 | |
} |
126 | |
else |
127 | |
{ |
128 | 0 | throw new DefaultMuleException(FileMessages.invalidFilter(filter)); |
129 | |
} |
130 | |
} |
131 | |
else |
132 | |
{ |
133 | 0 | files = file.listFiles(); |
134 | |
} |
135 | 0 | if (files.length > 0) |
136 | |
{ |
137 | 0 | result = files[0]; |
138 | |
} |
139 | |
} |
140 | |
} |
141 | 0 | return result; |
142 | |
} |
143 | 0 | catch (Exception e) |
144 | |
{ |
145 | 0 | throw new DefaultMuleException(FileMessages.errorWhileListingFiles(), e); |
146 | |
} |
147 | |
} |
148 | |
|
149 | |
@Override |
150 | |
protected MuleMessage doSend(MuleEvent event) throws Exception |
151 | |
{ |
152 | 0 | doDispatch(event); |
153 | 0 | return new DefaultMuleMessage(NullPayload.getInstance(), connector.getMuleContext()); |
154 | |
} |
155 | |
|
156 | |
@Override |
157 | |
protected void doDispose() |
158 | |
{ |
159 | |
|
160 | 0 | } |
161 | |
|
162 | |
@Override |
163 | |
protected void doConnect() throws Exception |
164 | |
{ |
165 | |
|
166 | 0 | } |
167 | |
|
168 | |
@Override |
169 | |
protected void doDisconnect() throws Exception |
170 | |
{ |
171 | |
|
172 | 0 | } |
173 | |
} |