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/05/28 01:05:15 UTC

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

Author: bfoster
Date: Fri May 27 23:05:14 2011
New Revision: 1128506

URL: http://svn.apache.org/viewvc?rev=1128506&view=rev
Log:

- added ftp Protocols

---------------

OODT-194

Added:
    oodt/branches/protocol/protocol-ftp/src/main/java/org/
    oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/
    oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/
    oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/
    oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/
    oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/
    oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CogJGlobusFtpProtocol.java   (with props)
    oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CogJGlobusFtpProtocolFactory.java   (with props)
    oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CommonsNetFtpProtocol.java   (with props)
    oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CommonsNetFtpProtocolFactory.java   (with props)
Modified:
    oodt/branches/protocol/protocol-ftp/pom.xml

Modified: oodt/branches/protocol/protocol-ftp/pom.xml
URL: http://svn.apache.org/viewvc/oodt/branches/protocol/protocol-ftp/pom.xml?rev=1128506&r1=1128505&r2=1128506&view=diff
==============================================================================
--- oodt/branches/protocol/protocol-ftp/pom.xml (original)
+++ oodt/branches/protocol/protocol-ftp/pom.xml Fri May 27 23:05:14 2011
@@ -75,7 +75,7 @@
 	<dependencies>
 		<dependency>
 			<groupId>org.apache.oodt</groupId>
-			<artifactId>protocol-api</artifactId>
+			<artifactId>cas-protocol-api</artifactId>
 			<version>${project.parent.version}</version>
 		</dependency>
 		<dependency>

Added: 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=1128506&view=auto
==============================================================================
--- oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CogJGlobusFtpProtocol.java (added)
+++ oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CogJGlobusFtpProtocol.java Fri May 27 23:05:14 2011
@@ -0,0 +1,137 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.protocol.ftp;
+
+//JDK imports
+import java.io.File;
+import java.util.List;
+import java.util.Vector;
+
+//Globus imports
+import org.apache.oodt.cas.protocol.Protocol;
+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.globus.ftp.FTPClient;
+import org.globus.ftp.FileInfo;
+
+/**
+ * FTP implementation of a {@link Protocol}
+ * 
+ * @author bfoster
+ */
+public class CogJGlobusFtpProtocol implements Protocol {
+
+  private FTPClient ftp;
+
+  private boolean isConnected;
+  
+  public void cd(ProtocolFile file) throws ProtocolException {
+      try {
+          ftp.changeDir(file.getPath());
+      } catch (Exception e) {
+          throw new ProtocolException("Failed to cd to " + file + " : "
+                  + e.getMessage());
+      }
+  }
+
+  public void connect(String host, Authentication auth) throws ProtocolException {
+      try {
+          ftp = new FTPClient(host, 21);
+      } catch (Exception e) {
+          throw new ProtocolException("Failed to connect to: " + host + " : "
+                  + e.getMessage());
+      }
+      isConnected = true;
+
+      try {
+          ftp.authorize(auth.getUser(), auth.getPass());
+          ftp.setActive(ftp.setLocalPassive());
+      } catch (Exception e) {
+          throw new ProtocolException("Failed to login to: " + host + " : "
+                  + e.getMessage());
+      }
+  }
+
+  public void close() throws ProtocolException {
+      try {
+          ftp.close();
+          isConnected = false;
+      } catch (Exception e) {
+          throw new ProtocolException("Error disconnecting from "
+                  + ftp.getHost() + " : " + e.getMessage());
+      }
+  }
+
+  public void get(ProtocolFile fromFile, File toFile)
+          throws ProtocolException {
+      try {
+          ftp.setActive(ftp.setLocalPassive());
+          ftp.get(fromFile.getPath(), toFile);
+      } catch (Exception e) {
+          throw new ProtocolException("Failed to download: " + fromFile.getName()
+                  + " : " + e.getMessage());
+      }
+  }
+  
+  public void put(File fromFile, ProtocolFile toFile) throws ProtocolException {
+	  try {
+		  ftp.put(fromFile, toFile.getPath(), false);
+	  }catch (Exception e) {
+		  throw new ProtocolException("Failed to put file '" + fromFile + "' : " + e.getMessage(), e);
+	  }
+  }
+  
+  public List<ProtocolFile> ls() throws ProtocolException {
+      try {
+          ftp.setActive(ftp.setLocalPassive());
+          Vector<FileInfo> fileList = (Vector<FileInfo>) ftp.list("*", null);
+          Vector<ProtocolFile> returnList = new Vector<ProtocolFile>();
+          String path = this.pwd().getPath();
+          for (FileInfo file : fileList) {
+              returnList.add(new ProtocolFile(path + File.separator 
+            		  + file.getName(), file.isDirectory()));
+          }
+          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);
+      } catch (Exception e) {
+          throw new ProtocolException("Failed to pwd : " + e.getMessage());
+      }
+  }
+
+  public boolean connected() {
+      return isConnected;
+  }
+
+  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);
+	}
+  }
+
+}

