@Test public void testEnsureDirectoryExistsParentNotExisted() throws IOException, SftpException { final ProcessContext processContext = mock(ProcessContext.class); final ChannelSftp channel = mock(ChannelSftp.class); when(channel.stat("/dir1/dir2/dir3")).thenThrow(new SftpException(SSH_FX_NO_SUCH_FILE, "No such file")); when(channel.stat("/dir1/dir2")).thenThrow(new SftpException(SSH_FX_NO_SUCH_FILE, "No such file")); final SFTPTransfer sftpTransfer = createSftpTransfer(processContext, channel); final MockFlowFile flowFile = new MockFlowFile(0); final File remoteDir = new File("/dir1/dir2/dir3"); sftpTransfer.ensureDirectoryExists(flowFile, remoteDir); verify(channel).stat(eq("/dir1/dir2/dir3")); verify(channel).stat(eq("/dir1/dir2")); verify(channel).stat(eq("/dir1")); verify(channel).mkdir(eq("/dir1/dir2")); verify(channel).mkdir(eq("/dir1/dir2/dir3")); }
@Override public void ensureDirectoryExists(final FlowFile flowFile, final File directoryName) throws IOException { final ChannelSftp channel = getChannel(flowFile); final String remoteDirectory = directoryName.getAbsolutePath().replace("\\", "/").replaceAll("^.\\:", ""); if (disableDirectoryListing) { try { channel.mkdir(remoteDirectory); return; } catch (SftpException e) { if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) { logger.debug(String.format("Could not create %s due to 'No such file'. Will try to create the parent dir.", remoteDirectory)); } else if (e.id == ChannelSftp.SSH_FX_FAILURE) { logger.debug("Could not blindly create remote directory due to " + e.getMessage(), e); return; } else { throw new IOException("Could not blindly create remote directory due to " + e.getMessage(), e); } } } else { try { channel.stat(remoteDirectory); return; } catch (final SftpException e) { if (e.id != ChannelSftp.SSH_FX_NO_SUCH_FILE) { throw new IOException("Failed to determine if remote directory exists at " + remoteDirectory + " due to " + e, e); } } } if (directoryName.getParent() != null && !directoryName.getParentFile().equals(new File(File.separator))) { ensureDirectoryExists(flowFile, directoryName.getParentFile()); } logger.debug("Remote Directory {} does not exist; creating it", new Object[] {remoteDirectory}); try { channel.mkdir(remoteDirectory); logger.debug("Created {}", new Object[] {remoteDirectory}); } catch (final SftpException e) { throw new IOException("Failed to create remote directory " + remoteDirectory + " due to " + e, e); } }
SFTPTransfer implements FileTransfer { @Override public void ensureDirectoryExists(final FlowFile flowFile, final File directoryName) throws IOException { final ChannelSftp channel = getChannel(flowFile); final String remoteDirectory = directoryName.getAbsolutePath().replace("\\", "/").replaceAll("^.\\:", ""); if (disableDirectoryListing) { try { channel.mkdir(remoteDirectory); return; } catch (SftpException e) { if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) { logger.debug(String.format("Could not create %s due to 'No such file'. Will try to create the parent dir.", remoteDirectory)); } else if (e.id == ChannelSftp.SSH_FX_FAILURE) { logger.debug("Could not blindly create remote directory due to " + e.getMessage(), e); return; } else { throw new IOException("Could not blindly create remote directory due to " + e.getMessage(), e); } } } else { try { channel.stat(remoteDirectory); return; } catch (final SftpException e) { if (e.id != ChannelSftp.SSH_FX_NO_SUCH_FILE) { throw new IOException("Failed to determine if remote directory exists at " + remoteDirectory + " due to " + e, e); } } } if (directoryName.getParent() != null && !directoryName.getParentFile().equals(new File(File.separator))) { ensureDirectoryExists(flowFile, directoryName.getParentFile()); } logger.debug("Remote Directory {} does not exist; creating it", new Object[] {remoteDirectory}); try { channel.mkdir(remoteDirectory); logger.debug("Created {}", new Object[] {remoteDirectory}); } catch (final SftpException e) { throw new IOException("Failed to create remote directory " + remoteDirectory + " due to " + e, e); } } }
SFTPTransfer implements FileTransfer { @Override public void ensureDirectoryExists(final FlowFile flowFile, final File directoryName) throws IOException { final ChannelSftp channel = getChannel(flowFile); final String remoteDirectory = directoryName.getAbsolutePath().replace("\\", "/").replaceAll("^.\\:", ""); if (disableDirectoryListing) { try { channel.mkdir(remoteDirectory); return; } catch (SftpException e) { if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) { logger.debug(String.format("Could not create %s due to 'No such file'. Will try to create the parent dir.", remoteDirectory)); } else if (e.id == ChannelSftp.SSH_FX_FAILURE) { logger.debug("Could not blindly create remote directory due to " + e.getMessage(), e); return; } else { throw new IOException("Could not blindly create remote directory due to " + e.getMessage(), e); } } } else { try { channel.stat(remoteDirectory); return; } catch (final SftpException e) { if (e.id != ChannelSftp.SSH_FX_NO_SUCH_FILE) { throw new IOException("Failed to determine if remote directory exists at " + remoteDirectory + " due to " + e, e); } } } if (directoryName.getParent() != null && !directoryName.getParentFile().equals(new File(File.separator))) { ensureDirectoryExists(flowFile, directoryName.getParentFile()); } logger.debug("Remote Directory {} does not exist; creating it", new Object[] {remoteDirectory}); try { channel.mkdir(remoteDirectory); logger.debug("Created {}", new Object[] {remoteDirectory}); } catch (final SftpException e) { throw new IOException("Failed to create remote directory " + remoteDirectory + " due to " + e, e); } } SFTPTransfer(final ProcessContext processContext, final ComponentLog logger); }
SFTPTransfer implements FileTransfer { @Override public void ensureDirectoryExists(final FlowFile flowFile, final File directoryName) throws IOException { final ChannelSftp channel = getChannel(flowFile); final String remoteDirectory = directoryName.getAbsolutePath().replace("\\", "/").replaceAll("^.\\:", ""); if (disableDirectoryListing) { try { channel.mkdir(remoteDirectory); return; } catch (SftpException e) { if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) { logger.debug(String.format("Could not create %s due to 'No such file'. Will try to create the parent dir.", remoteDirectory)); } else if (e.id == ChannelSftp.SSH_FX_FAILURE) { logger.debug("Could not blindly create remote directory due to " + e.getMessage(), e); return; } else { throw new IOException("Could not blindly create remote directory due to " + e.getMessage(), e); } } } else { try { channel.stat(remoteDirectory); return; } catch (final SftpException e) { if (e.id != ChannelSftp.SSH_FX_NO_SUCH_FILE) { throw new IOException("Failed to determine if remote directory exists at " + remoteDirectory + " due to " + e, e); } } } if (directoryName.getParent() != null && !directoryName.getParentFile().equals(new File(File.separator))) { ensureDirectoryExists(flowFile, directoryName.getParentFile()); } logger.debug("Remote Directory {} does not exist; creating it", new Object[] {remoteDirectory}); try { channel.mkdir(remoteDirectory); logger.debug("Created {}", new Object[] {remoteDirectory}); } catch (final SftpException e) { throw new IOException("Failed to create remote directory " + remoteDirectory + " due to " + e, e); } } SFTPTransfer(final ProcessContext processContext, final ComponentLog logger); static void validateProxySpec(ValidationContext context, Collection<ValidationResult> results); @Override String getProtocolName(); @Override List<FileInfo> getListing(); @Override InputStream getInputStream(final String remoteFileName); @Override InputStream getInputStream(final String remoteFileName, final FlowFile flowFile); @Override void flush(); @Override boolean flush(final FlowFile flowFile); @Override void deleteFile(final FlowFile flowFile, final String path, final String remoteFileName); @Override void deleteDirectory(final FlowFile flowFile, final String remoteDirectoryName); @Override void ensureDirectoryExists(final FlowFile flowFile, final File directoryName); @Override String getHomeDirectory(final FlowFile flowFile); @Override void close(); @Override boolean isClosed(); @Override @SuppressWarnings("unchecked") FileInfo getRemoteFileInfo(final FlowFile flowFile, final String path, String filename); @Override String put(final FlowFile flowFile, final String path, final String filename, final InputStream content); @Override void rename(final FlowFile flowFile, final String source, final String target); }
SFTPTransfer implements FileTransfer { @Override public void ensureDirectoryExists(final FlowFile flowFile, final File directoryName) throws IOException { final ChannelSftp channel = getChannel(flowFile); final String remoteDirectory = directoryName.getAbsolutePath().replace("\\", "/").replaceAll("^.\\:", ""); if (disableDirectoryListing) { try { channel.mkdir(remoteDirectory); return; } catch (SftpException e) { if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) { logger.debug(String.format("Could not create %s due to 'No such file'. Will try to create the parent dir.", remoteDirectory)); } else if (e.id == ChannelSftp.SSH_FX_FAILURE) { logger.debug("Could not blindly create remote directory due to " + e.getMessage(), e); return; } else { throw new IOException("Could not blindly create remote directory due to " + e.getMessage(), e); } } } else { try { channel.stat(remoteDirectory); return; } catch (final SftpException e) { if (e.id != ChannelSftp.SSH_FX_NO_SUCH_FILE) { throw new IOException("Failed to determine if remote directory exists at " + remoteDirectory + " due to " + e, e); } } } if (directoryName.getParent() != null && !directoryName.getParentFile().equals(new File(File.separator))) { ensureDirectoryExists(flowFile, directoryName.getParentFile()); } logger.debug("Remote Directory {} does not exist; creating it", new Object[] {remoteDirectory}); try { channel.mkdir(remoteDirectory); logger.debug("Created {}", new Object[] {remoteDirectory}); } catch (final SftpException e) { throw new IOException("Failed to create remote directory " + remoteDirectory + " due to " + e, e); } } SFTPTransfer(final ProcessContext processContext, final ComponentLog logger); static void validateProxySpec(ValidationContext context, Collection<ValidationResult> results); @Override String getProtocolName(); @Override List<FileInfo> getListing(); @Override InputStream getInputStream(final String remoteFileName); @Override InputStream getInputStream(final String remoteFileName, final FlowFile flowFile); @Override void flush(); @Override boolean flush(final FlowFile flowFile); @Override void deleteFile(final FlowFile flowFile, final String path, final String remoteFileName); @Override void deleteDirectory(final FlowFile flowFile, final String remoteDirectoryName); @Override void ensureDirectoryExists(final FlowFile flowFile, final File directoryName); @Override String getHomeDirectory(final FlowFile flowFile); @Override void close(); @Override boolean isClosed(); @Override @SuppressWarnings("unchecked") FileInfo getRemoteFileInfo(final FlowFile flowFile, final String path, String filename); @Override String put(final FlowFile flowFile, final String path, final String filename, final InputStream content); @Override void rename(final FlowFile flowFile, final String source, final String target); static final PropertyDescriptor PRIVATE_KEY_PATH; static final PropertyDescriptor PRIVATE_KEY_PASSPHRASE; static final PropertyDescriptor HOST_KEY_FILE; static final PropertyDescriptor STRICT_HOST_KEY_CHECKING; static final PropertyDescriptor PORT; static final PropertyDescriptor USE_KEEPALIVE_ON_TIMEOUT; static final PropertyDescriptor DISABLE_DIRECTORY_LISTING; static final PropertyDescriptor PROXY_CONFIGURATION_SERVICE; }