List of usage examples for org.apache.commons.net.ftp FTPClient connect
public void connect(InetAddress host, int port, InetAddress localAddr, int localPort) throws SocketException, IOException
From source file:org.sipfoundry.preflight.FTP.java
public ResultCode validate(int timeout, NetworkResources networkResources, JournalService journalService, InetAddress bindAddress) { ResultCode results = NONE;//from w ww .jav a 2s. com InetAddress ftpServerAddress = null; String testFile = new String("00D01EFFFFFE"); String[] verificationStrings = { "LIP-68XX configuration information", "[VOIP]", "outbound_proxy_server", "[PROVISION]", "decrypt_key" }; if (networkResources.configServer == null) { journalService.println("No FTP server provided, skipping test.\n"); return CONFIG_SERVER_MISSING; } journalService.println("Starting FTP server test."); if (IPAddressUtil.isLiteralIPAddress(networkResources.configServer)) { try { ftpServerAddress = InetAddress.getByName(networkResources.configServer); } catch (UnknownHostException e) { // Should never get here. e.printStackTrace(); } journalService.println("Using FTP server literal address: " + networkResources.configServer); } else { // Try to retrieve A RECORD for FTP server, checking each DNS server. SimpleResolver resolver = null; try { resolver = new SimpleResolver(); resolver.setLocalAddress(bindAddress); resolver.setTimeout(timeout); } catch (UnknownHostException e) { e.printStackTrace(); } for (InetAddress dnsServer : networkResources.domainNameServers) { journalService.println( "Looking up FTP server address via DNS server: " + dnsServer.getCanonicalHostName()); String targetMessage = new String(" FTP server address \"" + networkResources.configServer + "\""); resolver.setAddress(dnsServer); Lookup aLookup = null; try { aLookup = new Lookup(networkResources.configServer, Type.A); } catch (TextParseException e) { journalService.println(" is malformed.\n"); journalService.println(targetMessage); return FTP_ADDRESS_MALFORMED; } aLookup.setResolver(resolver); Record[] aRecords = aLookup.run(); switch (aLookup.getResult()) { case Lookup.SUCCESSFUL: if (aRecords != null) { InetAddress targetAddress = ((ARecord) aRecords[0]).getAddress(); targetMessage += " resolves to: " + targetAddress.getHostAddress(); journalService.println(targetMessage); if (ftpServerAddress == null) { ftpServerAddress = targetAddress; } else { // Check that multiple lookups result in same // address. if (!ftpServerAddress.equals(targetAddress)) { journalService.println(" FTP server address does not match prior lookup."); results = MULTIPLE_CONFIG_TARGETS; } } } else { targetMessage += " could not be resolved."; journalService.println(targetMessage); results = FTP_TARGET_UNRESOLVED; } break; case Lookup.UNRECOVERABLE: targetMessage += " [Unrecoverable error]"; journalService.println(targetMessage); results = FTP_TARGET_UNRESOLVED; break; case Lookup.TRY_AGAIN: targetMessage += " [Lookup timeout]"; journalService.println(targetMessage); results = FTP_TARGET_UNRESOLVED; break; case Lookup.HOST_NOT_FOUND: targetMessage += " could not be resolved."; journalService.println(targetMessage); results = FTP_TARGET_UNRESOLVED; break; case Lookup.TYPE_NOT_FOUND: targetMessage += " could not be resolved."; journalService.println(targetMessage); results = FTP_TARGET_UNRESOLVED; break; } } } if ((ftpServerAddress == null) || (results == MULTIPLE_CONFIG_TARGETS)) { journalService.println("Cannot recover from previous errors, aborting FTP test.\n"); return results; } journalService.println("Beginning FTP get request of test file: " + testFile); // Open the FTP connection. FTPClient ftp = new FTPClient(); ftp.setDefaultTimeout(timeout * 1000); ftp.addProtocolCommandListener(new PrintCommandListener(journalService)); try { int reply; ftp.connect(ftpServerAddress, 21, bindAddress, bindPort); // After connection, check reply code to verify. reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); journalService.println("FTP client failure: " + reply + "\n"); return FTP_CLIENT_FAILURE; } } catch (IOException e) { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException f) { // Ignore. } } journalService.println("FTP client failure: " + e.getMessage() + "\n"); return FTP_CLIENT_FAILURE; } try { if (!ftp.login(ftpUser, ftpPassword)) { ftp.logout(); journalService.println("FTP client unable to log in.\n"); return FTP_GET_FAILED; } journalService.println("FTP client connected to: " + ftp.getSystemName()); ftp.enterLocalPassiveMode(); ByteArrayOutputStream output = new ByteArrayOutputStream(); ftp.retrieveFile(testFile, output); // After receiving, check reply code to verify. int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); journalService.println("FTP get failure: " + reply + "\n"); return FTP_GET_FAILED; } ftp.logout(); String testFileContents = output.toString(); boolean verified = true; for (String verificationString : verificationStrings) { if (!testFileContents.contains(verificationString)) { verified = false; } } if (verified) { journalService.println("File received successfully."); } else { journalService.println("File received but contents do not verify."); System.err.println(testFileContents); results = FTP_CONTENTS_FAILED; } } catch (FTPConnectionClosedException e) { journalService.println("FTP server closed connection prematurely.\n"); return FTP_GET_FAILED; } catch (IOException e) { journalService.println("FTP client failure. " + e.getMessage() + "\n"); return FTP_CLIENT_FAILURE; } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException f) { // Ignore. } } } journalService.println(""); return results; }