com.jcraft.jsch.ChannelSftp.LsEntry Java Examples

The following examples show how to use com.jcraft.jsch.ChannelSftp.LsEntry. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SFTPRepository.java    From ant-ivy with Apache License 2.0 9 votes vote down vote up
@SuppressWarnings("unchecked")
public List<String> list(String parent) throws IOException {
    try {
        ChannelSftp c = getSftpChannel(parent);
        String path = getPath(parent);
        Collection<LsEntry> r = c.ls(path);
        if (r != null) {
            if (!path.endsWith("/")) {
                path = parent + "/";
            }
            List<String> result = new ArrayList<>();
            for (LsEntry entry : r) {
                if (".".equals(entry.getFilename()) || "..".equals(entry.getFilename())) {
                    continue;
                }
                result.add(path + entry.getFilename());
            }
            return result;
        }
    } catch (SftpException | URISyntaxException e) {
        throw new IOException("Failed to return a listing for '" + parent + "'", e);
    }
    return null;
}
 
Example #2
Source File: SFtpHelper.java    From openemm with GNU Affero General Public License v3.0 7 votes vote down vote up
/**
 * Ls.
 *
 * @param path the path
 * @return the vector
 * @throws SftpException the sftp exception
 */
@Override
@SuppressWarnings("unchecked")
public List<String> ls(String path) throws Exception {
	if (StringUtils.isEmpty(path)) {
		path = ".";
	} else if (!path.trim().startsWith("/")) {
		path = "./" + path;
	}
	
	checkForConnection();
	Vector<LsEntry> returnVector = channel.ls(path);
	List<String> returnList = new ArrayList<>();
	for (LsEntry item : returnVector) {
		returnList.add(item.getFilename());
	}
	return returnList;
}
 
Example #3
Source File: SFTPTransfer.java    From localization_nifi with Apache License 2.0 7 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public FileInfo getRemoteFileInfo(final FlowFile flowFile, final String path, String filename) throws IOException {
    final ChannelSftp sftp = getChannel(flowFile);
    final String fullPath;

    if (path == null) {
        fullPath = filename;
        int slashpos = filename.lastIndexOf('/');
        if (slashpos >= 0 && !filename.endsWith("/")) {
            filename = filename.substring(slashpos + 1);
        }
    } else {
        fullPath = path + "/" + filename;
    }

    final Vector<LsEntry> vector;
    try {
        vector = sftp.ls(fullPath);
    } catch (final SftpException e) {
        // ls throws exception if filename is not present
        if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
            return null;
        } else {
            throw new IOException("Failed to obtain file listing for " + fullPath, e);
        }
    }

    LsEntry matchingEntry = null;
    for (final LsEntry entry : vector) {
        if (entry.getFilename().equalsIgnoreCase(filename)) {
            matchingEntry = entry;
            break;
        }
    }

    return newFileInfo(matchingEntry, path);
}
 
Example #4
Source File: SFTPRepository.java    From ant-ivy with Apache License 2.0 6 votes vote down vote up
/**
 * This method is similar to getResource, except that the returned resource is fully initialized
 * (resolved in the sftp repository), and that the given string is a full remote path
 *
 * @param path
 *            the full remote path in the repository of the resource
 * @return a fully initialized resource, able to answer to all its methods without needing any
 *         further connection
 */
@SuppressWarnings("unchecked")
public Resource resolveResource(String path) {
    try {
        List<LsEntry> r = getSftpChannel(path).ls(getPath(path));
        if (r != null) {
            SftpATTRS attrs = r.get(0).getAttrs();
            return new BasicResource(path, true, attrs.getSize(),
                        attrs.getMTime() * MILLIS_PER_SECOND, false);
        }
    } catch (Exception e) {
        Message.debug("Error while resolving resource " + path, e);
        // silent fail, return nonexistent resource
    }

    return new BasicResource(path, false, 0, 0, false);
}
 
Example #5
Source File: SFTPUtil.java    From xnx3 with Apache License 2.0 6 votes vote down vote up
private List<FileBean> _buildFiles(Vector ls,String dir) throws Exception {  
    if (ls != null && ls.size() >= 0) {  
        List<FileBean> list = new ArrayList<FileBean>();
        for (int i = 0; i < ls.size(); i++) {  
            LsEntry f = (LsEntry) ls.get(i);  
            String nm = f.getFilename();  
            
            if (nm.equals(".") || nm.equals(".."))  
                continue;  
            SftpATTRS attr = f.getAttrs();  
            FileBean fileBean=new FileBean();
            if (attr.isDir()) {  
                fileBean.setDir(true);
            } else {  
                fileBean.setDir(false);
            }  
            fileBean.setAttrs(attr);
            fileBean.setFilePath(dir);
            fileBean.setFileName(nm);
            list.add(fileBean);  
        }  
        return list;  
    }  
    return null;  
}
 
