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 com.jcraft.jsch.ChannelSftp; |
21 |
|
import org.apache.camel.Processor; |
22 |
|
|
23 |
|
import java.io.ByteArrayOutputStream; |
24 |
|
import java.io.IOException; |
25 |
|
import java.util.concurrent.ScheduledExecutorService; |
26 |
|
|
27 |
|
public class SftpConsumer extends RemoteFileConsumer<RemoteFileExchange> { |
28 |
0 |
private boolean recursive = true; |
29 |
0 |
private String regexPattern = ""; |
30 |
0 |
private long lastPollTime = 0L; |
31 |
|
private final SftpEndpoint endpoint; |
32 |
|
private ChannelSftp channel; |
33 |
|
|
34 |
|
public SftpConsumer(SftpEndpoint endpoint, Processor processor, ChannelSftp channel) { |
35 |
0 |
super(endpoint, processor); |
36 |
0 |
this.endpoint = endpoint; |
37 |
0 |
this.channel = channel; |
38 |
0 |
} |
39 |
|
|
40 |
|
public SftpConsumer(SftpEndpoint endpoint, Processor processor, ChannelSftp channel, ScheduledExecutorService executor) { |
41 |
0 |
super(endpoint, processor, executor); |
42 |
0 |
this.endpoint = endpoint; |
43 |
0 |
this.channel = channel; |
44 |
0 |
} |
45 |
|
|
46 |
|
protected void poll() throws Exception { |
47 |
0 |
final String fileName = endpoint.getConfiguration().getFile(); |
48 |
0 |
if (endpoint.getConfiguration().isDirectory()) { |
49 |
0 |
pollDirectory(fileName); |
50 |
0 |
} |
51 |
|
else { |
52 |
0 |
channel.cd(fileName.substring(0, fileName.lastIndexOf('/'))); |
53 |
0 |
final ChannelSftp.LsEntry file = (ChannelSftp.LsEntry) channel.ls(fileName.substring(fileName.lastIndexOf('/') + 1)).get(0); |
54 |
0 |
pollFile(file); |
55 |
|
} |
56 |
0 |
lastPollTime = System.currentTimeMillis(); |
57 |
0 |
} |
58 |
|
|
59 |
|
protected void pollDirectory(String dir) throws Exception { |
60 |
0 |
channel.cd(dir); |
61 |
0 |
for (ChannelSftp.LsEntry sftpFile : (ChannelSftp.LsEntry[]) channel.ls(".").toArray(new ChannelSftp.LsEntry[]{})) { |
62 |
0 |
if (sftpFile.getFilename().startsWith(".")) { |
63 |
|
|
64 |
0 |
} |
65 |
0 |
else if (sftpFile.getAttrs().isDir()) { |
66 |
0 |
if (isRecursive()) { |
67 |
0 |
pollDirectory(getFullFileName(sftpFile)); |
68 |
0 |
} |
69 |
|
} |
70 |
|
else { |
71 |
0 |
pollFile(sftpFile); |
72 |
|
} |
73 |
|
} |
74 |
0 |
} |
75 |
|
|
76 |
|
protected String getFullFileName(ChannelSftp.LsEntry sftpFile) throws IOException { |
77 |
0 |
return channel.pwd() + "/" + sftpFile.getFilename(); |
78 |
|
} |
79 |
|
|
80 |
|
private void pollFile(ChannelSftp.LsEntry sftpFile) throws Exception { |
81 |
0 |
if (sftpFile.getAttrs().getMTime() * 1000 > lastPollTime) { |
82 |
0 |
if (isMatched(sftpFile)) { |
83 |
0 |
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
84 |
0 |
channel.get(sftpFile.getFilename(), byteArrayOutputStream); |
85 |
0 |
getProcessor().process(endpoint.createExchange(getFullFileName(sftpFile), byteArrayOutputStream)); |
86 |
|
} |
87 |
|
} |
88 |
0 |
} |
89 |
|
|
90 |
|
protected boolean isMatched(ChannelSftp.LsEntry sftpFile) { |
91 |
0 |
boolean result = true; |
92 |
0 |
if (regexPattern != null && regexPattern.length() > 0) { |
93 |
0 |
result = sftpFile.getFilename().matches(getRegexPattern()); |
94 |
|
} |
95 |
0 |
return result; |
96 |
|
} |
97 |
|
|
98 |
|
public boolean isRecursive() { |
99 |
0 |
return recursive; |
100 |
|
} |
101 |
|
|
102 |
|
public void setRecursive(boolean recursive) { |
103 |
0 |
this.recursive = recursive; |
104 |
0 |
} |
105 |
|
|
106 |
|
public long getLastPollTime() { |
107 |
0 |
return lastPollTime; |
108 |
|
} |
109 |
|
|
110 |
|
public void setLastPollTime(long lastPollTime) { |
111 |
0 |
this.lastPollTime = lastPollTime; |
112 |
0 |
} |
113 |
|
|
114 |
|
public String getRegexPattern() { |
115 |
0 |
return regexPattern; |
116 |
|
} |
117 |
|
|
118 |
|
public void setRegexPattern(String regexPattern) { |
119 |
0 |
this.regexPattern = regexPattern; |
120 |
0 |
} |
121 |
|
} |
122 |
|
|