001: /*
002: * This program is free software; you can redistribute it and/or
003: * modify it under the terms of the GNU General Public License
004: * as published by the Free Software Foundation; either version 2
005: * of the License, or (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package net.sf.jftp.net;
017:
018: import java.io.BufferedInputStream;
019: import java.io.BufferedOutputStream;
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.StreamTokenizer;
026: import java.util.Date;
027: import java.util.Enumeration;
028: import java.util.Vector;
029:
030: import javax.swing.JLabel;
031:
032: import net.sf.jftp.config.Settings;
033: import net.sf.jftp.system.StringUtils;
034: import net.sf.jftp.system.logging.Log;
035:
036: import com.sshtools.common.hosts.DialogHostKeyVerification;
037: import com.sshtools.j2ssh.SshClient;
038: import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
039: import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
040: import com.sshtools.j2ssh.authentication.PublicKeyAuthenticationClient;
041: import com.sshtools.j2ssh.configuration.SshConnectionProperties;
042: import com.sshtools.j2ssh.sftp.SftpFile;
043: import com.sshtools.j2ssh.sftp.SftpFileInputStream;
044: import com.sshtools.j2ssh.sftp.SftpFileOutputStream;
045: import com.sshtools.j2ssh.sftp.SftpSubsystemClient;
046: import com.sshtools.j2ssh.transport.publickey.SshPrivateKey;
047: import com.sshtools.j2ssh.transport.publickey.SshPrivateKeyFile;
048:
049: public class SftpConnection implements BasicConnection {
050: public static int smbBuffer = 32000;
051: private String path = "";
052: private String pwd = "/";
053: private Vector listeners = new Vector();
054: private String[] files;
055: private String[] size = new String[0];
056: private int[] perms = null;
057: private String user;
058: private String pass;
059: private String host;
060: private String baseFile;
061: private int fileCount;
062: private boolean isDirUpload = false;
063: private boolean shortProgress = false;
064: private int RW = SftpSubsystemClient.OPEN_CREATE
065: | SftpSubsystemClient.OPEN_WRITE;
066: private int W = SftpSubsystemClient.OPEN_CREATE;
067: private int R = SftpSubsystemClient.OPEN_READ;
068: private SftpSubsystemClient sftp = null;
069: private int port = 22;
070: private boolean connected = false;
071: private SshClient ssh = null;
072: private String keyfile = null;
073:
074: //private int timeout = 30000;
075:
076: //private SessionChannelClient session = null;
077: private SshConnectionProperties properties = new SshConnectionProperties();
078:
079: public SftpConnection(SshConnectionProperties properties) {
080: this .properties = properties;
081: this .host = properties.getHost();
082: this .port = properties.getPort();
083: }
084:
085: public SftpConnection(SshConnectionProperties properties,
086: String keyfile) {
087: this .properties = properties;
088: this .keyfile = keyfile;
089: this .host = properties.getHost();
090: this .port = properties.getPort();
091: }
092:
093: // seem to be broken, use the newer constructors
094: /*
095: public SftpConnection(String host)
096: {
097: this.host = host;
098: properties.setHost(host);
099: }
100:
101: public SftpConnection(String host, int port)
102: {
103: this.host = host;
104: this.port = port;
105: properties.setHost(host);
106: properties.setPort(port);
107: }
108: */
109:
110: private boolean login() {
111: try {
112: ssh = new SshClient();
113: //ssh.setSocketTimeout(timeout);
114: //ssh.setKexTimeout(timeout);
115:
116: Log.debug("Host: " + properties.getHost() + ":"
117: + properties.getPort());
118:
119: if (!Settings.getEnableSshKeys()) {
120: ssh.connect(properties,//new IgnoreHostKeyVerification());
121: new SftpVerification(
122: Settings.sshHostKeyVerificationFile));
123: } else {
124: ssh.connect(properties, new DialogHostKeyVerification(
125: new JLabel()));
126: }
127:
128: int result = -1;
129:
130: if (keyfile == null) {
131: //ssh.connect(host);
132: PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
133: pwd.setUsername(user);
134: pwd.setPassword(pass);
135:
136: result = ssh.authenticate(pwd);
137: } else {
138: Log.out("keyfile-auth: " + keyfile);
139:
140: PublicKeyAuthenticationClient pk = new PublicKeyAuthenticationClient();
141: pk.setUsername(user);
142:
143: SshPrivateKeyFile file = SshPrivateKeyFile
144: .parse(new File(keyfile));
145: SshPrivateKey key = file.toPrivateKey(pass);
146:
147: pk.setKey(key);
148: result = ssh.authenticate(pk);
149: }
150:
151: if (result == AuthenticationProtocolState.COMPLETE) {
152: //session = ssh.openSessionChannel();
153: //boolean ok = session.startSubsystem("sftp");
154: sftp = new SftpSubsystemClient();
155:
156: boolean ok = ssh.openChannel(sftp);
157:
158: boolean ok2 = sftp.startSubsystem();
159:
160: //Log.out("sftp sub ok: "+ok+" and "+ok2);
161: // newer api
162: //SftpClient sftp = ssh.openSftpClient();
163: connected = true;
164:
165: return true;
166: } else {
167: return false;
168: }
169: } catch (Exception ex) {
170: ex.printStackTrace();
171: Log.debug("Error: " + ex);
172:
173: return false;
174: }
175: }
176:
177: public int removeFileOrDir(String file) {
178: file = toSFTP(file);
179:
180: try {
181: SftpFile f;
182:
183: if (!file.endsWith("/")) {
184: Log.out(">>>>>>>> remove file: " + file);
185: f = sftp.openFile(file, RW);
186: sftp.removeFile(file);
187: //f.delete();
188: } else {
189: Log.out(">>>>>>>> remove dir: " + file);
190: f = sftp.openDirectory(file);
191: cleanSftpDir(file, f);
192: sftp.closeFile(f);
193: sftp.removeDirectory(file);
194: }
195: } catch (Exception ex) {
196: ex.printStackTrace();
197: Log.debug("Removal failed (" + ex + ").");
198: ex.printStackTrace();
199:
200: return -1;
201: }
202:
203: return 1;
204: }
205:
206: private void cleanSftpDir(String dir, SftpFile f2) throws Exception {
207: Log.out(">>>>>>>> cleanSftpDir: " + dir);
208:
209: Vector v = new Vector();
210:
211: while (sftp.listChildren(f2, v) > 0) {
212: ;
213: }
214:
215: String[] tmp = new String[v.size()];
216: SftpFile[] f = new SftpFile[v.size()];
217: Enumeration e = v.elements();
218: int x = 0;
219:
220: while (e.hasMoreElements()) {
221: f[x] = ((SftpFile) e.nextElement());
222: tmp[x] = f[x].getFilename();
223:
224: //Log.out("sftp delete: " + tmp[x]);
225: //Log.out(">>>>>>>> remove file/dir: " + tmp[x]);
226: if (f[x].isDirectory() && !tmp[x].endsWith("/")) {
227: tmp[x] = tmp[x] + "/";
228: }
229:
230: //sftp.closeFile(f[x]);
231: x++;
232: }
233:
234: if (tmp == null) {
235: return;
236: }
237:
238: for (int i = 0; i < tmp.length; i++) {
239: if (tmp[i].equals("./") || tmp[i].equals("../")) {
240: continue;
241: }
242:
243: SftpFile f3;
244:
245: //System.out.println(dir+tmp[i]);
246: if (tmp[i].endsWith("/")) {
247: f3 = sftp.openDirectory(dir + tmp[i]);
248: } else {
249: f3 = sftp.openFile(dir + tmp[i], RW);
250: }
251:
252: Log.out(">>>>>>>> remove file/dir: " + dir + tmp[i]);
253:
254: if (f3.isDirectory()) {
255: cleanSftpDir(dir + tmp[i], f3);
256:
257: //sftp.closeFile(f3);
258: sftp.removeDirectory(dir + tmp[i]);
259: } else {
260: //f3.delete();
261: sftp.removeFile(dir + tmp[i]);
262: }
263: }
264: }
265:
266: public void sendRawCommand(String cmd) {
267: }
268:
269: public void disconnect() {
270: try {
271: //sftp.stop();
272: //session.close();
273: ssh.disconnect();
274: } catch (Exception e) {
275: e.printStackTrace();
276: Log.debug("SftpSshClient.disconnect()" + e);
277: }
278:
279: connected = false;
280: }
281:
282: public boolean isConnected() {
283: return connected;
284: }
285:
286: public String getPWD() {
287: //Log.debug("PWD: " + pwd);
288: return toSFTPDir(pwd);
289: }
290:
291: public boolean mkdir(String dirName) {
292: try {
293: if (!dirName.endsWith("/")) {
294: dirName = dirName + "/";
295: }
296:
297: dirName = toSFTP(dirName);
298:
299: sftp.makeDirectory(dirName);
300:
301: fireDirectoryUpdate();
302:
303: return true;
304: } catch (Exception ex) {
305: Log.debug("Failed to create directory (" + ex + ").");
306:
307: return false;
308: }
309: }
310:
311: public void list() throws IOException {
312: }
313:
314: public boolean chdir(String p) {
315: return chdir(p, true);
316: }
317:
318: public boolean chdir(String p, boolean refresh) {
319: String tmp = toSFTP(p);
320:
321: try {
322: if (!tmp.endsWith("/")) {
323: tmp = tmp + "/";
324: }
325:
326: if (tmp.endsWith("../")) {
327: return cdup();
328: }
329:
330: // Log.out("sftp path: "+tmp);
331: SftpFile f = sftp.openDirectory(tmp);
332:
333: //Log.out("sftp after path...");
334: //if(!f.isDirectory()) return false;
335: sftp.closeFile(f);
336:
337: pwd = toSFTP(f.getAbsolutePath());
338:
339: //Log.debug("chdir: " + getPWD());
340: if (refresh) {
341: fireDirectoryUpdate();
342: }
343:
344: //System.out.println("chdir2: " + getPWD());
345: //Log.debug("Changed path to: " + tmp);
346: return true;
347: } catch (Exception ex) {
348: ex.printStackTrace();
349:
350: //System.out.println(tmp);
351: Log.debug("Could not change directory (" + ex + ").");
352:
353: return false;
354: }
355: }
356:
357: public boolean cdup() {
358: String tmp = pwd;
359:
360: if (pwd.endsWith("/")) {
361: tmp = pwd.substring(0, pwd.lastIndexOf("/"));
362: }
363:
364: return chdir(tmp.substring(0, tmp.lastIndexOf("/") + 1));
365: }
366:
367: public boolean chdirNoRefresh(String p) {
368: return chdir(p, false);
369: }
370:
371: public String getLocalPath() {
372: return path;
373: }
374:
375: public boolean setLocalPath(String p) {
376: if (StringUtils.isRelative(p)) {
377: p = path + p;
378: }
379:
380: p = p.replace('\\', '/');
381:
382: //System.out.println(", local 2:" + p);
383: File f = new File(p);
384:
385: if (f.exists()) {
386: try {
387: path = f.getCanonicalPath();
388: path = path.replace('\\', '/');
389:
390: if (!path.endsWith("/")) {
391: path = path + "/";
392: }
393:
394: //System.out.println("localPath: "+path);
395: } catch (IOException ex) {
396: Log.debug("Error: can not get pathname (local)!");
397:
398: return false;
399: }
400: } else {
401: Log.debug("(local) No such path: \"" + p + "\"");
402:
403: return false;
404: }
405:
406: return true;
407: }
408:
409: public String[] sortLs() {
410: try {
411: String t = getPWD();
412:
413: SftpFile fx = sftp.openDirectory(t);
414:
415: //if(fx == null) System.out.println("Sftp: fx null");
416: Vector v = new Vector();
417:
418: while (sftp.listChildren(fx, v) > 0) {
419: ;
420: }
421:
422: String[] tmp = new String[v.size()];
423: SftpFile[] f = new SftpFile[v.size()];
424: files = new String[tmp.length];
425: size = new String[tmp.length];
426: perms = new int[tmp.length];
427:
428: Enumeration e = v.elements();
429: int x = 0;
430:
431: while (e.hasMoreElements()) {
432: f[x] = ((SftpFile) e.nextElement());
433: tmp[x] = f[x].getFilename();
434:
435: size[x] = f[x].getAttributes().getSize().toString();
436:
437: //if(f[x].canWrite()) Log.debug(tmp[x]);
438: //if(f[x].canWrite()) perms[x] = FtpConnection.W;
439: //else
440: if (!f[x].canRead()) {
441: perms[x] = FtpConnection.DENIED;
442: } else {
443: perms[x] = FtpConnection.R;
444: }
445:
446: //Log.debugRaw(".");
447: if (f[x].isDirectory() && !tmp[x].endsWith("/")) {
448: tmp[x] = tmp[x] + "/";
449: }
450:
451: x++;
452: }
453:
454: sftp.closeFile(fx);
455:
456: for (int i = 0; i < tmp.length; i++) {
457: files[i] = tmp[i];
458: }
459:
460: //Log.debug(" done.");
461: return files;
462: } catch (Exception ex) {
463: //ex.printStackTrace();
464: //Log.debug(" Error while listing directory: " + ex);
465: return new String[0];
466: }
467: }
468:
469: public String[] sortSize() {
470: return size;
471: }
472:
473: public int[] getPermissions() {
474: return perms;
475: }
476:
477: public int handleUpload(String f) {
478: if (Settings.getEnableSftpMultiThreading()) {
479: SftpTransfer t = new SftpTransfer(properties,
480: getLocalPath(), getPWD(), f, user, pass, listeners,
481: Transfer.UPLOAD, keyfile);
482: } else {
483: upload(f);
484: }
485:
486: return 0;
487: }
488:
489: public int handleDownload(String f) {
490: if (Settings.getEnableSftpMultiThreading()) {
491: SftpTransfer t = new SftpTransfer(properties,
492: getLocalPath(), getPWD(), f, user, pass, listeners,
493: Transfer.DOWNLOAD, keyfile);
494: } else {
495: download(f);
496: }
497:
498: return 0;
499: }
500:
501: public int upload(String f) {
502: String file = toSFTP(f);
503:
504: if (file.endsWith("/")) {
505: String out = StringUtils.getDir(file);
506: uploadDir(file, getLocalPath() + out);
507: fireActionFinished(this );
508: } else {
509: String outfile = StringUtils.getFile(file);
510:
511: //System.out.println("transfer: " + file + ", " + getLocalPath() + outfile);
512: work(getLocalPath() + outfile, file, true);
513: fireActionFinished(this );
514: }
515:
516: return 0;
517: }
518:
519: public int download(String f) {
520: String file = toSFTP(f);
521:
522: if (file.endsWith("/")) {
523: String out = StringUtils.getDir(file);
524: downloadDir(file, getLocalPath() + out);
525: fireActionFinished(this );
526: } else {
527: String outfile = StringUtils.getFile(file);
528:
529: //System.out.println("transfer: " + file + ", " + getLocalPath() + outfile);
530: work(file, getLocalPath() + outfile, false);
531: fireActionFinished(this );
532: }
533:
534: return 0;
535: }
536:
537: private void downloadDir(String dir, String out) {
538: try {
539: //System.out.println("downloadDir: " + dir + "," + out);
540: fileCount = 0;
541: shortProgress = true;
542: baseFile = StringUtils.getDir(dir);
543:
544: SftpFile f2 = sftp.openDirectory(dir);
545:
546: Vector v = new Vector();
547:
548: while (sftp.listChildren(f2, v) > 0) {
549: ;
550: }
551:
552: String[] tmp = new String[v.size()];
553: SftpFile[] f = new SftpFile[v.size()];
554: Enumeration e = v.elements();
555: int x = 0;
556:
557: while (e.hasMoreElements()) {
558: f[x] = ((SftpFile) e.nextElement());
559: tmp[x] = f[x].getFilename();
560: Log.debugRaw(".");
561:
562: if (f[x].isDirectory() && !tmp[x].endsWith("/")) {
563: tmp[x] = tmp[x] + "/";
564: }
565:
566: //sftp.closeFile(f[x]);
567: x++;
568: }
569:
570: //sftp.closeFile(f2);
571: File fx = new File(out);
572: fx.mkdir();
573:
574: for (int i = 0; i < tmp.length; i++) {
575: if (tmp[i].equals("./") || tmp[i].equals("../")) {
576: continue;
577: }
578:
579: tmp[i] = tmp[i].replace('\\', '/');
580:
581: //System.out.println("1: " + dir+tmp[i] + ", " + out +tmp[i]);
582: SftpFile f3 = sftp.openFile(dir + tmp[i], R);
583:
584: if (f3.isDirectory()) {
585: if (!tmp[i].endsWith("/")) {
586: tmp[i] = tmp[i] + "/";
587: }
588:
589: downloadDir(dir + tmp[i], out + tmp[i]);
590: } else {
591: fileCount++;
592: fireProgressUpdate(baseFile, DataConnection.GETDIR
593: + ":" + fileCount, -1);
594: work(dir + tmp[i], out + tmp[i], false);
595: }
596:
597: //sftp.closeFile(f3);
598: }
599:
600: fireProgressUpdate(baseFile, DataConnection.DFINISHED + ":"
601: + fileCount, -1);
602: } catch (Exception ex) {
603: ex.printStackTrace();
604: System.out.println(dir + ", " + out);
605: Log.debug("Transfer error: " + ex);
606: fireProgressUpdate(baseFile, DataConnection.FAILED + ":"
607: + fileCount, -1);
608: }
609:
610: shortProgress = false;
611: }
612:
613: private void uploadDir(String dir, String out) {
614: try {
615: //System.out.println("uploadDir: " + dir + "," + out);
616: isDirUpload = true;
617: fileCount = 0;
618: shortProgress = true;
619: baseFile = StringUtils.getDir(dir);
620:
621: File f2 = new File(out);
622: String[] tmp = f2.list();
623:
624: if (tmp == null) {
625: return;
626: }
627:
628: sftp.makeDirectory(dir);
629: sftp.changePermissions(dir, "rwxr--r--");
630:
631: for (int i = 0; i < tmp.length; i++) {
632: if (tmp[i].equals("./") || tmp[i].equals("../")) {
633: continue;
634: }
635:
636: tmp[i] = tmp[i].replace('\\', '/');
637:
638: //System.out.println("1: " + dir+tmp[i] + ", " + out +tmp[i]);
639: File f3 = new File(out + tmp[i]);
640:
641: if (f3.isDirectory()) {
642: if (!tmp[i].endsWith("/")) {
643: tmp[i] = tmp[i] + "/";
644: }
645:
646: uploadDir(dir + tmp[i], out + tmp[i]);
647: } else {
648: fileCount++;
649: fireProgressUpdate(baseFile, DataConnection.PUTDIR
650: + ":" + fileCount, -1);
651: work(out + tmp[i], dir + tmp[i], true);
652: }
653: }
654:
655: fireProgressUpdate(baseFile, DataConnection.DFINISHED + ":"
656: + fileCount, -1);
657: } catch (Exception ex) {
658: ex.printStackTrace();
659: System.out.println(dir + ", " + out);
660: Log.debug("Transfer error: " + ex);
661: fireProgressUpdate(baseFile, DataConnection.FAILED + ":"
662: + fileCount, -1);
663: }
664:
665: isDirUpload = false;
666: shortProgress = true;
667: }
668:
669: private String toSFTP(String f) {
670: String file;
671:
672: if (f.startsWith("/")) {
673: file = f;
674: } else {
675: file = getPWD() + f;
676: }
677:
678: file = file.replace('\\', '/');
679:
680: //System.out.println("file: "+file);
681: return file;
682: }
683:
684: private String toSFTPDir(String f) {
685: String file;
686:
687: if (f.startsWith("/")) {
688: file = f;
689: } else {
690: file = pwd + f;
691: }
692:
693: file = file.replace('\\', '/');
694:
695: if (!file.endsWith("/")) {
696: file = file + "/";
697: }
698:
699: //System.out.println("file: "+file);
700: return file;
701: }
702:
703: private void work(String file, String outfile, boolean up) {
704: BufferedInputStream in = null;
705: BufferedOutputStream out = null;
706:
707: try {
708: SftpFile inf = null;
709: SftpFile of = null;
710: boolean outflag = false;
711:
712: if (up) {
713: in = new BufferedInputStream(new FileInputStream(file));
714: } else {
715: inf = sftp.openFile(file, R);
716: in = new BufferedInputStream(new SftpFileInputStream(
717: inf));
718: }
719:
720: if (up) {
721: outflag = true;
722:
723: //FileAttributes fa = new FileAttributes();
724: //fa.setPermissions("rwxr--r--");
725: //fa.setSize(new UnsignedInteger64(
726: // new java.math.BigInteger(""+new File(file).length())));
727:
728: //of = sftp.openFile(outfile, W);
729: //of.delete();
730: try {
731: sftp.removeFile(outfile);
732: } catch (Exception ex) {
733:
734: }
735:
736: of = sftp.openFile(outfile, RW); // , fa);
737: sftp.changePermissions(of, "rwxr--r--");
738: out = new BufferedOutputStream(
739: new SftpFileOutputStream(of));
740: } else {
741: out = new BufferedOutputStream(new FileOutputStream(
742: outfile));
743: }
744:
745: //System.out.println("out: " + outfile + ", in: " + file);
746: byte[] buf = new byte[smbBuffer];
747: int len = 0;
748: int reallen = 0;
749:
750: //System.out.println(file+":"+getLocalPath()+outfile);
751: while (true) {
752: len = in.read(buf);
753:
754: //System.out.print(".");
755: if (len == StreamTokenizer.TT_EOF) {
756: break;
757: }
758:
759: out.write(buf, 0, len);
760: reallen += len;
761:
762: //System.out.println(file + ":" + StringUtils.getFile(file));
763: if (outflag) {
764: fireProgressUpdate(StringUtils.getFile(outfile),
765: DataConnection.PUT, reallen);
766: } else {
767: fireProgressUpdate(StringUtils.getFile(file),
768: DataConnection.GET, reallen);
769: }
770: }
771:
772: fireProgressUpdate(file, DataConnection.FINISHED, -1);
773: } catch (IOException ex) {
774: ex.printStackTrace();
775: Log.debug("Error with file IO (" + ex + ")!");
776: fireProgressUpdate(file, DataConnection.FAILED, -1);
777: } finally {
778: try {
779: out.flush();
780: out.close();
781: in.close();
782: } catch (Exception ex) {
783: ex.printStackTrace();
784: }
785: }
786: }
787:
788: public boolean rename(String oldName, String newName) {
789: try {
790: oldName = toSFTP(oldName);
791: newName = toSFTP(newName);
792:
793: SftpFile f = sftp.openFile(oldName, RW);
794: f.rename(newName);
795:
796: return true;
797: } catch (Exception ex) {
798: ex.printStackTrace();
799:
800: Log.debug("Could rename file (" + ex + ").");
801:
802: return false;
803: }
804: }
805:
806: private void update(String file, String type, int bytes) {
807: if (listeners == null) {
808: return;
809: } else {
810: for (int i = 0; i < listeners.size(); i++) {
811: ConnectionListener listener = (ConnectionListener) listeners
812: .elementAt(i);
813: listener.updateProgress(file, type, bytes);
814: }
815: }
816: }
817:
818: public void addConnectionListener(ConnectionListener l) {
819: listeners.add(l);
820: }
821:
822: public void setConnectionListeners(Vector l) {
823: listeners = l;
824: }
825:
826: /** remote directory has changed */
827: public void fireDirectoryUpdate() {
828: if (listeners == null) {
829: return;
830: } else {
831: for (int i = 0; i < listeners.size(); i++) {
832: ((ConnectionListener) listeners.elementAt(i))
833: .updateRemoteDirectory(this );
834: }
835: }
836: }
837:
838: public boolean login(String user, String pass) {
839: this .user = user;
840: this .pass = pass;
841:
842: if (!login()) {
843: Log.debug("Login failed.");
844:
845: return false;
846: } else {
847: Log.debug("Authed successfully.");
848:
849: //if(!chdir(getPWD())) chdir("/");
850: }
851:
852: return true;
853: }
854:
855: /** progress update */
856: public void fireProgressUpdate(String file, String type, int bytes) {
857: if (listeners == null) {
858: return;
859: }
860:
861: for (int i = 0; i < listeners.size(); i++) {
862: ConnectionListener listener = (ConnectionListener) listeners
863: .elementAt(i);
864:
865: if (shortProgress && Settings.shortProgress) {
866: if (type.startsWith(DataConnection.DFINISHED)) {
867: listener.updateProgress(baseFile,
868: DataConnection.DFINISHED + ":" + fileCount,
869: bytes);
870: } else if (isDirUpload) {
871: listener.updateProgress(baseFile,
872: DataConnection.PUTDIR + ":" + fileCount,
873: bytes);
874: } else {
875: listener.updateProgress(baseFile,
876: DataConnection.GETDIR + ":" + fileCount,
877: bytes);
878: }
879: } else {
880: listener.updateProgress(file, type, bytes);
881: }
882: }
883: }
884:
885: public void fireActionFinished(SftpConnection con) {
886: if (listeners == null) {
887: return;
888: } else {
889: for (int i = 0; i < listeners.size(); i++) {
890: ((ConnectionListener) listeners.elementAt(i))
891: .actionFinished(con);
892: }
893: }
894: }
895:
896: public int upload(String file, InputStream i) {
897: BufferedOutputStream out = null;
898: BufferedInputStream in = null;
899:
900: try {
901: file = toSFTP(file);
902:
903: SftpFile of = sftp.openFile(file, RW);
904:
905: //of.delete();
906: //of = sftp.openFile(file, RW);
907: sftp.changePermissions(of, "rwxr--r--");
908:
909: out = new BufferedOutputStream(new SftpFileOutputStream(of));
910: in = new BufferedInputStream(i);
911:
912: //Log.debug(getLocalPath() + ":" + file+ ":"+getPWD());
913: byte[] buf = new byte[smbBuffer];
914: int len = 0;
915: int reallen = 0;
916:
917: while (true) {
918: len = in.read(buf);
919:
920: //System.out.print(".");
921: if (len == StreamTokenizer.TT_EOF) {
922: break;
923: }
924:
925: out.write(buf, 0, len);
926: reallen += len;
927:
928: fireProgressUpdate(StringUtils.getFile(file),
929: DataConnection.PUT, reallen);
930: }
931:
932: //byte[] fin = new byte[1];
933: //fin[0] = -1;
934: //out.write(fin, 0, 1);
935:
936: /*
937: try {
938: out.flush();
939: out.close();
940: } catch(Exception ex) {
941: Log.out("flush: "+ex);
942: }
943: */
944:
945: fireProgressUpdate(file, DataConnection.FINISHED, -1);
946:
947: return 0;
948: } catch (IOException ex) {
949: ex.printStackTrace();
950: Log.debug("Error with file IO (" + ex + ")!");
951: fireProgressUpdate(file, DataConnection.FAILED, -1);
952:
953: return -1;
954: } finally {
955: try {
956: out.flush();
957: out.close();
958: in.close();
959: } catch (Exception ex) {
960: ex.printStackTrace();
961: }
962: }
963: }
964:
965: public InputStream getDownloadInputStream(String file) {
966: try {
967: file = toSFTP(file);
968:
969: SftpFile inf = sftp.openFile(file, R);
970:
971: return new SftpFileInputStream(inf);
972: } catch (IOException ex) {
973: ex.printStackTrace();
974: Log.debug(ex.toString()
975: + " @SftpConnection::getDownloadInputStream");
976:
977: return null;
978: }
979: }
980:
981: public Date[] sortDates() {
982: return null;
983: }
984: }
|