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