Java Code Examples for com.jcraft.jsch.ChannelSftp#SSH_FX_NO_SUCH_FILE

The following examples show how to use com.jcraft.jsch.ChannelSftp#SSH_FX_NO_SUCH_FILE . 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 localization_nifi with Apache License 2.0 10 votes vote down vote up
@Override
public InputStream getInputStream(final String remoteFileName, final FlowFile flowFile) throws IOException {
    final ChannelSftp sftp = getChannel(flowFile);
    try {
        return sftp.get(remoteFileName);
    } catch (final SftpException e) {
        switch (e.id) {
            case ChannelSftp.SSH_FX_NO_SUCH_FILE:
                throw new FileNotFoundException("Could not find file " + remoteFileName + " on remote SFTP Server");
            case ChannelSftp.SSH_FX_PERMISSION_DENIED:
                throw new PermissionDeniedException("Insufficient permissions to read file " + remoteFileName + " from remote SFTP Server", e);
            default:
                throw new IOException("Failed to obtain file content for " + remoteFileName, e);
        }
    }
}
 
Example 2
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 3
Source File: SFTPTransfer.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void deleteFile(final String path, final String remoteFileName) throws IOException {
    final String fullPath = (path == null) ? remoteFileName : (path.endsWith("/")) ? path + remoteFileName : path + "/" + remoteFileName;
    try {
        sftp.rm(fullPath);
    } catch (final SftpException e) {
        switch (e.id) {
            case ChannelSftp.SSH_FX_NO_SUCH_FILE:
                throw new FileNotFoundException("Could not find file " + remoteFileName + " to remove from remote SFTP Server");
            case ChannelSftp.SSH_FX_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 4
Source File: SFTPTransfer.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void rename(final String source, final String target) throws IOException {
    final ChannelSftp sftp = getChannel(null);
    try {
        sftp.rename(source, target);
    } catch (final SftpException e) {
        switch (e.id) {
            case ChannelSftp.SSH_FX_NO_SUCH_FILE:
                throw new FileNotFoundException();
            case ChannelSftp.SSH_FX_PERMISSION_DENIED:
                throw new PermissionDeniedException("Could not rename remote file " + source + " to " + target + " due to insufficient permissions");
            default:
                throw new IOException(e);
        }
    }
}
 
Example 5
Source File: SftpBuildProcessAdapter.java    From teamcity-deployer-plugin with Apache License 2.0 6 votes vote down vote up
private void createRemotePath(@NotNull final ChannelSftp channel,
                              @NotNull final String destination) throws SftpException {
  final int endIndex = destination.lastIndexOf('/');
  if (endIndex > 0) {
    createRemotePath(channel, destination.substring(0, endIndex));
  }
  try {
    channel.stat(destination);
  } catch (SftpException e) {
    // dir does not exist.
    if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
      channel.mkdir(destination);
    }
  }

}
 
Example 6
Source File: SftpResourceAccessor.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public ExternalResourceMetaData getMetaData(URI uri) throws IOException {
    LockableSftpClient sftpClient = sftpClientFactory.createSftpClient(uri, credentials);
    try {
        SftpATTRS attributes = sftpClient.getSftpClient().lstat(uri.getPath());
        return attributes != null ? toMetaData(uri, attributes) : null;
    } catch (com.jcraft.jsch.SftpException e) {
        if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
            return null;
        }
        throw new SftpException(String.format("Could not get resource '%s'.", uri), e);
    } finally {
        sftpClientFactory.releaseSftpClient(sftpClient);
    }
}
 
Example 7
Source File: DefaultFileSystemExceptionFactory.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * If the {@link SftpException#id id} of the {@link SftpException} is not {@link ChannelSftp#SSH_FX_NO_SUCH_FILE} or
 * {@link ChannelSftp#SSH_FX_PERMISSION_DENIED}, this default implementation does not return a {@link FileSystemException}, but a
 * {@link NotLinkException} instead.
 */
@Override
public FileSystemException createReadLinkException(String link, SftpException exception) {
    if (exception.id == ChannelSftp.SSH_FX_NO_SUCH_FILE || exception.id == ChannelSftp.SSH_FX_PERMISSION_DENIED) {
        return asFileSystemException(link, null, exception);
    }
    final FileSystemException result = new NotLinkException(link, null, exception.getMessage());
    result.initCause(exception);
    return result;
}
 
Example 8
Source File: SftpResourceAccessor.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public ExternalResourceMetaData getMetaData(URI uri) throws IOException {
    LockableSftpClient sftpClient = sftpClientFactory.createSftpClient(uri, credentials);
    try {
        SftpATTRS attributes = sftpClient.getSftpClient().lstat(uri.getPath());
        return attributes != null ? toMetaData(uri, attributes) : null;
    } catch (com.jcraft.jsch.SftpException e) {
        if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
            return null;
        }
        throw new SftpException(String.format("Could not get resource '%s'.", uri), e);
    } finally {
        sftpClientFactory.releaseSftpClient(sftpClient);
    }
}
 
Example 9
Source File: Scar.java    From scar with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
void cd (ChannelSftp channel, String path) throws Exception {
	try {
		channel.cd(path);
	} catch (SftpException ex) {
		if (ex.id != ChannelSftp.SSH_FX_NO_SUCH_FILE) throw new Exception("Error setting remote directory: " + path, ex);
		mkdirs(channel, path);
	}
}
 
Example 10
Source File: SftpFileObject.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Fetches file attributes from server.
 *
 * @throws IOException
 */
private void statSelf() throws IOException {
    ChannelSftp channel = getAbstractFileSystem().getChannel();
    try {
        setStat(channel.stat(relPath));
    } catch (final SftpException e) {
        try {
            // maybe the channel has some problems, so recreate the channel and retry
            if (e.id != ChannelSftp.SSH_FX_NO_SUCH_FILE) {
                channel.disconnect();
                channel = getAbstractFileSystem().getChannel();
                setStat(channel.stat(relPath));
            } else {
                // Really does not exist
                attrs = null;
            }
        } catch (final SftpException innerEx) {
            // TODO - not strictly true, but jsch 0.1.2 does not give us
            // enough info in the exception. Should be using:
            // if ( e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE )
            // However, sometimes the exception has the correct id, and
            // sometimes
            // it does not. Need to look into why.

            // Does not exist
            attrs = null;
        }
    } finally {
        getAbstractFileSystem().putChannel(channel);
    }
}
 
Example 11
Source File: SftpFileObject.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Creates an input stream to read the file content from.
 */
@SuppressWarnings("resource")
@Override
protected InputStream doGetInputStream(final int bufferSize) throws Exception {
    // VFS-113: avoid npe
    synchronized (getAbstractFileSystem()) {
        final ChannelSftp channel = getAbstractFileSystem().getChannel();
        try {
            // return channel.get(getName().getPath());
            // hmmm - using the in memory method is soooo much faster ...

            // TODO - Don't read the entire file into memory. Use the
            // stream-based methods on ChannelSftp once they work properly

            /*
             * final ByteArrayOutputStream outstr = new ByteArrayOutputStream(); channel.get(relPath, outstr);
             * outstr.close(); return new ByteArrayInputStream(outstr.toByteArray());
             */

            InputStream inputStream;
            try {
                // VFS-210: sftp allows to gather an input stream even from a directory and will
                // fail on first read. So we need to check the type anyway
                if (!getType().hasContent()) {
                    throw new FileSystemException("vfs.provider/read-not-file.error", getName());
                }

                inputStream = channel.get(relPath);
            } catch (final SftpException e) {
                if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
                    throw new FileNotFoundException(getName());
                }

                throw new FileSystemException(e);
            }

            return new SftpInputStream(channel, inputStream, bufferSize);

        } finally {
            // getAbstractFileSystem().putChannel(channel);
        }
    }
}
 
Example 12
Source File: MiscUtils.java    From netbeans with Apache License 2.0 2 votes vote down vote up
/**
 * If an sftp exception occurs, a question arises, whether we should call ChannelSftp.quit().
 * On one hane, *not* calling quit can lead to infinite 4: errors.
 * On the other hand, there might be quite standard situations like file not exist or permission denied,
 * which do not require quitting the channel.
 * This method distinguishes the former and the latter cases.
 */
public static boolean mightBrokeSftpChannel(SftpException e) {
    // Or should it be just  return e.id == ChannelSftp.SSH_FX_FAILURE ?
    // well, let's be conservative
    return e.id != ChannelSftp.SSH_FX_NO_SUCH_FILE && e.id != ChannelSftp.SSH_FX_PERMISSION_DENIED;
}