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.transport.AbstractMessageDispatcher; |
21 | |
import org.mule.transport.file.i18n.FileMessages; |
22 | |
import org.mule.util.FileUtils; |
23 | |
import org.mule.util.IOUtils; |
24 | |
|
25 | |
import java.io.File; |
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 | 4 | super(endpoint); |
40 | 4 | this.connector = (FileConnector) endpoint.getConnector(); |
41 | |
|
42 | 4 | if (endpoint.getProperty("outputAppend") != null) |
43 | |
{ |
44 | 2 | throw new IllegalArgumentException("configuring outputAppend on the file endpoint is no longer support. You can configure a the File connector instead."); |
45 | |
} |
46 | 2 | } |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
protected void doDispatch(MuleEvent event) throws Exception |
54 | |
{ |
55 | 2 | Object data = event.transformMessage(); |
56 | |
|
57 | 2 | MuleMessage message = new DefaultMuleMessage(data, event.getMessage()); |
58 | |
|
59 | 2 | FileOutputStream fos = (FileOutputStream) connector.getOutputStream(event.getEndpoint(), message); |
60 | |
try |
61 | |
{ |
62 | 2 | if (event.getMessage().getStringProperty(FileConnector.PROPERTY_FILENAME, null) == null) |
63 | |
{ |
64 | 2 | event.getMessage().setStringProperty(FileConnector.PROPERTY_FILENAME, |
65 | |
message.getStringProperty(FileConnector.PROPERTY_FILENAME, "")); |
66 | |
} |
67 | |
|
68 | 2 | if (data instanceof byte[]) |
69 | |
{ |
70 | 0 | fos.write((byte[]) data); |
71 | |
} |
72 | 2 | else if (data instanceof String) |
73 | |
{ |
74 | 2 | fos.write(data.toString().getBytes(event.getEncoding())); |
75 | |
} |
76 | 0 | else if (data instanceof OutputHandler) |
77 | |
{ |
78 | 0 | ((OutputHandler) data).write(event, fos); |
79 | |
} |
80 | |
else |
81 | |
{ |
82 | 0 | InputStream is = (InputStream) event.transformMessage(InputStream.class); |
83 | 0 | IOUtils.copyLarge(is, fos); |
84 | 0 | is.close(); |
85 | |
} |
86 | |
} |
87 | |
finally |
88 | |
{ |
89 | 2 | logger.debug("Closing file"); |
90 | 2 | fos.close(); |
91 | 2 | } |
92 | 2 | } |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public Object getDelegateSession() throws MuleException |
100 | |
{ |
101 | 0 | return null; |
102 | |
} |
103 | |
|
104 | |
protected static File getNextFile(String dir, FilenameFilter filter) throws MuleException |
105 | |
{ |
106 | |
File[] files; |
107 | 4 | File file = FileUtils.newFile(dir); |
108 | 4 | File result = null; |
109 | |
try |
110 | |
{ |
111 | 4 | if (file.exists()) |
112 | |
{ |
113 | 4 | if (file.isFile()) |
114 | |
{ |
115 | 0 | result = file; |
116 | |
} |
117 | 4 | else if (file.isDirectory()) |
118 | |
{ |
119 | 4 | if (filter != null) |
120 | |
{ |
121 | 0 | files = file.listFiles(filter); |
122 | |
} |
123 | |
else |
124 | |
{ |
125 | 4 | files = file.listFiles(); |
126 | |
} |
127 | 4 | if (files.length > 0) |
128 | |
{ |
129 | 4 | result = files[0]; |
130 | |
} |
131 | |
} |
132 | |
} |
133 | 4 | return result; |
134 | |
} |
135 | 0 | catch (Exception e) |
136 | |
{ |
137 | 0 | throw new DefaultMuleException(FileMessages.errorWhileListingFiles(), e); |
138 | |
} |
139 | |
} |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
protected MuleMessage doSend(MuleEvent event) throws Exception |
147 | |
{ |
148 | 0 | doDispatch(event); |
149 | 0 | return event.getMessage(); |
150 | |
} |
151 | |
|
152 | |
protected void doDispose() |
153 | |
{ |
154 | |
|
155 | 2 | } |
156 | |
|
157 | |
protected void doConnect() throws Exception |
158 | |
{ |
159 | |
|
160 | 2 | } |
161 | |
|
162 | |
protected void doDisconnect() throws Exception |
163 | |
{ |
164 | |
|
165 | 2 | } |
166 | |
|
167 | |
} |