List of usage examples for org.apache.commons.net.ftp FTPClient disconnect
@Override public void disconnect() throws IOException
From source file:org.abstracthorizon.proximity.storage.remote.CommonsNetFtpRemotePeer.java
public boolean containsItem(String path) throws StorageException { FTPClient client = null; try {// w w w.j av a2 s . com client = getFTPClient(); try { if (client.changeWorkingDirectory( concatPaths(getRemoteUrl().getPath(), FilenameUtils.getPath(path)))) { FTPFile[] fileList = client.listFiles(FilenameUtils.getName(path)); if (fileList.length == 1) { return true; } else { return false; } } else { return false; } } catch (IOException ex) { throw new StorageException("Cannot execute FTP operation on remote peer.", ex); } } finally { try { if (client.isConnected()) { client.disconnect(); } } catch (IOException ex) { logger.warn("Could not disconnect FTPClient", ex); } } }
From source file:org.abstracthorizon.proximity.storage.remote.CommonsNetFtpRemotePeer.java
public Item retrieveItem(String path, boolean propsOnly) throws ItemNotFoundException, StorageException { String originatingUrlString = getAbsoluteUrl(path); FTPClient client = null; try {//from www . jav a2s . c o m client = getFTPClient(); try { if (client.changeWorkingDirectory( concatPaths(getRemoteUrl().getPath(), FilenameUtils.getPath(path)))) { FTPFile[] fileList = client.listFiles(FilenameUtils.getName(path)); if (fileList.length == 1) { FTPFile ftpFile = fileList[0]; ItemProperties properties = constructItemPropertiesFromGetResponse(path, originatingUrlString, ftpFile); Item result = new Item(); if (properties.isFile()) { // TODO: Solve this in a better way File tmpFile = File.createTempFile(FilenameUtils.getName(path), null); tmpFile.deleteOnExit(); FileOutputStream fos = new FileOutputStream(tmpFile); try { client.retrieveFile(FilenameUtils.getName(path), fos); fos.flush(); } finally { fos.close(); } result.setStream(new DeleteOnCloseFileInputStream(tmpFile)); } else { result.setStream(null); } result.setProperties(properties); return result; } else { throw new ItemNotFoundException( "Item " + path + " not found in FTP remote peer of " + getRemoteUrl()); } } else { throw new ItemNotFoundException("Path " + FilenameUtils.getPath(path) + " not found in FTP remote peer of " + getRemoteUrl()); } } catch (IOException ex) { throw new StorageException("Cannot execute FTP operation on remote peer.", ex); } } finally { try { if (client.isConnected()) { client.disconnect(); } } catch (IOException ex) { logger.warn("Could not disconnect FTPClient", ex); } } }
From source file:org.alfresco.bm.file.FtpTestFileService.java
/** * Does a listing of files on the FTP server *//*from w ww.java2 s .c om*/ @Override protected List<FileData> listRemoteFiles() { // Get a list of files from the FTP server FTPClient ftp = null; FTPFile[] ftpFiles = new FTPFile[0]; try { ftp = getFTPClient(); if (!ftp.changeWorkingDirectory(ftpPath)) { throw new IOException("Failed to change directory (leading '/' could be a problem): " + ftpPath); } ftpFiles = ftp.listFiles(); } catch (IOException e) { throw new RuntimeException("FTP file listing failed: " + this, e); } finally { try { if (null != ftp) { ftp.logout(); ftp.disconnect(); } } catch (IOException e) { logger.warn("Failed to close FTP connection: " + e.getMessage()); } } // Index each of the files List<FileData> remoteFileDatas = new ArrayList<FileData>(ftpFiles.length); for (FTPFile ftpFile : ftpFiles) { String ftpFilename = ftpFile.getName(); // Watch out for . and .. if (ftpFilename.equals(".") || ftpFilename.equals("..")) { continue; } String ftpExtension = FileData.getExtension(ftpFilename); long ftpSize = ftpFile.getSize(); FileData remoteFileData = new FileData(); remoteFileData.setRemoteName(ftpFilename); remoteFileData.setExtension(ftpExtension); remoteFileData.setSize(ftpSize); remoteFileDatas.add(remoteFileData); } // Done return remoteFileDatas; }
From source file:org.alfresco.bm.file.FtpTestFileService.java
@Override protected void downloadRemoteFile(FileData fileData, File localFile) throws IOException { String remoteName = ftpPath + "/" + fileData.getRemoteName(); FTPClient ftp = null; FileOutputStream fos = new FileOutputStream(localFile); OutputStream bos = null;/*from w w w. j a v a2 s . com*/ try { bos = new BufferedOutputStream(fos); // It does not exist locally, so go and retrieve it ftp = getFTPClient(); boolean success = ftp.retrieveFile(remoteName, bos); if (!success) { throw new IOException("Failed to complete download of file: " + fileData + " by " + this); } } finally { if (bos != null) { try { bos.close(); } catch (Throwable e) { } } if (fos != null) { try { fos.close(); } catch (Throwable e) { } } try { if (null != ftp) { ftp.logout(); ftp.disconnect(); } } catch (IOException e) { logger.warn("Failed to close FTP connection: " + e.getMessage()); } } }
From source file:org.alfresco.filesys.FTPServerTest.java
/** * Simple test that connects to the inbuilt ftp server and logs on * /* w ww . j a va 2s .c om*/ * @throws Exception */ public void testFTPConnect() throws Exception { logger.debug("Start testFTPConnect"); FTPClient ftp = connectClient(); try { int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { fail("FTP server refused connection."); } boolean login = ftp.login(USER_ADMIN, PASSWORD_ADMIN); assertTrue("admin login not successful", login); } finally { ftp.disconnect(); } }
From source file:org.alfresco.filesys.FTPServerTest.java
/** * Simple negative test that connects to the inbuilt ftp server and attempts to * log on with the wrong password./* w w w. ja va 2s . c o m*/ * * @throws Exception */ public void testFTPConnectNegative() throws Exception { logger.debug("Start testFTPConnectNegative"); FTPClient ftp = connectClient(); try { int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { fail("FTP server refused connection."); } boolean login = ftp.login(USER_ADMIN, "garbage"); assertFalse("admin login successful", login); // now attempt to list the files and check that the command does not // succeed FTPFile[] files = ftp.listFiles(); assertNotNull(files); assertTrue(files.length == 0); reply = ftp.getReplyCode(); assertTrue(FTPReply.isNegativePermanent(reply)); } finally { ftp.disconnect(); } }
From source file:org.alfresco.filesys.FTPServerTest.java
/** * Test CWD for FTP server//from w w w.j a v a 2 s . c om * * @throws Exception */ public void testCWD() throws Exception { logger.debug("Start testCWD"); FTPClient ftp = connectClient(); try { int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { fail("FTP server refused connection."); } boolean login = ftp.login(USER_ADMIN, PASSWORD_ADMIN); assertTrue("admin login successful", login); FTPFile[] files = ftp.listFiles(); reply = ftp.getReplyCode(); assertTrue(FTPReply.isPositiveCompletion(reply)); assertTrue(files.length == 1); boolean foundAlfresco = false; for (FTPFile file : files) { logger.debug("file name=" + file.getName()); assertTrue(file.isDirectory()); if (file.getName().equalsIgnoreCase("Alfresco")) { foundAlfresco = true; } } assertTrue(foundAlfresco); // Change to Alfresco Dir that we know exists reply = ftp.cwd("/Alfresco"); assertTrue(FTPReply.isPositiveCompletion(reply)); // relative path with space char reply = ftp.cwd("Data Dictionary"); assertTrue(FTPReply.isPositiveCompletion(reply)); // non existant absolute reply = ftp.cwd("/Garbage"); assertTrue(FTPReply.isNegativePermanent(reply)); reply = ftp.cwd("/Alfresco/User Homes"); assertTrue(FTPReply.isPositiveCompletion(reply)); // Wild card reply = ftp.cwd("/Alfresco/User*Homes"); assertTrue("unable to change to /Alfresco User*Homes/", FTPReply.isPositiveCompletion(reply)); // // Single char pattern match // reply = ftp.cwd("/Alfre?co"); // assertTrue("Unable to match single char /Alfre?co", FTPReply.isPositiveCompletion(reply)); // two level folder reply = ftp.cwd("/Alfresco/Data Dictionary"); assertTrue("unable to change to /Alfresco/Data Dictionary", FTPReply.isPositiveCompletion(reply)); // go up one reply = ftp.cwd(".."); assertTrue("unable to change to ..", FTPReply.isPositiveCompletion(reply)); reply = ftp.pwd(); ftp.getStatus(); assertTrue("unable to get status", FTPReply.isPositiveCompletion(reply)); // check we are at the correct point in the tree reply = ftp.cwd("Data Dictionary"); assertTrue(FTPReply.isPositiveCompletion(reply)); } finally { ftp.disconnect(); } }
From source file:org.alfresco.filesys.FTPServerTest.java
/** * Test CRUD for FTP server//from w w w . jav a 2 s . c o m * * @throws Exception */ public void testCRUD() throws Exception { final String PATH1 = "FTPServerTest"; final String PATH2 = "Second part"; logger.debug("Start testFTPCRUD"); FTPClient ftp = connectClient(); try { int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { fail("FTP server refused connection."); } boolean login = ftp.login(USER_ADMIN, PASSWORD_ADMIN); assertTrue("admin login successful", login); reply = ftp.cwd("/Alfresco/User Homes"); assertTrue(FTPReply.isPositiveCompletion(reply)); // Delete the root directory in case it was left over from a previous test run try { ftp.removeDirectory(PATH1); } catch (IOException e) { // ignore this error } // make root directory ftp.makeDirectory(PATH1); ftp.cwd(PATH1); // make sub-directory in new directory ftp.makeDirectory(PATH2); ftp.cwd(PATH2); // List the files in the new directory FTPFile[] files = ftp.listFiles(); assertTrue("files not empty", files.length == 0); // Create a file String FILE1_CONTENT_1 = "test file 1 content"; String FILE1_NAME = "testFile1.txt"; ftp.appendFile(FILE1_NAME, new ByteArrayInputStream(FILE1_CONTENT_1.getBytes("UTF-8"))); // Get the new file FTPFile[] files2 = ftp.listFiles(); assertTrue("files not one", files2.length == 1); InputStream is = ftp.retrieveFileStream(FILE1_NAME); String content = inputStreamToString(is); assertEquals("Content is not as expected", content, FILE1_CONTENT_1); ftp.completePendingCommand(); // Update the file contents String FILE1_CONTENT_2 = "That's how it is says Pooh!"; ftp.storeFile(FILE1_NAME, new ByteArrayInputStream(FILE1_CONTENT_2.getBytes("UTF-8"))); InputStream is2 = ftp.retrieveFileStream(FILE1_NAME); String content2 = inputStreamToString(is2); assertEquals("Content is not as expected", FILE1_CONTENT_2, content2); ftp.completePendingCommand(); // now delete the file we have been using. assertTrue(ftp.deleteFile(FILE1_NAME)); // negative test - file should have gone now. assertFalse(ftp.deleteFile(FILE1_NAME)); } finally { // clean up tree if left over from previous run ftp.disconnect(); } }
From source file:org.alfresco.filesys.FTPServerTest.java
/** * Test of obscure path names in the FTP server * //from www .j a v a 2 s. co m * RFC959 states that paths are constructed thus... * <string> ::= <char> | <char><string> * <pathname> ::= <string> * <char> ::= any of the 128 ASCII characters except <CR> and <LF> * * So we need to check how high characters and problematic are encoded */ public void testPathNames() throws Exception { logger.debug("Start testPathNames"); FTPClient ftp = connectClient(); String PATH1 = "testPathNames"; try { int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { fail("FTP server refused connection."); } boolean login = ftp.login(USER_ADMIN, PASSWORD_ADMIN); assertTrue("admin login successful", login); reply = ftp.cwd("/Alfresco/User*Homes"); assertTrue(FTPReply.isPositiveCompletion(reply)); // Delete the root directory in case it was left over from a previous test run try { ftp.removeDirectory(PATH1); } catch (IOException e) { // ignore this error } // make root directory for this test boolean success = ftp.makeDirectory(PATH1); assertTrue("unable to make directory:" + PATH1, success); success = ftp.changeWorkingDirectory(PATH1); assertTrue("unable to change to working directory:" + PATH1, success); assertTrue("with a space", ftp.makeDirectory("test space")); assertTrue("with exclamation", ftp.makeDirectory("space!")); assertTrue("with dollar", ftp.makeDirectory("space$")); assertTrue("with brackets", ftp.makeDirectory("space()")); assertTrue("with hash curley brackets", ftp.makeDirectory("space{}")); //Pound sign U+00A3 //Yen Sign U+00A5 //Capital Omega U+03A9 assertTrue("with pound sign", ftp.makeDirectory("pound \u00A3.world")); assertTrue("with yen sign", ftp.makeDirectory("yen \u00A5.world")); // Test steps that do not work // assertTrue("with omega", ftp.makeDirectory("omega \u03A9.world")); // assertTrue("with obscure ASCII chars", ftp.makeDirectory("?/.,<>")); } finally { // clean up tree if left over from previous run ftp.disconnect(); } }
From source file:org.alfresco.filesys.FTPServerTest.java
/** * Test of rename case ALF-20584//from w w w . j a va 2 s.c om * */ public void testRenameCase() throws Exception { logger.debug("Start testRenameCase"); FTPClient ftp = connectClient(); String PATH1 = "testRenameCase"; try { int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { fail("FTP server refused connection."); } boolean login = ftp.login(USER_ADMIN, PASSWORD_ADMIN); assertTrue("admin login successful", login); reply = ftp.cwd("/Alfresco/User*Homes"); assertTrue(FTPReply.isPositiveCompletion(reply)); // Delete the root directory in case it was left over from a previous test run try { ftp.removeDirectory(PATH1); } catch (IOException e) { // ignore this error } // make root directory for this test boolean success = ftp.makeDirectory(PATH1); assertTrue("unable to make directory:" + PATH1, success); ftp.cwd(PATH1); String FILE1_CONTENT_2 = "That's how it is says Pooh!"; ftp.storeFile("FileA.txt", new ByteArrayInputStream(FILE1_CONTENT_2.getBytes("UTF-8"))); assertTrue("unable to rename", ftp.rename("FileA.txt", "FILEA.TXT")); } finally { // clean up tree if left over from previous run ftp.disconnect(); } }