1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.camel.component.file.remote; |
19 |
|
|
20 |
|
import org.apache.camel.Processor; |
21 |
|
import org.apache.commons.net.ftp.FTPClient; |
22 |
|
import org.apache.commons.net.ftp.FTPFile; |
23 |
|
|
24 |
|
import java.io.ByteArrayOutputStream; |
25 |
|
import java.io.IOException; |
26 |
|
import java.util.concurrent.ScheduledExecutorService; |
27 |
|
|
28 |
|
public class FtpConsumer extends RemoteFileConsumer<RemoteFileExchange> { |
29 |
1 |
private boolean recursive = true; |
30 |
1 |
private String regexPattern = ""; |
31 |
1 |
private long lastPollTime = 0L; |
32 |
|
private final FtpEndpoint endpoint; |
33 |
|
private FTPClient client; |
34 |
|
|
35 |
|
public FtpConsumer(FtpEndpoint endpoint, Processor processor, FTPClient client) { |
36 |
1 |
super(endpoint, processor); |
37 |
1 |
this.endpoint = endpoint; |
38 |
1 |
this.client = client; |
39 |
1 |
} |
40 |
|
|
41 |
|
public FtpConsumer(FtpEndpoint endpoint, Processor processor, FTPClient client, ScheduledExecutorService executor) { |
42 |
0 |
super(endpoint, processor, executor); |
43 |
0 |
this.endpoint = endpoint; |
44 |
0 |
this.client = client; |
45 |
0 |
} |
46 |
|
|
47 |
|
protected void poll() throws Exception { |
48 |
1 |
final String fileName = endpoint.getConfiguration().getFile(); |
49 |
1 |
if (endpoint.getConfiguration().isDirectory()) { |
50 |
1 |
pollDirectory(fileName); |
51 |
0 |
} |
52 |
|
else { |
53 |
0 |
client.changeWorkingDirectory(fileName.substring(0, fileName.lastIndexOf('/'))); |
54 |
0 |
final FTPFile[] files = client.listFiles(fileName.substring(fileName.lastIndexOf('/') + 1)); |
55 |
0 |
pollFile(files[0]); |
56 |
|
} |
57 |
0 |
lastPollTime = System.currentTimeMillis(); |
58 |
0 |
} |
59 |
|
|
60 |
|
protected void pollDirectory(String dir) throws Exception { |
61 |
4 |
client.changeWorkingDirectory(dir); |
62 |
6 |
for (FTPFile ftpFile : client.listFiles()) { |
63 |
4 |
if (ftpFile.isFile()) { |
64 |
1 |
pollFile(ftpFile); |
65 |
1 |
} |
66 |
3 |
else if (ftpFile.isDirectory()) { |
67 |
3 |
if (isRecursive()) { |
68 |
3 |
pollDirectory(getFullFileName(ftpFile)); |
69 |
1 |
} |
70 |
|
} |
71 |
|
else { |
72 |
0 |
throw new RuntimeException(""); |
73 |
|
} |
74 |
|
} |
75 |
1 |
} |
76 |
|
|
77 |
|
protected String getFullFileName(FTPFile ftpFile) throws IOException { |
78 |
4 |
return client.printWorkingDirectory() + "/" + ftpFile.getName(); |
79 |
|
} |
80 |
|
|
81 |
|
private void pollFile(FTPFile ftpFile) throws Exception { |
82 |
1 |
if (ftpFile.getTimestamp().getTimeInMillis() > lastPollTime) { |
83 |
1 |
if (isMatched(ftpFile)) { |
84 |
1 |
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
85 |
1 |
client.retrieveFile(ftpFile.getName(), byteArrayOutputStream); |
86 |
1 |
getProcessor().process(endpoint.createExchange(getFullFileName(ftpFile), byteArrayOutputStream)); |
87 |
|
} |
88 |
|
} |
89 |
1 |
} |
90 |
|
|
91 |
|
protected boolean isMatched(FTPFile file) { |
92 |
1 |
boolean result = true; |
93 |
1 |
if (regexPattern != null && regexPattern.length() > 0) { |
94 |
0 |
result = file.getName().matches(getRegexPattern()); |
95 |
|
} |
96 |
1 |
return result; |
97 |
|
} |
98 |
|
|
99 |
|
public boolean isRecursive() { |
100 |
3 |
return recursive; |
101 |
|
} |
102 |
|
|
103 |
|
public void setRecursive(boolean recursive) { |
104 |
0 |
this.recursive = recursive; |
105 |
0 |
} |
106 |
|
|
107 |
|
public long getLastPollTime() { |
108 |
0 |
return lastPollTime; |
109 |
|
} |
110 |
|
|
111 |
|
public void setLastPollTime(long lastPollTime) { |
112 |
0 |
this.lastPollTime = lastPollTime; |
113 |
0 |
} |
114 |
|
|
115 |
|
public String getRegexPattern() { |
116 |
0 |
return regexPattern; |
117 |
|
} |
118 |
|
|
119 |
|
public void setRegexPattern(String regexPattern) { |
120 |
0 |
this.regexPattern = regexPattern; |
121 |
0 |
} |
122 |
|
} |