You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@oodt.apache.org by bf...@apache.org on 2011/06/09 23:16:22 UTC

svn commit: r1134082 - in /oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp: CogJGlobusFtpProtocol.java CommonsNetFtpProtocol.java

Author: bfoster
Date: Thu Jun  9 21:16:22 2011
New Revision: 1134082

URL: http://svn.apache.org/viewvc?rev=1134082&view=rev
Log:
- updated to add new Protocol method: ls(ProtocolFileFilter)

----------------
OODT-194

Modified:
    oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CogJGlobusFtpProtocol.java
    oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CommonsNetFtpProtocol.java

Modified: oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CogJGlobusFtpProtocol.java
URL: http://svn.apache.org/viewvc/oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CogJGlobusFtpProtocol.java?rev=1134082&r1=1134081&r2=1134082&view=diff
==============================================================================
--- oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CogJGlobusFtpProtocol.java (original)
+++ oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CogJGlobusFtpProtocol.java Thu Jun  9 21:16:22 2011
@@ -26,6 +26,7 @@ import org.apache.oodt.cas.protocol.Prot
 import org.apache.oodt.cas.protocol.ProtocolFile;
 import org.apache.oodt.cas.protocol.auth.Authentication;
 import org.apache.oodt.cas.protocol.exceptions.ProtocolException;
+import org.apache.oodt.cas.protocol.util.ProtocolFileFilter;
 import org.globus.ftp.FTPClient;
 import org.globus.ftp.FileInfo;
 
@@ -128,7 +129,25 @@ public class CogJGlobusFtpProtocol imple
                   + e.getMessage());
       }
   }
-
+  
+	public List<ProtocolFile> ls(ProtocolFileFilter filter) throws ProtocolException {
+    try {
+      ftp.setActive(ftp.setLocalPassive());
+      Vector<FileInfo> fileList = (Vector<FileInfo>) ftp.list("*", null);
+      Vector<ProtocolFile> returnList = new Vector<ProtocolFile>();
+      for (FileInfo file : fileList) {
+      	ProtocolFile pFile = new ProtocolFile(this.pwd(), file.getName(), file.isDirectory());
+      	if (filter.accept(pFile)) {
+      		returnList.add(pFile);
+      	}
+      }
+      return returnList;
+	  } catch (Exception e) {
+	      throw new ProtocolException("Failed to get list of files : "
+	              + e.getMessage());
+	  }
+  }
+	
   public ProtocolFile pwd() throws ProtocolException {
       try {
           return new ProtocolFile(ftp.getCurrentDir(), true);
@@ -143,11 +162,11 @@ public class CogJGlobusFtpProtocol imple
 
   public void delete(ProtocolFile file) throws ProtocolException {
 	  try {
-		ftp.deleteFile(file.getPath());
-	} catch (Exception e) {
-		throw new ProtocolException("Failed to download file '" 
-				+ file.getPath() + "' : " + e.getMessage(), e);
-	}
+	  	ftp.deleteFile(file.getPath());
+		} catch (Exception e) {
+			throw new ProtocolException("Failed to download file '" 
+					+ file.getPath() + "' : " + e.getMessage(), e);
+		}
   }
 
 }

Modified: oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CommonsNetFtpProtocol.java
URL: http://svn.apache.org/viewvc/oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CommonsNetFtpProtocol.java?rev=1134082&r1=1134081&r2=1134082&view=diff
==============================================================================
--- oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CommonsNetFtpProtocol.java (original)
+++ oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CommonsNetFtpProtocol.java Thu Jun  9 21:16:22 2011
@@ -31,6 +31,7 @@ import org.apache.oodt.cas.protocol.Prot
 import org.apache.oodt.cas.protocol.ProtocolFile;
 import org.apache.oodt.cas.protocol.auth.Authentication;
 import org.apache.oodt.cas.protocol.exceptions.ProtocolException;
