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