List of usage examples for org.apache.commons.net.ftp FTPClient appendFile
public boolean appendFile(String remote, InputStream local) throws IOException
From source file:org.alfresco.filesys.FTPServerTest.java
/** * Test CRUD for FTP server//from ww w . j a va 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
/** * Create a user other than "admin" who has access to a set of files. * //from ww w . j ava 2s .c o m * Create a folder containing test.docx as user one * Update that file as user two. * Check user one can see user two's changes. * * @throws Exception */ public void testTwoUserUpdate() throws Exception { logger.debug("Start testFTPConnect"); final String TEST_DIR = "/Alfresco/User Homes/" + USER_ONE; FTPClient ftpOne = connectClient(); FTPClient ftpTwo = connectClient(); try { int reply = ftpOne.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { fail("FTP server refused connection."); } reply = ftpTwo.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { fail("FTP server refused connection."); } boolean login = ftpOne.login(USER_ONE, PASSWORD_ONE); assertTrue("user one login not successful", login); login = ftpTwo.login(USER_TWO, PASSWORD_TWO); assertTrue("user two login not successful", login); boolean success = ftpOne.changeWorkingDirectory("Alfresco"); assertTrue("user one unable to cd to Alfreco", success); success = ftpOne.changeWorkingDirectory("User*Homes"); assertTrue("user one unable to cd to User*Homes", success); success = ftpOne.changeWorkingDirectory(USER_ONE); assertTrue("user one unable to cd to " + USER_ONE, success); success = ftpTwo.changeWorkingDirectory("Alfresco"); assertTrue("user two unable to cd to Alfreco", success); success = ftpTwo.changeWorkingDirectory("User*Homes"); assertTrue("user two unable to cd to User*Homes", success); success = ftpTwo.changeWorkingDirectory(USER_ONE); assertTrue("user two unable to cd " + USER_ONE, success); // Create a file as user one String FILE1_CONTENT_1 = "test file 1 content"; String FILE1_NAME = "test.docx"; success = ftpOne.appendFile(FILE1_NAME, new ByteArrayInputStream(FILE1_CONTENT_1.getBytes("UTF-8"))); assertTrue("user one unable to append file", success); // Update the file as user two String FILE1_CONTENT_2 = "test file content updated"; success = ftpTwo.storeFile(FILE1_NAME, new ByteArrayInputStream(FILE1_CONTENT_2.getBytes("UTF-8"))); assertTrue("user two unable to append file", success); // User one should read user2's content InputStream is1 = ftpOne.retrieveFileStream(FILE1_NAME); assertNotNull("is1 is null", is1); String content1 = inputStreamToString(is1); assertEquals("Content is not as expected", FILE1_CONTENT_2, content1); ftpOne.completePendingCommand(); // User two should read user2's content InputStream is2 = ftpTwo.retrieveFileStream(FILE1_NAME); assertNotNull("is2 is null", is2); String content2 = inputStreamToString(is2); assertEquals("Content is not as expected", FILE1_CONTENT_2, content2); ftpTwo.completePendingCommand(); logger.debug("Test finished"); } finally { ftpOne.dele(TEST_DIR); if (ftpOne != null) { ftpOne.disconnect(); } if (ftpTwo != null) { ftpTwo.disconnect(); } } }
From source file:org.alfresco.filesys.FTPServerTest.java
/** * Test a quota failue exception over FTP. * A file should not exist after a create and quota exception. */// w w w .j ava 2s. c om public void testFtpQuotaAndFtp() throws Exception { // Enable usages ContentUsageImpl contentUsage = (ContentUsageImpl) applicationContext.getBean("contentUsageImpl"); contentUsage.setEnabled(true); contentUsage.init(); UserUsageTrackingComponent userUsageTrackingComponent = (UserUsageTrackingComponent) applicationContext .getBean("userUsageTrackingComponent"); userUsageTrackingComponent.setEnabled(true); userUsageTrackingComponent.bootstrapInternal(); final String TEST_DIR = "/Alfresco/User Homes/" + USER_THREE; FTPClient ftpOne = connectClient(); try { int reply = ftpOne.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { fail("FTP server refused connection."); } boolean login = ftpOne.login(USER_THREE, PASSWORD_THREE); assertTrue("user three login not successful", login); boolean success = ftpOne.changeWorkingDirectory("Alfresco"); assertTrue("user three unable to cd to Alfreco", success); success = ftpOne.changeWorkingDirectory("User*Homes"); assertTrue("user one unable to cd to User*Homes", success); success = ftpOne.changeWorkingDirectory(USER_THREE); assertTrue("user one unable to cd to " + USER_THREE, success); /** * Create a file as user three which is bigger than the quota */ String FILE3_CONTENT_3 = "test file 3 content that needs to be greater than 100 bytes to result in a quota exception being thrown"; String FILE1_NAME = "test.docx"; // Should not be success success = ftpOne.appendFile(FILE1_NAME, new ByteArrayInputStream(FILE3_CONTENT_3.getBytes("UTF-8"))); assertFalse("user one can ignore quota", success); boolean deleted = ftpOne.deleteFile(FILE1_NAME); assertFalse("quota exception expected", deleted); logger.debug("test done"); } finally { // Disable usages contentUsage.setEnabled(false); contentUsage.init(); userUsageTrackingComponent.setEnabled(false); userUsageTrackingComponent.bootstrapInternal(); ftpOne.dele(TEST_DIR); if (ftpOne != null) { ftpOne.disconnect(); } } }
From source file:org.alfresco.filesys.FTPServerTest.java
/** * Test Setting the modification time FTP server * * @throws Exception//from w w w . ja va 2s .c o m */ public void testModificationTime() throws Exception { final String PATH1 = "FTPServerTest"; final String PATH2 = "ModificationTime"; logger.debug("Start testModificationTime"); 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"))); String pathname = "/Alfresco/User Homes" + "/" + PATH1 + "/" + PATH2 + "/" + FILE1_NAME; logger.debug("set modification time"); // YYYYMMDDhhmmss Time set to 2012 August 30 12:39:05 String olympicTime = "20120830123905"; ftp.setModificationTime(pathname, olympicTime); String extractedTime = ftp.getModificationTime(pathname); // Feature of the commons ftp library ExtractedTime has a "status code" first and is followed by newline chars assertTrue("time not set correctly by explicit set time", extractedTime.contains(olympicTime)); // 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 without setting time directly 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(); extractedTime = ftp.getModificationTime(pathname); assertFalse("time not moved on if time not explicitly set", extractedTime.contains(olympicTime)); // 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.covito.kit.file.support.FtpFileServiceImpl.java
/** * {@inheritDoc}//from w ww .j a v a2 s .c o m * * @author covito * @param path * @param is */ @Override public void append(String path, InputStream is) { init(); if (is == null) { throw new FileServiceException("InputStream is null"); } FTPClient client = getConnect(); try { if (!dealDocPath(client, path, false)) { throw new FileServiceException("path not exist!"); } boolean sucess = client.appendFile(getFilePath(path), is); if (!sucess) { log.error(client.getReplyString()); throw new FileServiceException(client.getReplyString()); } } catch (IOException e) { e.printStackTrace(); } }