KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > coldcore > coloradoftp > command > impl > ftp > StorCommand


1 /**
2  * Command STOR.
3  * See FTP spec for details on the command.
4  *
5  * This class is designed as the superclass for STOU and APPE commands.
6  */

7 package com.coldcore.coloradoftp.command.impl.ftp;
8
9 import com.coldcore.coloradoftp.command.Reply;
10 import com.coldcore.coloradoftp.command.impl.AbstractCommand;
11 import com.coldcore.coloradoftp.connection.DataConnectionMode;
12 import com.coldcore.coloradoftp.factory.ObjectFactory;
13 import com.coldcore.coloradoftp.factory.ObjectName;
14 import com.coldcore.coloradoftp.filesystem.FileSystem;
15 import com.coldcore.coloradoftp.filter.DataFilterApplicator;
16 import com.coldcore.coloradoftp.session.Session;
17 import com.coldcore.coloradoftp.session.SessionAttributeName;
18 import org.apache.log4j.Logger;
19
20 import java.nio.channels.WritableByteChannel JavaDoc;
21 import java.nio.channels.Channel JavaDoc;
22
23 public class StorCommand extends AbstractCommand {
24
25   private static Logger log = Logger.getLogger(StorCommand.class);
26
27
28   public Reply execute() {
29     Reply reply = getReply();
30     if (!testLogin()) return reply;
31
32     String JavaDoc filename = getFilename();
33     if (filename.length() == 0) {
34       reply.setCode("501");
35       reply.setText("Send file name.");
36       return reply;
37     }
38
39     //Test if there is data channel left in the session
40
closeSessionDataChannel();
41
42     Session session = controlConnection.getSession();
43     FileSystem fileSystem = (FileSystem) ObjectFactory.getObject(ObjectName.FILESYSTEM);
44     WritableByteChannel JavaDoc wbc = fileSystem.saveFile(filename, isAppend(), session);
45
46     DataFilterApplicator applicator = (DataFilterApplicator) ObjectFactory.getObject(ObjectName.DATA_FILTER_APPLICATOR);
47     wbc = applicator.applyFilters(wbc, session);
48
49     String JavaDoc type = (String JavaDoc) session.getAttribute(SessionAttributeName.DATA_TYPE);
50     if (type == null) type = "A";
51
52     if (!prepareForDataConnection()) {
53       try {
54         wbc.close();
55       } catch (Throwable JavaDoc e) {
56         log.error("Error closing data channel (ignoring)", e);
57       }
58       return reply;
59     }
60
61     session.setAttribute(SessionAttributeName.DATA_CONNECTION_FILENAME, filename);
62     session.setAttribute(SessionAttributeName.DATA_CONNECTION_MODE, getDataConnectionMode());
63     session.setAttribute(SessionAttributeName.DATA_CONNECTION_CHANNEL, wbc);
64
65     reply.setCode("150");
66     reply.setText("Opening "+type+" mode data connection for "+filename+".");
67     return reply;
68   }
69
70
71   /** Close a data channel if exists in the session */
72   protected void closeSessionDataChannel() {
73     Session session = controlConnection.getSession();
74     Channel JavaDoc odc = (Channel JavaDoc) session.getAttribute(SessionAttributeName.DATA_CONNECTION_CHANNEL);
75     if (odc != null) {
76       log.debug("Attempting to close data channel in session");
77       session.removeAttribute(SessionAttributeName.DATA_CONNECTION_CHANNEL);
78       try {
79         odc.close();
80       } catch (Throwable JavaDoc e) {
81         log.error("Error closing data channel (ignoring)", e);
82       }
83     }
84   }
85   
86
87   /** Get filename to save
88    * @return Filename
89    */

90   protected String JavaDoc getFilename() {
91     return getParameter();
92   }
93
94
95   /** Get data connection mode to use
96    * @return Data connection mode
97    */

98   protected DataConnectionMode getDataConnectionMode() {
99     return DataConnectionMode.STOR;
100   }
101
102
103   /** Test if file must be appended or overwritten
104    * @return TRUE to append or FALSE to overwrite
105    */

106   protected boolean isAppend() {
107     return false;
108   }
109 }
110
Popular Tags