Example #6
Source File: SFTPImportJob.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
protected void doTransferDirectory(ChannelSftp channel, IPath remotePath, SftpATTRS remoteAttributes, File localFile) throws SftpException, IOException {
	//create the local folder on import
	localFile.mkdirs();
	@SuppressWarnings("unchecked")
	Vector<LsEntry> remoteChildren = channel.ls(remotePath.toString());
	addTaskTotal(remoteChildren.size());

	//visit remote children
	for (LsEntry remoteChild : remoteChildren) {
		String childName = remoteChild.getFilename();
		if (shouldSkip(childName)) {
			taskItemLoaded();
			continue;
		}
		File localChild = new File(localFile, childName);
		if (remoteChild.getAttrs().isDir()) {
			doTransferDirectory(channel, remotePath.append(childName), remoteChild.getAttrs(), localChild);
		} else {
			doTransferFile(channel, remotePath.append(childName), remoteChild.getAttrs(), localChild);
		}
		taskItemLoaded();
	}
	synchronizeTimestamp(remoteAttributes, localFile);
}
 
Example #7
Source File: SFTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
public SFTPInfo(LsEntry file, String name, URI parent, URI self) throws UnsupportedEncodingException {
	this.file = file;
	this.parent = parent;
	if (name.equals(URIUtils.CURRENT_DIR_NAME)) {
		name = ""; // CLO-4118
	} else if (name.endsWith(URIUtils.PATH_SEPARATOR)) {
		name = name.substring(0, name.length()-1);
	}
	this.name = name;
	if (file.getAttrs().isDir() && !name.endsWith(URIUtils.PATH_SEPARATOR)) {
		name = name + URIUtils.PATH_SEPARATOR;
	}
	if (self != null) {
		this.uri = self;
	} else {
		this.uri = URIUtils.getChildURI(parent, name);
	}
}
 
Example #8
Source File: SFTPFileSystem.java    From sftp-fs with Apache License 2.0 6 votes vote down vote up
DirectoryStream<Path> newDirectoryStream(final SFTPPath path, Filter<? super Path> filter) throws IOException {
    try (Channel channel = channelPool.get()) {
        List<LsEntry> entries = channel.listFiles(path.path());
        boolean isDirectory = false;
        for (Iterator<LsEntry> i = entries.iterator(); i.hasNext(); ) {
            LsEntry entry = i.next();
            String filename = entry.getFilename();
            if (CURRENT_DIR.equals(filename)) {
                isDirectory = true;
                i.remove();
            } else if (PARENT_DIR.equals(filename)) {
                i.remove();
            }
        }

        if (!isDirectory) {
            // https://github.com/robtimus/sftp-fs/issues/4: don't fail immediately but check the attributes
            // Follow links to ensure the directory attribute can be read correctly
            SftpATTRS attributes = channel.readAttributes(path.path(), true);
            if (!attributes.isDir()) {
                throw new NotDirectoryException(path.path());
            }
        }
        return new SFTPPathDirectoryStream(path, entries, filter);
    }
}
 
Example #9
Source File: SFtpClientUtils.java    From bamboobsc with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static Vector<LsEntry> getRemoteFileList(String user, String password, String addr, int port, String cwd) throws JSchException, 
	SftpException, Exception {
	
	Session session = getSession(user, password, addr, port);	
	Vector<LsEntry> lsVec=null;
	Channel channel = session.openChannel("sftp");
	channel.connect();
	ChannelSftp sftpChannel = (ChannelSftp) channel;
	try {
		lsVec=(Vector<LsEntry>)sftpChannel.ls(cwd); //sftpChannel.lpwd()
	} catch (Exception e) {
		e.printStackTrace();
		throw e;
	} finally {
		sftpChannel.exit();
		channel.disconnect();
		session.disconnect();			
	}		
	return lsVec;		
}
 
