public void get(String source, File destination) throws IOException { fireTransferInitiated(getResource(source), TransferEvent.REQUEST_GET); ChannelSftp c = getSftpChannel(source); try { String path = getPath(source); c.get(path, destination.getAbsolutePath(), new MyProgressMonitor()); } catch (SftpException e) { IOException ex = new IOException( "impossible to get " + source + " on " + getHost() + (e.getMessage() != null ? ": " + e.getMessage() : "")); ex.initCause(e); throw ex; } catch (URISyntaxException e) { IOException ex = new IOException( "impossible to get " + source + " on " + getHost() + (e.getMessage() != null ? ": " + e.getMessage() : "")); ex.initCause(e); throw ex; } }
public InputStream openStream(SFTPResource resource) throws IOException { ChannelSftp c = getSftpChannel(resource.getName()); try { String path = getPath(resource.getName()); return c.get(path); } catch (SftpException e) { IOException ex = new IOException( "impossible to open stream for " + resource + " on " + getHost() + (e.getMessage() != null ? ": " + e.getMessage() : "")); ex.initCause(e); throw ex; } catch (URISyntaxException e) { IOException ex = new IOException( "impossible to open stream for " + resource + " on " + getHost() + (e.getMessage() != null ? ": " + e.getMessage() : "")); ex.initCause(e); throw ex; } }
/** Returns last line of output from the command */ public SampleResult sample(Entry e) { SampleResult res = new SampleResult(); res.setSampleLabel( getName() + ":(" + getUsername() + "@" + getHostname() + ":" + getPort() + ")"); // Set up sampler return types res.setSamplerData(action + " " + source); res.setDataType(SampleResult.TEXT); res.setContentType("text/plain"); String response; if (getSession() == null) { connect(); } try { if (getSession() == null) { log.error( "Failed to connect to server with credentials " + getUsername() + "@" + getHostname() + ":" + getPort() + " pw=" + getPassword()); throw new NullPointerException("Failed to connect to server: " + getFailureReason()); } response = doFileTransfer(getSession(), source, destination, res); res.setResponseData(response.getBytes()); res.setSuccessful(true); res.setResponseMessageOK(); } catch (JSchException e1) { res.setSuccessful(false); res.setResponseCode("JSchException"); res.setResponseMessage(e1.getMessage()); } catch (SftpException e1) { res.setSuccessful(false); res.setResponseCode("SftpException"); res.setResponseMessage(e1.getMessage()); } catch (IOException e1) { res.setSuccessful(false); res.setResponseCode("IOException"); res.setResponseMessage(e1.getMessage()); } catch (NullPointerException e1) { res.setSuccessful(false); res.setResponseCode("Connection Failed"); res.setResponseMessage(e1.getMessage()); } finally { // Try a disconnect/sesson = null here instead of in finalize. disconnect(); setSession(null); } return res; }
protected boolean fastExistsFile(String name) throws GenericFileOperationFailedException { LOG.trace("fastExistsFile({})", name); try { @SuppressWarnings("rawtypes") Vector files = channel.ls(name); if (files == null) { return false; } return files.size() >= 1; } catch (SftpException e) { // or an exception can be thrown with id 2 which means file does not exists if (ChannelSftp.SSH_FX_NO_SUCH_FILE == e.id) { return false; } // otherwise its a more serious error so rethrow throw new GenericFileOperationFailedException(e.getMessage(), e); } }
public boolean existsFile(String name) throws GenericFileOperationFailedException { LOG.trace("existsFile({})", name); if (endpoint.isFastExistsCheck()) { return fastExistsFile(name); } // check whether a file already exists String directory = FileUtil.onlyPath(name); if (directory == null) { // assume current dir if no path could be extracted directory = "."; } String onlyName = FileUtil.stripPath(name); try { @SuppressWarnings("rawtypes") Vector files = channel.ls(directory); // can return either null or an empty list depending on FTP servers if (files == null) { return false; } for (Object file : files) { ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) file; String existing = entry.getFilename(); LOG.trace("Existing file: {}, target file: {}", existing, name); existing = FileUtil.stripPath(existing); if (existing != null && existing.equals(onlyName)) { return true; } } return false; } catch (SftpException e) { // or an exception can be thrown with id 2 which means file does not exists if (ChannelSftp.SSH_FX_NO_SUCH_FILE == e.id) { return false; } // otherwise its a more serious error so rethrow throw new GenericFileOperationFailedException(e.getMessage(), e); } }
public void put(File source, String destination, boolean overwrite) throws IOException { fireTransferInitiated(getResource(destination), TransferEvent.REQUEST_PUT); ChannelSftp c = getSftpChannel(destination); try { String path = getPath(destination); if (!overwrite && checkExistence(path, c)) { throw new IOException("destination file exists and overwrite == false"); } if (path.indexOf('/') != -1) { mkdirs(path.substring(0, path.lastIndexOf('/')), c); } c.put(source.getAbsolutePath(), path, new MyProgressMonitor()); } catch (SftpException e) { IOException ex = new IOException(e.getMessage()); ex.initCause(e); throw ex; } catch (URISyntaxException e) { IOException ex = new IOException(e.getMessage()); ex.initCause(e); throw ex; } }