net.schmizz.sshj.sftp.RemoteResourceInfo Java Examples

The following examples show how to use net.schmizz.sshj.sftp.RemoteResourceInfo. 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: ChrootSFTPClient.java    From datacollector with Apache License 2.0 6 votes vote down vote up
public List<SimplifiedRemoteResourceInfo> ls(String path, RemoteResourceFilter filter) throws IOException {
  final RemoteDirectory dir = sftpClient.getSFTPEngine().openDir(prependRoot(path));
  try {
    List<RemoteResourceInfo> dirScan = dir.scan(filter);
    List<SimplifiedRemoteResourceInfo> results = new ArrayList<>(dirScan.size());
    for (RemoteResourceInfo remoteResourceInfo : dirScan) {
      // This is needed in order to remove the root from the paths (RemoteResourceInfo is unfortunately immutable)
      results.add(
          new SimplifiedRemoteResourceInfo(
              removeRoot(remoteResourceInfo.getPath()),
              remoteResourceInfo.getAttributes().getMtime(),
              remoteResourceInfo.getAttributes().getType())
      );
    }
    return results;
  } finally {
    dir.close();
  }
}
 
Example #2
Source File: LastUpdatedRemoteResourceFilterTest.java    From lognavigator with Apache License 2.0 6 votes vote down vote up
@Test
public void testAcceptWithLimitNotReached() throws Exception {
	
	// given
	RemoteResourceInfo resource1 = createRemoteResourceInfo(2L);
	RemoteResourceInfo resource2 = createRemoteResourceInfo(1L);
	int resourceMaxCount = 2;
	
	// when
	LastUpdatedRemoteResourceFilter resourceFilter = new LastUpdatedRemoteResourceFilter(resourceMaxCount);
	resourceFilter.accept(resource1);
	resourceFilter.accept(resource2);
	Collection<RemoteResourceInfo> remoteResourceInfos = resourceFilter.getRemoteResourceInfos();
	
	// then
	Assert.assertEquals(2, remoteResourceInfos.size());
	Iterator<RemoteResourceInfo> resourceIterator = remoteResourceInfos.iterator();
	Assert.assertTrue(resource2 == resourceIterator.next());
	Assert.assertTrue(resource1 == resourceIterator.next());
}
 
Example #3
Source File: LastUpdatedRemoteResourceFilterTest.java    From lognavigator with Apache License 2.0 6 votes vote down vote up
@Test
public void testAcceptWithLimitReached() throws Exception {
	
	// given
	RemoteResourceInfo resource1 = createRemoteResourceInfo(1L);
	RemoteResourceInfo resource2 = createRemoteResourceInfo(2L);
	RemoteResourceInfo resource3 = createRemoteResourceInfo(3L);
	int resourceMaxCount = 2;
	
	// when
	LastUpdatedRemoteResourceFilter resourceFilter = new LastUpdatedRemoteResourceFilter(resourceMaxCount);
	resourceFilter.accept(resource1);
	resourceFilter.accept(resource2);
	resourceFilter.accept(resource3);
	Collection<RemoteResourceInfo> remoteResourceInfos = resourceFilter.getRemoteResourceInfos();
	
	// then
	Assert.assertEquals(2, remoteResourceInfos.size());
	Iterator<RemoteResourceInfo> resourceIterator = remoteResourceInfos.iterator();
	Assert.assertTrue(resource2 == resourceIterator.next());
	Assert.assertTrue(resource3 == resourceIterator.next());
}
 
Example #4
Source File: LastUpdatedRemoteResourceFilterTest.java    From lognavigator with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompare() throws Exception {
	
	// given
	RemoteResourceInfo resource1 = createRemoteResourceInfo(2L);
	RemoteResourceInfo resource2 = createRemoteResourceInfo(1L);
	int resourceMaxCount = 2;
	
	// when
	LastUpdatedRemoteResourceFilter resourceFilter = new LastUpdatedRemoteResourceFilter(resourceMaxCount);
	int result = resourceFilter.compare(resource1, resource2);
	
	// then
	final int greaterThanReturn = 1;
	Assert.assertEquals(greaterThanReturn, result);
}
 
