Author: ngn
Date: Mon Dec  1 12:54:54 2008
New Revision: 722225

URL: http://svn.apache.org/viewvc?rev=722225&view=rev
Log:
Implement the MFMT command (FTPSERVER-224)

Added:
    
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/command/impl/MFMT.java
    
mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/clienttests/MFMTTest.java
Modified:
    
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/command/CommandFactoryFactory.java
    
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFtpFile.java
    
mina/ftpserver/trunk/core/src/main/resources/org/apache/ftpserver/message/FtpStatus.properties
    
mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/commands/impl/listing/LISTFileFormaterTest.java
    
mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/commands/impl/listing/MLSTFileFormaterTest.java
    
mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/commands/impl/listing/NLSTFileFormaterTest.java

Modified: 
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/command/CommandFactoryFactory.java
URL: 
http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/command/CommandFactoryFactory.java?rev=722225&r1=722224&r2=722225&view=diff
==============================================================================
--- 
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/command/CommandFactoryFactory.java
 (original)
+++ 
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/command/CommandFactoryFactory.java
 Mon Dec  1 12:54:54 2008
@@ -39,6 +39,7 @@
 import org.apache.ftpserver.command.impl.LIST;
 import org.apache.ftpserver.command.impl.MD5;
 import org.apache.ftpserver.command.impl.MDTM;
+import org.apache.ftpserver.command.impl.MFMT;
 import org.apache.ftpserver.command.impl.MKD;
 import org.apache.ftpserver.command.impl.MLSD;
 import org.apache.ftpserver.command.impl.MLST;
@@ -95,6 +96,7 @@
         DEFAULT_COMMAND_MAP.put("LANG", new LANG());
         DEFAULT_COMMAND_MAP.put("LIST", new LIST());
         DEFAULT_COMMAND_MAP.put("MD5", new MD5());
+        DEFAULT_COMMAND_MAP.put("MFMT", new MFMT());
         DEFAULT_COMMAND_MAP.put("MMD5", new MD5());
         DEFAULT_COMMAND_MAP.put("MDTM", new MDTM());
         DEFAULT_COMMAND_MAP.put("MLST", new MLST());

Added: 
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/command/impl/MFMT.java
URL: 
http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/command/impl/MFMT.java?rev=722225&view=auto
==============================================================================
--- 
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/command/impl/MFMT.java
 (added)
+++ 
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/command/impl/MFMT.java
 Mon Dec  1 12:54:54 2008