Example #10
Source File: SftpSinkConfiguration.java    From spring-cloud-stream-app-starters with Apache License 2.0 6 votes vote down vote up
@Bean
public IntegrationFlow ftpInboundFlow(SftpSinkProperties properties, SessionFactory<LsEntry> ftpSessionFactory) {
	SftpMessageHandlerSpec handlerSpec =
		Sftp.outboundAdapter(new SftpRemoteFileTemplate(ftpSessionFactory), properties.getMode())
			.remoteDirectory(properties.getRemoteDir())
			.remoteFileSeparator(properties.getRemoteFileSeparator())
			.autoCreateDirectory(properties.isAutoCreateDir())
			.temporaryFileSuffix(properties.getTmpFileSuffix());
	if (properties.getFilenameExpression() != null) {
		handlerSpec.fileNameExpression(properties.getFilenameExpression().getExpressionString());
	}
	return IntegrationFlows.from(Sink.INPUT)
		.handle(handlerSpec,
			new Consumer<GenericEndpointSpec<FileTransferringMessageHandler<LsEntry>>>() {
				@Override
				public void accept(GenericEndpointSpec<FileTransferringMessageHandler<LsEntry>> e) {
					e.autoStartup(false);
				}
			})
		.get();
}
 
Example #11
Source File: SFTPTransfer.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
private FileInfo newFileInfo(final LsEntry entry, String path) {
    if (entry == null) {
        return null;
    }
    final File newFullPath = new File(path, entry.getFilename());
    final String newFullForwardPath = newFullPath.getPath().replace("\\", "/");

    String perms = entry.getAttrs().getPermissionsString();
    if (perms.length() > 9) {
        perms = perms.substring(perms.length() - 9);
    }

    FileInfo.Builder builder = new FileInfo.Builder()
        .filename(entry.getFilename())
        .fullPathFileName(newFullForwardPath)
        .directory(entry.getAttrs().isDir())
        .size(entry.getAttrs().getSize())
        .lastModifiedTime(entry.getAttrs().getMTime() * 1000L)
        .permissions(perms)
        .owner(Integer.toString(entry.getAttrs().getUId()))
        .group(Integer.toString(entry.getAttrs().getGId()));
    return builder.build();
}
 
Example #12
Source File: SftpTransferManager.java    From desktop with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Map<String, RemoteFile> list() throws StorageException {
    connect();

    try {
        Map<String, RemoteFile> files = new HashMap<String, RemoteFile>();
        List<LsEntry> sftpFiles = sftp.ls(getConnection().getPath());

        for (LsEntry file: sftpFiles) {                 
            if(file.getFilename().compareTo(".") != 0 && file.getFilename().compareTo("..") != 0){
                files.put(file.getFilename(), new RemoteFile(file.getFilename(), file.getAttrs().getSize(), file));
                if(file.getLongname().startsWith("d")){
                    files.putAll(getDirectoryList(file.getFilename()));
                }                    
            }
        }

        return files;
    }
    catch (SftpException ex) {
        logger.error("Unable to list SFTP directory. --> " + ex.getMessage(), ex);
        throw new StorageException(ex);
    }
}
 
Example #13
Source File: SFTPUtil.java    From anyline with Apache License 2.0 6 votes vote down vote up
/**  
 * 删除文件夹-sftp协议.如果文件夹有内容,则会抛出异常.  
 * @param path 文件夹路径  
 * @throws SftpException   SftpException
 */   
public void deleteDir(String path) throws SftpException {   
    @SuppressWarnings("unchecked")   
    Vector<LsEntry> vector = client.ls(path);   
    if (vector.size() == 1) { // 文件,直接删除   
        client.rm(path);   
    } else if (vector.size() == 2) { // 空文件夹,直接删除   
        client.rmdir(path);   
    } else {   
        String fileName = "";   
        // 删除文件夹下所有文件   
        for (LsEntry en : vector) {   
            fileName = en.getFilename();   
            if (".".equals(fileName) || "..".equals(fileName)) {   
                continue;   
            } else {   
            	deleteDir(path + "/" + fileName);   
            }   
        }   
        // 删除文件夹   
        client.rmdir(path);   
    }   
}
 
Example #14
Source File: SftpTransferManager.java    From desktop with GNU General Public License v3.0 6 votes vote down vote up
public Map<String, RemoteFile> getDirectoryList(String folderPath) throws StorageException{
    
    try {
        Map<String, RemoteFile> files = new HashMap<String, RemoteFile>();

        List<LsEntry> sftpFiles = sftp.ls(getConnection().getPath() + "/" + folderPath);
        for (LsEntry file: sftpFiles) {                
            if(file.getFilename().compareTo(".") != 0 && file.getFilename().compareTo("..") != 0){
                files.put(folderPath+"/"+file.getFilename(), new RemoteFile(folderPath+"/"+file.getFilename(), file.getAttrs().getSize(), file));
                if(file.getLongname().startsWith("d")){
                    files.putAll(getDirectoryList(folderPath+"/"+file.getFilename()));
                }                    
            }
        }

        return files;
    } catch (SftpException ex) {
        logger.error("Unable to list SFTP directory.", ex);
        throw new StorageException(ex);
    }
}
 