Example #5
Source File: FileFilter.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
public boolean accept(RemoteResourceInfo resource) {
  if (resource.isDirectory()) {
    return true;
  }
  if (resource.isRegularFile()) {
    Matcher matcher = regex.matcher(resource.getName());
    if (matcher.matches()) {
      return true;
    }
  }
  LOG.trace("{} was not accepted", resource.getPath());
  return false;
}
 
Example #6
Source File: TestFileFilter.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private RemoteResourceInfo createRemoteResourceInfo(String fileNmae, boolean isFile) {
  RemoteResourceInfo resourceInfo = Mockito.mock(RemoteResourceInfo.class);
  Mockito.when(resourceInfo.getName()).thenReturn(fileNmae);
  Mockito.when(resourceInfo.isDirectory()).thenReturn(!isFile);
  Mockito.when(resourceInfo.isRegularFile()).thenReturn(isFile);
  return resourceInfo;
}
 
Example #7
Source File: LastUpdatedRemoteResourceFilter.java    From lognavigator with Apache License 2.0 5 votes vote down vote up
/**
 * Stores the resource in remoteResourceInfos set (which is sorted by mtime).
 * Keeps only the 'resourceMaxCount' last modified resources in remoteResourceInfos.
 * Always return false, so that no resource is stored in client list (the real filtered result list is stored in this instance, in 'remoteResourceInfos')
 */
@Override
public boolean accept(RemoteResourceInfo resource) {
	remoteResourceInfos.add(resource);
	if (remoteResourceInfos.size() > resourceMaxCount) {
		remoteResourceInfos.remove(remoteResourceInfos.first());
	}
	return false;
}
 
Example #8
Source File: LastUpdatedRemoteResourceFilter.java    From lognavigator with Apache License 2.0 5 votes vote down vote up
/**
 * Comparator method which sorts by mtime ascending
 */
@Override
public int compare(RemoteResourceInfo o1, RemoteResourceInfo o2) {
	if (o1 == o2) {
		return 0;
	}
	else {
		long diff = o1.getAttributes().getMtime() - o2.getAttributes().getMtime();
		return (diff >= 0) ? 1 : -1;
	}
}
 
Example #9
Source File: SFTPTransfer.java    From nifi with Apache License 2.0 5 votes vote down vote up
private FileInfo newFileInfo(final RemoteResourceInfo entry, String path) {
    if (entry == null) {
        return null;
    }
    final File newFullPath = new File(path, entry.getName());
    final String newFullForwardPath = newFullPath.getPath().replace("\\", "/");

    final StringBuilder permsBuilder = new StringBuilder();
    final Set<FilePermission> permissions = entry.getAttributes().getPermissions();

    appendPermission(permsBuilder, permissions, FilePermission.USR_R, "r");
    appendPermission(permsBuilder, permissions, FilePermission.USR_W, "w");
    appendPermission(permsBuilder, permissions, FilePermission.USR_X, "x");

    appendPermission(permsBuilder, permissions, FilePermission.GRP_R, "r");
    appendPermission(permsBuilder, permissions, FilePermission.GRP_W, "w");
    appendPermission(permsBuilder, permissions, FilePermission.GRP_X, "x");

    appendPermission(permsBuilder, permissions, FilePermission.OTH_R, "r");
    appendPermission(permsBuilder, permissions, FilePermission.OTH_W, "w");
    appendPermission(permsBuilder, permissions, FilePermission.OTH_X, "x");

    final FileInfo.Builder builder = new FileInfo.Builder()
        .filename(entry.getName())
        .fullPathFileName(newFullForwardPath)
        .directory(entry.isDirectory())
        .size(entry.getAttributes().getSize())
        .lastModifiedTime(entry.getAttributes().getMtime() * 1000L)
        .permissions(permsBuilder.toString())
        .owner(Integer.toString(entry.getAttributes().getUID()))
        .group(Integer.toString(entry.getAttributes().getGID()));
    return builder.build();
}
 
