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