You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2014/12/10 14:39:37 UTC

svn commit: r1644401 - in /commons/proper/net/trunk/src: changes/changes.xml main/java/org/apache/commons/net/ftp/FTPClient.java main/java/org/apache/commons/net/ftp/parser/MLSxEntryParser.java

Author: sebb
Date: Wed Dec 10 13:39:36 2014
New Revision: 1644401

URL: http://svn.apache.org/r1644401
Log:
NET-565 Add FTPClient method to return an FTPFile from an MDTM command

Modified:
    commons/proper/net/trunk/src/changes/changes.xml
    commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPClient.java
    commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/parser/MLSxEntryParser.java

Modified: commons/proper/net/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/changes/changes.xml?rev=1644401&r1=1644400&r2=1644401&view=diff
==============================================================================
--- commons/proper/net/trunk/src/changes/changes.xml [utf-8] (original)
+++ commons/proper/net/trunk/src/changes/changes.xml [utf-8] Wed Dec 10 13:39:36 2014
@@ -68,8 +68,12 @@ This is mainly a bug-fix release. See fu
   IMAPExportMbox (example app) allows IMAP folders to be exported into an mbox file.
   This is the inverse of the IMAPImportMbox example added previously
         ">
+            <action issue="NET-565" type="update" dev="sebb">
+            Add FTPClient method to return an FTPFile from an MDTM command
+            </action>
             <action issue="NET-564" type="update" dev="sebb">
             FTPFile.toFormattedString - allow specification of TimeZone for display
+            </action>
             <action issue="NET-562" type="update" dev="sebb">
             FTPFile.toFormattedString should print only signficant parts of the parsed date
             </action>

Modified: commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPClient.java
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPClient.java?rev=1644401&r1=1644400&r2=1644401&view=diff
==============================================================================
--- commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPClient.java (original)
+++ commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPClient.java Wed Dec 10 13:39:36 2014
@@ -3490,6 +3490,31 @@ implements Configurable
 
 
     /**
+     * Issue the FTP MDTM command (not supported by all servers) to retrieve the last
+     * modification time of a file. The modification string should be in the
+     * ISO 3077 form "YYYYMMDDhhmmss(.xxx)?". The timestamp represented should also be in
+     * GMT, but not all FTP servers honour this.
+     *
+     * @param pathname The file path to query.
+     * @return A FTPFile representing the last file modification time, may be {@code null}.
+     * The FTPFile timestamp will be null if a parse error occurs.
+     * @throws IOException if an I/O error occurs.
+     * @since 3.4
+     */
+    public FTPFile mdtmFile(String pathname) throws IOException {
+        if (FTPReply.isPositiveCompletion(mdtm(pathname))) {
+            String reply = getReplyStrings()[0].substring(4); // skip the return code (e.g. 213) and the space
+            FTPFile file = new FTPFile();
+            file.setName(pathname);
+            file.setRawListing(reply);
+            file.setTimestamp(MLSxEntryParser.parseGMTdateTime(reply));
+            return file;
+        }
+        return null;
+    }
+
+
+    /**
      * Issue the FTP MFMT command (not supported by all servers) which sets the last
      * modified time of a file.
      *

Modified: commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/parser/MLSxEntryParser.java
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/parser/MLSxEntryParser.java?rev=1644401&r1=1644400&r2=1644401&view=diff
==============================================================================
--- commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/parser/MLSxEntryParser.java (original)
+++ commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/parser/MLSxEntryParser.java Wed Dec 10 13:39:36 2014
@@ -18,6 +18,7 @@
 package org.apache.commons.net.ftp.parser;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.Calendar;
 import java.util.GregorianCalendar;
 import java.util.HashMap;
 import java.util.Locale;
@@ -138,22 +139,7 @@ public class MLSxEntryParser extends FTP
                 file.setSize(Long.parseLong(factvalue));
             }
             else if ("modify".equals(factname)) {
-                // YYYYMMDDHHMMSS[.sss]
-                SimpleDateFormat sdf; // Not thread-safe
-                if (factvalue.contains(".")){
-                    sdf = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
-                } else {
-                    sdf = new SimpleDateFormat("yyyyMMddHHmmss");
-                }
-                TimeZone GMT = TimeZone.getTimeZone("GMT"); // both need to be set for the parse to work OK
-                sdf.setTimeZone(GMT);
-                GregorianCalendar gc = new GregorianCalendar(GMT);
-                try {
-                    gc.setTime(sdf.parse(factvalue));
-                    file.setTimestamp(gc);
-                } catch (ParseException e) {
-                    // TODO ??
-                }
+                file.setTimestamp(parseGMTdateTime(factvalue));
             }
             else if ("type".equals(factname)) {
                     Integer intType = TYPE_TO_INT.get(valueLowerCase);
@@ -190,6 +176,30 @@ public class MLSxEntryParser extends FTP
         return file;
     }
 
+    /**
+     * Parse a GMT time stamp of the form YYYYMMDDHHMMSS[.sss]
+     *
+     * @param timestamp the date-time to parse
+     * @return a Calendar entry, may be {@code null} 
+     */
+    public static Calendar parseGMTdateTime(String timestamp) {
+        SimpleDateFormat sdf;
+        if (timestamp.contains(".")){
+            sdf = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
+        } else {
+            sdf = new SimpleDateFormat("yyyyMMddHHmmss");
+        }
+        TimeZone GMT = TimeZone.getTimeZone("GMT"); // both need to be set for the parse to work OK
+        sdf.setTimeZone(GMT);
+        GregorianCalendar gc = new GregorianCalendar(GMT);
+        try {
+            gc.setTime(sdf.parse(timestamp));
+            return gc;
+        } catch (ParseException e) {
+            return null;
+        }
+    }
+
     //              perm-fact    = "Perm" "=" *pvals
     //              pvals        = "a" / "c" / "d" / "e" / "f" /
     //                             "l" / "m" / "p" / "r" / "w"