Propchange: oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CogJGlobusFtpProtocol.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CogJGlobusFtpProtocolFactory.java
URL: http://svn.apache.org/viewvc/oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CogJGlobusFtpProtocolFactory.java?rev=1128506&view=auto
==============================================================================
--- oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CogJGlobusFtpProtocolFactory.java (added)
+++ oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CogJGlobusFtpProtocolFactory.java Fri May 27 23:05:14 2011
@@ -0,0 +1,39 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.protocol.ftp;
+
+//OODT imports
+import org.apache.oodt.cas.protocol.ProtocolFactory;
+
+/**
+ * Factory for creating {@link CogJGlobusFtpProtocol}s
+ * 
+ * @author bfoster
+ * @version $Revision$
+ * 
+ */
+public class CogJGlobusFtpProtocolFactory implements ProtocolFactory {
+
+  public CogJGlobusFtpProtocol newInstance() {
+    return new CogJGlobusFtpProtocol();
+  }
+
+  public String getSchema() {
+	return "ftp";
+  }
+
+}

Propchange: oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CogJGlobusFtpProtocolFactory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 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=1128506&view=auto
==============================================================================
--- oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CommonsNetFtpProtocol.java (added)
+++ oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CommonsNetFtpProtocol.java Fri May 27 23:05:14 2011
@@ -0,0 +1,208 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.protocol.ftp;
+
+//JDK imports
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.util.LinkedList;
+import java.util.List;
+
+//APACHE imports
+import org.apache.commons.net.ftp.FTPClient;
+import org.apache.commons.net.ftp.FTPFile;
+import org.apache.oodt.cas.protocol.Protocol;
+import org.apache.oodt.cas.protocol.ProtocolFile;
+import org.apache.oodt.cas.protocol.auth.Authentication;
+import org.apache.oodt.cas.protocol.exceptions.ProtocolException;
+
+/**
+ * This class is responsible for FTP transfers. It is built as a wrapper around
+ * Apache's FTPClient class in order to connect it into the Crawler's Protocol
+ * infrastructure.
+ * 
+ * @author bfoster
+ * 
+ */
+public class CommonsNetFtpProtocol implements Protocol {
+
+    private FTPClient ftp;
+
+    /**
+     * 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);
+
+        } 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());
+        }
+    }
+
+    /**
+     * {@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);
+        }
+    }
+}

Propchange: oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CommonsNetFtpProtocol.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CommonsNetFtpProtocolFactory.java
URL: http://svn.apache.org/viewvc/oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CommonsNetFtpProtocolFactory.java?rev=1128506&view=auto
==============================================================================
--- oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CommonsNetFtpProtocolFactory.java (added)
+++ oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CommonsNetFtpProtocolFactory.java Fri May 27 23:05:14 2011
@@ -0,0 +1,38 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.oodt.cas.protocol.ftp;
+
+//OODT imports
+import org.apache.oodt.cas.protocol.ProtocolFactory;
+
+/**
+ * Constructs new {@link CommonsNetFtpProtocol}s.
+ * 
+ * @author bfoster
+ * @version $Revision$
+ */
+public class CommonsNetFtpProtocolFactory implements ProtocolFactory {
+
+  public CommonsNetFtpProtocol newInstance() {
+    return new CommonsNetFtpProtocol();
+  }
+
+  public String getSchema() {
+	return "ftp";
+  }
+
+}

Propchange: oodt/branches/protocol/protocol-ftp/src/main/java/org/apache/oodt/cas/protocol/ftp/CommonsNetFtpProtocolFactory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain