Author: rgoers Date: Tue Nov 2 14:30:48 2010 New Revision: 1030062 URL: http://svn.apache.org/viewvc?rev=1030062&view=rev Log: Fix VFS 305 - add encoding option for FTP provider. Add test case for VFS-322
Added: commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs/provider/tar/test/LargeTarTestCase.java Modified: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/ftp/FtpClientFactory.java commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/ftp/FtpFileSystemConfigBuilder.java commons/proper/vfs/trunk/src/changes/changes.xml Modified: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/ftp/FtpClientFactory.java URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/ftp/FtpClientFactory.java?rev=1030062&r1=1030061&r2=1030062&view=diff ============================================================================== --- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/ftp/FtpClientFactory.java (original) +++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/ftp/FtpClientFactory.java Tue Nov 2 14:30:48 2010 @@ -181,6 +181,14 @@ public final class FtpClientFactory { client.enterLocalPassiveMode(); } + + String controlEncoding = FtpFileSystemConfigBuilder.getInstance().getControlEncoding(fileSystemOptions); + if (controlEncoding != null) + { + client.setControlEncoding(controlEncoding); + } + + } catch (final IOException e) { Modified: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/ftp/FtpFileSystemConfigBuilder.java URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/ftp/FtpFileSystemConfigBuilder.java?rev=1030062&r1=1030061&r2=1030062&view=diff ============================================================================== --- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/ftp/FtpFileSystemConfigBuilder.java (original) +++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/ftp/FtpFileSystemConfigBuilder.java Tue Nov 2 14:30:48 2010 @@ -46,6 +46,8 @@ public final class FtpFileSystemConfigBu FtpFileSystemConfigBuilder.class.getName() + ".SERVER_TIME_ZONE_ID"; private static final String SHORT_MONTH_NAMES = FtpFileSystemConfigBuilder.class.getName() + ".SHORT_MONTH_NAMES"; + private static final String ENCODING = + FtpFileSystemConfigBuilder.class.getName() + ".ENCODING"; private FtpFileSystemConfigBuilder() { @@ -306,4 +308,17 @@ public final class FtpFileSystemConfigBu setParam(opts, SHORT_MONTH_NAMES, clone); } + + /** + * see {...@link org.apache.commons.net.ftp.FTP#setControlEncoding} for details and examples. + * @param opts The FileSystemOptions. + * @param shortMonthNames an array of short month name Strings. + */ + public void setControlEncoding(FileSystemOptions opts, String encoding){ + setParam(opts, ENCODING, encoding); + } + + public String getControlEncoding(FileSystemOptions opts) { + return (String) getParam(opts, ENCODING); + } } Added: commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs/provider/tar/test/LargeTarTestCase.java URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs/provider/tar/test/LargeTarTestCase.java?rev=1030062&view=auto ============================================================================== --- commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs/provider/tar/test/LargeTarTestCase.java (added) +++ commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs/provider/tar/test/LargeTarTestCase.java Tue Nov 2 14:30:48 2010 @@ -0,0 +1,184 @@ +package org.apache.commons.vfs.provider.tar.test; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +import junit.framework.TestCase; +import org.apache.commons.compress.archivers.ArchiveStreamFactory; +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; +import org.apache.commons.compress.compressors.CompressorStreamFactory; +import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; +import org.apache.commons.compress.utils.IOUtils; +import org.apache.commons.vfs.CacheStrategy; +import org.apache.commons.vfs.FileObject; +import org.apache.commons.vfs.cache.SoftRefFilesCache; +import org.apache.commons.vfs.impl.DefaultFileSystemManager; +import org.apache.commons.vfs.provider.local.DefaultLocalFileProvider; +import org.apache.commons.vfs.provider.tar.TarFileProvider; + +//@SuppressWarnings("nls") +public class LargeTarTestCase extends TestCase { + private final static String baseDir = "target/test-classes/test-data/"; + + private DefaultFileSystemManager manager; + private final static String largeFilePath = baseDir; + private final static String largeFileName = "largefile"; + + + public void setUp() throws Exception { + manager = new DefaultFileSystemManager(); + + manager.setFilesCache(new SoftRefFilesCache()); + manager.setCacheStrategy(CacheStrategy.ON_RESOLVE); + + manager.addProvider("file", new DefaultLocalFileProvider()); + manager.addProvider("tgz", new TarFileProvider()); + manager.addProvider("tar", new TarFileProvider()); + + createLargeFile(largeFilePath, largeFileName); + } + + public void testLargeFile() throws Exception { + File realFile = new File(largeFilePath + largeFileName + ".tar.gz"); + + FileObject file = manager.resolveFile("tgz:file://" + realFile.getCanonicalPath() + "!/"); + + assertNotNull(file); + List files = Arrays.asList(file.getChildren()); + + assertNotNull(files); + assertEquals(1, files.size()); + FileObject f = (FileObject) files.get(0); + + assertTrue("Expected file not found: " + largeFileName + ".txt", + f.getName().getBaseName().equals(largeFileName + ".txt")); + } + +/* + public void testFileCheck() throws Exception { + String[] expectedFiles = { + "plugins.tsv", + "languages.tsv", + "browser_type.tsv", + "timezones.tsv", + "color_depth.tsv", + "resolution.tsv", + "connection_type.tsv", + "search_engines.tsv", + "javascript_version.tsv", + "operating_systems.tsv", + "country.tsv", + "browser.tsv" + }; + + fileCheck(expectedFiles, "tar:file://c:/temp/data/data/data-small.tar"); + } */ + + protected void fileCheck(String[] expectedFiles, String tarFile) throws Exception { + assertNotNull(manager); + FileObject file = manager.resolveFile(tarFile); + + assertNotNull(file); + List files = Arrays.asList(file.getChildren()); + + assertNotNull(files); + for(int i=0; i < expectedFiles.length; ++i) { + String expectedFile = expectedFiles[i]; + assertTrue("Expected file not found: " + expectedFile, fileExists(expectedFile, files)); + } + } + + /** + * Search for the expected file in a given list, without using the full path + * @param expectedFile + * @param files + * @return + */ + protected boolean fileExists(String expectedFile, List files) { + Iterator iter = files.iterator(); + while (iter.hasNext()) { + FileObject file = (FileObject) iter.next(); + if(file.getName().getBaseName().equals(expectedFile)) { + return true; + } + } + + return false; + } + + protected boolean endsWith(String testString, String[] testList) { + for(int i=0; i < testList.length; ++i) { + String testItem = testList[i]; + if(testString.endsWith(testItem)) { + return true; + } + } + return false; + } + + //@SuppressWarnings("unused") + protected void createLargeFile(String path, String name) throws Exception { + long _1K = 1024; + long _1M = 1024 * _1K; + long _256M = 256 * _1M; + long _512M = 512 * _1M; + long _1G = 1024 * _1M; + + // File size of 3 GB + long fileSize = 3 * _1G; + + File tarGzFile = new File(path + name + ".tar.gz"); + + if(!tarGzFile.exists()) { + System.out.println("This test is a bit slow. It needs to write a 3GB file to your hard drive"); + + // Create archive + OutputStream outTarFileStream = new FileOutputStream(path + name + ".tar"); + + TarArchiveOutputStream outTarStream = (TarArchiveOutputStream)new ArchiveStreamFactory() + .createArchiveOutputStream(ArchiveStreamFactory.TAR, outTarFileStream); + + // Create archive contents + TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(name + ".txt"); + tarArchiveEntry.setSize(fileSize); + + outTarStream.putArchiveEntry(tarArchiveEntry); + for(long i = 0; i < fileSize; i++) { + outTarStream.write('a'); + } + + outTarStream.closeArchiveEntry(); + outTarStream.close(); + + outTarFileStream.close(); + + // Create compressed archive + OutputStream outGzipFileStream = new FileOutputStream(path + name + ".tar.gz"); + + GzipCompressorOutputStream outGzipStream = (GzipCompressorOutputStream)new CompressorStreamFactory() + .createCompressorOutputStream(CompressorStreamFactory.GZIP, outGzipFileStream); + + // Compress archive + InputStream inTarFileStream = new FileInputStream(path + name + ".tar"); + // TODO: Change to a Piped Stream to conserve disk space + IOUtils.copy(inTarFileStream, outGzipStream); + inTarFileStream.close(); + + outGzipStream.close(); + outGzipFileStream.close(); + + // Cleanup original tar + File tarFile = new File(path + name + ".tar"); + if(tarFile.exists()) { + tarFile.delete(); + } + } + } +} Modified: commons/proper/vfs/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/src/changes/changes.xml?rev=1030062&r1=1030061&r2=1030062&view=diff ============================================================================== --- commons/proper/vfs/trunk/src/changes/changes.xml (original) +++ commons/proper/vfs/trunk/src/changes/changes.xml Tue Nov 2 14:30:48 2010 @@ -23,6 +23,9 @@ <body> <release version="2.0" date="in SVN" description=""> + <action dev="rgoers" type="fix" issue="VFS-305" due-to="Tom"> + Add encoding option to FTP provider. + </action> <action dev="rgoers" type="fix" issue="VFS-315" due-to="David Hausladen"> Fix potential NullPointerException if the DavProperty is null or contains null values. </action>