1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.ftp; |
12 | |
|
13 | |
import org.mule.api.MuleMessage; |
14 | |
import org.mule.api.endpoint.EndpointURI; |
15 | |
import org.mule.api.endpoint.InboundEndpoint; |
16 | |
import org.mule.api.lifecycle.CreateException; |
17 | |
import org.mule.api.lifecycle.InitialisationException; |
18 | |
import org.mule.transport.AbstractMessageRequester; |
19 | |
|
20 | |
import java.io.File; |
21 | |
import java.io.FilenameFilter; |
22 | |
import java.io.IOException; |
23 | |
|
24 | |
import org.apache.commons.net.ftp.FTPClient; |
25 | |
import org.apache.commons.net.ftp.FTPFile; |
26 | |
import org.apache.commons.net.ftp.FTPReply; |
27 | |
|
28 | |
public class FtpMessageRequester extends AbstractMessageRequester |
29 | |
{ |
30 | |
protected final FtpConnector connector; |
31 | |
|
32 | |
public FtpMessageRequester(InboundEndpoint endpoint) |
33 | |
{ |
34 | 0 | super(endpoint); |
35 | 0 | this.connector = (FtpConnector) endpoint.getConnector(); |
36 | 0 | } |
37 | |
|
38 | |
@Override |
39 | |
protected void doDispose() |
40 | |
{ |
41 | |
|
42 | 0 | } |
43 | |
|
44 | |
@Override |
45 | |
protected void doConnect() throws Exception |
46 | |
{ |
47 | |
|
48 | 0 | } |
49 | |
|
50 | |
@Override |
51 | |
protected void doDisconnect() throws Exception |
52 | |
{ |
53 | |
try |
54 | |
{ |
55 | 0 | EndpointURI uri = endpoint.getEndpointURI(); |
56 | 0 | FTPClient client = connector.getFtp(uri); |
57 | 0 | connector.destroyFtp(uri, client); |
58 | |
} |
59 | 0 | catch (Exception e) |
60 | |
{ |
61 | |
|
62 | 0 | } |
63 | 0 | } |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
@Override |
77 | |
protected MuleMessage doRequest(long timeout) throws Exception |
78 | |
{ |
79 | 0 | FTPClient client = null; |
80 | |
try |
81 | |
{ |
82 | 0 | client = connector.createFtpClient(endpoint); |
83 | 0 | FTPFile fileToProcess = findFileToProcess(client); |
84 | 0 | if (fileToProcess == null) |
85 | |
{ |
86 | 0 | return null; |
87 | |
} |
88 | |
|
89 | 0 | fileToProcess = prepareFile(client, fileToProcess); |
90 | |
|
91 | 0 | FtpMuleMessageFactory messageFactory = createMuleMessageFactory(client); |
92 | 0 | return messageFactory.create(fileToProcess, endpoint.getEncoding()); |
93 | |
} |
94 | |
finally |
95 | |
{ |
96 | 0 | connector.releaseFtp(endpoint.getEndpointURI(), client); |
97 | |
} |
98 | |
} |
99 | |
|
100 | |
@Override |
101 | |
protected void initializeMessageFactory() throws InitialisationException |
102 | |
{ |
103 | |
|
104 | |
|
105 | |
|
106 | 0 | } |
107 | |
|
108 | |
protected FtpMuleMessageFactory createMuleMessageFactory(FTPClient client) throws CreateException |
109 | |
{ |
110 | 0 | FtpMuleMessageFactory factory = (FtpMuleMessageFactory) createMuleMessageFactory(); |
111 | |
|
112 | 0 | factory.setStreaming(false); |
113 | 0 | factory.setFtpClient(client); |
114 | |
|
115 | 0 | return factory; |
116 | |
} |
117 | |
|
118 | |
protected FTPFile prepareFile(FTPClient client, FTPFile file) throws IOException |
119 | |
{ |
120 | 0 | return file; |
121 | |
} |
122 | |
|
123 | |
protected FTPFile findFileToProcess(FTPClient client) throws Exception |
124 | |
{ |
125 | 0 | FTPFile[] files = listFiles(client); |
126 | |
|
127 | 0 | if (files != null) |
128 | |
{ |
129 | 0 | FilenameFilter filenameFilter = getFilenameFilter(); |
130 | 0 | for (int i = 0; i < files.length; i++) |
131 | |
{ |
132 | 0 | FTPFile file = files[i]; |
133 | 0 | if (file.isFile()) |
134 | |
{ |
135 | 0 | if (filenameFilter.accept(null, file.getName())) |
136 | |
{ |
137 | 0 | if (connector.validateFile(file)) |
138 | |
{ |
139 | |
|
140 | 0 | return file; |
141 | |
} |
142 | |
} |
143 | |
} |
144 | |
} |
145 | |
} |
146 | |
|
147 | 0 | return null; |
148 | |
} |
149 | |
|
150 | |
protected FTPFile[] listFiles(FTPClient client) throws IOException |
151 | |
{ |
152 | 0 | FTPFile[] files = client.listFiles(); |
153 | 0 | if (!FTPReply.isPositiveCompletion(client.getReplyCode())) |
154 | |
{ |
155 | 0 | throw new IOException("Ftp error: " + client.getReplyCode()); |
156 | |
} |
157 | |
|
158 | 0 | if (files == null || files.length == 0) |
159 | |
{ |
160 | 0 | return null; |
161 | |
} |
162 | |
|
163 | 0 | return files; |
164 | |
} |
165 | |
|
166 | |
protected FilenameFilter getFilenameFilter() |
167 | |
{ |
168 | 0 | if (endpoint.getFilter() instanceof FilenameFilter) |
169 | |
{ |
170 | 0 | return (FilenameFilter) endpoint.getFilter(); |
171 | |
} |
172 | |
|
173 | 0 | return new AcceptAllFilenameFilter(); |
174 | |
} |
175 | |
|
176 | |
private static class AcceptAllFilenameFilter implements FilenameFilter |
177 | |
{ |
178 | |
public AcceptAllFilenameFilter() |
179 | |
{ |
180 | 0 | super(); |
181 | 0 | } |
182 | |
|
183 | |
public boolean accept(File dir, String name) |
184 | |
{ |
185 | 0 | return true; |
186 | |
} |
187 | |
} |
188 | |
} |