Java Code Examples for net.schmizz.sshj.sftp.SFTPException#getStatusCode()

The following examples show how to use net.schmizz.sshj.sftp.SFTPException#getStatusCode() . 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 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 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: 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 4
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 5
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);
    }
}