FtpRetriever implements ImageRetrieverInterface { @Override public void setupDataRetrieval(Map<String, ?> dataSourceConfig, ObjectRecognitionCore source) throws Exception { log = LoggerFactory.getLogger(this.getClass().getCanonicalName() + "#" + source.getSourceName()); if (dataSourceConfig.get(NO_DEFAULT) instanceof Boolean && (Boolean) dataSourceConfig.get(NO_DEFAULT)) { noDefault = true; return; } if (dataSourceConfig.get(SERVER) instanceof String) { server = obtainDomainName((String) dataSourceConfig.get(SERVER)); } else { throw new Exception(this.getClass().getCanonicalName() + ".noServerSpecified: " + "No server was specified in the configuration setup." ); } if (dataSourceConfig.get(CON_TYPE) instanceof String) { String type = (String) dataSourceConfig.get(CON_TYPE); if (type.equalsIgnoreCase(FTPS)) { conType = FTPS; if (dataSourceConfig.get(IMPLICIT) instanceof Boolean && (Boolean) dataSourceConfig.get(IMPLICIT)) { isImplicit = true; } if (dataSourceConfig.get(PROTOCOL) instanceof String) { protocol = (String) dataSourceConfig.get(PROTOCOL); } } else if (type.equalsIgnoreCase(SFTP)) { conType = SFTP; } else if (!type.equalsIgnoreCase(FTP)) { log.warn("Unexpected value of 'conType'. Should be 'ftps', 'sftp', or 'ftp'. Defaulting to 'ftp'"); } } if (dataSourceConfig.get(USERNAME) instanceof String) { username = (String) dataSourceConfig.get(USERNAME); } else { throw new Exception(this.getClass().getCanonicalName() + ".noUsername: " + "No username was given in the configuration setup"); } if (dataSourceConfig.get(PASSWORD) instanceof String) { password = (String) dataSourceConfig.get("password"); } else { throw new Exception(this.getClass().getCanonicalName() + ".noPassword: " + "No password was given in the configuration setup"); } if (conType == FTP || conType == FTPS) { try { ftpClient = connectToFtpServer(server, username, password, conType, protocol, isImplicit); } catch (Exception e) { throw new Exception(this.getClass().getCanonicalName() + "." + e.getMessage() , e); } } else { try { session = connectToSftpServer(server, username, password); } catch (Exception e) { throw new Exception(this.getClass().getCanonicalName() + "." + e.getMessage() , e); } } }  @Override void setupDataRetrieval(Map<String, ?> dataSourceConfig, ObjectRecognitionCore source); @Override ImageRetrieverResults getImage(); @Override ImageRetrieverResults getImage(Map<String, ?> request); @Override void close(); FTPClient connectToFtpServer(String server, String username,
                String password, String conType, String protocol, boolean isImplicit); Session connectToSftpServer(String server, String username, String password); ByteArrayOutputStream readFromFtpServer(FTPClient client, String filePath); ChannelSftp createChannelFromSession(Session session); ByteArrayOutputStream readFromSftpServer(ChannelSftp sftpChannel, String fileName); String obtainDomainName(String url); void quietClose(Closeable closeable);  }
@Test public void testSetup() { Map<String, String> config = workingFtpConfig(); try { retriever.setupDataRetrieval(config, source); } catch (Exception e) { fail("Should not fail with full config. exception message was : " + e.getMessage()); } config = workingFtpConfig(); config.remove("username"); try { retriever.setupDataRetrieval(config, source); fail("Should fail when missing username"); } catch (Exception e) { assertTrue("Failure should be caused by a missing username. Error actually was: " + e.getMessage() , e.getMessage().startsWith(FtpRetriever.class.getCanonicalName() + ".noUsername")); } config = workingFtpConfig(); config.remove("password"); try { retriever.setupDataRetrieval(config, source); fail("Should fail when missing password"); } catch (Exception e) { assertTrue("Failure should be caused by a missing password. Error actually was: " + e.getMessage() , e.getMessage().startsWith(FtpRetriever.class.getCanonicalName() + ".noPassword")); } config = workingFtpConfig(); config.remove("server"); try { retriever.setupDataRetrieval(config, source); fail("Should fail when missing server"); } catch (Exception e) { assertTrue("Failure should be caused by a missing server URL. Error actually was: " + e.getMessage() , e.getMessage().startsWith(FtpRetriever.class.getCanonicalName() + ".noServerSpecified")); } config = workingFtpConfig(); config.remove("conType"); try { retriever.setupDataRetrieval(config, source); } catch (Exception e) { fail("Should use default FTP when missing conType"); } }