@@ -0,0 +1,163 @@
+/*
+ * 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.ftpserver.command.impl;
+
+import java.io.IOException;
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.TimeZone;
+
+import org.apache.ftpserver.command.AbstractCommand;
+import org.apache.ftpserver.ftplet.FtpFile;
+import org.apache.ftpserver.ftplet.FtpReply;
+import org.apache.ftpserver.ftplet.FtpRequest;
+import org.apache.ftpserver.impl.FtpIoSession;
+import org.apache.ftpserver.impl.FtpServerContext;
+import org.apache.ftpserver.impl.LocalizedFtpReply;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Command for changing the modified time of a file. 
+ * <p>
+ * Specified in the following document:
+ * http://www.omz13.com/downloads/draft-somers-ftp-mfxx-00.html#anchor8
+ * </p>
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
+ * @version $Rev$, $Date$
+ */
+public class MFMT extends AbstractCommand {
+
+    private final Logger LOG = LoggerFactory.getLogger(MFMT.class);
+
+    /**
+     * Execute command.
+     */
+    public void execute(final FtpIoSession session,
+            final FtpServerContext context, final FtpRequest request)
+            throws IOException {
+
+        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
+        
+        // reset state variables
+        session.resetState();
+
+        String argument = request.getArgument();
+
+        if (argument == null || argument.trim().length() == 0) {
+            session
+                    .write(LocalizedFtpReply
+                            .translate(
+                                    session,
+                                    request,
+                                    context,
+                                    
FtpReply.REPLY_501_SYNTAX_ERROR_IN_PARAMETERS_OR_ARGUMENTS,
+                                    "MFMT.invalid", null));
+            return;
+        }
+        
+        String[] arguments = argument.split(" ");
+
+        if(arguments.length != 2) {
+            session
+            .write(LocalizedFtpReply
+                    .translate(
+                            session,
+                            request,
+                            context,
+                            
FtpReply.REPLY_501_SYNTAX_ERROR_IN_PARAMETERS_OR_ARGUMENTS,
+                            "MFMT.invalid", null));
+            return;
+        }
+       
+        String timestamp = arguments[0].trim();
+        
+        df.setLenient(false);
+        df.setTimeZone(TimeZone.getTimeZone("GMT"));
+        
+        try {
+            Date time = df.parse(timestamp);
+            
+            String fileName = arguments[1].trim();
+            
+            // get file object
+            FtpFile file = null;
+            
+            try {
+                file = session.getFileSystemView().getFile(fileName);
+            } catch (Exception ex) {
+                LOG.debug("Exception getting the file object: " + fileName, 
ex);
+            }
+            
+            if (file == null || !file.doesExist()) {
+                session
+                .write(LocalizedFtpReply
+                        .translate(
+                                session,
+                                request,
+                                context,
+                                FtpReply.REPLY_550_REQUESTED_ACTION_NOT_TAKEN,
+                                "MFMT.filemissing", fileName));
+                return;
+            }
+            
+            // check file
+            if (!file.isFile()) {
+                session
+                .write(LocalizedFtpReply
+                        .translate(
+                                session,
+                                request,
+                                context,
+                                
FtpReply.REPLY_501_SYNTAX_ERROR_IN_PARAMETERS_OR_ARGUMENTS,
+                                "MFMT.invalid", null));
+                return;
+            }
+
+            // all checks okay, lets go
+            file.setLastModified(time.getTime());
+
+            session
+            .write(LocalizedFtpReply
+                    .translate(
+                            session,
+                            request,
+                            context,
+                            FtpReply.REPLY_213_FILE_STATUS,
+                            "MFMT", "ModifyTime=" + timestamp + " " + 
fileName));
+            return;
+
+        } catch (ParseException e) {
+            session
+            .write(LocalizedFtpReply
+                    .translate(
+                            session,
+                            request,
+                            context,
+                            
FtpReply.REPLY_501_SYNTAX_ERROR_IN_PARAMETERS_OR_ARGUMENTS,
+                            "MFMT.invalid", null));
+            return;
+        }
+        
+
+    }
+}

Modified: 
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFtpFile.java
URL: 
http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFtpFile.java?rev=722225&r1=722224&r2=722225&view=diff
==============================================================================
--- 
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFtpFile.java
 (original)
+++ 
mina/ftpserver/trunk/core/src/main/java/org/apache/ftpserver/filesystem/nativefs/impl/NativeFtpFile.java
 Mon Dec  1 12:54:54 2008
