1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package com.mindtree.techworks.insight.download;
26
27 import java.io.File;
28 import java.io.FileNotFoundException;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.net.PasswordAuthentication;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import com.mindtree.techworks.insight.receiver.WildCardMatcher;
36 import com.sshtools.j2ssh.SftpClient;
37 import com.sshtools.j2ssh.SshClient;
38 import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
39 import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
40 import com.sshtools.j2ssh.configuration.SshConnectionProperties;
41 import com.sshtools.j2ssh.sftp.SftpFile;
42
43
44
45
46
47
48
49
50
51 public class SFTPRemoteClient extends RemoteClient {
52
53
54
55
56
57 static {
58
59 RemoteClientFactory.registerClient("SFTP_CLIENT",
60 SFTPRemoteClient.class);
61 }
62
63
64
65
66
67
68
69
70 public static final PasswordAuthentication anonPassAuth = new PasswordAuthentication(
71 "anonymous", "insight@mindtree.com".toCharArray());
72
73
74
75
76
77
78
79
80 private SshClient sshClient;
81
82
83
84
85 SFTPFileset fileset;
86
87
88
89
90 private SftpClient sftpClient;
91
92
93
94
95
96
97
98
99 protected void setFileset (Fileset fileset) throws RemoteClientException {
100
101 if (fileset.getType() != Fileset.SFTP_FILESET
102 || !(fileset instanceof SFTPFileset)) {
103 throw new RemoteClientException(
104 "This client can handle only SFTPFileset");
105 }
106 this.fileset = (SFTPFileset) fileset;
107
108 }
109
110
111
112
113
114
115
116
117 protected String[] downloadFile (String fileName)
118 throws RemoteClientException {
119
120 isBusy = true;
121 checkConnection();
122
123 List matchingFileNames = new ArrayList(5);
124
125 List downloadedFiles = new ArrayList(5);
126 List remoteFiles;
127 File destinationFile = null;
128 FileOutputStream fos = null;
129
130 if(WildCardMatcher.hasWildCardEntries(fileName)){
131 int fileSeperatorIndex = (fileName.lastIndexOf('\\') == -1) ? fileName.lastIndexOf('/') : fileName.lastIndexOf('\\');
132 String remoteDirectory = (fileName.substring(0, fileSeperatorIndex));
133 String pattern = (fileName.substring(fileSeperatorIndex + 1));
134 try{
135
136 remoteFiles = sftpClient.ls(remoteDirectory);
137
138 matchingFileNames = getMatchingFiles(remoteFiles, pattern);
139 }
140 catch(IOException ie){
141 throw new RemoteClientException("Could not read remote fileset", ie);
142 }
143 }
144 else{
145 matchingFileNames.add(fileName);
146 }
147
148 for(int i = 0; i < matchingFileNames.size(); i++){
149 fileName = (String)matchingFileNames.get(i);
150
151 try {
152 destinationFile = getDestinationFile(fileName, '/');
153 fos = new FileOutputStream(destinationFile);
154 } catch (FileNotFoundException e) {
155
156 throw new RemoteClientException(
157 "Could not write to temporary file.", e);
158 } catch (IOException e) {
159 throw new RemoteClientException("Could not create temporary file.",
160 e);
161 }
162
163 try {
164
165 sftpClient.get(fileName, fos);
166 downloadedFiles.add(destinationFile.getAbsolutePath());
167
168 } catch (IOException e) {
169 throw new RemoteClientException("Could not download file", e);
170 } finally {
171 isBusy = false;
172
173 try {
174
175 fos.close();
176 } catch (IOException e1) {
177
178 }
179 }
180 }
181
182 String[] downloadedFileNames = new String[downloadedFiles.size()];
183 for(int i = 0; i < downloadedFiles.size(); i++){
184 downloadedFileNames[i] = (String)downloadedFiles.get(i);
185 }
186
187 return downloadedFileNames;
188 }
189
190
191
192
193
194
195
196 private List getMatchingFiles(List remoteFiles, String pattern){
197
198 List matchingFiles = new ArrayList(5);
199 for(int i = 0; i < remoteFiles.size(); i++){
200 String filePath = ((SftpFile)remoteFiles.get(i)).getAbsolutePath();
201 String fileName = ((SftpFile)remoteFiles.get(i)).getFilename();
202
203 WildCardMatcher matcher = new WildCardMatcher(pattern);
204 if(matcher.matches(fileName)){
205 matchingFiles.add(filePath);
206 }
207 else{
208 }
209 }
210 return matchingFiles;
211 }
212
213
214
215
216 protected void closeConnection () throws RemoteClientException {
217
218 if (null != sshClient && sshClient.isConnected()) {
219 try {
220 sftpClient.quit();
221 } catch (IOException e) {
222
223 e.printStackTrace(System.err);
224 }
225 sshClient.disconnect();
226 }
227
228 }
229
230
231
232
233
234
235
236
237
238
239 private synchronized void createSftpConnection ()
240 throws RemoteClientException {
241
242 sshClient = new SshClient();
243
244 String host = fileset.getHost();
245 int port = fileset.getPort();
246 String startDirectory = fileset.getDefaultDirectory();
247 PasswordAuthentication passwordAuthentication = fileset
248 .getPasswordAuthentication();
249
250
251 if (null == passwordAuthentication) {
252 passwordAuthentication = anonPassAuth;
253 }
254
255
256 SshConnectionProperties sshConnectionProperties = new SshConnectionProperties();
257 sshConnectionProperties.setHost(host);
258
259
260
261 if (port != -1) {
262 sshConnectionProperties.setPort(port);
263 }
264
265
266
267 try {
268 sshClient.connect(sshConnectionProperties,
269 new SshKnownHostKeyVerification(insight));
270
271
272
273
274 PasswordAuthenticationClient passwordAuthenticationClient = new PasswordAuthenticationClient();
275 passwordAuthenticationClient.setUsername(passwordAuthentication
276 .getUserName());
277 passwordAuthenticationClient.setPassword(new StringBuffer().append(
278 passwordAuthentication.getPassword()).toString());
279
280 if (sshClient.authenticate(passwordAuthenticationClient) != AuthenticationProtocolState.COMPLETE) {
281 throw new RemoteClientException(
282 "Could not complete authentication!");
283 }
284
285 sftpClient = sshClient.openSftpClient();
286
287
288 if (null != startDirectory && startDirectory.length() > 0) {
289 sftpClient.cd(startDirectory);
290 }
291
292 } catch (IOException e) {
293 throw new RemoteClientException("Cannot open connection", e);
294 }
295 }
296
297
298
299
300
301
302
303
304 private void checkConnection () throws RemoteClientException {
305
306 if (null == sshClient || !sshClient.isConnected()) {
307 createSftpConnection();
308 }
309 }
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399 }