Example #15
Source File: SftpCommandTests.java    From vividus with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings({ "PMD.ReplaceVectorWithList", "PMD.UseArrayListInsteadOfVector" })
void shouldExecuteLsCommand() throws IOException, SftpException
{
    ChannelSftp channel = mock(ChannelSftp.class);
    String path = "/dir-to-list";
    LsEntry lsEntry1 = mock(LsEntry.class);
    when(lsEntry1.getFilename()).thenReturn("file1.txt");
    LsEntry lsEntry2 = mock(LsEntry.class);
    when(lsEntry2.getFilename()).thenReturn("file2.story");
    Vector<LsEntry> ls = new Vector<>();
    ls.add(lsEntry1);
    ls.add(lsEntry2);
    when(channel.ls(path)).thenReturn(ls);
    String result = SftpCommand.LS.execute(channel, path);
    assertEquals("file1.txt,file2.story", result);
}
 
Example #16
Source File: SFTPConnection.java    From davos with MIT License 5 votes vote down vote up
private FTPFile toFtpFile(LsEntry lsEntry, String filePath) throws SftpException {

        String name = lsEntry.getFilename();
        long fileSize = lsEntry.getAttrs().getSize();
        int mTime = lsEntry.getAttrs().getMTime();
        boolean directory = lsEntry.getAttrs().isDir();

        return new FTPFile(name, fileSize, filePath, (long) mTime * 1000, directory);
    }
 
Example #17
Source File: SFTPConnectionTest.java    From davos with MIT License 5 votes vote down vote up
private void initRecursiveListings() throws SftpException {

        Vector<LsEntry> entries = new Vector<LsEntry>();

        entries.add(createSingleEntry(".", 123l, 1394525265, true));
        entries.add(createSingleEntry("..", 123l, 1394525265, true));
        entries.add(createSingleEntry("file1.txt", 123l, 1394525265, false));
        entries.add(createSingleEntry("file2.txt", 456l, 1394652161, false));
        entries.add(createSingleEntry("directory1", 789l, 1391879364, true));

        when(mockChannel.ls("path/to/folder/")).thenReturn(entries);

        Vector<LsEntry> subEntries = new Vector<LsEntry>();

        subEntries.add(createSingleEntry(".", 123l, 1394525265, true));
        subEntries.add(createSingleEntry("..", 123l, 1394525265, true));
        subEntries.add(createSingleEntry("file3.txt", 789l, 1394525265, false));
        subEntries.add(createSingleEntry("directory2", 789l, 1394525265, true));
        subEntries.add(createSingleEntry("file4.txt", 789l, 1394525265, false));

        when(mockChannel.ls("path/to/folder/directory1/")).thenReturn(subEntries);

        Vector<LsEntry> subSubEntries = new Vector<LsEntry>();

        subSubEntries.add(createSingleEntry(".", 123l, 1394525265, true));
        subSubEntries.add(createSingleEntry("..", 123l, 1394525265, true));
        subSubEntries.add(createSingleEntry("file5.txt", 789l, 1394525265, false));
        subSubEntries.add(createSingleEntry("file6.txt", 789l, 1394525265, false));

        when(mockChannel.ls("path/to/folder/directory1/directory2/")).thenReturn(subSubEntries);
    }
 
Example #18
Source File: SFTPConnectionTest.java    From davos with MIT License 5 votes vote down vote up
private LsEntry createSingleEntry(String fileName, long size, int mTime, boolean directory) {

        SftpATTRS attributes = mock(SftpATTRS.class);
        when(attributes.getSize()).thenReturn(size);
        when(attributes.getMTime()).thenReturn(mTime);

        LsEntry entry = mock(LsEntry.class);
        when(entry.getAttrs()).thenReturn(attributes);
        when(entry.getFilename()).thenReturn(fileName);
        when(entry.getAttrs().isDir()).thenReturn(directory);

        return entry;
    }
 
