List of usage examples for org.apache.commons.net.ftp FTPClient disconnect
@Override public void disconnect() throws 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. j a v a2 s .c o m*/ 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; }
From source file:org.soitoolkit.commons.mule.ftp.FtpUtil.java
/** * Ensures that the directory exists and is writable by deleting the * directory and then recreate it./*from w ww. ja v a 2s . c om*/ * * @param muleContext * @param endpointName * @throws Exception */ public static void initEndpointDirectory(MuleContext muleContext, String endpointName) throws Exception { logger.info("Init directory for endpoint: {}", endpointName); FTPClient ftpClient = null; try { ftpClient = getFtpClient(muleContext, endpointName); EndpointURI endpointURI = getImmutableEndpoint(muleContext, endpointName).getEndpointURI(); String path = endpointURI.getPath(); try { if (path.startsWith("/~/")) { path = path.substring(3); // Strip off the leading "/~/" to apply the command to the home-folder } else if (path.startsWith("/")) { path = path.substring(1); // Strip off the leading "/" to apply the command to the home-folder } recursiveDeleteDirectory(ftpClient, path); recursiveCreateDirectory(ftpClient, path); } catch (IOException e) { if (logger.isErrorEnabled()) logger.error("Failed to recursivly delete endpoint " + endpointName, e); } } finally { if (ftpClient != null) { ftpClient.disconnect(); } } }
From source file:org.spongepowered.repoindexer.FTPUtils.java
public static boolean uploadFTP(String user, String pass, String server, File local, String remoteLocation) { FTPClient ftpClient = new FTPClient(); boolean done = false; try {//w ww. ja va 2 s. c om ftpClient.connect(server); ftpClient.login(user, pass); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); InputStream inputStream = new FileInputStream(local); done = ftpClient.storeFile(remoteLocation, inputStream); inputStream.close(); } catch (IOException ex) { System.out.println("Error: " + ex.getMessage()); ex.printStackTrace(); return false; } finally { try { if (ftpClient.isConnected()) { ftpClient.logout(); ftpClient.disconnect(); } } catch (IOException ex) { ex.printStackTrace(); } return done; } }
From source file:org.springframework.integration.ftp.QueuedFtpClientPool.java
public void releaseClient(FTPClient client) { if ((client != null) && !pool.offer(client)) { try {// ww w .j a va 2 s .c o m client.disconnect(); } catch (IOException e) { log.warn("Error disconnecting ftpclient", e); } } }
From source file:org.structr.files.ftp.FtpAccessTest.java
public void test01LoginFailed() { try (final Tx tx = StructrApp.getInstance(securityContext).tx()) { FTPClient ftp = new FTPClient(); ftp.connect("127.0.0.1", ftpPort); logger.log(Level.INFO, "Reply from FTP server:", ftp.getReplyString()); int reply = ftp.getReplyCode(); assertTrue(FTPReply.isPositiveCompletion(reply)); boolean loginSuccess = ftp.login("jt978hasdl", "lj3498ha"); logger.log(Level.INFO, "Try to login as jt978hasdl/lj3498ha:", loginSuccess); assertFalse(loginSuccess);// w w w . jav a2s .c o m ftp.disconnect(); } catch (IOException | FrameworkException ex) { logger.log(Level.SEVERE, "Error in FTP test", ex); fail("Unexpected exception: " + ex.getMessage()); } }
From source file:org.structr.files.ftp.FtpDirectoriesTest.java
public void test01ListDirectories() { final String name1 = "FTPdir1"; final String name2 = "FTPdir2"; FTPClient ftp = setupFTPClient(); FTPFile[] dirs = null;//from w ww . j av a 2s. c om try (final Tx tx = app.tx()) { dirs = ftp.listDirectories(); assertNotNull(dirs); assertEquals(0, dirs.length); // Create folder by API methods createFTPDirectory(null, name1); tx.success(); } catch (IOException | FrameworkException ex) { ex.printStackTrace(); fail("Unexpected exception: " + ex.getMessage()); } try (final Tx tx = app.tx()) { dirs = ftp.listDirectories(); assertNotNull(dirs); assertEquals(1, dirs.length); assertEquals(name1, dirs[0].getName()); // Create second folder in / createFTPDirectory(null, name2); tx.success(); } catch (IOException | FrameworkException ex) { ex.printStackTrace(); fail("Unexpected exception: " + ex.getMessage()); } try (final Tx tx = app.tx()) { dirs = ftp.listDirectories(); assertNotNull(dirs); assertEquals(2, dirs.length); assertEquals(name1, dirs[0].getName()); assertEquals(name2, dirs[1].getName()); ftp.disconnect(); tx.success(); } catch (IOException | FrameworkException ex) { ex.printStackTrace(); fail("Unexpected exception: " + ex.getMessage()); } }
From source file:org.structr.files.ftp.FtpDirectoriesTest.java
public void test02MkDir() { FTPClient ftp = setupFTPClient(); FTPFile[] dirs = null;/*from w w w . j a v a 2 s .com*/ final String name1 = "FTPdir1"; final String name2 = "FTPdir2"; boolean success = false; try (final Tx tx = StructrApp.getInstance(securityContext).tx()) { assertEmptyDirectory(ftp); // Create folder by mkdir FTP command success = ftp.makeDirectory(name1); assertTrue(success); tx.success(); } catch (IOException | FrameworkException ex) { ex.printStackTrace(); fail("Unexpected exception: " + ex.getMessage()); } try (final Tx tx = app.tx()) { dirs = ftp.listDirectories(); assertNotNull(dirs); assertEquals(1, dirs.length); assertEquals(name1, dirs[0].getName()); // Create second folder in / success = ftp.makeDirectory(name2); assertTrue(success); tx.success(); } catch (IOException | FrameworkException ex) { ex.printStackTrace(); fail("Unexpected exception: " + ex.getMessage()); } try (final Tx tx = app.tx()) { dirs = ftp.listDirectories(); assertNotNull(dirs); assertEquals(2, dirs.length); assertEquals(name1, dirs[0].getName()); assertEquals(name2, dirs[1].getName()); ftp.disconnect(); tx.success(); } catch (IOException | FrameworkException ex) { ex.printStackTrace(); fail("Unexpected exception: " + ex.getMessage()); } }
From source file:org.structr.files.ftp.FtpDirectoriesTest.java
public void test03MkdirCd() { FTPClient ftp = setupFTPClient(); final String name1 = "/FTPdir1"; try (final Tx tx = app.tx()) { FTPFile[] dirs = ftp.listDirectories(); assertNotNull(dirs);/* w w w . j a v a 2 s . c om*/ assertEquals(0, dirs.length); // Create folder by mkdir FTP command ftp.makeDirectory(name1); tx.success(); } catch (IOException | FrameworkException ex) { ex.printStackTrace(); fail("Unexpected exception: " + ex.getMessage()); } try (final Tx tx = app.tx()) { ftp.changeWorkingDirectory(name1); assertEmptyDirectory(ftp); String newWorkingDirectory = ftp.printWorkingDirectory(); assertEquals(name1, newWorkingDirectory); ftp.disconnect(); tx.success(); } catch (IOException | FrameworkException ex) { ex.printStackTrace(); fail("Unexpected exception: " + ex.getMessage()); } }
From source file:org.structr.files.ftp.FtpDirectoriesTest.java
public void test04MkdirCdMkdirCd() { FTPClient ftp = setupFTPClient(); try (final Tx tx = StructrApp.getInstance(securityContext).tx()) { assertEmptyDirectory(ftp);/*w ww .j a v a2 s . co m*/ String name1 = "/FTPdir1"; // Create folder by mkdir FTP command ftp.makeDirectory(name1); ftp.changeWorkingDirectory(name1); String newWorkingDirectory = ftp.printWorkingDirectory(); assertEquals(name1, newWorkingDirectory); assertEmptyDirectory(ftp); String name2 = name1.concat("/").concat("FTPdir2"); // Create folder by mkdir FTP command ftp.makeDirectory(name2); ftp.changeWorkingDirectory(name2); newWorkingDirectory = ftp.printWorkingDirectory(); assertEquals(name2, newWorkingDirectory); assertEmptyDirectory(ftp); ftp.disconnect(); tx.success(); } catch (IOException | FrameworkException ex) { ex.printStackTrace(); fail("Unexpected exception: " + ex.getMessage()); } }
From source file:org.structr.files.ftp.FtpDirectoriesTest.java
public void test05CdUp() { FTPClient ftp = setupFTPClient(); try (final Tx tx = StructrApp.getInstance(securityContext).tx()) { assertEmptyDirectory(ftp);// w w w. j a v a2 s .c om String name1 = "/FTPdir1"; // Create folder by mkdir FTP command ftp.makeDirectory(name1); ftp.changeWorkingDirectory(name1); String name2 = name1.concat("/").concat("FTPdir2"); // Create folder by mkdir FTP command ftp.makeDirectory(name2); ftp.changeWorkingDirectory(name2); boolean success = ftp.changeToParentDirectory(); assertTrue(success); String newWorkingDirectory = ftp.printWorkingDirectory(); assertEquals(name1, newWorkingDirectory); ftp.disconnect(); tx.success(); } catch (IOException | FrameworkException ex) { logger.log(Level.SEVERE, "Error", ex); fail("Unexpected exception: " + ex.getMessage()); } }