net.schmizz.sshj.sftp.SFTPClient Java Examples

The following examples show how to use net.schmizz.sshj.sftp.SFTPClient. 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
/**
 * Wraps the provided {@link SFTPClient} at the given root.  The given root can either be an absolute path or a path
 * relative to the user's home directory.
 *
 * @param sftpClient The {@link SFTPClient} to wrap
 * @param root The root directory to use
 * @param rootRelativeToUserDir true if the given root is relative to the user's home dir, false if not
 * @param makeRoot will create the root dir if true and it doesn't already exist
 * @param disableReadAheadStream disables the use of
 *   the {@link net.schmizz.sshj.sftp.RemoteFile.ReadAheadRemoteFileInputStream} class when opening files for reading,
 *   since there appears to be an issue with that class, and large files, when using on conjunction with S3 at least
 *   (see https://github.com/hierynomus/sshj/issues/505).  If this is set to true, then the
 *   {@link net.schmizz.sshj.sftp.RemoteFile.RemoteFileInputStream} will be opened instead, which is far less
 *   performant, but does not seem to trigger the problem.
 *
 * @throws IOException
 */
public ChrootSFTPClient(
    SFTPClient sftpClient,
    String root,
    boolean rootRelativeToUserDir,
    boolean makeRoot,
    boolean disableReadAheadStream
) throws
    IOException {
  this.sftpClient = sftpClient;
  if (rootRelativeToUserDir) {
    String userDir = sftpClient.canonicalize(".");
    root = Paths.get(userDir, root).toString();
  }
  if (sftpClient.statExistence(root) == null) {
    if (makeRoot) {
      sftpClient.mkdirs(root);
    } else {
      throw new SFTPException(root + " does not exist");
    }
  }
  this.root = root;
  this.disableReadAheadStream = disableReadAheadStream;
}
 
Example #2
Source File: SFTPTransfer.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void rename(final FlowFile flowFile, final String source, final String target) throws IOException {
    final SFTPClient sftpClient = getSFTPClient(flowFile);
    try {
        sftpClient.rename(source, target);
    } catch (final SFTPException e) {
        switch (e.getStatusCode()) {
            case NO_SUCH_FILE:
                throw new FileNotFoundException("No such file or directory");
            case PERMISSION_DENIED:
                throw new PermissionDeniedException("Could not rename remote file " + source + " to " + target + " due to insufficient permissions");
            default:
                throw new IOException(e);
        }
    }
}
 
Example #3
Source File: TestSFTPTransfer.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnsureDirectoryExistsFailedToStat() throws IOException, SFTPException {
    final ProcessContext processContext = mock(ProcessContext.class);
    final SFTPClient sftpClient = mock(SFTPClient.class);
    // stat for the parent was successful, simulating that dir2 exists, but no dir3.
    when(sftpClient.stat("/dir1/dir2/dir3")).thenThrow(new SFTPException(Response.StatusCode.FAILURE, "Failure"));

    final SFTPTransfer sftpTransfer = createSftpTransfer(processContext, sftpClient);
    final MockFlowFile flowFile = new MockFlowFile(0);
    final File remoteDir = new File("/dir1/dir2/dir3");
    try {
        sftpTransfer.ensureDirectoryExists(flowFile, remoteDir);
        fail("Should fail");
    } catch (IOException e) {
        assertEquals("Failed to determine if remote directory exists at /dir1/dir2/dir3 due to 4: Failure", e.getMessage());
    }

    // Dir existence check should be done by stat
    verify(sftpClient).stat(eq("/dir1/dir2/dir3"));
}
 
Example #4
Source File: SFTPTransfer.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void deleteFile(final FlowFile flowFile, final String path, final String remoteFileName) throws IOException {
    final SFTPClient sftpClient = getSFTPClient(flowFile);
    final String fullPath = (path == null) ? remoteFileName : (path.endsWith("/")) ? path + remoteFileName : path + "/" + remoteFileName;
    try {
        sftpClient.rm(fullPath);
    } catch (final SFTPException e) {
        switch (e.getStatusCode()) {
            case NO_SUCH_FILE:
                throw new FileNotFoundException("Could not find file " + remoteFileName + " to remove from remote SFTP Server");
            case PERMISSION_DENIED:
                throw new PermissionDeniedException("Insufficient permissions to delete file " + remoteFileName + " from remote SFTP Server", e);
            default:
                throw new IOException("Failed to delete remote file " + fullPath, e);
        }
    }
}
 
Example #5
Source File: TestSFTPTransfer.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnsureDirectoryExistsNotExisted() throws IOException, SFTPException {
    final ProcessContext processContext = mock(ProcessContext.class);
    final SFTPClient sftpClient = mock(SFTPClient.class);
    // stat for the parent was successful, simulating that dir2 exists, but no dir3.
    when(sftpClient.stat("/dir1/dir2/dir3")).thenThrow(new SFTPException(Response.StatusCode.NO_SUCH_FILE, "No such file"));

    final SFTPTransfer sftpTransfer = createSftpTransfer(processContext, sftpClient);
    final MockFlowFile flowFile = new MockFlowFile(0);
    final File remoteDir = new File("/dir1/dir2/dir3");
    sftpTransfer.ensureDirectoryExists(flowFile, remoteDir);

    // Dir existence check should be done by stat
    verify(sftpClient).stat(eq("/dir1/dir2/dir3")); // dir3 was not found
    verify(sftpClient).stat(eq("/dir1/dir2")); // so, dir2 was checked
    verify(sftpClient).mkdir(eq("/dir1/dir2/dir3")); // dir2 existed, so dir3 was created.
}
 
Example #6
Source File: TestSFTPTransfer.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnsureDirectoryExistsParentNotExisted() throws IOException, SFTPException {
    final ProcessContext processContext = mock(ProcessContext.class);
    final SFTPClient sftpClient = mock(SFTPClient.class);

    // stat for the dir1 was successful, simulating that dir1 exists, but no dir2 and dir3.
    when(sftpClient.stat("/dir1/dir2/dir3")).thenThrow(new SFTPException(Response.StatusCode.NO_SUCH_FILE, "No such file"));
    when(sftpClient.stat("/dir1/dir2")).thenThrow(new SFTPException(Response.StatusCode.NO_SUCH_FILE, "No such file"));

    final SFTPTransfer sftpTransfer = createSftpTransfer(processContext, sftpClient);
    final MockFlowFile flowFile = new MockFlowFile(0);
    final File remoteDir = new File("/dir1/dir2/dir3");
    sftpTransfer.ensureDirectoryExists(flowFile, remoteDir);

    // Dir existence check should be done by stat
    verify(sftpClient).stat(eq("/dir1/dir2/dir3")); // dir3 was not found
    verify(sftpClient).stat(eq("/dir1/dir2")); // dir2 was not found, too
    verify(sftpClient).stat(eq("/dir1")); // dir1 was found
    verify(sftpClient).mkdir(eq("/dir1/dir2")); // dir1 existed, so dir2 was created.
    verify(sftpClient).mkdir(eq("/dir1/dir2/dir3")); // then dir3 was created.
}
 
Example #7
Source File: TestSFTPTransfer.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnsureDirectoryExistsNotExistedFailedToCreate() throws IOException, SFTPException {
    final ProcessContext processContext = mock(ProcessContext.class);
    final SFTPClient sftpClient = mock(SFTPClient.class);

    // stat for the parent was successful, simulating that dir2 exists, but no dir3.
    when(sftpClient.stat("/dir1/dir2/dir3")).thenThrow(new SFTPException(Response.StatusCode.NO_SUCH_FILE, "No such file"));
    // Failed to create dir3.
    doThrow(new SFTPException(Response.StatusCode.FAILURE, "Failed")).when(sftpClient).mkdir(eq("/dir1/dir2/dir3"));

    final SFTPTransfer sftpTransfer = createSftpTransfer(processContext, sftpClient);
    final MockFlowFile flowFile = new MockFlowFile(0);
    final File remoteDir = new File("/dir1/dir2/dir3");
    try {
        sftpTransfer.ensureDirectoryExists(flowFile, remoteDir);
        fail("Should fail");
    } catch (IOException e) {
        assertEquals("Failed to create remote directory /dir1/dir2/dir3 due to 4: Failed", e.getMessage());
    }

    // Dir existence check should be done by stat
    verify(sftpClient).stat(eq("/dir1/dir2/dir3")); // dir3 was not found
    verify(sftpClient).stat(eq("/dir1/dir2")); // so, dir2 was checked
    verify(sftpClient).mkdir(eq("/dir1/dir2/dir3")); // dir2 existed, so dir3 was created.
}
 
Example #8
Source File: TestSFTPTransfer.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnsureDirectoryExistsBlindlyAlreadyExisted() throws IOException, SFTPException {
    final ProcessContext processContext = mock(ProcessContext.class);
    when(processContext.getProperty(SFTPTransfer.DISABLE_DIRECTORY_LISTING)).thenReturn(new MockPropertyValue("true"));

    final SFTPClient sftpClient = mock(SFTPClient.class);
    // If the dir existed, a failure exception is thrown, but should be swallowed.
    doThrow(new SFTPException(Response.StatusCode.FAILURE, "Failure")).when(sftpClient).mkdir(eq("/dir1/dir2/dir3"));

    final SFTPTransfer sftpTransfer = createSftpTransfer(processContext, sftpClient);
    final MockFlowFile flowFile = new MockFlowFile(0);
    final File remoteDir = new File("/dir1/dir2/dir3");
    sftpTransfer.ensureDirectoryExists(flowFile, remoteDir);

    // stat should not be called.
    verify(sftpClient, times(0)).stat(eq("/dir1/dir2/dir3"));
    verify(sftpClient).mkdir(eq("/dir1/dir2/dir3")); // dir3 was created blindly.
}
 
Example #9
Source File: TestChrootSFTPClient.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testArchiveConcatRelativeSeparator() throws Exception {
  // Can't use SSHD because we need to access the root of the disk for these tests, which we might not have permission
  // so we'll need to mock things instead
  String[] values = getConcatRelativeSeparatorValues();
  for (int i = 0; i < getConcatRelativeSeparatorValues().length; i+=3) {
    String userDir = values[i];
    String archiveDir = values[i+1];
    String expected = values[i+2];

    SFTPClient sftpClient = Mockito.mock(SFTPClient.class);
    Mockito.when(sftpClient.canonicalize(".")).thenReturn(userDir);
    Mockito.when(sftpClient.statExistence(Mockito.anyString())).thenReturn(FileAttributes.EMPTY);

    ChrootSFTPClient chrootSFTPClient = new ChrootSFTPClient(sftpClient, "/root", true, false, false);
    chrootSFTPClient.setArchiveDir(archiveDir, true);
    chrootSFTPClient.archive("/path");
    Mockito.verify(sftpClient).rename(Mockito.anyString(), Mockito.eq(Paths.get(expected, "/path").toString()));
  }
}
 
Example #10
Source File: TestSFTPTransfer.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnsureDirectoryExistsBlindlyFailed() throws IOException, SFTPException {
    final ProcessContext processContext = mock(ProcessContext.class);
    when(processContext.getProperty(SFTPTransfer.DISABLE_DIRECTORY_LISTING)).thenReturn(new MockPropertyValue("true"));

    final SFTPClient sftpClient = mock(SFTPClient.class);
    doThrow(new SFTPException(Response.StatusCode.PERMISSION_DENIED, "Permission denied")).when(sftpClient).mkdir(eq("/dir1/dir2/dir3"));

    final SFTPTransfer sftpTransfer = createSftpTransfer(processContext, sftpClient);
    final MockFlowFile flowFile = new MockFlowFile(0);
    final File remoteDir = new File("/dir1/dir2/dir3");
    try {
        sftpTransfer.ensureDirectoryExists(flowFile, remoteDir);
        fail("Should fail");
    } catch (IOException e) {
        assertEquals("Could not blindly create remote directory due to Permission denied", e.getMessage());
    }

    // stat should not be called.
    verify(sftpClient, times(0)).stat(eq("/dir1/dir2/dir3"));
    verify(sftpClient).mkdir(eq("/dir1/dir2/dir3")); // dir3 was created blindly.
}
 
Example #11
Source File: TestChrootSFTPClient.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testRootConcatRelativeSeparator() throws Exception {
  // Can't use SSHD because we need to access the root of the disk for these tests, which we might not have permission
  // so we'll need to mock things instead
  String[] values = getConcatRelativeSeparatorValues();
  for (int i = 0; i < getConcatRelativeSeparatorValues().length; i+=3) {
    String userDir = values[i];
    String root = values[i+1];
    String expected = values[i+2];

    SFTPClient sftpClient = Mockito.mock(SFTPClient.class);
    Mockito.when(sftpClient.canonicalize(".")).thenReturn(userDir);
    Mockito.when(sftpClient.statExistence(Mockito.anyString())).thenReturn(FileAttributes.EMPTY);

    new ChrootSFTPClient(sftpClient, root, true, false, false);
    Mockito.verify(sftpClient).statExistence(expected);
  }
}
 
Example #12
Source File: TestSFTPTransfer.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testEnsureDirectoryExistsBlindlyParentNotExisted() throws IOException, SFTPException {
    final ProcessContext processContext = mock(ProcessContext.class);
    when(processContext.getProperty(SFTPTransfer.DISABLE_DIRECTORY_LISTING)).thenReturn(new MockPropertyValue("true"));

    final SFTPClient sftpClient = mock(SFTPClient.class);
    final AtomicInteger mkdirCount = new AtomicInteger(0);
    doAnswer(invocation -> {
        final int cnt = mkdirCount.getAndIncrement();
        if (cnt == 0) {
            // If the parent dir does not exist, no such file exception is thrown.
            throw new SFTPException(Response.StatusCode.NO_SUCH_FILE, "Failure");
        } else {
            logger.info("Created the dir successfully for the 2nd time");
        }
        return true;
    }).when(sftpClient).mkdir(eq("/dir1/dir2/dir3"));

    final SFTPTransfer sftpTransfer = createSftpTransfer(processContext, sftpClient);
    final MockFlowFile flowFile = new MockFlowFile(0);
    final File remoteDir = new File("/dir1/dir2/dir3");
    sftpTransfer.ensureDirectoryExists(flowFile, remoteDir);

    // stat should not be called.
    verify(sftpClient, times(0)).stat(eq("/dir1/dir2/dir3"));
    // dir3 was created blindly, but failed for the 1st time, and succeeded for the 2nd time.
    verify(sftpClient, times(2)).mkdir(eq("/dir1/dir2/dir3"));
    verify(sftpClient).mkdir(eq("/dir1/dir2")); // dir2 was created successfully.
}
 
Example #13
Source File: TestSFTPTransfer.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testEnsureDirectoryExistsBlindlyNotExisted() throws IOException, SFTPException {
    final ProcessContext processContext = mock(ProcessContext.class);
    when(processContext.getProperty(SFTPTransfer.DISABLE_DIRECTORY_LISTING)).thenReturn(new MockPropertyValue("true"));

    final SFTPClient sftpClient = mock(SFTPClient.class);
    final SFTPTransfer sftpTransfer = createSftpTransfer(processContext, sftpClient);
    final MockFlowFile flowFile = new MockFlowFile(0);
    final File remoteDir = new File("/dir1/dir2/dir3");
    sftpTransfer.ensureDirectoryExists(flowFile, remoteDir);

    // stat should not be called.
    verify(sftpClient, times(0)).stat(eq("/dir1/dir2/dir3"));
    verify(sftpClient).mkdir(eq("/dir1/dir2/dir3")); // dir3 was created blindly.
}
 
Example #14
Source File: SftpFileTransferLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenUploadFileUsingSshj_thenSuccess() throws IOException {
    SSHClient sshClient = setupSshj();
    SFTPClient sftpClient = sshClient.newSFTPClient();
    sftpClient.put(localFile, remoteDir + "sshjFile.txt");
    sftpClient.close();
    sshClient.disconnect();
}
 
Example #15
Source File: SFTPRemoteConnector.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
public void verifyAndReconnect() throws StageException {
  boolean done = false;
  int retryCounter = 0;
  boolean reconnect = false;
  while (!done && retryCounter < MAX_RETRIES) {
    try {
      if (reconnect) {
        sshClient = sshClientRebuilder.build();
        SFTPClient sftpClientRaw = sshClient.newSFTPClient();
        sftpClientRaw.getSFTPEngine().setTimeoutMs(remoteConfig.dataTimeout * 1000);
        sftpClient.setSFTPClient(sftpClientRaw);
        reconnect = false;
      }
      sftpClient.ls();
      done = true;
    } catch (IOException ioe) {
      // ls can fail due to session is down, a timeout, etc; so try getting a new connection
      if (retryCounter < MAX_RETRIES - 1) {
        LOG.info("Got IOException when trying to ls remote directory. '{}'", ioe.getMessage(), ioe);
        LOG.warn("Retrying connection to remote directory");
        reconnect = true;
      } else {
        throw new StageException(Errors.REMOTE_09, ioe.getMessage(), ioe);
      }
    }
    retryCounter++;
  }
}
 
Example #16
Source File: TestSFTPTransfer.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testEnsureDirectoryExistsAlreadyExisted() throws IOException, SFTPException {
    final ProcessContext processContext = mock(ProcessContext.class);
    final SFTPClient sftpClient = mock(SFTPClient.class);
    final SFTPTransfer sftpTransfer = createSftpTransfer(processContext, sftpClient);
    final MockFlowFile flowFile = new MockFlowFile(0);
    final File remoteDir = new File("/dir1/dir2/dir3");
    sftpTransfer.ensureDirectoryExists(flowFile, remoteDir);

    // Dir existence check should be done by stat
    verify(sftpClient).stat(eq("/dir1/dir2/dir3"));
}
 
Example #17
Source File: SftpFileTransferLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenDownloadFileUsingSshj_thenSuccess() throws IOException {
    SSHClient sshClient = setupSshj();
    SFTPClient sftpClient = sshClient.newSFTPClient();
    sftpClient.get(remoteFile, localDir + "sshjFile.txt");
    sftpClient.close();
    sshClient.disconnect();
}
 
Example #18
Source File: TestSFTPTransfer.java    From nifi with Apache License 2.0 5 votes vote down vote up
private SFTPTransfer createSftpTransfer(ProcessContext processContext, SFTPClient sftpClient) {
    final ComponentLog componentLog = mock(ComponentLog.class);
    return new SFTPTransfer(processContext, componentLog) {
        @Override
        protected SFTPClient getSFTPClient(FlowFile flowFile) throws IOException {
            return sftpClient;
        }
    };
}
 
Example #19
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 #20
Source File: SFTPTransfer.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void deleteDirectory(final FlowFile flowFile, final String remoteDirectoryName) throws IOException {
    final SFTPClient sftpClient = getSFTPClient(flowFile);
    try {
        sftpClient.rmdir(remoteDirectoryName);
    } catch (final SFTPException e) {
        throw new IOException("Failed to delete remote directory " + remoteDirectoryName, e);
    }
}
 
Example #21
Source File: TestChrootSFTPClient.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public ChrootSFTPClient createChrootSFTPClient(
    SFTPClient sftpClient,
    String root,
    boolean rootRelativeToUserDir,
    String archiveDir,
    boolean archiveDirRelativeToUserDir,
    boolean disableReadAheadStream
) throws IOException {
  ChrootSFTPClient client = new ChrootSFTPClient(sftpClient, root, rootRelativeToUserDir, false, disableReadAheadStream);
  client.setArchiveDir(archiveDir, archiveDirRelativeToUserDir);
  return client;
}
 
Example #22
Source File: TestChrootSFTPClient.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public ChrootSFTPClient createChrootSFTPClient(
    SFTPClient sftpClient,
    String root,
    boolean rootRelativeToUserDir,
    String archiveDir,
    boolean archiveDirRelativeToUserDir
) throws IOException {
  return createChrootSFTPClient(sftpClient, root, rootRelativeToUserDir, archiveDir, archiveDirRelativeToUserDir, false);
}
 
Example #23
Source File: TestChrootSFTPClient.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private List<ChrootSFTPClient> getClientsWithEquivalentRoots(SSHClient sshClient, String archiveDir)
    throws Exception {
  SFTPClient rawSftpClient = sshClient.newSFTPClient();
  // The following clients are all equivalent, just using different ways of declaring the paths
  List<ChrootSFTPClient> clients = new ArrayList<>();
  // root and archiveDir are both absolute paths
  clients.add(createChrootSFTPClient(rawSftpClient, path, false, archiveDir, false));
  // root is relative to home dir, archiveDir is absolute
  clients.add(createChrootSFTPClient(rawSftpClient, "/", true, archiveDir, false));
  // root is relative to home dir, archiveDir is absolute
  clients.add(createChrootSFTPClient(rawSftpClient, "", true, archiveDir, false));
  if (archiveDir != null) {
    String relArchiveDir = Paths.get(path).relativize(Paths.get(archiveDir)).toString();
    // root is absolute, archiveDir is relative to home dir
    clients.add(createChrootSFTPClient(rawSftpClient, path, false, "/" + relArchiveDir, true));
    // root and archiveDir are both relative to home dir
    clients.add(createChrootSFTPClient(rawSftpClient, "/", true, "/" + relArchiveDir, true));
    // root and archiveDir are both relative to home dir
    clients.add(createChrootSFTPClient(rawSftpClient, "", true, "/" + relArchiveDir, true));
    // root is absolute, archiveDir is relative to home dir
    clients.add(createChrootSFTPClient(rawSftpClient, path, false, relArchiveDir, true));
    // root and archiveDir are both relative to home dir
    clients.add(createChrootSFTPClient(rawSftpClient, "/", true, relArchiveDir, true));
    // root and archiveDir are both relative to home dir
    clients.add(createChrootSFTPClient(rawSftpClient, "", true, relArchiveDir, true));
  }
  return clients;
}
 
Example #24
Source File: TestChrootSFTPClient.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testOpenForReadingReadAheadInputStreamDisabled() throws Exception {
  File file = testFolder.newFile("file.txt");
  String text = "hello";
  Files.write(text.getBytes(Charset.forName("UTF-8")), file);
  Assert.assertEquals(text, Files.readFirstLine(file, Charset.forName("UTF-8")));

  path = testFolder.getRoot().getAbsolutePath();
  setupSSHD(path);
  SSHClient sshClient = createSSHClient();
  final SFTPClient sftpClient = sshClient.newSFTPClient();
  final ChrootSFTPClient chrootSFTPClient = createChrootSFTPClient(
      sftpClient,
      path,
      false,
      null,
      false,
      true
  );
  final InputStream is = chrootSFTPClient.openForReading(file.getName());
  try {
    Assert.assertThat(is, instanceOf(RemoteFile.RemoteFileInputStream.class));
    Assert.assertTrue(is.getClass().getName().startsWith(SFTPStreamFactory.class.getCanonicalName()));
    Assert.assertNotNull(is);
    Assert.assertEquals(text, IOUtils.toString(is, Charset.forName("UTF-8")));
  } finally {
    is.close();
  }
}
 
Example #25
Source File: TestSFTPStreamFactory.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  File file = testFolder.newFile("file.txt");
  String text = "hello";
  Files.write(text.getBytes(Charset.forName("UTF-8")), file);
  Assert.assertEquals(text, Files.readFirstLine(file, Charset.forName("UTF-8")));

  path = testFolder.getRoot().getAbsolutePath();
  setupSSHD(path);
  SSHClient sshClient = createSSHClient();

  SFTPClient sftpClient = sshClient.newSFTPClient();
  remoteFile = sftpClient.open(file.getName(), EnumSet.of(OpenMode.READ));
  remoteFile = Mockito.spy(remoteFile);
}
 
Example #26
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 #27
Source File: SshjTool.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@Override
public SFTPClient create() throws IOException {
    checkConnected();
    sftp = sshClientConnection.ssh.newSFTPClient();
    return sftp;
}
 
Example #28
Source File: SshjTool.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
private CloseFtpChannelOnCloseInputStream(InputStream proxy, SFTPClient sftp) {
    super(proxy);
    this.sftp = sftp;
}
 
Example #29
Source File: ChrootSFTPClient.java    From datacollector with Apache License 2.0 4 votes vote down vote up
public void setSFTPClient(SFTPClient sftpClient) {
  this.sftpClient = sftpClient;
}