@@ -187,6 +187,13 @@
     }
 
     /**
+     * [EMAIL PROTECTED]
+     */
+    public void setLastModified(long time) {
+        file.setLastModified(time);
+    }
+    
+    /**
      * Check read permission.
      */
     public boolean isReadable() {

Modified: 
mina/ftpserver/trunk/core/src/main/resources/org/apache/ftpserver/message/FtpStatus.properties
URL: 
http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/main/resources/org/apache/ftpserver/message/FtpStatus.properties?rev=722225&r1=722224&r2=722225&view=diff
==============================================================================
--- 
mina/ftpserver/trunk/core/src/main/resources/org/apache/ftpserver/message/FtpStatus.properties
 (original)
+++ 
mina/ftpserver/trunk/core/src/main/resources/org/apache/ftpserver/message/FtpStatus.properties
 Mon Dec  1 12:54:54 2008
@@ -67,7 +67,7 @@
 425.EPSV=Can't open passive connection.
 229.EPSV=Entering Passive Mode ({output.msg})
 
-211.FEAT=Extensions supported\n SIZE\n MDTM\n REST STREAM\n LANG 
en;zh-tw;ja;is\n MLST Size;Modify;Type;Perm\n AUTH SSL\n AUTH TLS\n MODE Z\n 
UTF8\n TVFS\n MD5\n MMD5\nEnd
+211.FEAT=Extensions supported\n SIZE\n MDTM\n REST STREAM\n LANG 
en;zh-tw;ja;is\n MLST Size;Modify;Type;Perm\n AUTH SSL\n AUTH TLS\n MODE Z\n 
UTF8\n TVFS\n MD5\n MMD5\n MFMT\nEnd
 
 214=The following commands are implemented.\nABOR  APPE  CDUP  CWD   DELE  
HELP  LIST  MDTM\nMKD   MODE  NLST  NOOP  PASS  PASV  PORT  PWD\nQUIT  REST  
RETR  RMD   RNFR  RNTO  SITE  SIZE\nSTAT  STOR  STOU  STRU  SYST  TYPE  
USER\nEnd of help.
 214.ABOR=Syntax\: ABOR
@@ -273,4 +273,8 @@
 251.MD5={output.msg}
 252.MMD5={output.msg}
 504.MD5.invalid=Command Not Implemented for the Specified Argument
-502.MD5.notimplemened=Command Not Implemented
\ No newline at end of file
+502.MD5.notimplemened=Command Not Implemented
+
+501.MFMT={output.msg}
+501.MFMT.invalid=Command Not Implemented for the Specified Arguments
+550.MFMT.filemissing=File missing {output.msg}
\ No newline at end of file

Added: 
mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/clienttests/MFMTTest.java
URL: 
http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/clienttests/MFMTTest.java?rev=722225&view=auto
==============================================================================
--- 
mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/clienttests/MFMTTest.java
 (added)
+++ 
mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/clienttests/MFMTTest.java
 Mon Dec  1 12:54:54 2008
@@ -0,0 +1,131 @@
+/*
+ * 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.ftpserver.clienttests;
+
+import java.io.File;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.TimeZone;
+
+/**
+*
+* @author The Apache MINA Project ([EMAIL PROTECTED])
+* @version $Rev$, $Date$
+*
+*/
+public class MFMTTest extends ClientTestTemplate {
+    private static final File TEST_FILE1 = new File(ROOT_DIR, "test1.txt");
+
+    private static final File TEST_DIR1 = new File(ROOT_DIR, "dir1");
+
+    private static final File TEST_FILE_IN_DIR1 = new File(TEST_DIR1,
+            "test4.txt");
+
+    private static final Calendar EXPECTED_TIME = new GregorianCalendar(2002, 
6, 17, 21, 7, 15);        
+    static {
+        EXPECTED_TIME.setTimeZone(TimeZone.getTimeZone("GMT"));
+    }
+
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        assertTrue(TEST_FILE1.createNewFile());
+        assertTrue(TEST_DIR1.mkdir());
+        assertTrue(TEST_FILE_IN_DIR1.createNewFile());
+        
+        
+        client.login(ADMIN_USERNAME, ADMIN_PASSWORD);
+    }
+
+    public void testNoArgument() throws Exception {
+        // must return 500 or 501 errors
+        assertEquals(501, client.sendCommand("MFMT"));
+    }
+
+    public void testSingleArgument() throws Exception {
+        // must return 500 or 501 errors
+        assertEquals(501, client.sendCommand("MFMT", "20020717210715 "));
+    }
+
+    public void testNoManyArguments() throws Exception {
+        // must return 500 or 501 errors
+        assertEquals(501, client.sendCommand("MFMT", "20020717210715 test1.txt 
too many"));
+    }
+    
+    public void testNonTimestampArgument() throws Exception {
+        // must return 500 or 501 errors
+        assertEquals(501, client.sendCommand("MFMT", "incorrect test1.txt"));
+    }
+
+    public void testNegativeTimestamp() throws Exception {
+        // must return 500 or 501 errors
+        assertEquals(501, client.sendCommand("MFMT", "-20020717210715 
test1.txt"));
+    }
+
+    public void testIncorrectTimestamp() throws Exception {
+        // must return 500 or 501 errors
+        assertEquals(501, client.sendCommand("MFMT", "20021317210715 
test1.txt"));
+    }
+
+    public void testNotCompleteTimestamp() throws Exception {
+        // must return 500 or 501 errors
+        assertEquals(501, client.sendCommand("MFMT", "20021317 test1.txt"));
+    }
+
+    
+    public void testNonExistingFile() throws Exception {
+        // should return 550
+        assertEquals(550, client.sendCommand("MFMT", "20020717210715 
dummy.txt"));
+    }
+
+    public void testDirectory() throws Exception {
+        // should return 550
+        assertEquals(501, client.sendCommand("MFMT", "20020717210715 " + 
TEST_DIR1.getName()));
+    }
+
+    
+    public void testFeatures() throws Exception {
+        client.sendCommand("FEAT");
+        
+        String result = client.getReplyString();
+        
+        assertTrue(result.contains(" MFMT\r\n"));
+    }
+
+    @SuppressWarnings("deprecation")
+    public void testSetTime() throws Exception {
+        
+        assertEquals(213, client.sendCommand("MFMT", "20020717210715 
test1.txt"));
+        
+        
assertEquals(EXPECTED_TIME.getTimeInMillis(),TEST_FILE1.lastModified());
+    }
+    
+    @SuppressWarnings("deprecation")
+    public void testSetTimeFullPath() throws Exception {
+        assertEquals(213, client.sendCommand("MFMT", "20020717210715 
dir1/test4.txt"));
+        
+        assertEquals(EXPECTED_TIME.getTimeInMillis(), 
TEST_FILE_IN_DIR1.lastModified());
+    }
+    
+}

Modified: 
mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/commands/impl/listing/LISTFileFormaterTest.java
URL: 
http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/commands/impl/listing/LISTFileFormaterTest.java?rev=722225&r1=722224&r2=722225&view=diff
==============================================================================
--- 
mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/commands/impl/listing/LISTFileFormaterTest.java
 (original)
+++ 
mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/commands/impl/listing/LISTFileFormaterTest.java
 Mon Dec  1 12:54:54 2008
@@ -129,6 +129,11 @@
         public boolean move(FtpFile destination) {
             return false;
         }
+
+        public void setLastModified(long time) {
+            // TODO Auto-generated method stub
+            
+        }
     }
 
     public void testSingleFile() {

Modified: 
mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/commands/impl/listing/MLSTFileFormaterTest.java
URL: 
http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/commands/impl/listing/MLSTFileFormaterTest.java?rev=722225&r1=722224&r2=722225&view=diff
==============================================================================
--- 
mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/commands/impl/listing/MLSTFileFormaterTest.java
 (original)
+++ 
mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/commands/impl/listing/MLSTFileFormaterTest.java
 Mon Dec  1 12:54:54 2008
@@ -125,6 +125,11 @@
         public boolean move(FtpFile destination) {
             return false;
         }
+
+        public void setLastModified(long time) {
+            // TODO Auto-generated method stub
+            
+        }
     }
 
     public void testSingleFile() {

Modified: 
mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/commands/impl/listing/NLSTFileFormaterTest.java
URL: 
http://svn.apache.org/viewvc/mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/commands/impl/listing/NLSTFileFormaterTest.java?rev=722225&r1=722224&r2=722225&view=diff
==============================================================================
--- 
mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/commands/impl/listing/NLSTFileFormaterTest.java
 (original)
+++ 
mina/ftpserver/trunk/core/src/test/java/org/apache/ftpserver/commands/impl/listing/NLSTFileFormaterTest.java
 Mon Dec  1 12:54:54 2008
@@ -121,6 +121,11 @@
         public boolean move(FtpFile destination) {
             return false;
         }
+
+        public void setLastModified(long time) {
+            // TODO Auto-generated method stub
+            
+        }
     }
 
     public void testSingleFile() {


Reply via email to