Example #19
Source File: SFTPClient.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public String[] dir() throws KettleJobException {
  String[] fileList = null;

  try {
    java.util.Vector<?> v = c.ls( "." );
    java.util.Vector<String> o = new java.util.Vector<String>();
    if ( v != null ) {
      for ( int i = 0; i < v.size(); i++ ) {
        Object obj = v.elementAt( i );
        if ( obj != null && obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry ) {
          LsEntry lse = (com.jcraft.jsch.ChannelSftp.LsEntry) obj;
          if ( !lse.getAttrs().isDir() ) {
            o.add( lse.getFilename() );
          }
        }
      }
    }
    if ( o.size() > 0 ) {
      fileList = new String[o.size()];
      o.copyInto( fileList );
    }
  } catch ( SftpException e ) {
    throw new KettleJobException( e );
  }

  return fileList;
}
 
Example #20
Source File: SftpSourceTestConfiguration.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
	@SuppressWarnings("unchecked")
	SessionFactory<LsEntry> ftpSessionFactory = Mockito.mock(SessionFactory.class);
	@SuppressWarnings("unchecked")
	Session<LsEntry> session = mock(Session.class);
	when(ftpSessionFactory.getSession()).thenReturn(session);
	return ftpSessionFactory;
}
 
Example #21
Source File: SFTPUtils.java    From axelor-open-suite with GNU Affero General Public License v3.0 5 votes vote down vote up
/** Returns a list of all files in given directory {@code dir}. */
@SuppressWarnings("unchecked")
public static List<LsEntry> getFiles(ChannelSftp channel, String dir) throws SftpException {
  List<LsEntry> files = new ArrayList<>((Vector<LsEntry>) channel.ls(dir));

  files.sort(
      Comparator.comparing((LsEntry file) -> file.getAttrs().getMTime())
          .thenComparing(LsEntry::getFilename));

  return files;
}
 
Example #22
Source File: SFTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Info info(LsEntry file, String name, URI parent, URI self) {
	if (file == null) {
		return null;
	}
	try {
		return new SFTPInfo(file, (name != null) ? name : file.getFilename(), parent, self);
	} catch (IOException ex) {
		return null;
	}
}
 
Example #23
Source File: SFTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
private List<Info> list(URI parentUri, ChannelSftp channel, ListParameters params) throws IOException, SftpException {
	if (Thread.currentThread().isInterrupted()) {
		throw new IOException(FileOperationMessages.getString("IOperationHandler.interrupted")); //$NON-NLS-1$
	}
	Info rootInfo = info(parentUri, channel);
	if (rootInfo == null) {
		throw new FileNotFoundException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.file_not_found"), parentUri.toString())); //$NON-NLS-1$
	}
	if (parentUri.toString().endsWith(URIUtils.PATH_SEPARATOR) && !rootInfo.isDirectory()) {
		throw new FileNotFoundException(format(FileOperationMessages.getString("IOperationHandler.not_a_directory"), parentUri)); //$NON-NLS-1$
	}
	if (!rootInfo.isDirectory()) {
		return Arrays.asList(rootInfo);
	} else {
		@SuppressWarnings("unchecked")
		Vector<LsEntry> files = channel.ls(getPath(parentUri));
		List<Info> result = new ArrayList<Info>(files.size());
		for (LsEntry file: files) {
			if ((file != null) && !file.getFilename().equals(URIUtils.CURRENT_DIR_NAME) && !file.getFilename().equals(URIUtils.PARENT_DIR_NAME)) {
				Info child = info(file, null, parentUri, null);
				result.add(child);
				if (params.isRecursive() && file.getAttrs().isDir()) {
					result.addAll(list(child.getURI(), channel, params));
				}
			}
		}
		return result;
	}
}
 
Example #24
Source File: PooledSFTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
public SFTPInfo(LsEntry file, String name, URI parent, URI self) throws UnsupportedEncodingException {
	this.file = file;
	this.parent = parent;
	this.name = name;
	this.attrs = file.getAttrs();
	if (file.getAttrs().isDir() && !name.endsWith(URIUtils.PATH_SEPARATOR)) {
		name = name + URIUtils.PATH_SEPARATOR;
	}
	if (self != null) {
		this.uri = self;
	} else {
		this.uri = URIUtils.getChildURI(parent, name);
	}
}
 
Example #25
Source File: PooledSFTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Info info(LsEntry file, String name, URI parent, URI self) {
	if (file == null) {
		return null;
	}
	try {
		return new SFTPInfo(file, (name != null) ? name : file.getFilename(), parent, self);
	} catch (IOException ex) {
		return null;
	}
}
 
