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.MuleException;
16 import org.mule.api.MuleMessage;
17 import org.mule.api.endpoint.InboundEndpoint;
18 import org.mule.api.lifecycle.CreateException;
19 import org.mule.api.routing.filter.Filter;
20 import org.mule.transport.AbstractMessageRequester;
21 import org.mule.transport.file.i18n.FileMessages;
22 import org.mule.util.FileUtils;
23
24 import java.io.File;
25 import java.io.FileFilter;
26 import java.io.FileNotFoundException;
27 import java.io.FilenameFilter;
28 import java.io.IOException;
29
30
31
32
33 public class FileMessageRequester extends AbstractMessageRequester
34 {
35 private final FileConnector connector;
36
37 private FilenameFilter filenameFilter = null;
38 private FileFilter fileFilter = null;
39
40 public FileMessageRequester(InboundEndpoint endpoint) throws MuleException
41 {
42 super(endpoint);
43 this.connector = (FileConnector) endpoint.getConnector();
44
45 Filter filter = endpoint.getFilter();
46 if (filter instanceof FilenameFilter)
47 {
48 filenameFilter = (FilenameFilter) filter;
49 }
50 else if (filter instanceof FileFilter)
51 {
52 fileFilter = (FileFilter) filter;
53 }
54 else if (filter != null)
55 {
56 throw new CreateException(FileMessages.invalidFileFilter(endpoint.getEndpointURI()), this);
57 }
58 }
59
60
61
62
63
64
65 public Object getDelegateSession() throws MuleException
66 {
67 return null;
68 }
69
70
71
72
73
74
75
76
77
78
79
80 @Override
81 protected MuleMessage doRequest(long timeout) throws Exception
82 {
83 File file = FileUtils.newFile(endpoint.getEndpointURI().getAddress());
84 File result = null;
85
86 if (file.exists())
87 {
88 if (file.isFile())
89 {
90 result = file;
91 }
92 else if (file.isDirectory())
93 {
94 if (fileFilter != null)
95 {
96 result = FileMessageDispatcher.getNextFile(
97 endpoint.getEndpointURI().getAddress(), fileFilter);
98 }
99 else
100 {
101 result = FileMessageDispatcher.getNextFile(
102 endpoint.getEndpointURI().getAddress(), filenameFilter);
103 }
104 }
105
106 if (result != null)
107 {
108 boolean checkFileAge = connector.getCheckFileAge();
109 if (checkFileAge)
110 {
111 long fileAge = connector.getFileAge();
112 long lastMod = result.lastModified();
113 long now = System.currentTimeMillis();
114 long thisFileAge = now - lastMod;
115 if (thisFileAge < fileAge)
116 {
117 if (logger.isDebugEnabled())
118 {
119 logger.debug("The file has not aged enough yet, will return nothing for: "
120 + result.getCanonicalPath());
121 }
122 return null;
123 }
124 }
125
126
127 String sourceFileOriginalName = result.getName();
128
129
130 File destinationFile = null;
131 String movDir = getMoveDirectory();
132 if (movDir != null)
133 {
134 String destinationFileName = sourceFileOriginalName;
135 String moveToPattern = getMoveToPattern();
136 if (moveToPattern != null)
137 {
138
139
140 DefaultMuleMessage parserMesssage = new DefaultMuleMessage(null,
141 connector.getMuleContext());
142 parserMesssage.setOutboundProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, sourceFileOriginalName);
143
144 destinationFileName =
145 connector.getFilenameParser().getFilename(parserMesssage, moveToPattern);
146 }
147
148 destinationFile = FileUtils.newFile(movDir, destinationFileName);
149 }
150
151 MuleMessage returnMessage = null;
152 String encoding = endpoint.getEncoding();
153 try
154 {
155 if (connector.isStreaming())
156 {
157 ReceiverFileInputStream receiverStream = new ReceiverFileInputStream(result,
158 connector.isAutoDelete(), destinationFile);
159 returnMessage = createMuleMessage(receiverStream, encoding);
160 }
161 else
162 {
163 returnMessage = createMuleMessage(result, encoding);
164 }
165 }
166 catch (FileNotFoundException e)
167 {
168
169
170 logger.error("File being read disappeared!", e);
171 return null;
172 }
173 returnMessage.setOutboundProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, sourceFileOriginalName);
174
175 if (!connector.isStreaming())
176 {
177 moveOrDelete(result, destinationFile);
178 }
179
180
181
182 return returnMessage;
183 }
184 }
185 return null;
186 }
187
188 private void moveOrDelete(final File sourceFile, File destinationFile) throws DefaultMuleException
189 {
190
191 if (destinationFile != null)
192 {
193
194 try
195 {
196 FileUtils.moveFile(sourceFile, destinationFile);
197 }
198 catch (IOException e)
199 {
200 throw new DefaultMuleException(FileMessages.failedToMoveFile(sourceFile.getAbsolutePath(),
201 destinationFile.getAbsolutePath()));
202 }
203 }
204 if (connector.isAutoDelete())
205 {
206
207 if (destinationFile == null)
208 {
209 System.out.println("\n\n***** FileMessageRequester moveOrDelete\n\n");
210
211 if (!sourceFile.delete())
212 {
213 throw new DefaultMuleException(FileMessages.failedToDeleteFile(sourceFile));
214 }
215 }
216 else
217 {
218
219
220 }
221 }
222
223 }
224
225 @Override
226 protected void doDispose()
227 {
228
229 }
230
231 @Override
232 protected void doConnect() throws Exception
233 {
234
235 }
236
237 @Override
238 protected void doDisconnect() throws Exception
239 {
240
241 }
242
243 protected String getMoveDirectory()
244 {
245 String moveDirectory = (String) endpoint.getProperty(FileConnector.PROPERTY_MOVE_TO_DIRECTORY);
246 if (moveDirectory == null)
247 {
248 moveDirectory = connector.getMoveToDirectory();
249 }
250 return moveDirectory;
251 }
252
253 protected String getMoveToPattern()
254 {
255 String pattern = (String) endpoint.getProperty(FileConnector.PROPERTY_MOVE_TO_PATTERN);
256 if (pattern == null)
257 {
258 pattern = connector.getMoveToPattern();
259 }
260 return pattern;
261 }
262
263 }