+import org.apache.oodt.cas.protocol.util.ProtocolFileFilter;
 
 /**
  * This class is responsible for FTP transfers. It is built as a wrapper around
@@ -42,177 +43,202 @@ import org.apache.oodt.cas.protocol.exce
  */
 public class CommonsNetFtpProtocol implements Protocol {
 
-    private FTPClient ftp;
-    private String homeDir; 
-    
-    /**
-     * Creates a new FtpClient
-     */
-    public CommonsNetFtpProtocol() {
-        ftp = new FTPClient();
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public void connect(String host, Authentication auth) throws ProtocolException {
-        // server cannot be null
-        if (host == null) {
-            throw new ProtocolException("Tried to connect to server == NULL");
-        }
-
-        try {
-            ftp.connect(host);
-            ftp.enterLocalPassiveMode();
-        } catch (Exception e) {
-            throw new ProtocolException("Failed to connect to server : "
-                    + e.getMessage());
-        }
-
-        try {
-            // try logging in
-            if (!ftp.login(auth.getUser(), auth.getPass())) {
-                throw new ProtocolException("Failed logging into host " + host
-                        + " as user " + auth.getUser());
-            }
-
-            // set file type to binary
-            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
-
-            homeDir = ftp.printWorkingDirectory();
-        } catch (Exception e) {
-            // login failed
-            throw new ProtocolException(
-                    "Exception thrown while logging into host " + host
-                            + " as user " + auth.getUser());
-        }
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public ProtocolFile pwd() throws ProtocolException {
-        try {
-            return new ProtocolFile(ftp.printWorkingDirectory(), true);
-        } catch (Exception e) {
-            throw new ProtocolException("Failed to pwd : " + e.getMessage());
-        }
-    }
-
-    public List<ProtocolFile> ls() throws ProtocolException {
-        try {
-            FTPFile[] files = ftp.listFiles();
-            List<ProtocolFile> returnFiles = new LinkedList<ProtocolFile>();
-            for (int i = 0; i < files.length; i++) {
-                FTPFile file = files[i];
-                if (file == null)
-                    continue;
-                String path = this.pwd().getPath();
-                returnFiles.add(new ProtocolFile(path + "/" + file.getName(), file.isDirectory()));
-            }
-            // System.out.println("RETURN FILES: " + returnFiles);
-            return returnFiles;
-        } catch (Exception e) {
-            throw new ProtocolException("Failed to get file list : "
-                    + e.getMessage());
-        }
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public void get(ProtocolFile fromFile, File toFile)
-            throws ProtocolException {
-        // file or toLocalFile cannot be null
-        if (fromFile == null || toFile == null) {
-            throw new ProtocolException(
-                    "Can't download file -> ProtocolFile == null || toLocalFile == null");
-        }
-
-        // download file
-        OutputStream os = null;
-        try {
-            os = new FileOutputStream(toFile);
-            if (ftp.retrieveFile(fromFile.getName(), os))// {
-                throw new ProtocolException("Failed to download file "
-                        + fromFile.getName());
-            // }
-        } catch (Exception e) {
-            // download failed
-        	toFile.delete();
-            throw new ProtocolException("FAILED to download: " + fromFile.getName()
-                    + " : " + e.getMessage());
-        } finally {
-            // close output stream
-            if (os != null)
-                try {
-                    os.close();
-                } catch (Exception e) {
-                	toFile.delete();
-                    throw new ProtocolException(
-                            "Failed to close outputstream : " + e.getMessage());
-                }
-        }
-    }
-    
-    /**
-     * {@inheritDoc}
-     */
-    public void put(File fromFile, ProtocolFile toFile) throws ProtocolException {
-    	try {
-    		ftp.storeFile(toFile.getPath(), new FileInputStream(fromFile));
-    	}catch (Exception e) {
-    		throw new ProtocolException("Failed to put file '" + fromFile + "' : " + e.getMessage(), e);
-    	}
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public void cd(ProtocolFile file) throws ProtocolException {
-        try {
-            if (!ftp.changeWorkingDirectory(file.getPath()))
-                throw new Exception("Directory change method returned false");
-        } catch (Exception e) {
-            throw new ProtocolException("Failed to cd to " + file.getPath() + " : "
-                    + e.getMessage());
-        }
-    }
-    
-    public void cdRoot() throws ProtocolException {
-    	cd(new ProtocolFile(ProtocolFile.SEPARATOR, true));
-    }
-    
-    public void cdHome() throws ProtocolException {
-    	cd(new ProtocolFile(homeDir, true));
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public void close() throws ProtocolException {
-        try {
-            ftp.disconnect();
-        } catch (Exception e) {
-            throw new ProtocolException("Failed to disconnect from server");
-        }
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public boolean connected() {
-        return ftp.isConnected();
-    }
-    
-    /**
-     * {@inheritDoc}
-     */
-    public void delete(ProtocolFile file) throws ProtocolException {
-        try {
-            ftp.deleteFile(file.getPath());
-        } catch (Exception e) {
-            throw new ProtocolException("Failed to delete file '" + file.getPath() + "' : " + e.getMessage(), e);
-        }
-    }
+	private FTPClient ftp;
+	private String homeDir;
+
+	/**
+	 * Creates a new FtpClient
+	 */
+	public CommonsNetFtpProtocol() {
+		ftp = new FTPClient();
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public void connect(String host, Authentication auth)
+			throws ProtocolException {
+		// server cannot be null
+		if (host == null) {
+			throw new ProtocolException("Tried to connect to server == NULL");
+		}
+
+		try {
+			ftp.connect(host);
+			ftp.enterLocalPassiveMode();
+		} catch (Exception e) {
+			throw new ProtocolException("Failed to connect to server : "
+					+ e.getMessage());
+		}
+
+		try {
+			// try logging in
+			if (!ftp.login(auth.getUser(), auth.getPass())) {
+				throw new ProtocolException("Failed logging into host " + host
+						+ " as user " + auth.getUser());
+			}
+
+			// set file type to binary
+			ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
+
+			homeDir = ftp.printWorkingDirectory();
+		} catch (Exception e) {
+			// login failed
+			throw new ProtocolException("Exception thrown while logging into host "
+					+ host + " as user " + auth.getUser());
+		}
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public ProtocolFile pwd() throws ProtocolException {
+		try {
+			return new ProtocolFile(ftp.printWorkingDirectory(), true);
+		} catch (Exception e) {
+			throw new ProtocolException("Failed to pwd : " + e.getMessage());
+		}
+	}
+
+	public List<ProtocolFile> ls() throws ProtocolException {
+		try {
+			FTPFile[] files = ftp.listFiles();
+			List<ProtocolFile> returnFiles = new LinkedList<ProtocolFile>();
+			for (int i = 0; i < files.length; i++) {
+				FTPFile file = files[i];
+				if (file == null)
+					continue;
+				String path = this.pwd().getPath();
+				returnFiles.add(new ProtocolFile(path + "/" + file.getName(), file
+						.isDirectory()));
+			}
+			return returnFiles;
+		} catch (Exception e) {
+			throw new ProtocolException("Failed to get file list : " + e.getMessage());
+		}
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public List<ProtocolFile> ls(ProtocolFileFilter filter)
+			throws ProtocolException {
+		try {
+			FTPFile[] files = ftp.listFiles();
+			List<ProtocolFile> returnFiles = new LinkedList<ProtocolFile>();
+			for (int i = 0; i < files.length; i++) {
+				FTPFile file = files[i];
+				if (file == null)
+					continue;
+				String path = this.pwd().getPath();
+				ProtocolFile pFile = new ProtocolFile(path + "/" + file.getName(), file
+						.isDirectory());
+				if (filter.accept(pFile)) {
+					returnFiles.add(pFile);
+				}
+			}
+			return returnFiles;
+		} catch (Exception e) {
+			throw new ProtocolException("Failed to get file list : " + e.getMessage());
+		}
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public void get(ProtocolFile fromFile, File toFile) throws ProtocolException {
+		// file or toLocalFile cannot be null
+		if (fromFile == null || toFile == null) {
+			throw new ProtocolException(
+					"Can't download file -> ProtocolFile == null || toLocalFile == null");
+		}
+
+		// download file
+		OutputStream os = null;
+		try {
+			os = new FileOutputStream(toFile);
+			if (ftp.retrieveFile(fromFile.getName(), os))// {
+				throw new ProtocolException("Failed to download file "
+						+ fromFile.getName());
+			// }
+		} catch (Exception e) {
+			// download failed
+			toFile.delete();
+			throw new ProtocolException("FAILED to download: " + fromFile.getName()
+					+ " : " + e.getMessage());
+		} finally {
+			// close output stream
+			if (os != null)
+				try {
+					os.close();
+				} catch (Exception e) {
+					toFile.delete();
+					throw new ProtocolException("Failed to close outputstream : "
+							+ e.getMessage());
+				}
+		}
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public void put(File fromFile, ProtocolFile toFile) throws ProtocolException {
+		try {
+			ftp.storeFile(toFile.getPath(), new FileInputStream(fromFile));
+		} catch (Exception e) {
+			throw new ProtocolException("Failed to put file '" + fromFile + "' : "
+					+ e.getMessage(), e);
+		}
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public void cd(ProtocolFile file) throws ProtocolException {
+		try {
+			if (!ftp.changeWorkingDirectory(file.getPath()))
+				throw new Exception("Directory change method returned false");
+		} catch (Exception e) {
+			throw new ProtocolException("Failed to cd to " + file.getPath() + " : "
+					+ e.getMessage());
+		}
+	}
+
+	public void cdRoot() throws ProtocolException {
+		cd(new ProtocolFile(ProtocolFile.SEPARATOR, true));
+	}
+
+	public void cdHome() throws ProtocolException {
+		cd(new ProtocolFile(homeDir, true));
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public void close() throws ProtocolException {
+		try {
+			ftp.disconnect();
+		} catch (Exception e) {
+			throw new ProtocolException("Failed to disconnect from server");
+		}
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public boolean connected() {
+		return ftp.isConnected();
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public void delete(ProtocolFile file) throws ProtocolException {
+		try {
+			ftp.deleteFile(file.getPath());
+		} catch (Exception e) {
+			throw new ProtocolException("Failed to delete file '" + file.getPath()
+					+ "' : " + e.getMessage(), e);
+		}
+	}
 }