Author: chatellier Date: 2009-10-27 17:50:23 +0000 (Tue, 27 Oct 2009) New Revision: 2705 Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/InvalidPassphraseException.java isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHAgent.java isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHException.java isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHUserInfo.java isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHUtils.java isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/package-info.java Log: Code refactoring for scp to prepare following sftp commit. Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/InvalidPassphraseException.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/InvalidPassphraseException.java 2009-10-27 17:05:06 UTC (rev 2704) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/InvalidPassphraseException.java 2009-10-27 17:50:23 UTC (rev 2705) @@ -19,13 +19,13 @@ package fr.ifremer.isisfish.util.ssh; /** - * TODO COMMENT ME. + * Exception throws for password input cancellation. * * @author chatellier - * @version $Revision: 1.0 $ + * @version $Revision$ * - * Last update : $Date: 1 avr. 2009 $ - * By : $Author: chatellier $ + * Last update : $Date$ + * By : $Author$ */ public class InvalidPassphraseException extends Exception { Property changes on: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/InvalidPassphraseException.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHAgent.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHAgent.java 2009-10-27 17:05:06 UTC (rev 2704) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHAgent.java 2009-10-27 17:50:23 UTC (rev 2705) @@ -41,10 +41,10 @@ * This is a singleton class. * * @author chatellier - * @version $Revision: 1.0 $ + * @version $Revision$ * - * Last update : $Date: 1 avr. 2009 $ - * By : $Author: chatellier $ + * Last update : $Date$ + * By : $Author$ */ public class SSHAgent { Property changes on: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHAgent.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHException.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHException.java 2009-10-27 17:05:06 UTC (rev 2704) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHException.java 2009-10-27 17:50:23 UTC (rev 2705) @@ -22,10 +22,10 @@ * SSHException. * * @author chatellier - * @version $Revision: 1.0 $ + * @version $Revision$ * - * Last update : $Date: 14 janv. 2009 $ - * By : $Author: chatellier $ + * Last update : $Date$ + * By : $Author$ */ public class SSHException extends Exception { @@ -33,13 +33,6 @@ private static final long serialVersionUID = -198651402309210758L; /** - * Constructs a new exception with null as its detail message. - */ - public SSHException() { - super(); - } - - /** * Constructs a new exception with the specified detail message. * * @param message message Property changes on: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHException.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHUserInfo.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHUserInfo.java 2009-10-27 17:05:06 UTC (rev 2704) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHUserInfo.java 2009-10-27 17:50:23 UTC (rev 2705) @@ -38,10 +38,10 @@ * Password, passphrase... * * @author chatellier - * @version $Revision: 1.0 $ + * @version $Revision$ * - * Last update : $Date: 1 déc. 2008 $ - * By : $Author: chatellier $ + * Last update : $Date$ + * By : $Author$ */ public class SSHUserInfo implements UserInfo, UIKeyboardInteractive { Property changes on: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHUserInfo.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHUtils.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHUtils.java 2009-10-27 17:05:06 UTC (rev 2704) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHUtils.java 2009-10-27 17:50:23 UTC (rev 2705) @@ -49,10 +49,10 @@ * - exec command * * @author chatellier - * @version $Revision: 1.0 $ + * @version $Revision$ * - * Last update : $Date: 13 janv. 2009 $ - * By : $Author: chatellier $ + * Last update : $Date$ + * By : $Author$ */ public class SSHUtils { @@ -69,6 +69,94 @@ } /** + * Send an ack. + * @param out the output stream to use + * @throws IOException on error + */ + protected static void sendAck(OutputStream out) throws IOException { + byte[] buf = new byte[1]; + buf[0] = 0; + out.write(buf); + out.flush(); + } + + /** + * Reads the response, throws a BuildException if the response + * indicates an error. + * @param in the input stream to use + * @throws IOException on I/O error + */ + protected static void waitForAck(InputStream in) throws IOException, + SSHException { + int b = in.read(); + + // b may be 0 for success, + // 1 for error, + // 2 for fatal error, + + if (b == -1) { + // didn't receive any response + throw new SSHException("No response from server"); + } else if (b != 0) { + StringBuffer sb = new StringBuffer(); + + int c = in.read(); + while (c > 0 && c != '\n') { + sb.append((char) c); + c = in.read(); + } + + if (b == 1) { + throw new SSHException("server indicated an error: " + + sb.toString()); + } else if (b == 2) { + throw new SSHException("server indicated a fatal error: " + + sb.toString()); + } else { + throw new SSHException("unknown response, code " + b + + " message: " + sb.toString()); + } + } + } + + /** + * Track progress every 10% if 100kb < filesize < 1mb. For larger + * files track progress for every percent transmitted. + * @param filesize the size of the file been transmitted + * @param totalLength the total transmission size + * @param percentTransmitted the current percent transmitted + * @return the percent that the file is of the total + */ + protected static int trackProgress(long filesize, long totalLength, + int percentTransmitted) { + + int percent = (int) Math.round(Math + .floor((totalLength / (double) filesize) * 100)); + + if (percent > percentTransmitted) { + if (filesize < 1048576) { + if (percent % 5 == 0) { + if (percent == 100) { + System.out.println(" 100%"); + } else { + System.out.print("*"); + } + } + } else { + if (percent == 50) { + System.out.println(" 50%"); + } else if (percent == 100) { + System.out.println(" 100%"); + } else { + System.out.print("."); + } + } + } + + return percent; + } + + /** * Exec command on remote server. * * @param session opened valid session @@ -197,96 +285,7 @@ } } - /** - * Send an ack. - * @param out the output stream to use - * @throws IOException on error - */ - protected static void sendAck(OutputStream out) throws IOException { - byte[] buf = new byte[1]; - buf[0] = 0; - out.write(buf); - out.flush(); - } - - /** - * Reads the response, throws a BuildException if the response - * indicates an error. - * @param in the input stream to use - * @throws IOException on I/O error - */ - protected static void waitForAck(InputStream in) throws IOException, - SSHException { - int b = in.read(); - - // b may be 0 for success, - // 1 for error, - // 2 for fatal error, - - if (b == -1) { - // didn't receive any response - throw new SSHException("No response from server"); - } else if (b != 0) { - StringBuffer sb = new StringBuffer(); - - int c = in.read(); - while (c > 0 && c != '\n') { - sb.append((char) c); - c = in.read(); - } - - if (b == 1) { - throw new SSHException("server indicated an error: " - + sb.toString()); - } else if (b == 2) { - throw new SSHException("server indicated a fatal error: " - + sb.toString()); - } else { - throw new SSHException("unknown response, code " + b - + " message: " + sb.toString()); - } - } - } - - /** - * Track progress every 10% if 100kb < filesize < 1mb. For larger - * files track progress for every percent transmitted. - * @param filesize the size of the file been transmitted - * @param totalLength the total transmission size - * @param percentTransmitted the current percent transmitted - * @return the percent that the file is of the total - */ - protected static int trackProgress(long filesize, long totalLength, - int percentTransmitted) { - - // CheckStyle:MagicNumber OFF - int percent = (int) Math.round(Math - .floor((totalLength / (double) filesize) * 100)); - - if (percent > percentTransmitted) { - if (filesize < 1048576) { - if (percent % 5 == 0) { - if (percent == 100) { - System.out.println(" 100%"); - } else { - System.out.print("*"); - } - } - } else { - if (percent == 50) { - System.out.println(" 50%"); - } else if (percent == 100) { - System.out.println(" 100%"); - } else { - System.out.print("."); - } - } - } - // CheckStyle:MagicNumber ON - - return percent; - } - + // scp protected static void startRemoteCpProtocol(InputStream in, OutputStream out, File localFile) throws IOException, SSHException { File startFile = localFile; @@ -322,6 +321,7 @@ } } + // scp protected static File parseAndCreateDirectory(String serverResponse, File localFile) { int start = serverResponse.indexOf(" "); @@ -337,6 +337,7 @@ return null; } + // scp protected static void parseAndFetchFile(String serverResponse, File localFile, OutputStream out, InputStream in) throws IOException, SSHException { @@ -354,6 +355,7 @@ sendAck(out); } + // scp protected static void fetchFile(File localFile, long filesize, OutputStream out, InputStream in) throws IOException { byte[] buf = new byte[BUFFER_SIZE]; @@ -396,6 +398,7 @@ } } + // scp protected static void doSingleTransfer(Session session, File localFile, String remoteFilePath) throws IOException, JSchException, SSHException { @@ -417,6 +420,7 @@ } } + // scp protected static void sendFileToRemote(File localFile, InputStream in, OutputStream out) throws IOException, SSHException { // send "C0644 filesize filename", where filename should not include '/' Property changes on: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/SSHUtils.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Property changes on: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/util/ssh/package-info.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision