net.schmizz.sshj.sftp.SFTPException Java Examples

The following examples show how to use net.schmizz.sshj.sftp.SFTPException. 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: 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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
Source File: SFTPTransfer.java    From nifi with Apache License 2.0 5 votes vote down vote up
private String getMessage(final SFTPException e) {
    if (e.getStatusCode() != null) {
        return e.getStatusCode().getCode() + ": " + e.getMessage();
    } else {
        return e.getMessage();
    }
}
 
Example #11
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 #12
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 #13
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 #14
Source File: TestChrootSFTPClient.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private void expectNotExist(Callable callable) throws Exception {
  try {
    callable.call();
    Assert.fail("Expected an SFTPException");
  } catch (SFTPException e) {
    Assert.assertEquals("No such file or directory", e.getMessage());
  }
}
 
Example #15
Source File: TestChrootSFTPClient.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testArchiveTargetExist() throws Exception {
  testArchiveHelper(new ArchiveHelperCallable() {
    @Override
    public void call(
        ChrootSFTPClient sftpClient, File fromFile, String fromPath, File toFile, String toPath
    ) throws Exception {
      String fromText = "hello";
      Files.write(fromText.getBytes(Charset.forName("UTF-8")), fromFile);
      Assert.assertEquals(fromText, Files.readFirstLine(fromFile, Charset.forName("UTF-8")));
      sftpClient.stat(fromPath);

      toFile.getParentFile().mkdirs();
      String toText = "goodbye";
      Files.write(toText.getBytes(Charset.forName("UTF-8")), toFile);
      Assert.assertEquals(toText, Files.readFirstLine(toFile, Charset.forName("UTF-8")));
      sftpClient.stat(toPath);

      try {
        sftpClient.archive(fromPath);
        Assert.fail("Expected an SFTPException");
      } catch (SFTPException e) {
        Assert.assertEquals("File/Directory already exists", e.getMessage());
      }

      Assert.assertEquals(toText, Files.readFirstLine(toFile, Charset.forName("UTF-8")));
      Assert.assertEquals(toText, IOUtils.toString(sftpClient.openForReading(toPath)));
      sftpClient.stat(toPath);

      Assert.assertEquals(fromText, Files.readFirstLine(fromFile, Charset.forName("UTF-8")));
      Assert.assertEquals(fromText, IOUtils.toString(sftpClient.openForReading(fromPath)));
      sftpClient.stat(fromPath);
    }
  });
}
 
Example #16
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 #17
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 #18
Source File: SFTPRemoteFile.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isReadable() throws IOException {
  try (InputStream ignored = createInputStream()) {
    LOG.trace("File is readable");
    return true;
  } catch (IOException e) {
    if (e instanceof SFTPException) {
      SFTPException ex = (SFTPException)e;
      LOG.error(Utils.format("Error checking isReadable {}", ex.getStatusCode()), ex);
      if (ex.getStatusCode() == Response.StatusCode.PERMISSION_DENIED || ex.getStatusCode() == Response.StatusCode.NO_SUCH_FILE) {
        return false;
      }
    }
    throw e;
  }
}
 
Example #19
Source File: SFTPSession.java    From cyberduck with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Promise<Response, SFTPException> request(final Request req) throws IOException {
    transcript.log(Type.request, String.format("%d %s", req.getRequestID(), req.getType()));
    return super.request(req);
}