Example #10
Source File: SFTPTransfer.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public FileInfo getRemoteFileInfo(final FlowFile flowFile, final String path, String filename) throws IOException {
    final SFTPClient sftpClient = getSFTPClient(flowFile);

    final List<RemoteResourceInfo> remoteResources;
    try {
        remoteResources = sftpClient.ls(path);
    } catch (final SFTPException e) {
        if (e.getStatusCode() == Response.StatusCode.NO_SUCH_FILE) {
            return null;
        } else {
            throw new IOException("Failed to obtain file listing for " + path, e);
        }
    }

    RemoteResourceInfo matchingEntry = null;
    for (final RemoteResourceInfo entry : remoteResources) {
        if (entry.getName().equalsIgnoreCase(filename)) {
            matchingEntry = entry;
            break;
        }
    }

    // Previously JSCH would perform a listing on the full path (path + filename) and would get an exception when it wasn't
    // a file and then return null, so to preserve that behavior we return null if the matchingEntry is a directory
    if (matchingEntry != null && matchingEntry.isDirectory()) {
        return null;
    } else {
        return newFileInfo(matchingEntry, path);
    }
}
 
Example #11
Source File: SshLogAccessService.java    From lognavigator with Apache License 2.0 4 votes vote down vote up
@Override
protected Set<FileInfo> listFilesUsingNativeSystem(LogAccessConfig logAccessConfig, String subPath) throws LogAccessException {
	
	// Get ssh client
	SSHClient sshClient = sshClientThreadLocal.get();

	// Define target directory
	String targetPath = logAccessConfig.getDirectory();
	if (subPath != null) {
		targetPath += "/" + subPath;
	}

	// List files and directories (keep only the 'fileListMaxCount' last modified resources)
	SFTPClient sftpClient = null;
	Collection<RemoteResourceInfo> remoteResourceInfos;
	try {
		sftpClient = sshClient.newSFTPClient();
		LastUpdatedRemoteResourceFilter remoteResourcefilter = new LastUpdatedRemoteResourceFilter(configService.getFileListMaxCount());
		sftpClient.ls(targetPath, remoteResourcefilter);
		remoteResourceInfos = remoteResourcefilter.getRemoteResourceInfos();
	}
	catch (IOException e) {
		throw new LogAccessException("Error when listing files and directories on " + logAccessConfig, e);
	}
	finally {
		IOUtils.closeQuietly(sftpClient, sshClient);
	}
	
	// Extract meta-informations
	Set<FileInfo> fileInfos = new TreeSet<FileInfo>();
	for (RemoteResourceInfo remoteResourceInfo : remoteResourceInfos) {
		FileInfo fileInfo = new FileInfo();
		fileInfo.setFileName(remoteResourceInfo.getName());
		fileInfo.setRelativePath(remoteResourceInfo.getPath().substring(logAccessConfig.getDirectory().length() + 1).replace('\\', '/'));
		fileInfo.setDirectory(remoteResourceInfo.isDirectory());
		fileInfo.setLastModified(new Date(remoteResourceInfo.getAttributes().getMtime() * 1000L));
		fileInfo.setFileSize(remoteResourceInfo.isDirectory() ? 0L : remoteResourceInfo.getAttributes().getSize());
		fileInfo.setLogAccessType(LogAccessType.SSH);
		fileInfos.add(fileInfo);
	}
	
	// Return meta-informations about files and folders
	return fileInfos;
}
 
Example #12
Source File: LastUpdatedRemoteResourceFilter.java    From lognavigator with Apache License 2.0 4 votes vote down vote up
public LastUpdatedRemoteResourceFilter(int resourceMaxCount) {
	this.resourceMaxCount = resourceMaxCount;
	this.remoteResourceInfos = new TreeSet<RemoteResourceInfo>(this);
}
 
Example #13
Source File: LastUpdatedRemoteResourceFilter.java    From lognavigator with Apache License 2.0 4 votes vote down vote up
/**
 * Return the 'resourceMaxCount' last modified resources filtered 
 */
public Collection<RemoteResourceInfo> getRemoteResourceInfos() {
	return remoteResourceInfos;
}
 
Example #14
Source File: LastUpdatedRemoteResourceFilterTest.java    From lognavigator with Apache License 2.0 4 votes vote down vote up
private RemoteResourceInfo createRemoteResourceInfo(long mtime) {
	PathComponents comps = new PathComponents("parent", "name" + mtime, "/");
	FileAttributes attrs = new FileAttributes(0, 0, 0, 0, null, 0L, mtime, new HashMap<String, String>());
	return new RemoteResourceInfo(comps, attrs);
}