Example #26
Source File: PooledSFTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
private List<Info> list(URI parentUri, ChannelSftp channel, ListParameters params) throws IOException, SftpException {
	if (Thread.currentThread().isInterrupted()) {
		throw new IOException(FileOperationMessages.getString("IOperationHandler.interrupted")); //$NON-NLS-1$
	}
	Info rootInfo = info(parentUri, channel);
	if (rootInfo == null) {
		throw new FileNotFoundException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.file_not_found"), parentUri.toString())); //$NON-NLS-1$
	}
	if (parentUri.toString().endsWith(URIUtils.PATH_SEPARATOR) && !rootInfo.isDirectory()) {
		throw new FileNotFoundException(format(FileOperationMessages.getString("IOperationHandler.not_a_directory"), parentUri)); //$NON-NLS-1$
	}
	if (!rootInfo.isDirectory()) {
		return Arrays.asList(rootInfo);
	} else {
		@SuppressWarnings("unchecked")
		Vector<LsEntry> files = channel.ls(getPath(parentUri));
		List<Info> result = new ArrayList<Info>(files.size());
		for (LsEntry file: files) {
			if ((file != null) && !file.getFilename().equals(URIUtils.CURRENT_DIR_NAME) && !file.getFilename().equals(URIUtils.PARENT_DIR_NAME)) {
				Info child = info(file, null, parentUri, null);
				result.add(child);
				if (params.isRecursive() && file.getAttrs().isDir()) {
					result.addAll(list(child.getURI(), channel, params));
				}
			}
		}
		return result;
	}
}
 
Example #27
Source File: SftpFsHelper.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> ls(String path) throws FileBasedHelperException {
  try {
    List<String> list = new ArrayList<>();
    ChannelSftp channel = getSftpChannel();
    Vector<LsEntry> vector = channel.ls(path);
    for (LsEntry entry : vector) {
      list.add(entry.getFilename());
    }
    channel.disconnect();
    return list;
  } catch (SftpException e) {
    throw new FileBasedHelperException("Cannot execute ls command on sftp connection", e);
  }
}
 
Example #28
Source File: SftpUtils.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
public static void landFile(File fileToLand, UploadProperties properties) throws Exception {
    JSch jsch = new JSch();
    
    String host = properties.getSftpServer();
    String user = properties.getUser();
    String password = properties.getPassword();
    int port = properties.getPort();
    
    Session session = jsch.getSession(user, host, port);
    session.setOutputStream(System.out);
    
    session.setPassword(password);
    
    session.setConfig("StrictHostKeyChecking", "no");
    session.connect(TIMEOUT);
    
    Channel channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp c = (ChannelSftp) channel;
    
    // delete any existing file with the target filename, so that rename will work
    @SuppressWarnings("unchecked")
    Vector<LsEntry> files = c.ls(".");
    for (LsEntry file : files) {
        if (FINAL_REMOTE_FILENAME.equals(file.getFilename())) {
            c.rm(FINAL_REMOTE_FILENAME);
        }
    }
    
    // transmit file, using temp remote name, so ingestion won't process file until complete
    c.put(new FileInputStream(fileToLand), TEMP_REMOTE_FILENAME, ChannelSftp.OVERWRITE);
    
    // rename remote file so ingestion can begin
    c.rename(TEMP_REMOTE_FILENAME, FINAL_REMOTE_FILENAME);
    
    c.disconnect();
    channel.disconnect();
    session.disconnect();
}
 
Example #29
Source File: SFtpHelper.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public Vector<LsEntry> ls(String path, String fileNameWithWildcard) throws Exception {
	if (StringUtils.isEmpty(path)) {
		path = ".";
	} else if (!path.trim().startsWith("/")) {
		path = "./" + path;
	}
	
	checkForConnection();
	Vector<LsEntry> returnVector = channel.ls(path + "/" + fileNameWithWildcard);
	return returnVector;
}
 
Example #30
Source File: SSHChannelPool.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
List<LsEntry> listFiles(String path) throws IOException {
    final List<LsEntry> entries = new ArrayList<>();
    LsEntrySelector selector = entry -> {
        entries.add(entry);
        return LsEntrySelector.CONTINUE;
    };
    try {
        channel.ls(path, selector);
    } catch (SftpException e) {
        throw exceptionFactory.createListFilesException(path, e);
    }
    return entries;
}