agave-systems/systems-core/src/main/java/org/iplantc/service/transfer/sftp/SftpRelay.java
/*
     * The SFTP copy method performs a remote copy on a file or folder. Unlike
     * other RemoteDataClients, this call is done purely server side and as such
     * requires no data movement. It should be nearly instantaneous. The down side
     * is that it opens a session to the remote system, so this may run into
     * trouble if the environment isn't stable.
     *
     * @param remotesrc remote source. If a folder, the contents will be copied to the {@code remotedest}
     * @param remotedest remote destination. If a folder, it will receive the contents of the {@code remotesrc}
     * @param listener The listener to update. This will be updated on start and
     * finish with no updated inbetween.
     * @throws IOException, RemoteDataException
     */
The SFTP copy method performs a remote copy on a file or folder. Unlike
other RemoteDataClients, this call is done purely server side and as such
requires no data movement. It should be nearly instantaneous. The down side
is that it opens a session to the remote system, so this may run into
trouble if the environment isn't stable.

@param remotesrc remote source. If a folder, the contents will be copied to the  remotedest
@param remotedest remote destination. If a folder, it will receive the contents of the  remotesrc
@param listener The listener to update. This will be updated on start and
finish with no updated inbetween.
The SFTP copy method performs a remote copy on a file or folder . Unlike other RemoteDataClients this call is done purely server side and as such requires no data movement . It should be nearly instantaneous . The down side is that it opens a session to the remote system so this may run into trouble if the environment isn ' t stable . @param remotesrc remote source . If a folder the contents will be copied to the remotedest @param remotedest remote destination . If a folder it will receive the contents of the remotesrc @param listener The listener to update . This will be updated on start and finish with no updated inbetween .
@Override
    public void copy(String remotesrc, String remotedest, RemoteTransferListener listener)
            throws IOException, RemoteDataException, RemoteDataSyntaxException {
        if (!doesExist(remotesrc)) {
            throw new FileNotFoundException("No such file or directory");
        }
        String resolvedSrc = resolvePath(remotesrc);
        String resolvedDest = resolvePath(remotedest);
        nativeAuthenticate();
        Shell shell = null;
        try {
            if (ssh2.isAuthenticated()) {
                long remoteDestLength = length(remotesrc);
                if (listener != null) {
                    listener.started(remoteDestLength, remotedest);
                }
                fileInfoCache.remove(resolvedDest);
                if (isDirectory(remotesrc)) {
                    resolvedSrc = StringUtils.stripEnd(resolvedSrc, "/") + "/.";
                }
                String copyCommand = String.format("cp -rLf \"%s\" \"%s\"", resolvedSrc, resolvedDest);
                MaverickSSHSubmissionClient proxySubmissionClient = null;
                String proxyResponse = null;
                try {
                    proxySubmissionClient = new MaverickSSHSubmissionClient(getHost(), port, username,
                            password, proxyHost, proxyPort, publicKey, privateKey);
                    proxyResponse = proxySubmissionClient.runCommand(copyCommand);
                } catch (RemoteDataException e) {
                    throw e;
                } catch (Exception e) {
                    throw new RemoteDataException("Failed to connect to destination server " + getHost() + ":" + port, e);
                } finally {
                    try {
                        proxySubmissionClient.close();
                    } catch (Exception ignored) {
                    }
                }
                if (proxyResponse.length() > 0) {
                    if (listener != null) {
                        listener.failed();
                    }
                    if (StringUtils.containsIgnoreCase(proxyResponse, "No such file or directory")) {
                        throw new FileNotFoundException("No such file or directory");
                    } else if (StringUtils.startsWithIgnoreCase(proxyResponse, "cp:")) {
                        throw new RemoteDataException("Copy failure: " + proxyResponse.substring(3));
                    } else {
                        throw new RemoteDataException("Failed to perform a remote copy command on " + host + ". " +
                                proxyResponse);
                    }
                } else {
                    if (listener != null) {
                        listener.progressed(remoteDestLength);
                        listener.completed();
                    }
                }
            } else {
                throw new RemoteDataException("Failed to authenticate to remote host");
            }
        } catch (FileNotFoundException | RemoteDataException e) {
            throw e;
        } catch (Throwable t) {
            throw new RemoteDataException("Failed to perform a remote copy command on " + host, t);
        } finally {
            try {
                shell.close();
            } catch (Throwable ignored) {
            }
        }
    }
@ Override public void copy ( String remotesrc , String remotedest , RemoteTransferListener listener ) throws IOException , RemoteDataException , RemoteDataSyntaxException { if ( ! doesExist ( remotesrc ) ) { throw new FileNotFoundException ( "No such file or directory" ) ; } String resolvedSrc = resolvePath ( remotesrc ) ; String resolvedDest = resolvePath ( remotedest ) ; nativeAuthenticate ( ) ; Shell shell = null ; try { if ( ssh2 . isAuthenticated ( ) ) { long remoteDestLength = length ( remotesrc ) ; if ( listener != null ) { listener . started ( remoteDestLength , remotedest ) ; } fileInfoCache . remove ( resolvedDest ) ; if ( isDirectory ( remotesrc ) ) { resolvedSrc = StringUtils . stripEnd ( resolvedSrc , "/" ) + "/." ; } String copyCommand = String . format ( "cp -rLf \"%s\" \"%s\"" , resolvedSrc , resolvedDest ) ; MaverickSSHSubmissionClient proxySubmissionClient = null ; String proxyResponse = null ; try { proxySubmissionClient = new MaverickSSHSubmissionClient ( getHost ( ) , port , username , password , proxyHost , proxyPort , publicKey , privateKey ) ; proxyResponse = proxySubmissionClient . runCommand ( copyCommand ) ; } catch ( RemoteDataException e ) { throw e ; } catch ( Exception e ) { throw new RemoteDataException ( "Failed to connect to destination server " + getHost ( ) + ":" + port , e ) ; } finally { try { proxySubmissionClient . close ( ) ; } catch ( Exception ignored ) { } } if ( proxyResponse . length ( ) > 0 ) { if ( listener != null ) { listener . failed ( ) ; } if ( StringUtils . containsIgnoreCase ( proxyResponse , "No such file or directory" ) ) { throw new FileNotFoundException ( "No such file or directory" ) ; } else if ( StringUtils . startsWithIgnoreCase ( proxyResponse , "cp:" ) ) { throw new RemoteDataException ( "Copy failure: " + proxyResponse . substring ( 3 ) ) ; } else { throw new RemoteDataException ( "Failed to perform a remote copy command on " + host + ". " + proxyResponse ) ; } } else { if ( listener != null ) { listener . progressed ( remoteDestLength ) ; listener . completed ( ) ; } } } else { throw new RemoteDataException ( "Failed to authenticate to remote host" ) ; } } catch ( FileNotFoundException | RemoteDataException e ) { throw e ; } catch ( Throwable t ) { throw new RemoteDataException ( "Failed to perform a remote copy command on " + host , t ) ; } finally { try { shell . close ( ) ; } catch ( Throwable ignored ) { } } }
The SFTP copy method performs a remote copy on a file or folder.
The SFTP copy method performs a remote copy on a file or folder .
// ensure remote path exists before attempting the copy // ensure the native streaming client is authenticated in case this is the first // use of the native maverick library. //                log.debug("Performing remote copy on " + host + ": " + copyCommand); // We use the heuristic that a copy failure due to invalid // user input produces a message that begins with 'cp:'.